|
|
|
@ -16,43 +16,131 @@
|
|
|
|
|
*/ |
|
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
|
import org.apache.dolphinscheduler.api.ApiApplicationServer; |
|
|
|
|
import org.apache.dolphinscheduler.api.enums.Status; |
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.enums.DependResult; |
|
|
|
|
import org.apache.dolphinscheduler.common.enums.ExecutionStatus; |
|
|
|
|
import org.apache.dolphinscheduler.common.enums.UserType; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.DateUtils; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.Project; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.User; |
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import org.apache.dolphinscheduler.dao.mapper.*; |
|
|
|
|
import org.apache.dolphinscheduler.service.process.ProcessService; |
|
|
|
|
import org.junit.Assert; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
import org.junit.runner.RunWith; |
|
|
|
|
import org.mockito.InjectMocks; |
|
|
|
|
import org.mockito.Mock; |
|
|
|
|
import org.mockito.Mockito; |
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.text.MessageFormat; |
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class) |
|
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
|
|
@RunWith(MockitoJUnitRunner.Silent.class) |
|
|
|
|
@SpringBootTest(classes = ApiApplicationServer.class) |
|
|
|
|
public class ProcessInstanceServiceTest { |
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProcessInstanceServiceTest.class); |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
@InjectMocks |
|
|
|
|
ProcessInstanceService processInstanceService; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ProjectMapper projectMapper; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ProjectService projectService; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ProcessService processService; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ProcessInstanceMapper processInstanceMapper; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ProcessDefinitionMapper processDefineMapper; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ProcessDefinitionService processDefinitionService; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
ExecutorService execService; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
TaskInstanceMapper taskInstanceMapper; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
LoggerService loggerService; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
WorkerGroupMapper workerGroupMapper; |
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
|
UsersService usersService; |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void viewVariables() { |
|
|
|
|
try { |
|
|
|
|
Map<String, Object> map = processInstanceService.viewVariables(-1); |
|
|
|
|
Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); |
|
|
|
|
logger.info(JSON.toJSONString(map)); |
|
|
|
|
}catch (Exception e){ |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
public void testQueryProcessInstanceList() { |
|
|
|
|
String projectName = "project_test1"; |
|
|
|
|
User loginUser = getAdminUser(); |
|
|
|
|
Map<String, Object> result = new HashMap<>(5); |
|
|
|
|
putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); |
|
|
|
|
|
|
|
|
|
//project auth fail
|
|
|
|
|
when(projectMapper.queryByName(projectName)).thenReturn(null); |
|
|
|
|
when(projectService.checkProjectAndAuth(loginUser,null,projectName)).thenReturn(result); |
|
|
|
|
Map<String, Object> proejctAuthFailRes = processInstanceService.queryProcessInstanceList(loginUser, projectName, 46, "2020-01-01 00:00:00", |
|
|
|
|
"2020-01-02 00:00:00", "", "test_user", ExecutionStatus.SUBMITTED_SUCCESS, |
|
|
|
|
"192.168.xx.xx", 1, 10); |
|
|
|
|
Assert.assertEquals(Status.PROJECT_NOT_FOUNT, proejctAuthFailRes.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
//project auth success
|
|
|
|
|
putMsg(result, Status.SUCCESS, projectName); |
|
|
|
|
Project project = getProject(projectName); |
|
|
|
|
Date start = DateUtils.getScheduleDate("2020-01-01 00:00:00"); |
|
|
|
|
Date end = DateUtils.getScheduleDate("2020-01-02 00:00:00"); |
|
|
|
|
ProcessInstance processInstance = getProcessInstance(); |
|
|
|
|
List<ProcessInstance> processInstanceList = new ArrayList<>(); |
|
|
|
|
Page<ProcessInstance> pageReturn = new Page<>(1, 10); |
|
|
|
|
processInstanceList.add(processInstance); |
|
|
|
|
pageReturn.setRecords(processInstanceList); |
|
|
|
|
when(projectMapper.queryByName(projectName)).thenReturn(project); |
|
|
|
|
when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); |
|
|
|
|
when(usersService.queryUser(loginUser.getId())).thenReturn(loginUser); |
|
|
|
|
when(usersService.getUserIdByName(loginUser.getUserName())).thenReturn(loginUser.getId()); |
|
|
|
|
when(processInstanceMapper.queryProcessInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(-1), Mockito.any(), |
|
|
|
|
eq("192.168.xx.xx"), eq(start), eq(end))).thenReturn(pageReturn); |
|
|
|
|
when(usersService.queryUser(processInstance.getExecutorId())).thenReturn(loginUser); |
|
|
|
|
Map<String, Object> successRes = processInstanceService.queryProcessInstanceList(loginUser, projectName, 1, "2020-01-01 00:00:00", |
|
|
|
|
"2020-01-02 00:00:00", "", loginUser.getUserName(), ExecutionStatus.SUBMITTED_SUCCESS, |
|
|
|
|
"192.168.xx.xx", 1, 10); |
|
|
|
|
Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
//executor null
|
|
|
|
|
when(usersService.queryUser(loginUser.getId())).thenReturn(null); |
|
|
|
|
when(usersService.getUserIdByName(loginUser.getUserName())).thenReturn(-1); |
|
|
|
|
Map<String, Object> executorExistRes = processInstanceService.queryProcessInstanceList(loginUser, projectName, 1, "2020-01-01 00:00:00", |
|
|
|
|
"2020-01-02 00:00:00", "", "admin", ExecutionStatus.SUBMITTED_SUCCESS, |
|
|
|
|
"192.168.xx.xx", 1, 10); |
|
|
|
|
Assert.assertEquals(Status.SUCCESS, executorExistRes.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
//executor name empty
|
|
|
|
|
when(processInstanceMapper.queryProcessInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(0), Mockito.any(), |
|
|
|
|
eq("192.168.xx.xx"), eq(start), eq(end))).thenReturn(pageReturn); |
|
|
|
|
Map<String, Object> executorEmptyRes = processInstanceService.queryProcessInstanceList(loginUser, projectName, 1, "2020-01-01 00:00:00", |
|
|
|
|
"2020-01-02 00:00:00", "", "", ExecutionStatus.SUBMITTED_SUCCESS, |
|
|
|
|
"192.168.xx.xx", 1, 10); |
|
|
|
|
Assert.assertEquals(Status.SUCCESS, executorEmptyRes.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -63,21 +151,58 @@ public class ProcessInstanceServiceTest {
|
|
|
|
|
try { |
|
|
|
|
Map<String, DependResult> resultMap = |
|
|
|
|
processInstanceService.parseLogForDependentResult(logString); |
|
|
|
|
Assert.assertEquals(resultMap.size() , 1); |
|
|
|
|
Assert.assertEquals(1 , resultMap.size()); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void queryProcessInstanceList() throws Exception { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get Mock Admin User |
|
|
|
|
* @return admin user |
|
|
|
|
*/ |
|
|
|
|
private User getAdminUser() { |
|
|
|
|
User loginUser = new User(); |
|
|
|
|
loginUser.setId(27); |
|
|
|
|
loginUser.setId(-1); |
|
|
|
|
loginUser.setUserName("admin"); |
|
|
|
|
loginUser.setUserType(UserType.GENERAL_USER); |
|
|
|
|
Map<String, Object> map = processInstanceService.queryProcessInstanceList(loginUser, "project_test1", 0, "", "", "", ExecutionStatus.FAILURE, "", 1, 5); |
|
|
|
|
return loginUser; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); |
|
|
|
|
logger.info(JSON.toJSONString(map)); |
|
|
|
|
/** |
|
|
|
|
* get mock Project |
|
|
|
|
* @param projectName projectName |
|
|
|
|
* @return Project |
|
|
|
|
*/ |
|
|
|
|
private Project getProject(String projectName){ |
|
|
|
|
Project project = new Project(); |
|
|
|
|
project.setId(1); |
|
|
|
|
project.setName(projectName); |
|
|
|
|
project.setUserId(1); |
|
|
|
|
return project; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get Mock process instance |
|
|
|
|
* @return process instance |
|
|
|
|
*/ |
|
|
|
|
private ProcessInstance getProcessInstance() { |
|
|
|
|
ProcessInstance processInstance = new ProcessInstance(); |
|
|
|
|
processInstance.setId(1); |
|
|
|
|
processInstance.setName("test_process_instance"); |
|
|
|
|
processInstance.setStartTime(new Date()); |
|
|
|
|
processInstance.setEndTime(new Date()); |
|
|
|
|
return processInstance; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void putMsg(Map<String, Object> result, Status status, Object... statusParams) { |
|
|
|
|
result.put(Constants.STATUS, status); |
|
|
|
|
if (statusParams != null && statusParams.length > 0) { |
|
|
|
|
result.put(Constants.MSG, MessageFormat.format(status.getMsg(), statusParams)); |
|
|
|
|
} else { |
|
|
|
|
result.put(Constants.MSG, status.getMsg()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |