From 2c8db981d9fa7a7b267004c26466a5fb6d21879f Mon Sep 17 00:00:00 2001 From: huangguojie2024 <503601315@qq.com> Date: Thu, 20 Feb 2025 13:25:38 +0800 Subject: [PATCH] Add ut cases for command task classes in server module. --- .../server/command/task/AbstractTask.java | 4 + .../command/task/ComponentAddTaskTest.java | 137 +++++++++++++++++ .../command/task/ComponentCheckTaskTest.java | 145 ++++++++++++++++++ .../task/ComponentConfigureTaskTest.java | 116 ++++++++++++++ .../command/task/ComponentInitTaskTest.java | 116 ++++++++++++++ .../task/ComponentPrepareTaskTest.java | 116 ++++++++++++++ .../command/task/ComponentStartTaskTest.java | 133 ++++++++++++++++ .../command/task/ComponentStopTaskTest.java | 133 ++++++++++++++++ .../command/task/HostCheckTaskTest.java | 117 ++++++++++++++ .../server/command/task/SetupJdkTaskTest.java | 117 ++++++++++++++ 10 files changed, 1134 insertions(+) create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentAddTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentCheckTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentConfigureTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentInitTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentPrepareTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStartTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStopTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/HostCheckTaskTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/SetupJdkTaskTest.java diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/task/AbstractTask.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/task/AbstractTask.java index 818b868ab..e67981d92 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/task/AbstractTask.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/command/task/AbstractTask.java @@ -132,4 +132,8 @@ public TaskPO getTaskPO() { return taskPO; } + + protected void setTaskContextForTest(TaskContext taskContext) { + this.taskContext = taskContext; + } } diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentAddTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentAddTaskTest.java new file mode 100644 index 000000000..7b7ed8c78 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentAddTaskTest.java @@ -0,0 +1,137 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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 static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +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 ComponentAddTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentAddTask componentAddTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentAddTask = mock(ComponentAddTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentAddTask).setTaskContextForTest(any()); + componentAddTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentAddTask).injectBeans(); + componentAddTask.injectBeans(); + + doCallRealMethod().when(componentAddTask).loadTaskPO(any()); + lenient().when(componentAddTask.getTaskPO()).thenCallRealMethod(); + componentAddTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentAddTask).getCommand(); + Command command = componentAddTask.getCommand(); + assertEquals("add", command.getCode()); + assertEquals("Add", command.getName()); + } + + @Test + public void testGetCustomCommand() { + doCallRealMethod().when(componentAddTask).getCustomCommand(); + assertDoesNotThrow(() -> componentAddTask.getCustomCommand()); + } + + @Test + public void testBeforeRun() { + doCallRealMethod().when(componentAddTask).beforeRun(); + componentAddTask.beforeRun(); + verify(taskDao, times(1)).partialUpdateById(any()); + } + + @Test + public void tesGetTaskContext() { + doCallRealMethod().when(componentAddTask).getTaskContext(); + assertEquals(taskContext, componentAddTask.getTaskContext()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentAddTask).getName(); + assertEquals("Add TestComponentDisplayName on TestHostname", componentAddTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentCheckTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentCheckTaskTest.java new file mode 100644 index 000000000..e3645cf79 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentCheckTaskTest.java @@ -0,0 +1,145 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ComponentPO; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentCheckTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentCheckTask componentCheckTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentCheckTask = mock(ComponentCheckTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentCheckTask).setTaskContextForTest(any()); + componentCheckTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentCheckTask).injectBeans(); + componentCheckTask.injectBeans(); + + doCallRealMethod().when(componentCheckTask).loadTaskPO(any()); + lenient().when(componentCheckTask.getTaskPO()).thenCallRealMethod(); + componentCheckTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentCheckTask).getCommand(); + Command command = componentCheckTask.getCommand(); + assertEquals("check", command.getCode()); + assertEquals("Check", command.getName()); + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(componentCheckTask).onSuccess(); + List componentPOS = new ArrayList<>(); + componentPOS.add(new ComponentPO()); + when(componentDao.findByQuery(any())).thenReturn(componentPOS); + + componentCheckTask.onSuccess(); + verify(taskDao, times(1)).partialUpdateById(any()); + verify(componentDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testOnFailure() { + doCallRealMethod().when(componentCheckTask).onFailure(); + List componentPOS = new ArrayList<>(); + componentPOS.add(new ComponentPO()); + when(componentDao.findByQuery(any())).thenReturn(componentPOS); + + componentCheckTask.onFailure(); + verify(taskDao, times(1)).partialUpdateById(any()); + verify(componentDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentCheckTask).getName(); + assertEquals("Check TestComponentDisplayName on TestHostname", componentCheckTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentConfigureTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentConfigureTaskTest.java new file mode 100644 index 000000000..14456943d --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentConfigureTaskTest.java @@ -0,0 +1,116 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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 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.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentConfigureTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentConfigureTask componentConfigureTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentConfigureTask = mock(ComponentConfigureTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentConfigureTask).setTaskContextForTest(any()); + componentConfigureTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentConfigureTask).injectBeans(); + componentConfigureTask.injectBeans(); + + doCallRealMethod().when(componentConfigureTask).loadTaskPO(any()); + lenient().when(componentConfigureTask.getTaskPO()).thenCallRealMethod(); + componentConfigureTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentConfigureTask).getCommand(); + Command command = componentConfigureTask.getCommand(); + assertEquals("configure", command.getCode()); + assertEquals("Configure", command.getName()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentConfigureTask).getName(); + assertEquals("Configure TestComponentDisplayName on TestHostname", componentConfigureTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentInitTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentInitTaskTest.java new file mode 100644 index 000000000..063af2b4d --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentInitTaskTest.java @@ -0,0 +1,116 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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 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.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentInitTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentInitTask componentInitTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentInitTask = mock(ComponentInitTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentInitTask).setTaskContextForTest(any()); + componentInitTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentInitTask).injectBeans(); + componentInitTask.injectBeans(); + + doCallRealMethod().when(componentInitTask).loadTaskPO(any()); + lenient().when(componentInitTask.getTaskPO()).thenCallRealMethod(); + componentInitTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentInitTask).getCommand(); + Command command = componentInitTask.getCommand(); + assertEquals("init", command.getCode()); + assertEquals("Init", command.getName()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentInitTask).getName(); + assertEquals("Init TestComponentDisplayName on TestHostname", componentInitTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentPrepareTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentPrepareTaskTest.java new file mode 100644 index 000000000..4148cca7e --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentPrepareTaskTest.java @@ -0,0 +1,116 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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 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.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentPrepareTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentPrepareTask componentPrepareTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentPrepareTask = mock(ComponentPrepareTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentPrepareTask).setTaskContextForTest(any()); + componentPrepareTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentPrepareTask).injectBeans(); + componentPrepareTask.injectBeans(); + + doCallRealMethod().when(componentPrepareTask).loadTaskPO(any()); + lenient().when(componentPrepareTask.getTaskPO()).thenCallRealMethod(); + componentPrepareTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentPrepareTask).getCommand(); + Command command = componentPrepareTask.getCommand(); + assertEquals("prepare", command.getCode()); + assertEquals("Prepare", command.getName()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentPrepareTask).getName(); + assertEquals("Prepare TestComponentDisplayName on TestHostname", componentPrepareTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStartTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStartTaskTest.java new file mode 100644 index 000000000..34f61dc6a --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStartTaskTest.java @@ -0,0 +1,133 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ComponentPO; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentStartTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentStartTask componentStartTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentStartTask = mock(ComponentStartTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentStartTask).setTaskContextForTest(any()); + componentStartTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentStartTask).injectBeans(); + componentStartTask.injectBeans(); + + doCallRealMethod().when(componentStartTask).loadTaskPO(any()); + lenient().when(componentStartTask.getTaskPO()).thenCallRealMethod(); + componentStartTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentStartTask).getCommand(); + Command command = componentStartTask.getCommand(); + assertEquals("start", command.getCode()); + assertEquals("Start", command.getName()); + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(componentStartTask).onSuccess(); + List componentPOS = new ArrayList<>(); + componentPOS.add(new ComponentPO()); + when(componentDao.findByQuery(any())).thenReturn(componentPOS); + + componentStartTask.onSuccess(); + verify(taskDao, times(1)).partialUpdateById(any()); + verify(componentDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentStartTask).getName(); + assertEquals("Start TestComponentDisplayName on TestHostname", componentStartTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStopTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStopTaskTest.java new file mode 100644 index 000000000..8fe12b08d --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStopTaskTest.java @@ -0,0 +1,133 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.ComponentPO; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.ComponentDao; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ComponentStopTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Mock + private ComponentDao componentDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private ComponentStopTask componentStopTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + when(SpringContextHolder.getBean(ComponentDao.class)).thenReturn(componentDao); + + componentStopTask = mock(ComponentStopTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(componentStopTask).setTaskContextForTest(any()); + componentStopTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(componentStopTask).injectBeans(); + componentStopTask.injectBeans(); + + doCallRealMethod().when(componentStopTask).loadTaskPO(any()); + lenient().when(componentStopTask.getTaskPO()).thenCallRealMethod(); + componentStopTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(3)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(componentStopTask).getCommand(); + Command command = componentStopTask.getCommand(); + assertEquals("stop", command.getCode()); + assertEquals("Stop", command.getName()); + } + + @Test + public void testOnSuccess() { + doCallRealMethod().when(componentStopTask).onSuccess(); + List componentPOS = new ArrayList<>(); + componentPOS.add(new ComponentPO()); + when(componentDao.findByQuery(any())).thenReturn(componentPOS); + + componentStopTask.onSuccess(); + verify(taskDao, times(1)).partialUpdateById(any()); + verify(componentDao, times(1)).partialUpdateById(any()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(componentStopTask).getName(); + assertEquals("Stop TestComponentDisplayName on TestHostname", componentStopTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/HostCheckTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/HostCheckTaskTest.java new file mode 100644 index 000000000..d1596c563 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/HostCheckTaskTest.java @@ -0,0 +1,117 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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 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.when; + +@ExtendWith(MockitoExtension.class) +public class HostCheckTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private HostCheckTask hostCheckTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + + hostCheckTask = mock(HostCheckTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(hostCheckTask).setTaskContextForTest(any()); + hostCheckTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(hostCheckTask).injectBeans(); + hostCheckTask.injectBeans(); + + doCallRealMethod().when(hostCheckTask).loadTaskPO(any()); + lenient().when(hostCheckTask.getTaskPO()).thenCallRealMethod(); + hostCheckTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(2)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(hostCheckTask).getCommand(); + Command command = hostCheckTask.getCommand(); + assertEquals("custom", command.getCode()); + assertEquals("Custom", command.getName()); + } + + @Test + public void testGetCustomCommand() { + doCallRealMethod().when(hostCheckTask).getCustomCommand(); + assertEquals("check_host", hostCheckTask.getCustomCommand()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(hostCheckTask).getName(); + assertEquals("Check host TestHostname", hostCheckTask.getName()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/SetupJdkTaskTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/SetupJdkTaskTest.java new file mode 100644 index 000000000..4d8575770 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/SetupJdkTaskTest.java @@ -0,0 +1,117 @@ +/* + * 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.task; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.dao.po.TaskPO; +import org.apache.bigtop.manager.dao.repository.HostDao; +import org.apache.bigtop.manager.dao.repository.TaskDao; +import org.apache.bigtop.manager.server.holder.SpringContextHolder; + +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 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.when; + +@ExtendWith(MockitoExtension.class) +public class SetupJdkTaskTest { + + private MockedStatic springContextHolderMockedStatic; + + @Mock + private HostDao hostDao; + + @Mock + private TaskDao taskDao; + + @Spy + private TaskContext taskContext; + + @Spy + private TaskPO taskPO; + + private SetupJdkTask setupJdkTask; + + @BeforeEach + public void setUp() { + springContextHolderMockedStatic = mockStatic(SpringContextHolder.class); + when(SpringContextHolder.getBean(HostDao.class)).thenReturn(hostDao); + when(SpringContextHolder.getBean(TaskDao.class)).thenReturn(taskDao); + + setupJdkTask = mock(SetupJdkTask.class); + + taskContext.setComponentDisplayName("TestComponentDisplayName"); + taskContext.setHostname("TestHostname"); + taskContext.setComponentName("TestComponentName"); + taskContext.setClusterId(123L); + + doCallRealMethod().when(setupJdkTask).setTaskContextForTest(any()); + setupJdkTask.setTaskContextForTest(taskContext); + + doCallRealMethod().when(setupJdkTask).injectBeans(); + setupJdkTask.injectBeans(); + + doCallRealMethod().when(setupJdkTask).loadTaskPO(any()); + lenient().when(setupJdkTask.getTaskPO()).thenCallRealMethod(); + setupJdkTask.loadTaskPO(taskPO); + } + + @AfterEach + public void tearDown() { + springContextHolderMockedStatic.close(); + } + + @Test + public void testInjectBeans() { + springContextHolderMockedStatic.verify(() -> SpringContextHolder.getBean(any(Class.class)), times(2)); + } + + @Test + public void testGetCommand() { + doCallRealMethod().when(setupJdkTask).getCommand(); + Command command = setupJdkTask.getCommand(); + assertEquals("custom", command.getCode()); + assertEquals("Custom", command.getName()); + } + + @Test + public void testGetCustomCommand() { + doCallRealMethod().when(setupJdkTask).getCustomCommand(); + assertEquals("setup_jdk", setupJdkTask.getCustomCommand()); + } + + @Test + public void testGetName() { + doCallRealMethod().when(setupJdkTask).getName(); + assertEquals("Setup jdk for TestHostname", setupJdkTask.getName()); + } +}