Browse Source

improve test coverage

pull/3/MERGE
Zhou Zheng 4 years ago
parent
commit
bbfd35be05
  1. 30
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java
  2. 21
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java
  3. 3
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/DagHelper.java
  4. 10
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/utils/DagHelperTest.java

30
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java

@ -255,18 +255,32 @@ public class ExecutorService2Test {
List<Integer> mockRes = new ArrayList<>();
mockRes.add(1);
mockRes.add(2);
Mockito.when(processService.verifyIsNeedCreateCommand(any(Command.class)))
.thenReturn(true);
// check execute type error
processInstance.setState(ExecutionStatus.SUCCESS);
Map<String, Object> checkExeTypeRes = executorService.execute(loginUser, projectName, processInstanceId, ExecuteType.RESUME_FROM_FORCED_SUCCESS);
Assert.assertEquals(Status.PROCESS_INSTANCE_STATE_OPERATION_ERROR, checkExeTypeRes.get(Constants.STATUS));
// no valid forced success task
processInstance.setState(ExecutionStatus.FAILURE);
Map<String, Object> noValidTaskRes = executorService.execute(loginUser, projectName, processInstanceId, ExecuteType.RESUME_FROM_FORCED_SUCCESS);
Assert.assertEquals(Status.NO_VALID_FORCED_SUCCESS_TASK, noValidTaskRes.get(Constants.STATUS));
// have forced success in sub-process
Mockito.when(processService.findTaskIdByInstanceStatusAndType(anyInt(), any(ExecutionStatus[].class), any(TaskType.class)))
.thenReturn(mockRes);
Mockito.when(processService.haveForcedSuccessInSubProcess(anyInt()))
.thenReturn(true);
Mockito.when(processService.verifyIsNeedCreateCommand(any(Command.class)))
.thenReturn(true);
Map<String, Object> result = executorService.execute(loginUser, projectName, processInstanceId, ExecuteType.RESUME_FROM_FORCED_SUCCESS);
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
verify(processService, times(1)).createCommand(any(Command.class));
Map<String, Object> successRes1 = executorService.execute(loginUser, projectName, processInstanceId, ExecuteType.RESUME_FROM_FORCED_SUCCESS);
Assert.assertEquals(Status.SUCCESS, successRes1.get(Constants.STATUS));
// test success
Mockito.when(processService.findTaskIdByInstanceState(processInstanceId, ExecutionStatus.FORCED_SUCCESS)).thenReturn(mockRes);
Map<String, Object> successRes = executorService.execute(loginUser, projectName, processInstanceId, ExecuteType.RESUME_FROM_FORCED_SUCCESS);
Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS));
verify(processService, times(2)).createCommand(any(Command.class));
}
private List<Server> getMasterServersList() {

21
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java

@ -223,12 +223,19 @@ public class TaskInstanceServiceTest {
int taskId = 1;
TaskInstance task = getTaskInstance();
Map<String, Object> tmpResult = new HashMap<>(5);
putMsg(tmpResult, Status.SUCCESS);
when(projectMapper.queryByName("test")).thenReturn(project);
when(projectService.checkProjectAndAuth(user, project, projectName)).thenReturn(tmpResult);
Map<String, Object> mockSuccess = new HashMap<>(5);
putMsg(mockSuccess, Status.SUCCESS);
when(projectMapper.queryByName(projectName)).thenReturn(project);
// user auth failed
Map<String, Object> mockFailure = new HashMap<>(5);
putMsg(mockFailure, Status.USER_NO_OPERATION_PROJECT_PERM, user.getUserName(), projectName);
when(projectService.checkProjectAndAuth(user, project, projectName)).thenReturn(mockFailure);
Map<String, Object> authFailRes = taskInstanceService.forceSingleTaskSuccess(user, projectName, taskId);
Assert.assertNotSame(Status.SUCCESS, authFailRes.get(Constants.STATUS));
// test task not found
when(projectService.checkProjectAndAuth(user, project, projectName)).thenReturn(mockSuccess);
when(taskInstanceMapper.selectById(Mockito.anyInt())).thenReturn(null);
Map<String, Object> taskNotFoundRes = taskInstanceService.forceSingleTaskSuccess(user, projectName, taskId);
Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND, taskNotFoundRes.get(Constants.STATUS));
@ -239,6 +246,12 @@ public class TaskInstanceServiceTest {
Map<String, Object> taskStateErrorRes = taskInstanceService.forceSingleTaskSuccess(user, projectName, taskId);
Assert.assertEquals(Status.TASK_INSTANCE_STATE_OPERATION_ERROR, taskStateErrorRes.get(Constants.STATUS));
// test error
task.setState(ExecutionStatus.FAILURE);
when(taskInstanceMapper.updateById(task)).thenReturn(0);
Map<String, Object> errorRes = taskInstanceService.forceSingleTaskSuccess(user, projectName, taskId);
Assert.assertEquals(Status.FORCE_TASK_SUCCESS_ERROR, errorRes.get(Constants.STATUS));
// test success
task.setState(ExecutionStatus.FAILURE);
when(taskInstanceMapper.updateById(task)).thenReturn(1);

3
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/DagHelper.java

@ -31,6 +31,7 @@ import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -323,7 +324,7 @@ public class DagHelper {
} else if (taskInstance.getState().typeIsFailure()) {
return conditionsParameters.getFailedNode();
}
return null;
return Collections.emptyList();
}
/**

10
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/utils/DagHelperTest.java

@ -181,7 +181,17 @@ public class DagHelperTest {
dag.getNode("3").setConditionResult("{\"successNode\":[\"5\"],\"failedNode\":[\"6\"]}");
dag.getNode("3").setType(TaskType.CONDITIONS.toString());
// test success branch
Assert.assertEquals(1, DagHelper.getStartVertex(null, dag, completeTaskList).size());
Assert.assertEquals(true, DagHelper.getStartVertex(null, dag, completeTaskList).contains("5"));
// test failure branch
task3.setState(ExecutionStatus.FAILURE);
Assert.assertEquals(1, DagHelper.getStartVertex(null, dag, completeTaskList).size());
Assert.assertEquals(true, DagHelper.getStartVertex(null, dag, completeTaskList).contains("6"));
// test state error
task3.setState(ExecutionStatus.PAUSE);
Assert.assertEquals(0, DagHelper.getStartVertex(null, dag, completeTaskList).size());
}
}

Loading…
Cancel
Save