Browse Source

增加了修改task状态的单元测试

pull/3/MERGE
Zhou Zheng 4 years ago
parent
commit
815f62ad57
  1. 2
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TaskInstanceService.java
  2. 20
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TaskInstanceControllerTest.java
  3. 29
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java

2
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TaskInstanceService.java

@ -167,7 +167,6 @@ public class TaskInstanceService extends BaseService {
// check whether the task instance state type is failure
if (!task.getState().typeIsFailure()) {
// FIXME: 这个status的msg中是包含参数的,需要处理一下(已完善,待测试)
putMsg(result, Status.TASK_INSTANCE_STATE_OPETATION_ERROR, taskInstanceId, task.getState().toString());
return result;
}
@ -179,7 +178,6 @@ public class TaskInstanceService extends BaseService {
putMsg(result, Status.SUCCESS);
}
else {
// FIXME: 或许应该再加一个状态码,如果按照process的执行逻辑,那么是不需要的
putMsg(result, Status.FORCE_TASK_SUCCESS_ERROR);
}

20
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TaskInstanceControllerTest.java

@ -28,7 +28,12 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -64,7 +69,18 @@ public class TaskInstanceControllerTest extends AbstractControllerTest{
}
@Test
public void forceSingleTaskSuccess() {
// TODO: 加入测试
public void forceSingleTaskSuccess() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("taskInstanceId","104");
MvcResult mvcResult = mockMvc.perform(post("/projects/{projectName}/task-instance/force-success","test")
.header(SESSION_ID, sessionId)
.params(paramsMap))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
}
}

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

@ -45,6 +45,7 @@ import java.text.MessageFormat;
import java.util.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.Silent.class)
@ -192,6 +193,32 @@ public class TaskInstanceServiceTest {
@Test
public void forceSingleTaskSuccess() {
// TODO: 加入测试
User user = getAdminUser();
String projectName = "test";
Project project = getProject(projectName);
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);
// test task not found
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));
// test task instance state error
task.setState(ExecutionStatus.SUCCESS);
when(taskInstanceMapper.selectById(1)).thenReturn(task);
Map<String, Object> taskStateErrorRes = taskInstanceService.forceSingleTaskSuccess(user, projectName, taskId);
Assert.assertEquals(Status.TASK_INSTANCE_STATE_OPETATION_ERROR, taskStateErrorRes.get(Constants.STATUS));
// test success
task.setState(ExecutionStatus.FAILURE);
when(taskInstanceMapper.updateById(task)).thenReturn(1);
Map<String, Object> successRes = taskInstanceService.forceSingleTaskSuccess(user, projectName, taskId);
Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS));
}
}
Loading…
Cancel
Save