This is an automated email from the ASF dual-hosted git repository.
wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git
The following commit(s) were added to refs/heads/main by this push:
new 6e55d6c7 BIGTOP-4375: Add ut cases for command task classes in server
module (#182)
6e55d6c7 is described below
commit 6e55d6c780cba52978a321632ad8ae4447b26d25
Author: xianrenzw <[email protected]>
AuthorDate: Tue Mar 25 17:48:03 2025 +0800
BIGTOP-4375: Add ut cases for command task classes in server module (#182)
---
.../server/command/task/ComponentAddTaskTest.java | 160 ++++++++++++++++++++
.../command/task/ComponentCheckTaskTest.java | 168 +++++++++++++++++++++
.../command/task/ComponentConfigureTaskTest.java | 139 +++++++++++++++++
.../server/command/task/ComponentInitTaskTest.java | 139 +++++++++++++++++
.../command/task/ComponentPrepareTaskTest.java | 139 +++++++++++++++++
.../command/task/ComponentStartTaskTest.java | 156 +++++++++++++++++++
.../server/command/task/ComponentStopTaskTest.java | 156 +++++++++++++++++++
.../server/command/task/HostCheckTaskTest.java | 140 +++++++++++++++++
.../server/command/task/SetupJdkTaskTest.java | 140 +++++++++++++++++
9 files changed, 1337 insertions(+)
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 00000000..455e0a2c
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentAddTaskTest.java
@@ -0,0 +1,160 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentAddTask, "taskContext",
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 testGetTaskContext() {
+ doCallRealMethod().when(componentAddTask).getTaskContext();
+ assertEquals(taskContext, componentAddTask.getTaskContext());
+ }
+
+ @Test
+ public void testGetTaskPO() {
+ doCallRealMethod().when(componentAddTask).getName();
+ doCallRealMethod().when(componentAddTask).getCommand();
+
+ componentAddTask.loadTaskPO(null);
+ TaskPO result = componentAddTask.getTaskPO();
+
+ assertEquals("Add TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Add", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..fd04a50d
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentCheckTaskTest.java
@@ -0,0 +1,168 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentCheckTask, "taskContext",
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<ComponentPO> 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<ComponentPO> 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 testGetTaskPO() {
+ doCallRealMethod().when(componentCheckTask).getName();
+ doCallRealMethod().when(componentCheckTask).getCommand();
+
+ componentCheckTask.loadTaskPO(null);
+ TaskPO result = componentCheckTask.getTaskPO();
+
+ assertEquals("Check TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Check", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..d511cee2
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentConfigureTaskTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentConfigureTask, "taskContext",
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 testGetTaskPO() {
+ doCallRealMethod().when(componentConfigureTask).getName();
+ doCallRealMethod().when(componentConfigureTask).getCommand();
+
+ componentConfigureTask.loadTaskPO(null);
+ TaskPO result = componentConfigureTask.getTaskPO();
+
+ assertEquals("Configure TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Configure", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..71bfc543
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentInitTaskTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentInitTask, "taskContext",
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 testGetTaskPO() {
+ doCallRealMethod().when(componentInitTask).getName();
+ doCallRealMethod().when(componentInitTask).getCommand();
+
+ componentInitTask.loadTaskPO(null);
+ TaskPO result = componentInitTask.getTaskPO();
+
+ assertEquals("Init TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Init", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..95719529
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentPrepareTaskTest.java
@@ -0,0 +1,139 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentPrepareTask, "taskContext",
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 testGetTaskPO() {
+ doCallRealMethod().when(componentPrepareTask).getName();
+ doCallRealMethod().when(componentPrepareTask).getCommand();
+
+ componentPrepareTask.loadTaskPO(null);
+ TaskPO result = componentPrepareTask.getTaskPO();
+
+ assertEquals("Prepare TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Prepare", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..d7e53e4f
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStartTaskTest.java
@@ -0,0 +1,156 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentStartTask, "taskContext",
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<ComponentPO> 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 testGetTaskPO() {
+ doCallRealMethod().when(componentStartTask).getName();
+ doCallRealMethod().when(componentStartTask).getCommand();
+
+ componentStartTask.loadTaskPO(null);
+ TaskPO result = componentStartTask.getTaskPO();
+
+ assertEquals("Start TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Start", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..37d70369
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/ComponentStopTaskTest.java
@@ -0,0 +1,156 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(componentStopTask, "taskContext",
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<ComponentPO> 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 testGetTaskPO() {
+ doCallRealMethod().when(componentStopTask).getName();
+ doCallRealMethod().when(componentStopTask).getCommand();
+
+ componentStopTask.loadTaskPO(null);
+ TaskPO result = componentStopTask.getTaskPO();
+
+ assertEquals("Stop TestComponentDisplayName on TestHostname",
result.getName());
+ assertEquals("Stop", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertNull(result.getCustomCommand());
+ }
+
+ @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 00000000..437fcaa9
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/HostCheckTaskTest.java
@@ -0,0 +1,140 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(hostCheckTask, "taskContext",
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 testGetTaskPO() {
+ doCallRealMethod().when(hostCheckTask).getCustomCommand();
+ doCallRealMethod().when(hostCheckTask).getName();
+ doCallRealMethod().when(hostCheckTask).getCommand();
+
+ hostCheckTask.loadTaskPO(null);
+ TaskPO result = hostCheckTask.getTaskPO();
+
+ assertEquals("Check host TestHostname", result.getName());
+ assertEquals("Custom", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertEquals("check_host", result.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 00000000..7a946a21
--- /dev/null
+++
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/command/task/SetupJdkTaskTest.java
@@ -0,0 +1,140 @@
+/*
+ * 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.common.utils.JsonUtils;
+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 org.springframework.test.util.ReflectionTestUtils;
+
+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<SpringContextHolder> 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.setServiceName("TestServiceName");
+ taskContext.setServiceUser("TestServiceUser");
+ taskContext.setComponentName("TestComponentName");
+ taskContext.setClusterId(123L);
+
+ ReflectionTestUtils.setField(setupJdkTask, "taskContext", 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 testGetTaskPO() {
+ doCallRealMethod().when(setupJdkTask).getCustomCommand();
+ doCallRealMethod().when(setupJdkTask).getName();
+ doCallRealMethod().when(setupJdkTask).getCommand();
+
+ setupJdkTask.loadTaskPO(null);
+ TaskPO result = setupJdkTask.getTaskPO();
+
+ assertEquals("Setup jdk for TestHostname", result.getName());
+ assertEquals("Custom", result.getCommand());
+
+ assertEquals(JsonUtils.writeAsString(taskContext),
result.getContext());
+ assertEquals("TestHostname", result.getHostname());
+ assertEquals("TestServiceName", result.getServiceName());
+ assertEquals("TestServiceUser", result.getServiceUser());
+ assertEquals("TestComponentName", result.getComponentName());
+ assertEquals("setup_jdk", result.getCustomCommand());
+ }
+
+ @Test
+ public void testGetName() {
+ doCallRealMethod().when(setupJdkTask).getName();
+ assertEquals("Setup jdk for TestHostname", setupJdkTask.getName());
+ }
+}