From 40bcb2e818241c92bbd2bcfcdb732d05e2e19b9c Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 9 Mar 2021 12:55:56 +0800 Subject: [PATCH] [Feature][jsonsplit]fix TaskInstanceMapperTest (#5010) * fix TaskInstanceMapperTest --- .../service/impl/TaskInstanceServiceImpl.java | 2 +- .../api/service/TaskInstanceServiceTest.java | 9 +- .../dao/mapper/TaskInstanceMapper.java | 4 +- .../dao/mapper/TaskInstanceMapper.xml | 8 +- .../dao/mapper/TaskInstanceMapperTest.java | 143 ++++++++++++------ 5 files changed, 110 insertions(+), 56 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java index a06a2e75b8..2afa5b935a 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java @@ -118,7 +118,7 @@ public class TaskInstanceServiceImpl extends BaseServiceImpl implements TaskInst int executorId = usersService.getUserIdByName(executorName); IPage taskInstanceIPage = taskInstanceMapper.queryTaskInstanceListPaging( - page, project.getId(), processInstanceId, processInstanceName, searchVal, taskName, executorId, statusArray, host, start, end + page, project.getCode(), processInstanceId, processInstanceName, searchVal, taskName, executorId, statusArray, host, start, end ); Set exclusionSet = new HashSet<>(); exclusionSet.add(Constants.CLASS); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java index d698486517..32de49fc3e 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java @@ -117,7 +117,7 @@ public class TaskInstanceServiceTest { when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); when(usersService.queryUser(loginUser.getId())).thenReturn(loginUser); when(usersService.getUserIdByName(loginUser.getUserName())).thenReturn(loginUser.getId()); - when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), eq(""), + when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getCode()), eq(1), eq(""), eq(""), eq(""), eq(0), Mockito.any(), eq("192.168.xx.xx"), eq(start), eq(end))).thenReturn(pageReturn); when(usersService.queryUser(processInstance.getExecutorId())).thenReturn(loginUser); when(processService.findProcessInstanceDetailById(taskInstance.getProcessInstanceId())).thenReturn(processInstance); @@ -127,7 +127,7 @@ public class TaskInstanceServiceTest { Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS)); //executor name empty - when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), eq(""), + when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getCode()), eq(1), eq(""), eq(""), eq(""), eq(0), Mockito.any(), eq("192.168.xx.xx"), eq(start), eq(end))).thenReturn(pageReturn); Map executorEmptyRes = taskInstanceService.queryTaskListPaging(loginUser, projectName, 1, "", "", "", "2020-01-01 00:00:00", "2020-01-02 00:00:00", "", ExecutionStatus.SUCCESS, "192.168.xx.xx", 1, 20); @@ -141,14 +141,14 @@ public class TaskInstanceServiceTest { Assert.assertEquals(Status.SUCCESS, executorNullRes.get(Constants.STATUS)); //start/end date null - when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), eq(""), + when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getCode()), eq(1), eq(""), eq(""), eq(""), eq(0), Mockito.any(), eq("192.168.xx.xx"), any(), any())).thenReturn(pageReturn); Map executorNullDateRes = taskInstanceService.queryTaskListPaging(loginUser, projectName, 1, "", "", "", null, null, "", ExecutionStatus.SUCCESS, "192.168.xx.xx", 1, 20); Assert.assertEquals(Status.SUCCESS, executorNullDateRes.get(Constants.STATUS)); //start date error format - when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), eq(""), + when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getCode()), eq(1), eq(""), eq(""), eq(""), eq(0), Mockito.any(), eq("192.168.xx.xx"), any(), any())).thenReturn(pageReturn); Map executorErrorStartDateRes = taskInstanceService.queryTaskListPaging(loginUser, projectName, 1, "", "", "", "error date", null, "", ExecutionStatus.SUCCESS, "192.168.xx.xx", 1, 20); @@ -179,6 +179,7 @@ public class TaskInstanceServiceTest { */ private Project getProject(String projectName) { Project project = new Project(); + project.setCode(1L); project.setId(1); project.setName(projectName); project.setUserId(1); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.java index d75e2b0a0f..833827bca3 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.java @@ -52,7 +52,7 @@ public interface TaskInstanceMapper extends BaseMapper { @Param("name") String name); Integer countTask( - @Param("projectIds") Integer[] projectIds, + @Param("projectCodes") Long[] projectCodes, @Param("taskIds") int[] taskIds); List countTaskInstanceStateByUser( @@ -61,7 +61,7 @@ public interface TaskInstanceMapper extends BaseMapper { @Param("projectCodes") Long[] projectCodes); IPage queryTaskInstanceListPaging(IPage page, - @Param("projectId") int projectId, + @Param("projectCode") Long projectCode, @Param("processInstanceId") Integer processInstanceId, @Param("processInstanceName") String processInstanceName, @Param("searchVal") String searchVal, diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.xml index b142ce8dd6..defccace58 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapper.xml @@ -99,9 +99,9 @@ select count(1) as count from t_ds_task_instance task,t_ds_process_definition process where task.process_definition_code=process.code - - and process.project_id in - + + and process.project_code in + #{i} @@ -122,7 +122,7 @@ from t_ds_task_instance instance left join t_ds_process_definition define on instance.process_definition_code = define.code left join t_ds_process_instance process on process.id=instance.process_instance_id - where define.project_id = #{projectId} + where define.project_code = #{projectCode} and instance.start_time > #{startTime} and instance.start_time #{endTime} diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapperTest.java index eba50fbdf6..a0930fbbdd 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskInstanceMapperTest.java @@ -17,8 +17,6 @@ package org.apache.dolphinscheduler.dao.mapper; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.ExecutionStatus; import org.apache.dolphinscheduler.common.enums.Flag; @@ -40,13 +38,15 @@ import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; + @RunWith(SpringRunner.class) @SpringBootTest @Transactional @Rollback(true) public class TaskInstanceMapperTest { - @Autowired TaskInstanceMapper taskInstanceMapper; @@ -64,21 +64,32 @@ public class TaskInstanceMapperTest { * * @return TaskInstance */ - private TaskInstance insertOne() { + private TaskInstance insertTaskInstance(int processInstanceId) { //insertOne - return insertOne("us task", 1, ExecutionStatus.RUNNING_EXECUTION, TaskType.SHELL.toString()); + return insertTaskInstance("us task", processInstanceId, ExecutionStatus.RUNNING_EXECUTION, TaskType.SHELL.toString()); } /** - * construct a task instance and then insert + * insert * - * @param taskName - * @param processInstanceId - * @param state - * @param taskType - * @return + * @return ProcessInstance + */ + private ProcessInstance insertProcessInstance() { + ProcessInstance processInstance = new ProcessInstance(); + processInstance.setId(1); + processInstance.setName("taskName"); + processInstance.setState(ExecutionStatus.RUNNING_EXECUTION); + processInstance.setStartTime(new Date()); + processInstance.setEndTime(new Date()); + processInstance.setProcessDefinitionCode(1L); + processInstanceMapper.insert(processInstance); + return processInstanceMapper.queryByProcessDefineCode(1L,1).get(0); + } + + /** + * construct a task instance and then insert */ - private TaskInstance insertOne(String taskName, int processInstanceId, ExecutionStatus state, String taskType) { + private TaskInstance insertTaskInstance(String taskName, int processInstanceId, ExecutionStatus state, String taskType) { TaskInstance taskInstance = new TaskInstance(); taskInstance.setFlag(Flag.YES); taskInstance.setName(taskName); @@ -88,6 +99,7 @@ public class TaskInstanceMapperTest { taskInstance.setTaskJson("{}"); taskInstance.setProcessInstanceId(processInstanceId); taskInstance.setTaskType(taskType); + taskInstance.setProcessDefinitionCode(1L); taskInstanceMapper.insert(taskInstance); return taskInstance; } @@ -96,10 +108,13 @@ public class TaskInstanceMapperTest { * test update */ @Test - public void testUpdate(){ - //insertOne - TaskInstance taskInstance = insertOne(); - //update + public void testUpdate() { + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance taskInstance = insertTaskInstance(processInstance.getId()); + // update int update = taskInstanceMapper.updateById(taskInstance); Assert.assertEquals(1, update); taskInstanceMapper.deleteById(taskInstance.getId()); @@ -110,7 +125,12 @@ public class TaskInstanceMapperTest { */ @Test public void testDelete() { - TaskInstance taskInstance = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance taskInstance = insertTaskInstance(processInstance.getId()); + int delete = taskInstanceMapper.deleteById(taskInstance.getId()); Assert.assertEquals(1, delete); } @@ -120,7 +140,11 @@ public class TaskInstanceMapperTest { */ @Test public void testQuery() { - TaskInstance taskInstance = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance taskInstance = insertTaskInstance(processInstance.getId()); //query List taskInstances = taskInstanceMapper.selectList(null); taskInstanceMapper.deleteById(taskInstance.getId()); @@ -132,8 +156,12 @@ public class TaskInstanceMapperTest { */ @Test public void testQueryTaskByProcessIdAndState() { - TaskInstance task = insertOne(); - task.setProcessInstanceId(110); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); + task.setProcessInstanceId(processInstance.getId()); taskInstanceMapper.updateById(task); List taskInstances = taskInstanceMapper.queryTaskByProcessIdAndState( task.getProcessInstanceId(), @@ -148,10 +176,14 @@ public class TaskInstanceMapperTest { */ @Test public void testFindValidTaskListByProcessId() { - TaskInstance task = insertOne(); - TaskInstance task2 = insertOne(); - task.setProcessInstanceId(110); - task2.setProcessInstanceId(110); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); + TaskInstance task2 = insertTaskInstance(processInstance.getId()); + task.setProcessInstanceId(processInstance.getId()); + task2.setProcessInstanceId(processInstance.getId()); taskInstanceMapper.updateById(task); taskInstanceMapper.updateById(task2); @@ -176,7 +208,11 @@ public class TaskInstanceMapperTest { */ @Test public void testQueryByHostAndStatus() { - TaskInstance task = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); task.setHost("111.111.11.11"); taskInstanceMapper.updateById(task); @@ -192,7 +228,11 @@ public class TaskInstanceMapperTest { */ @Test public void testSetFailoverByHostAndStateArray() { - TaskInstance task = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); task.setHost("111.111.11.11"); taskInstanceMapper.updateById(task); @@ -210,7 +250,11 @@ public class TaskInstanceMapperTest { */ @Test public void testQueryByInstanceIdAndName() { - TaskInstance task = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); task.setHost("111.111.11.11"); taskInstanceMapper.updateById(task); @@ -227,20 +271,26 @@ public class TaskInstanceMapperTest { */ @Test public void testCountTask() { - TaskInstance task = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); ProcessDefinition definition = new ProcessDefinition(); + definition.setCode(1L); definition.setProjectCode(1111L); + definition.setCreateTime(new Date()); + definition.setUpdateTime(new Date()); processDefinitionMapper.insert(definition); task.setProcessDefinitionId(definition.getId()); taskInstanceMapper.updateById(task); int countTask = taskInstanceMapper.countTask( - new Integer[0], + new Long[0], new int[0] ); int countTask2 = taskInstanceMapper.countTask( - new Integer[]{definition.getProjectId()}, + new Long[]{definition.getProjectCode()}, new int[]{task.getId()} ); taskInstanceMapper.deleteById(task.getId()); @@ -257,12 +307,17 @@ public class TaskInstanceMapperTest { @Test public void testCountTaskInstanceStateByUser() { - TaskInstance task = insertOne(); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); ProcessDefinition definition = new ProcessDefinition(); definition.setCode(1111L); definition.setProjectId(1111); definition.setProjectCode(1111L); - + definition.setCreateTime(new Date()); + definition.setUpdateTime(new Date()); processDefinitionMapper.insert(definition); task.setProcessDefinitionId(definition.getId()); taskInstanceMapper.updateById(task); @@ -282,30 +337,28 @@ public class TaskInstanceMapperTest { */ @Test public void testQueryTaskInstanceListPaging() { - TaskInstance task = insertOne(); - ProcessDefinition definition = new ProcessDefinition(); + definition.setCode(1L); definition.setProjectId(1111); definition.setProjectCode(1111L); + definition.setCreateTime(new Date()); + definition.setUpdateTime(new Date()); processDefinitionMapper.insert(definition); - ProcessInstance processInstance = new ProcessInstance(); - processInstance.setProcessDefinitionId(definition.getId()); - processInstance.setState(ExecutionStatus.RUNNING_EXECUTION); - processInstance.setName("ut process"); - processInstance.setStartTime(new Date()); - processInstance.setEndTime(new Date()); - processInstance.setCommandType(CommandType.START_PROCESS); - processInstanceMapper.insert(processInstance); + // insert ProcessInstance + ProcessInstance processInstance = insertProcessInstance(); + + // insert taskInstance + TaskInstance task = insertTaskInstance(processInstance.getId()); task.setProcessDefinitionId(definition.getId()); task.setProcessInstanceId(processInstance.getId()); taskInstanceMapper.updateById(task); - Page page = new Page(1,3); + Page page = new Page(1, 3); IPage taskInstanceIPage = taskInstanceMapper.queryTaskInstanceListPaging( page, - definition.getProjectId(), + definition.getProjectCode(), task.getProcessInstanceId(), "", "", @@ -313,7 +366,7 @@ public class TaskInstanceMapperTest { 0, new int[0], "", - null,null + null, null ); processInstanceMapper.deleteById(processInstance.getId()); taskInstanceMapper.deleteById(task.getId());