Skip to content

Commit

Permalink
Add ut cases for command task classes in server module.
Browse files Browse the repository at this point in the history
  • Loading branch information
xianrenzw committed Feb 20, 2025
1 parent 3fc2812 commit 2c8db98
Show file tree
Hide file tree
Showing 10 changed files with 1,134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ public TaskPO getTaskPO() {

return taskPO;
}

protected void setTaskContextForTest(TaskContext taskContext) {
this.taskContext = taskContext;
}
}
Original file line number Diff line number Diff line change
@@ -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<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.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());
}
}
Original file line number Diff line number Diff line change
@@ -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<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.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<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 testGetName() {
doCallRealMethod().when(componentCheckTask).getName();
assertEquals("Check TestComponentDisplayName on TestHostname", componentCheckTask.getName());
}
}
Loading

0 comments on commit 2c8db98

Please sign in to comment.