|
|
@ -16,36 +16,209 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.api.ApiApplicationServer; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.api.enums.Status; |
|
|
|
import org.apache.dolphinscheduler.api.enums.Status; |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.enums.CommandType; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.enums.ExecutionStatus; |
|
|
|
import org.apache.dolphinscheduler.common.enums.UserType; |
|
|
|
import org.apache.dolphinscheduler.common.enums.UserType; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.queue.ITaskQueue; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.queue.TaskQueueFactory; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.utils.DateUtils; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.dao.ProcessDao; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.CommandCount; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.Project; |
|
|
|
import org.apache.dolphinscheduler.dao.entity.User; |
|
|
|
import org.apache.dolphinscheduler.dao.entity.User; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.dao.mapper.*; |
|
|
|
|
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Assert; |
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.mockito.InjectMocks; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.mockito.Mockito; |
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
import org.powermock.api.mockito.PowerMockito; |
|
|
|
import org.springframework.test.context.junit4.SpringRunner; |
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
|
|
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class) |
|
|
|
@RunWith(PowerMockRunner.class) |
|
|
|
@SpringBootTest(classes = ApiApplicationServer.class) |
|
|
|
@PrepareForTest({TaskQueueFactory.class}) |
|
|
|
public class DataAnalysisServiceTest { |
|
|
|
public class DataAnalysisServiceTest { |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DataAnalysisServiceTest.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@InjectMocks |
|
|
|
private DataAnalysisService dataAnalysisService; |
|
|
|
private DataAnalysisService dataAnalysisService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ProjectMapper projectMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ProjectService projectService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ProcessInstanceMapper processInstanceMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ProcessDefinitionMapper processDefinitionMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
CommandMapper commandMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ErrorCommandMapper errorCommandMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
TaskInstanceMapper taskInstanceMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ITaskQueue taskQueue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
ProcessDao processDao; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Project project; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> resultMap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private User user; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
|
|
|
public void setUp() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user = new User(); |
|
|
|
|
|
|
|
project = new Project(); |
|
|
|
|
|
|
|
project.setId(1); |
|
|
|
|
|
|
|
resultMap = new HashMap<>(); |
|
|
|
|
|
|
|
Mockito.when(projectMapper.selectById(1)).thenReturn(project); |
|
|
|
|
|
|
|
Mockito.when(projectService.hasProjectAndPerm(user,project,resultMap)).thenReturn(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
|
|
|
|
public void after(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user = null; |
|
|
|
|
|
|
|
projectMapper = null; |
|
|
|
|
|
|
|
resultMap = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void countDefinitionByUser(){ |
|
|
|
public void testCountTaskStateByProject(){ |
|
|
|
User loginUser = new User(); |
|
|
|
|
|
|
|
loginUser.setId(27); |
|
|
|
String startDate = "2020-02-11 16:02:18"; |
|
|
|
loginUser.setUserType(UserType.GENERAL_USER); |
|
|
|
String endDate = "2020-02-11 16:03:18"; |
|
|
|
Map<String, Object> map = dataAnalysisService.countDefinitionByUser(loginUser, 21); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); |
|
|
|
//checkProject false
|
|
|
|
|
|
|
|
Map<String, Object> result = dataAnalysisService.countTaskStateByProject(user, 2, startDate, endDate); |
|
|
|
|
|
|
|
Assert.assertTrue(result.isEmpty()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// task instance state count error
|
|
|
|
|
|
|
|
result = dataAnalysisService.countTaskStateByProject(user, 1, startDate, endDate); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.TASK_INSTANCE_STATE_COUNT_ERROR,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//SUCCESS
|
|
|
|
|
|
|
|
Mockito.when(taskInstanceMapper.countTaskInstanceStateByUser(DateUtils.getScheduleDate(startDate), |
|
|
|
|
|
|
|
DateUtils.getScheduleDate(endDate), new Integer[]{1})).thenReturn(getTaskInstanceStateCounts()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = dataAnalysisService.countTaskStateByProject(user, 1, startDate, endDate); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testCountProcessInstanceStateByProject(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String startDate = "2020-02-11 16:02:18"; |
|
|
|
|
|
|
|
String endDate = "2020-02-11 16:03:18"; |
|
|
|
|
|
|
|
//checkProject false
|
|
|
|
|
|
|
|
Map<String, Object> result = dataAnalysisService.countProcessInstanceStateByProject(user,2,startDate,endDate); |
|
|
|
|
|
|
|
Assert.assertTrue(result.isEmpty()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//COUNT_PROCESS_INSTANCE_STATE_ERROR
|
|
|
|
|
|
|
|
result = dataAnalysisService.countProcessInstanceStateByProject(user,1,startDate,endDate); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.COUNT_PROCESS_INSTANCE_STATE_ERROR,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//SUCCESS
|
|
|
|
|
|
|
|
Mockito.when(processInstanceMapper.countInstanceStateByUser(DateUtils.getScheduleDate(startDate), |
|
|
|
|
|
|
|
DateUtils.getScheduleDate(endDate), new Integer[]{1})).thenReturn(getTaskInstanceStateCounts()); |
|
|
|
|
|
|
|
result = dataAnalysisService.countProcessInstanceStateByProject(user,1,startDate,endDate); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testCountDefinitionByUser(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> result = dataAnalysisService.countDefinitionByUser(user,1); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testCountCommandState(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String startDate = "2020-02-11 16:02:18"; |
|
|
|
|
|
|
|
String endDate = "2020-02-11 16:03:18"; |
|
|
|
|
|
|
|
//checkProject false
|
|
|
|
|
|
|
|
Map<String, Object> result = dataAnalysisService.countCommandState(user,2,startDate,endDate); |
|
|
|
|
|
|
|
Assert.assertTrue(result.isEmpty()); |
|
|
|
|
|
|
|
List<CommandCount> commandCounts = new ArrayList<>(1); |
|
|
|
|
|
|
|
CommandCount commandCount = new CommandCount(); |
|
|
|
|
|
|
|
commandCount.setCommandType(CommandType.START_PROCESS); |
|
|
|
|
|
|
|
commandCounts.add(commandCount); |
|
|
|
|
|
|
|
Mockito.when(commandMapper.countCommandState(0, DateUtils.getScheduleDate(startDate), |
|
|
|
|
|
|
|
DateUtils.getScheduleDate(endDate), new Integer[]{1})).thenReturn(commandCounts); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mockito.when(errorCommandMapper.countCommandState( DateUtils.getScheduleDate(startDate), |
|
|
|
|
|
|
|
DateUtils.getScheduleDate(endDate), new Integer[]{1})).thenReturn(commandCounts); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = dataAnalysisService.countCommandState(user,1,startDate,endDate); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testCountQueueState(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PowerMockito.mockStatic(TaskQueueFactory.class); |
|
|
|
|
|
|
|
List<String> taskQueueList = new ArrayList<>(1); |
|
|
|
|
|
|
|
taskQueueList.add("1_0_1_1_-1"); |
|
|
|
|
|
|
|
List<String> taskKillList = new ArrayList<>(1); |
|
|
|
|
|
|
|
taskKillList.add("1-0"); |
|
|
|
|
|
|
|
PowerMockito.when(taskQueue.getAllTasks(Constants.DOLPHINSCHEDULER_TASKS_QUEUE)).thenReturn(taskQueueList); |
|
|
|
|
|
|
|
PowerMockito.when(taskQueue.getAllTasks(Constants.DOLPHINSCHEDULER_TASKS_KILL)).thenReturn(taskKillList); |
|
|
|
|
|
|
|
PowerMockito.when(TaskQueueFactory.getTaskQueueInstance()).thenReturn(taskQueue); |
|
|
|
|
|
|
|
//checkProject false
|
|
|
|
|
|
|
|
Map<String, Object> result = dataAnalysisService.countQueueState(user,2); |
|
|
|
|
|
|
|
Assert.assertTrue(result.isEmpty()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = dataAnalysisService.countQueueState(user,1); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
//admin
|
|
|
|
|
|
|
|
user.setUserType(UserType.ADMIN_USER); |
|
|
|
|
|
|
|
result = dataAnalysisService.countQueueState(user,1); |
|
|
|
|
|
|
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* get list |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private List<ExecuteStatusCount> getTaskInstanceStateCounts(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ExecuteStatusCount> taskInstanceStateCounts = new ArrayList<>(1); |
|
|
|
|
|
|
|
ExecuteStatusCount executeStatusCount = new ExecuteStatusCount(); |
|
|
|
|
|
|
|
executeStatusCount.setExecutionStatus(ExecutionStatus.RUNNING_EXEUTION); |
|
|
|
|
|
|
|
taskInstanceStateCounts.add(executeStatusCount); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return taskInstanceStateCounts; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |