diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/AbstractClusterJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/AbstractClusterJob.java index d6bfbd71b..94d760a4b 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/AbstractClusterJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/AbstractClusterJob.java @@ -23,6 +23,7 @@ import org.apache.bigtop.manager.dao.repository.ComponentDao; import org.apache.bigtop.manager.server.command.job.AbstractJob; import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; import org.apache.bigtop.manager.server.holder.SpringContextHolder; import java.util.ArrayList; @@ -62,4 +63,9 @@ protected Map> getComponentHostsMap() { return componentHostsMap; } + + protected void setJobContextAndStagesForTest(JobContext jobContext, List stages) { + this.jobContext = jobContext; + this.stages = stages; + } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJob.java index 0661b3223..82eb5835b 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJob.java @@ -21,7 +21,6 @@ import org.apache.bigtop.manager.dao.po.JobPO; import org.apache.bigtop.manager.dao.po.StagePO; import org.apache.bigtop.manager.dao.po.TaskPO; -import org.apache.bigtop.manager.server.command.job.AbstractJob; import org.apache.bigtop.manager.server.command.job.JobContext; import org.apache.bigtop.manager.server.command.stage.HostCheckStage; import org.apache.bigtop.manager.server.command.stage.SetupJdkStage; @@ -38,7 +37,7 @@ import java.util.List; -public class ClusterAddJob extends AbstractJob { +public class ClusterAddJob extends AbstractClusterJob { private HostService hostService; @@ -92,7 +91,7 @@ public String getName() { return "Add cluster"; } - private void saveCluster() { + protected void saveCluster() { CommandDTO commandDTO = jobContext.getCommandDTO(); ClusterDTO clusterDTO = ClusterConverter.INSTANCE.fromCommand2DTO(commandDTO.getClusterCommand()); clusterPO = clusterDao.findByName(clusterDTO.getName()); @@ -104,7 +103,7 @@ private void saveCluster() { clusterDao.save(clusterPO); } - private void saveHosts() { + protected void saveHosts() { CommandDTO commandDTO = jobContext.getCommandDTO(); List hostDTOList = commandDTO.getClusterCommand().getHosts(); for (HostDTO hostDTO : hostDTOList) { @@ -113,7 +112,7 @@ private void saveHosts() { } } - private void linkJobToCluster() { + protected void linkJobToCluster() { JobPO jobPO = getJobPO(); jobPO.setClusterId(clusterPO.getId()); jobDao.partialUpdateById(jobPO); diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/AbstractComponentJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/AbstractComponentJob.java index 3374cf220..699a28ba0 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/AbstractComponentJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/AbstractComponentJob.java @@ -23,6 +23,7 @@ import org.apache.bigtop.manager.dao.repository.ServiceDao; import org.apache.bigtop.manager.server.command.job.AbstractJob; import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; import org.apache.bigtop.manager.server.holder.SpringContextHolder; import java.util.HashMap; @@ -64,4 +65,9 @@ protected Map> getComponentHostsMap() { return componentHostsMap; } + + protected void setJobContextAndStagesForTest(JobContext jobContext, List stages) { + this.jobContext = jobContext; + this.stages = stages; + } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJob.java index 99a3fd018..e75bb3cb7 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJob.java @@ -84,7 +84,7 @@ public String getName() { return "Add components"; } - private void saveComponents(ComponentCommandDTO componentCommand) { + protected void saveComponents(ComponentCommandDTO componentCommand) { Long clusterId = jobContext.getCommandDTO().getClusterId(); String componentName = componentCommand.getComponentName(); ServiceDTO serviceDTO = StackUtils.getServiceDTOByComponentName(componentName); diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/AbstractHostJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/AbstractHostJob.java index 7135efc0b..3fcd841ba 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/AbstractHostJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/AbstractHostJob.java @@ -23,6 +23,7 @@ import org.apache.bigtop.manager.dao.repository.ComponentDao; import org.apache.bigtop.manager.server.command.job.AbstractJob; import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; import org.apache.bigtop.manager.server.holder.SpringContextHolder; import java.util.ArrayList; @@ -71,4 +72,9 @@ private List getHostnames() { .flatMap(hostCommandDTO -> hostCommandDTO.getHostnames().stream()) .toList(); } + + protected void setJobContextAndStagesForTest(JobContext jobContext, List stages) { + this.jobContext = jobContext; + this.stages = stages; + } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/HostAddJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/HostAddJob.java index ba045d31a..eda5aba05 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/HostAddJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/host/HostAddJob.java @@ -68,7 +68,7 @@ public String getName() { return "Add hosts"; } - private void saveHosts() { + protected void saveHosts() { CommandDTO commandDTO = jobContext.getCommandDTO(); List hostDTOList = HostConverter.INSTANCE.fromCommand2DTO(commandDTO.getHostCommands()); for (HostDTO hostDTO : hostDTOList) { diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/AbstractServiceJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/AbstractServiceJob.java index d32400b9f..3725f5348 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/AbstractServiceJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/AbstractServiceJob.java @@ -27,6 +27,7 @@ import org.apache.bigtop.manager.dao.repository.ServiceDao; import org.apache.bigtop.manager.server.command.job.AbstractJob; import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; import org.apache.bigtop.manager.server.holder.SpringContextHolder; import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; @@ -84,4 +85,9 @@ private List getServiceNames() { .map(ServiceCommandDTO::getServiceName) .toList(); } + + protected void setJobContextAndStagesForTest(JobContext jobContext, List stages) { + this.jobContext = jobContext; + this.stages = stages; + } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJob.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJob.java index 7ffadc055..566a4c3ae 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJob.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJob.java @@ -135,7 +135,7 @@ public String getName() { return "Add services"; } - private void saveService(ServiceCommandDTO serviceCommand) { + protected void saveService(ServiceCommandDTO serviceCommand) { CommandDTO commandDTO = jobContext.getCommandDTO(); Long clusterId = commandDTO.getClusterId(); String serviceName = serviceCommand.getServiceName(); diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJobTest.java new file mode 100644 index 000000000..89eb8bf1e --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterAddJobTest.java @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.cluster; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.command.stage.StageContext; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; +import org.apache.bigtop.manager.server.service.HostService; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ClusterAddJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostService hostService; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ClusterAddJob clusterAddJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostService.class)).thenReturn(hostService); + + clusterAddJob = mock(ClusterAddJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(clusterAddJob).setJobContextAndStagesForTest(any(), any()); + clusterAddJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(clusterAddJob).injectBeans(); + clusterAddJob.injectBeans(); + + doCallRealMethod().when(clusterAddJob).loadJobPO(any()); + lenient().when(clusterAddJob.getJobPO()).thenCallRealMethod(); + clusterAddJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(6)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(clusterAddJob).beforeCreateStages(); + + clusterAddJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + + jobContext.setRetryFlag(true); + clusterAddJob.beforeCreateStages(); + verify(clusterDao, times(1)).findByName("testName"); + } + + @Test + public void testCreateStages() { + try (MockedStatic stageContextMockedStatic = mockStatic(StageContext.class)) { + + when(StageContext.fromCommandDTO(any())).thenReturn(mock(StageContext.class)); + doCallRealMethod().when(clusterAddJob).createStages(); + + clusterAddJob.createStages(); + verify(stages, times(2)).add(any()); + } + } + + @Test + public void testBeforeRun() { + doCallRealMethod().when(clusterAddJob).beforeRun(); + doNothing().when(clusterAddJob).saveCluster(); + doNothing().when(clusterAddJob).saveHosts(); + doNothing().when(clusterAddJob).linkJobToCluster(); + + jobContext.setRetryFlag(true); + clusterAddJob.beforeRun(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(clusterAddJob, never()).saveCluster(); + verify(clusterAddJob, never()).saveHosts(); + verify(clusterAddJob, never()).linkJobToCluster(); + + jobContext.setRetryFlag(false); + clusterAddJob.beforeRun(); + verify(clusterAddJob, times(1)).saveCluster(); + verify(clusterAddJob, times(1)).saveHosts(); + verify(clusterAddJob, times(1)).linkJobToCluster(); + } + + @Test + public void testGetName() { + doCallRealMethod().when(clusterAddJob).getName(); + assertEquals("Add cluster", clusterAddJob.getName()); + } + + @Test + public void testSaveCluster() { + doCallRealMethod().when(clusterAddJob).saveCluster(); + clusterAddJob.saveCluster(); + verify(clusterDao, times(1)).save(any()); + } + + @Test + public void testSaveHosts() { + doCallRealMethod().when(clusterAddJob).beforeCreateStages(); + doCallRealMethod().when(clusterAddJob).saveHosts(); + clusterAddJob.beforeCreateStages(); + clusterAddJob.saveHosts(); + verify(hostService, times(2)).add(any()); + } + + @Test + public void testLinkJobToCluster() { + doCallRealMethod().when(clusterAddJob).beforeCreateStages(); + doCallRealMethod().when(clusterAddJob).linkJobToCluster(); + clusterAddJob.beforeCreateStages(); + clusterAddJob.linkJobToCluster(); + verify(jobDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetJobContext() { + doCallRealMethod().when(clusterAddJob).getJobContext(); + assertEquals(jobContext, clusterAddJob.getJobContext()); + } + + @Test + public void testGetStages() { + doCallRealMethod().when(clusterAddJob).getStages(); + assertEquals(stages, clusterAddJob.getStages()); + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(clusterAddJob).onSuccess(); + clusterAddJob.onSuccess(); + verify(jobDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testOnFailure() { + doCallRealMethod().when(clusterAddJob).onFailure(); + clusterAddJob.onFailure(); + verify(taskDao, times(1)).partialUpdateByIds(any()); + verify(stageDao, times(1)).partialUpdateByIds(any()); + verify(jobDao, times(1)).partialUpdateById(any()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterRestartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterRestartJobTest.java new file mode 100644 index 000000000..b40d81037 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterRestartJobTest.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.cluster; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ClusterRestartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + private ClusterRestartJob clusterRestartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + clusterRestartJob = mock(ClusterRestartJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(clusterRestartJob).setJobContextAndStagesForTest(any(), any()); + clusterRestartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(clusterRestartJob).injectBeans(); + clusterRestartJob.injectBeans(); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(5)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(clusterRestartJob).beforeCreateStages(); + clusterRestartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(clusterRestartJob).createStages(); + when(clusterRestartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + clusterRestartJob.createStages(); + verify(stages, times(2)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(clusterRestartJob).getName(); + assertEquals("Restart cluster", clusterRestartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterStartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterStartJobTest.java new file mode 100644 index 000000000..79141ae83 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterStartJobTest.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.cluster; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ClusterStartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + private ClusterStartJob clusterStartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + clusterStartJob = mock(ClusterStartJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(clusterStartJob).setJobContextAndStagesForTest(any(), any()); + clusterStartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(clusterStartJob).injectBeans(); + clusterStartJob.injectBeans(); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(5)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(clusterStartJob).beforeCreateStages(); + clusterStartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(clusterStartJob).createStages(); + when(clusterStartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any())).thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + clusterStartJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(clusterStartJob).getName(); + assertEquals("Start cluster", clusterStartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterStopJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterStopJobTest.java new file mode 100644 index 000000000..8830b647a --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/cluster/ClusterStopJobTest.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.cluster; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ClusterStopJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + private ClusterStopJob ClusterStopJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + ClusterStopJob = mock(ClusterStopJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(ClusterStopJob).setJobContextAndStagesForTest(any(), any()); + ClusterStopJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(ClusterStopJob).injectBeans(); + ClusterStopJob.injectBeans(); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(5)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(ClusterStopJob).beforeCreateStages(); + ClusterStopJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(ClusterStopJob).createStages(); + when(ClusterStopJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any())).thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + ClusterStopJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(ClusterStopJob).getName(); + assertEquals("Stop cluster", ClusterStopJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJobTest.java new file mode 100644 index 000000000..4344f4a3b --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentAddJobTest.java @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.component; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ComponentCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentAddJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private HostDao hostDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ComponentAddJob componentAddJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentAddJob = mock(ComponentAddJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + List componentCommands = new ArrayList<>(); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + commandDTO.setComponentCommands(componentCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(componentAddJob).setJobContextAndStagesForTest(any(), any()); + componentAddJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(componentAddJob).injectBeans(); + componentAddJob.injectBeans(); + + doCallRealMethod().when(componentAddJob).loadJobPO(any()); + lenient().when(componentAddJob.getJobPO()).thenCallRealMethod(); + componentAddJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(7)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(componentAddJob).beforeCreateStages(); + componentAddJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(componentAddJob).createStages(); + when(componentAddJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any())).thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + componentAddJob.createStages(); + verify(stages, times(3)).addAll(any()); + } + } + + @Test + public void testBeforeRun() { + doCallRealMethod().when(componentAddJob).beforeRun(); + + jobContext.setRetryFlag(true); + componentAddJob.beforeRun(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(jobContext, never()).getCommandDTO(); + + jobContext.setRetryFlag(false); + componentAddJob.beforeRun(); + verify(jobContext, times(1)).getCommandDTO(); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentAddJob).getName(); + assertEquals("Add components", componentAddJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentRestartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentRestartJobTest.java new file mode 100644 index 000000000..aab9a60bc --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentRestartJobTest.java @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.component; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ComponentCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentRestartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private HostDao hostDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ComponentRestartJob componentRestartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentRestartJob = mock(ComponentRestartJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + List componentCommands = new ArrayList<>(); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + commandDTO.setComponentCommands(componentCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(componentRestartJob).setJobContextAndStagesForTest(any(), any()); + componentRestartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(componentRestartJob).injectBeans(); + componentRestartJob.injectBeans(); + + doCallRealMethod().when(componentRestartJob).loadJobPO(any()); + lenient().when(componentRestartJob.getJobPO()).thenCallRealMethod(); + componentRestartJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(7)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(componentRestartJob).beforeCreateStages(); + componentRestartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(componentRestartJob).createStages(); + when(componentRestartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any())).thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + componentRestartJob.createStages(); + verify(stages, times(2)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentRestartJob).getName(); + assertEquals("Restart components", componentRestartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentStartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentStartJobTest.java new file mode 100644 index 000000000..c4c1da9b2 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentStartJobTest.java @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.component; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ComponentCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentStartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private HostDao hostDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ComponentStartJob componentStartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentStartJob = mock(ComponentStartJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + List componentCommands = new ArrayList<>(); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + commandDTO.setComponentCommands(componentCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(componentStartJob).setJobContextAndStagesForTest(any(), any()); + componentStartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(componentStartJob).injectBeans(); + componentStartJob.injectBeans(); + + doCallRealMethod().when(componentStartJob).loadJobPO(any()); + lenient().when(componentStartJob.getJobPO()).thenCallRealMethod(); + componentStartJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(7)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(componentStartJob).beforeCreateStages(); + componentStartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(componentStartJob).createStages(); + when(componentStartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any())).thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + componentStartJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentStartJob).getName(); + assertEquals("Start components", componentStartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentStopJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentStopJobTest.java new file mode 100644 index 000000000..8ef5d60dc --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/component/ComponentStopJobTest.java @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.component; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ComponentCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentStopJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private HostDao hostDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ComponentStopJob componentStopJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentStopJob = mock(ComponentStopJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + List componentCommands = new ArrayList<>(); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + commandDTO.setComponentCommands(componentCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(componentStopJob).setJobContextAndStagesForTest(any(), any()); + componentStopJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(componentStopJob).injectBeans(); + componentStopJob.injectBeans(); + + doCallRealMethod().when(componentStopJob).loadJobPO(any()); + lenient().when(componentStopJob.getJobPO()).thenCallRealMethod(); + componentStopJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(7)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(componentStopJob).beforeCreateStages(); + componentStopJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(componentStopJob).createStages(); + when(componentStopJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any())).thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + componentStopJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentStopJob).getName(); + assertEquals("Stop components", componentStopJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostAddJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostAddJobTest.java new file mode 100644 index 000000000..483ead91c --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostAddJobTest.java @@ -0,0 +1,196 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.host; + +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.command.stage.StageContext; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; +import org.apache.bigtop.manager.server.service.HostService; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class HostAddJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostService hostService; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private HostAddJob hostAddJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(HostService.class)).thenReturn(hostService); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + hostAddJob = mock(HostAddJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + ; + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(hostAddJob).setJobContextAndStagesForTest(any(), any()); + hostAddJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(hostAddJob).injectBeans(); + hostAddJob.injectBeans(); + + doCallRealMethod().when(hostAddJob).loadJobPO(any()); + lenient().when(hostAddJob.getJobPO()).thenCallRealMethod(); + hostAddJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(6)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(hostAddJob).beforeCreateStages(); + hostAddJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic stageContextMockedStatic = mockStatic(StageContext.class)) { + + when(StageContext.fromCommandDTO(any())).thenReturn(mock(StageContext.class)); + doCallRealMethod().when(hostAddJob).createStages(); + + hostAddJob.createStages(); + verify(stages, times(2)).add(any()); + } + } + + @Test + public void testBeforeRun() { + doCallRealMethod().when(hostAddJob).beforeRun(); + + jobContext.setRetryFlag(true); + hostAddJob.beforeRun(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(hostAddJob, never()).saveHosts(); + + jobContext.setRetryFlag(false); + hostAddJob.beforeRun(); + verify(hostAddJob, times(1)).saveHosts(); + } + + @Test + public void testGetName() { + doCallRealMethod().when(hostAddJob).getName(); + assertEquals("Add hosts", hostAddJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostRestartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostRestartJobTest.java new file mode 100644 index 000000000..62a54e0f2 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostRestartJobTest.java @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.host; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class HostRestartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private HostRestartJob hostRestartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + hostRestartJob = mock(HostRestartJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + ; + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(hostRestartJob).setJobContextAndStagesForTest(any(), any()); + hostRestartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(hostRestartJob).injectBeans(); + hostRestartJob.injectBeans(); + + doCallRealMethod().when(hostRestartJob).loadJobPO(any()); + lenient().when(hostRestartJob.getJobPO()).thenCallRealMethod(); + hostRestartJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(5)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(hostRestartJob).beforeCreateStages(); + hostRestartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(hostRestartJob).createStages(); + when(hostRestartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + hostRestartJob.createStages(); + verify(stages, times(2)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(hostRestartJob).getName(); + assertEquals("Restart host", hostRestartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostStartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostStartJobTest.java new file mode 100644 index 000000000..afc6aa1cc --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostStartJobTest.java @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.host; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class HostStartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private HostStartJob hostStartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + hostStartJob = mock(HostStartJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + ; + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(hostStartJob).setJobContextAndStagesForTest(any(), any()); + hostStartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(hostStartJob).injectBeans(); + hostStartJob.injectBeans(); + + doCallRealMethod().when(hostStartJob).loadJobPO(any()); + lenient().when(hostStartJob.getJobPO()).thenCallRealMethod(); + hostStartJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(5)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(hostStartJob).beforeCreateStages(); + hostStartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(hostStartJob).createStages(); + when(hostStartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + hostStartJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(hostStartJob).getName(); + assertEquals("Start host", hostStartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostStopJobHostStopJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostStopJobHostStopJobTest.java new file mode 100644 index 000000000..aa9d61acf --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/host/HostStopJobHostStopJobTest.java @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.host; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.HostDTO; +import org.apache.bigtop.manager.server.model.dto.command.ClusterCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class HostStopJobHostStopJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private HostStopJob hostStopJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + hostStopJob = mock(HostStopJob.class); + + ClusterCommandDTO clusterCommandDTO = new ClusterCommandDTO(); + clusterCommandDTO.setName("testName"); + clusterCommandDTO.setDisplayName("testDisplayName"); + clusterCommandDTO.setDesc("testDescription."); + clusterCommandDTO.setType(1); + clusterCommandDTO.setUserGroup("testUserGroup"); + clusterCommandDTO.setRootDir("/test/root/dir"); + + HostDTO hostDTO1 = new HostDTO(); + HostDTO hostDTO2 = new HostDTO(); + + List hosts = new ArrayList<>(); + hosts.add(hostDTO1); + hosts.add(hostDTO2); + clusterCommandDTO.setHosts(hosts); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setClusterCommand(clusterCommandDTO); + ; + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(hostStopJob).setJobContextAndStagesForTest(any(), any()); + hostStopJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(hostStopJob).injectBeans(); + hostStopJob.injectBeans(); + + doCallRealMethod().when(hostStopJob).loadJobPO(any()); + lenient().when(hostStopJob.getJobPO()).thenCallRealMethod(); + hostStopJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(5)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(hostStopJob).beforeCreateStages(); + hostStopJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(hostStopJob).createStages(); + when(hostStopJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + hostStopJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(hostStopJob).getName(); + assertEquals("Stop host", hostStopJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJobTest.java new file mode 100644 index 000000000..7e0aa6022 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceAddJobTest.java @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.service; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.po.ServicePO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigSnapshotDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ServiceAddJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ServiceConfigDao serviceConfigDao; + + @Mock + private ServiceConfigSnapshotDao serviceConfigSnapshotDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostDao hostDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ServiceAddJob serviceAddJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ServiceConfigDao.class)).thenReturn(serviceConfigDao); + when(SpringContextHolder.getBean(ServiceConfigSnapshotDao.class)).thenReturn(serviceConfigSnapshotDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + + serviceAddJob = mock(ServiceAddJob.class); + + ServiceCommandDTO serviceCommandDTO = new ServiceCommandDTO(); + serviceCommandDTO.setServiceName("testName"); + + ArrayList serviceCommands = new ArrayList<>(); + serviceCommands.add(serviceCommandDTO); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setServiceCommands(serviceCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + doCallRealMethod().when(serviceAddJob).setJobContextAndStagesForTest(any(), any()); + serviceAddJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(serviceAddJob).injectBeans(); + serviceAddJob.injectBeans(); + + doCallRealMethod().when(serviceAddJob).loadJobPO(any()); + lenient().when(serviceAddJob.getJobPO()).thenCallRealMethod(); + serviceAddJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(9)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(serviceAddJob).beforeCreateStages(); + serviceAddJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(serviceAddJob).createStages(); + when(serviceAddJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + serviceAddJob.createStages(); + verify(stages, times(4)).addAll(any()); + } + } + + @Test + public void testBeforeRun() { + doCallRealMethod().when(serviceAddJob).beforeRun(); + + jobContext.setRetryFlag(true); + serviceAddJob.beforeRun(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceAddJob, never()).saveService(any()); + + jobContext.setRetryFlag(false); + serviceAddJob.beforeRun(); + verify(serviceAddJob, times(1)).saveService(any()); + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(serviceAddJob).onSuccess(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceAddJob.onSuccess(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateByIds(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(serviceAddJob).getName(); + assertEquals("Add services", serviceAddJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceCheckJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceCheckJobTest.java new file mode 100644 index 000000000..203d1cfbf --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceCheckJobTest.java @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.service; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.po.ServicePO; +import org.apache.bigtop.manager.dao.po.StagePO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigSnapshotDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ServiceCheckJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ServiceConfigDao serviceConfigDao; + + @Mock + private ServiceConfigSnapshotDao serviceConfigSnapshotDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostDao hostDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ServiceCheckJob serviceCheckJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ServiceConfigDao.class)).thenReturn(serviceConfigDao); + when(SpringContextHolder.getBean(ServiceConfigSnapshotDao.class)).thenReturn(serviceConfigSnapshotDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + + serviceCheckJob = mock(ServiceCheckJob.class); + + ServiceCommandDTO serviceCommandDTO = new ServiceCommandDTO(); + serviceCommandDTO.setServiceName("testName"); + + ArrayList serviceCommands = new ArrayList<>(); + serviceCommands.add(serviceCommandDTO); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setServiceCommands(serviceCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + Stage stage = mock(Stage.class); + lenient().when(stage.getStagePO()).thenReturn(new StagePO()); + lenient().when(stage.getTasks()).thenReturn(new ArrayList<>()); + stages.add(stage); + + doCallRealMethod().when(serviceCheckJob).setJobContextAndStagesForTest(any(), any()); + serviceCheckJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(serviceCheckJob).injectBeans(); + serviceCheckJob.injectBeans(); + + doCallRealMethod().when(serviceCheckJob).loadJobPO(any()); + lenient().when(serviceCheckJob.getJobPO()).thenCallRealMethod(); + serviceCheckJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(9)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(serviceCheckJob).beforeCreateStages(); + serviceCheckJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(serviceCheckJob).createStages(); + when(serviceCheckJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + serviceCheckJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(serviceCheckJob).onSuccess(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceCheckJob.onSuccess(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testOnFailure() { + doCallRealMethod().when(serviceCheckJob).onFailure(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceCheckJob.onFailure(); + verify(taskDao, times(1)).partialUpdateByIds(any()); + verify(stageDao, times(1)).partialUpdateByIds(any()); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(serviceCheckJob).getName(); + assertEquals("Check services", serviceCheckJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceConfigureJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceConfigureJobTest.java new file mode 100644 index 000000000..abfb053ff --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceConfigureJobTest.java @@ -0,0 +1,196 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.service; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.po.StagePO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigSnapshotDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ServiceConfigureJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ServiceConfigDao serviceConfigDao; + + @Mock + private ServiceConfigSnapshotDao serviceConfigSnapshotDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostDao hostDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ServiceConfigureJob serviceConfigureJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ServiceConfigDao.class)).thenReturn(serviceConfigDao); + when(SpringContextHolder.getBean(ServiceConfigSnapshotDao.class)).thenReturn(serviceConfigSnapshotDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + + serviceConfigureJob = mock(ServiceConfigureJob.class); + + ServiceCommandDTO serviceCommandDTO = new ServiceCommandDTO(); + serviceCommandDTO.setServiceName("testName"); + + ArrayList serviceCommands = new ArrayList<>(); + serviceCommands.add(serviceCommandDTO); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setServiceCommands(serviceCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + Stage stage = mock(Stage.class); + lenient().when(stage.getStagePO()).thenReturn(new StagePO()); + lenient().when(stage.getTasks()).thenReturn(new ArrayList<>()); + stages.add(stage); + + doCallRealMethod().when(serviceConfigureJob).setJobContextAndStagesForTest(any(), any()); + serviceConfigureJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(serviceConfigureJob).injectBeans(); + serviceConfigureJob.injectBeans(); + + doCallRealMethod().when(serviceConfigureJob).loadJobPO(any()); + lenient().when(serviceConfigureJob.getJobPO()).thenCallRealMethod(); + serviceConfigureJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(9)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(serviceConfigureJob).beforeCreateStages(); + serviceConfigureJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(serviceConfigureJob).createStages(); + when(serviceConfigureJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + serviceConfigureJob.createStages(); + verify(stages, times(3)).addAll(any()); + } + } + + @Test + public void testGetName() { + doCallRealMethod().when(serviceConfigureJob).getName(); + assertEquals("Configure services", serviceConfigureJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceRestartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceRestartJobTest.java new file mode 100644 index 000000000..63ff92bce --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceRestartJobTest.java @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.service; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.po.ServicePO; +import org.apache.bigtop.manager.dao.po.StagePO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigSnapshotDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ServiceRestartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ServiceConfigDao serviceConfigDao; + + @Mock + private ServiceConfigSnapshotDao serviceConfigSnapshotDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostDao hostDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ServiceRestartJob serviceRestartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ServiceConfigDao.class)).thenReturn(serviceConfigDao); + when(SpringContextHolder.getBean(ServiceConfigSnapshotDao.class)).thenReturn(serviceConfigSnapshotDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + + serviceRestartJob = mock(ServiceRestartJob.class); + + ServiceCommandDTO serviceCommandDTO = new ServiceCommandDTO(); + serviceCommandDTO.setServiceName("testName"); + + ArrayList serviceCommands = new ArrayList<>(); + serviceCommands.add(serviceCommandDTO); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setServiceCommands(serviceCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + Stage stage = mock(Stage.class); + lenient().when(stage.getStagePO()).thenReturn(new StagePO()); + lenient().when(stage.getTasks()).thenReturn(new ArrayList<>()); + stages.add(stage); + + doCallRealMethod().when(serviceRestartJob).setJobContextAndStagesForTest(any(), any()); + serviceRestartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(serviceRestartJob).injectBeans(); + serviceRestartJob.injectBeans(); + + doCallRealMethod().when(serviceRestartJob).loadJobPO(any()); + lenient().when(serviceRestartJob.getJobPO()).thenCallRealMethod(); + serviceRestartJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(9)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(serviceRestartJob).beforeCreateStages(); + serviceRestartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(serviceRestartJob).createStages(); + when(serviceRestartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + serviceRestartJob.createStages(); + verify(stages, times(2)).addAll(any()); + } + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(serviceRestartJob).onSuccess(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceRestartJob.onSuccess(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testOnFailure() { + doCallRealMethod().when(serviceRestartJob).onFailure(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceRestartJob.onFailure(); + verify(taskDao, times(1)).partialUpdateByIds(any()); + verify(stageDao, times(1)).partialUpdateByIds(any()); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(serviceRestartJob).getName(); + assertEquals("Restart services", serviceRestartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceStartJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceStartJobTest.java new file mode 100644 index 000000000..7e4cd8626 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceStartJobTest.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.service; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.po.ServicePO; +import org.apache.bigtop.manager.dao.po.StagePO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigSnapshotDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ServiceStartJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ServiceConfigDao serviceConfigDao; + + @Mock + private ServiceConfigSnapshotDao serviceConfigSnapshotDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostDao hostDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ServiceStartJob serviceStartJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ServiceConfigDao.class)).thenReturn(serviceConfigDao); + when(SpringContextHolder.getBean(ServiceConfigSnapshotDao.class)).thenReturn(serviceConfigSnapshotDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + + serviceStartJob = mock(ServiceStartJob.class); + + ServiceCommandDTO serviceCommandDTO = new ServiceCommandDTO(); + serviceCommandDTO.setServiceName("testName"); + + ArrayList serviceCommands = new ArrayList<>(); + serviceCommands.add(serviceCommandDTO); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setServiceCommands(serviceCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + Stage stage = mock(Stage.class); + lenient().when(stage.getStagePO()).thenReturn(new StagePO()); + lenient().when(stage.getTasks()).thenReturn(new ArrayList<>()); + stages.add(stage); + + doCallRealMethod().when(serviceStartJob).setJobContextAndStagesForTest(any(), any()); + serviceStartJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(serviceStartJob).injectBeans(); + serviceStartJob.injectBeans(); + + doCallRealMethod().when(serviceStartJob).loadJobPO(any()); + lenient().when(serviceStartJob.getJobPO()).thenCallRealMethod(); + serviceStartJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(9)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(serviceStartJob).beforeCreateStages(); + serviceStartJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(serviceStartJob).createStages(); + when(serviceStartJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + serviceStartJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(serviceStartJob).onSuccess(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceStartJob.onSuccess(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(serviceStartJob).getName(); + assertEquals("Start services", serviceStartJob.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceStopJobTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceStopJobTest.java new file mode 100644 index 000000000..1a78d1ad4 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/job/service/ServiceStopJobTest.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.command.job.service; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ClusterPO; +import org.apache.bigtop.manager.dao.po.JobPO; +import org.apache.bigtop.manager.dao.po.ServicePO; +import org.apache.bigtop.manager.dao.po.StagePO; +import org.apache.bigtop.manager.dao.repository.ClusterDao; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.JobDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigDao; +import org.apache.bigtop.manager.dao.repository.ServiceConfigSnapshotDao; +import org.apache.bigtop.manager.dao.repository.ServiceDao; +import org.apache.bigtop.manager.dao.repository.StageDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.command.helper.ComponentStageHelper; +import org.apache.bigtop.manager.server.command.job.JobContext; +import org.apache.bigtop.manager.server.command.stage.Stage; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; +import org.apache.bigtop.manager.server.model.dto.CommandDTO; +import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ServiceStopJobTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private ClusterDao clusterDao; + + @Mock + private JobDao jobDao; + + @Mock + private StageDao stageDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ServiceDao serviceDao; + + @Mock + private ServiceConfigDao serviceConfigDao; + + @Mock + private ServiceConfigSnapshotDao serviceConfigSnapshotDao; + + @Mock + private ComponentDao componentDao; + + @Mock + private HostDao hostDao; + + @Spy + private JobContext jobContext; + + @Mock + private List stages; + + @Mock + private ClusterPO clusterPO; + + @Spy + private JobPO jobPO; + + private ServiceStopJob serviceStopJob; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(ClusterDao.class)).thenReturn(clusterDao); + when(SpringContextHolder.getBean(JobDao.class)).thenReturn(jobDao); + when(SpringContextHolder.getBean(StageDao.class)).thenReturn(stageDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ServiceDao.class)).thenReturn(serviceDao); + when(SpringContextHolder.getBean(ServiceConfigDao.class)).thenReturn(serviceConfigDao); + when(SpringContextHolder.getBean(ServiceConfigSnapshotDao.class)).thenReturn(serviceConfigSnapshotDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + + serviceStopJob = mock(ServiceStopJob.class); + + ServiceCommandDTO serviceCommandDTO = new ServiceCommandDTO(); + serviceCommandDTO.setServiceName("testName"); + + ArrayList serviceCommands = new ArrayList<>(); + serviceCommands.add(serviceCommandDTO); + + CommandDTO commandDTO = new CommandDTO(); + commandDTO.setClusterId(123L); + commandDTO.setServiceCommands(serviceCommands); + + clusterPO.setId(1L); + lenient().when(clusterDao.findById(123L)).thenReturn(clusterPO); + lenient().when(clusterDao.findByName("testName")).thenReturn(clusterPO); + + jobContext.setCommandDTO(commandDTO); + jobContext.setRetryFlag(false); + + Stage stage = mock(Stage.class); + lenient().when(stage.getStagePO()).thenReturn(new StagePO()); + lenient().when(stage.getTasks()).thenReturn(new ArrayList<>()); + stages.add(stage); + + doCallRealMethod().when(serviceStopJob).setJobContextAndStagesForTest(any(), any()); + serviceStopJob.setJobContextAndStagesForTest(jobContext, stages); + + doCallRealMethod().when(serviceStopJob).injectBeans(); + serviceStopJob.injectBeans(); + + doCallRealMethod().when(serviceStopJob).loadJobPO(any()); + lenient().when(serviceStopJob.getJobPO()).thenCallRealMethod(); + serviceStopJob.loadJobPO(jobPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(9)); + } + + @Test + public void testBeforeCreateStages() { + doCallRealMethod().when(serviceStopJob).beforeCreateStages(); + serviceStopJob.beforeCreateStages(); + verify(clusterDao, times(1)).findById(123L); + } + + @Test + public void testCreateStages() { + try (MockedStatic componentStageHelperMockedStatic = + mockStatic(ComponentStageHelper.class)) { + + doCallRealMethod().when(serviceStopJob).createStages(); + when(serviceStopJob.getComponentHostsMap()).thenReturn(new HashMap<>()); + when(ComponentStageHelper.createComponentStages(any(), any(Command.class), any())) + .thenReturn(new ArrayList<>()); + when(stages.addAll(any())).thenReturn(true); + + serviceStopJob.createStages(); + verify(stages, times(1)).addAll(any()); + } + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(serviceStopJob).onSuccess(); + when(serviceDao.findByClusterIdAndName(any(), any())).thenReturn(new ServicePO()); + + serviceStopJob.onSuccess(); + verify(jobDao, times(1)).partialUpdateById(any()); + verify(serviceDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(serviceStopJob).getName(); + assertEquals("Stop services", serviceStopJob.getName()); + } +}