@ -17,17 +17,34 @@
package org.apache.dolphinscheduler.api.service ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TASK_DEFINITION ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TASK_DEFINITION_CREATE ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TASK_DEFINITION_DELETE ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TASK_DEFINITION_UPDATE ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.WORKFLOW_SWITCH_TO_THIS_VERSION ;
import static org.mockito.ArgumentMatchers.isA ;
import org.apache.dolphinscheduler.api.dto.task.TaskCreateRequest ;
import org.apache.dolphinscheduler.api.dto.task.TaskUpdateRequest ;
import org.apache.dolphinscheduler.api.dto.taskRelation.TaskRelationUpdateUpstreamRequest ;
import org.apache.dolphinscheduler.api.dto.workflow.WorkflowUpdateRequest ;
import org.apache.dolphinscheduler.api.enums.Status ;
import org.apache.dolphinscheduler.api.exceptions.ServiceException ;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl ;
import org.apache.dolphinscheduler.api.service.impl.TaskDefinitionServiceImpl ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.Flag ;
import org.apache.dolphinscheduler.common.enums.Priority ;
import org.apache.dolphinscheduler.common.enums.ReleaseState ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition ;
import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation ;
import org.apache.dolphinscheduler.dao.entity.Project ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog ;
import org.apache.dolphinscheduler.dao.entity.User ;
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper ;
import org.apache.dolphinscheduler.dao.mapper.ProcessTaskRelationMapper ;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper ;
import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper ;
@ -42,15 +59,15 @@ import java.util.List;
import java.util.Map ;
import org.junit.Assert ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.jupiter.api.Assertions ;
import org.junit.runner.RunWith ;
import org.mockito.InjectMocks ;
import org.mockito.Mock ;
import org.mockito.Mockito ;
import org.mockito.junit.MockitoJUnitRunner ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.* ;
@RunWith ( MockitoJUnitRunner . class )
public class TaskDefinitionServiceImplTest {
@ -78,148 +95,156 @@ public class TaskDefinitionServiceImplTest {
@Mock
private TaskPluginManager taskPluginManager ;
@Test
public void createTaskDefinition ( ) {
long projectCode = 1L ;
@Mock
private ProcessTaskRelationService processTaskRelationService ;
Project project = getProject ( projectCode ) ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
@Mock
private ProcessDefinitionMapper processDefinitionMapper ;
@Mock
private ProcessDefinitionService processDefinitionService ;
private static final String TASK_PARAMETER =
"{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ; ;
private static final long PROJECT_CODE = 1L ;
private static final long PROCESS_DEFINITION_CODE = 2L ;
private static final long TASK_CODE = 3L ;
private static final int VERSION = 1 ;
private static final int RESOURCE_RATE = - 1 ;
protected User user ;
protected Exception exception ;
@Before
public void before ( ) {
User loginUser = new User ( ) ;
loginUser . setId ( - 1 ) ;
loginUser . setId ( 1 ) ;
loginUser . setTenantId ( 2 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
loginUser . setUserName ( "admin" ) ;
user = loginUser ;
}
@Test
public void createTaskDefinition ( ) {
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION_CREATE ) ) . thenReturn ( result ) ;
String createTaskDefinitionJson = "[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , TASK_DEFINITION_CREATE ) )
. thenReturn ( result ) ;
String createTaskDefinitionJson =
"[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
List < TaskDefinitionLog > taskDefinitions = JSONUtils . toList ( createTaskDefinitionJson , TaskDefinitionLog . class ) ;
Mockito . when ( processService . saveTaskDefine ( loginUser , projectCode , taskDefinitions , Boolean . TRUE ) ) . thenReturn ( 1 ) ;
Mockito . when ( processService . saveTaskDefine ( user , PROJECT_CODE , taskDefinitions , Boolean . TRUE ) )
. thenReturn ( 1 ) ;
Mockito . when ( taskPluginManager . checkTaskParameters ( Mockito . any ( ) ) ) . thenReturn ( true ) ;
Map < String , Object > relation = taskDefinitionService
. createTaskDefinition ( loginUser , projectCode , createTaskDefinitionJson ) ;
. createTaskDefinition ( user , PROJECT_CODE , createTaskDefinitionJson ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
}
@Test
public void updateTaskDefinition ( ) {
String taskDefinitionJson = "{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}" ;
long projectCode = 1L ;
long taskCode = 1L ;
Project project = getProject ( projectCode ) ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
User loginUser = new User ( ) ;
loginUser . setId ( - 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
String taskDefinitionJson =
"{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}" ;
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION_UPDATE ) ) . thenReturn ( result ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , TASK_DEFINITION_UPDATE ) )
. thenReturn ( result ) ;
Mockito . when ( processService . isTaskOnline ( taskCode ) ) . thenReturn ( Boolean . FALSE ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) ) . thenReturn ( new TaskDefinition ( ) ) ;
Mockito . when ( processService . isTaskOnline ( TASK_CODE ) ) . thenReturn ( Boolean . FALSE ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( new TaskDefinition ( ) ) ;
Mockito . when ( taskDefinitionMapper . updateById ( Mockito . any ( TaskDefinitionLog . class ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionLogMapper . insert ( Mockito . any ( TaskDefinitionLog . class ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionLogMapper . queryMaxVersionForDefinition ( taskCode ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionLogMapper . queryMaxVersionForDefinition ( TASK_CODE ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskPluginManager . checkTaskParameters ( Mockito . any ( ) ) ) . thenReturn ( true ) ;
result = taskDefinitionService . updateTaskDefinition ( loginUser , projectCode , taskCode , taskDefinitionJson ) ;
result = taskDefinitionService . updateTaskDefinition ( user , PROJECT_CODE , TASK_CODE , taskDefinitionJson ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@Test
public void queryTaskDefinitionByName ( ) {
String taskName = "task" ;
long projectCode = 1L ;
long processCode = 1L ;
Project project = getProject ( projectCode ) ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
User loginUser = new User ( ) ;
loginUser . setId ( - 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION ) ) . thenReturn ( result ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , TASK_DEFINITION ) )
. thenReturn ( result ) ;
Mockito . when ( taskDefinitionMapper . queryByName ( project . getCode ( ) , processCode , taskName ) )
. thenReturn ( new TaskDefinition ( ) ) ;
Mockito . when ( taskDefinitionMapper . queryByName ( project . getCode ( ) , PROCESS_DEFINITION_CODE , taskName ) )
. thenReturn ( new TaskDefinition ( ) ) ;
Map < String , Object > relation = taskDefinitionService
. queryTaskDefinitionByName ( loginUser , projectCode , processCode , taskName ) ;
. queryTaskDefinitionByName ( user , PROJECT_CODE , PROCESS_DEFINITION_CODE , taskName ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
}
@Test
public void deleteTaskDefinitionByCode ( ) {
long projectCode = 1L ;
long taskCode = 1L ;
Project project = getProject ( projectCode ) ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
User loginUser = new User ( ) ;
loginUser . setId ( - 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION_DELETE ) ) . thenReturn ( result ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) ) . thenReturn ( getTaskDefinition ( ) ) ;
Mockito . when ( processTaskRelationMapper . queryDownstreamByTaskCode ( taskCode ) )
. thenReturn ( new ArrayList < > ( ) ) ;
Mockito . when ( taskDefinitionMapper . deleteByCode ( taskCode ) )
. thenReturn ( 1 ) ;
Map < String , Object > relation = taskDefinitionService
. deleteTaskDefinitionByCode ( loginUser , projectCode , taskCode ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
// error task definition not find
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . deleteTaskDefinitionByCode ( user , TASK_CODE ) ) ;
Assertions . assertEquals ( Status . TASK_DEFINE_NOT_EXIST . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error delete single task definition object
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( getTaskDefinition ( ) ) ;
Mockito . when ( taskDefinitionMapper . deleteByCode ( TASK_CODE ) ) . thenReturn ( 0 ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . deleteTaskDefinitionByCode ( user , TASK_CODE ) ) ;
Assertions . assertEquals ( Status . DELETE_TASK_DEFINE_BY_CODE_MSG_ERROR . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// success
Mockito . doNothing ( ) . when ( projectService ) . checkProjectAndAuthThrowException ( user , project ,
TASK_DEFINITION_DELETE ) ;
Mockito . when ( processTaskRelationMapper . queryDownstreamByTaskCode ( TASK_CODE ) ) . thenReturn ( new ArrayList < > ( ) ) ;
Mockito . when ( taskDefinitionMapper . deleteByCode ( TASK_CODE ) ) . thenReturn ( 1 ) ;
Assertions . assertDoesNotThrow ( ( ) - > taskDefinitionService . deleteTaskDefinitionByCode ( user , TASK_CODE ) ) ;
}
@Test
public void switchVersion ( ) {
int version = 1 ;
long taskCode = 11L ;
long projectCode = 1L ;
Project project = getProject ( projectCode ) ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
User loginUser = new User ( ) ;
loginUser . setId ( - 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , WORKFLOW_SWITCH_TO_THIS_VERSION ) ) . thenReturn ( result ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when (
projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , WORKFLOW_SWITCH_TO_THIS_VERSION ) )
. thenReturn ( result ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( taskCode , version ) )
. thenReturn ( new TaskDefinitionLog ( ) ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( TASK_CODE , VERSION ) )
. thenReturn ( new TaskDefinitionLog ( ) ) ;
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
taskDefinition . setProjectCode ( projectCode ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) )
. thenReturn ( taskDefinition ) ;
taskDefinition . setProjectCode ( PROJECT_CODE ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) )
. thenReturn ( taskDefinition ) ;
Mockito . when ( taskDefinitionMapper . updateById ( new TaskDefinitionLog ( ) ) ) . thenReturn ( 1 ) ;
Map < String , Object > relation = taskDefinitionService
. switchVersion ( loginUser , projectCode , taskCode , version ) ;
. switchVersion ( user , PROJECT_CODE , TASK_CODE , VERSION ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
}
@ -233,56 +258,36 @@ public class TaskDefinitionServiceImplTest {
}
}
/ * *
* get mock Project
*
* @param projectCode projectCode
* @return Project
* /
private Project getProject ( long projectCode ) {
Project project = new Project ( ) ;
project . setId ( 1 ) ;
project . setCode ( projectCode ) ;
project . setName ( "test" ) ;
project . setUserId ( 1 ) ;
return project ;
}
private TaskDefinition getTaskDefinition ( ) {
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
taskDefinition . setProjectCode ( 1L ) ;
taskDefinition . setCode ( 1L ) ;
taskDefinition . setVersion ( 1 ) ;
taskDefinition . setTaskType ( "SHELL" ) ;
return taskDefinition ;
}
@Test
public void checkJson ( ) {
String taskDefinitionJson = "[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
String taskDefinitionJson =
"[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
List < TaskDefinitionLog > taskDefinitionLogs = JSONUtils . toList ( taskDefinitionJson , TaskDefinitionLog . class ) ;
Assert . assertFalse ( taskDefinitionLogs . isEmpty ( ) ) ;
String taskJson = "[{\"name\":\"shell1\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],"
+ "\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},"
+ "\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\",\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\","
+ "\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"},{\"name\":\"shell2\",\"description\":\"\","
+ "\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 2\",\"conditionResult\":{\"successNode\""
+ ":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\","
+ "\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\",\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"}]" ;
String taskJson =
"[{\"name\":\"shell1\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],"
+ "\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},"
+ "\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\",\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\","
+ "\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"},{\"name\":\"shell2\",\"description\":\"\","
+ "\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 2\",\"conditionResult\":{\"successNode\""
+ ":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\","
+ "\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\",\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"}]" ;
taskDefinitionLogs = JSONUtils . toList ( taskJson , TaskDefinitionLog . class ) ;
Assert . assertFalse ( taskDefinitionLogs . isEmpty ( ) ) ;
String taskParams = "{\"resourceList\":[],\"localParams\":[{\"prop\":\"datetime\",\"direct\":\"IN\",\"type\":\"VARCHAR\","
+ "\"value\":\"${system.datetime}\"}],\"rawScript\":\"echo ${datetime}\",\"conditionResult\":\"{\\\"successNode\\\":[\\\"\\\"],"
+ "\\\"failedNode\\\":[\\\"\\\"]}\",\"dependence\":{}}" ;
String taskParams =
"{\"resourceList\":[],\"localParams\":[{\"prop\":\"datetime\",\"direct\":\"IN\",\"type\":\"VARCHAR\","
+ "\"value\":\"${system.datetime}\"}],\"rawScript\":\"echo ${datetime}\",\"conditionResult\":\"{\\\"successNode\\\":[\\\"\\\"],"
+ "\\\"failedNode\\\":[\\\"\\\"]}\",\"dependence\":{}}" ;
Map parameters = JSONUtils . parseObject ( taskParams , Map . class ) ;
Assert . assertNotNull ( parameters ) ;
String params = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ;
String params =
"{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ;
Map parameters1 = JSONUtils . parseObject ( params , Map . class ) ;
Assert . assertNotNull ( parameters1 ) ;
}
@ -295,43 +300,238 @@ public class TaskDefinitionServiceImplTest {
@Test
public void testReleaseTaskDefinition ( ) {
long projectCode = 1L ;
long taskCode = 1L ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( getProject ( projectCode ) ) ;
Project project = getProject ( projectCode ) ;
User loginUser = new User ( ) ;
loginUser . setId ( 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( getProject ( ) ) ;
Project project = getProject ( ) ;
// check task dose not exist
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . TASK_DEFINE_NOT_EXIST , taskCode ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , null ) ) . thenReturn ( result ) ;
Map < String , Object > map = taskDefinitionService . releaseTaskDefinition ( loginUser , projectCode , taskCode , ReleaseState . OFFLINE ) ;
putMsg ( result , Status . TASK_DEFINE_NOT_EXIST , TASK_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , null ) ) . thenReturn ( result ) ;
Map < String , Object > map =
taskDefinitionService . releaseTaskDefinition ( user , PROJECT_CODE , TASK_CODE , ReleaseState . OFFLINE ) ;
Assert . assertEquals ( Status . TASK_DEFINE_NOT_EXIST , map . get ( Constants . STATUS ) ) ;
// process definition offline
putMsg ( result , Status . SUCCESS ) ;
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
taskDefinition . setProjectCode ( projectCode ) ;
taskDefinition . setProjectCode ( PROJECT_CODE ) ;
taskDefinition . setVersion ( 1 ) ;
taskDefinition . setCode ( taskCode ) ;
String params = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ;
taskDefinition . setCode ( TASK_CODE ) ;
String params =
"{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ;
taskDefinition . setTaskParams ( params ) ;
taskDefinition . setTaskType ( "SHELL" ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) ) . thenReturn ( taskDefinition ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( taskDefinition ) ;
TaskDefinitionLog taskDefinitionLog = new TaskDefinitionLog ( taskDefinition ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( taskCode , taskDefinition . getVersion ( ) ) ) . thenReturn ( taskDefinitionLog ) ;
Map < String , Object > offlineTaskResult = taskDefinitionService . releaseTaskDefinition ( loginUser , projectCode , taskCode , ReleaseState . OFFLINE ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( TASK_CODE , taskDefinition . getVersion ( ) ) )
. thenReturn ( taskDefinitionLog ) ;
Map < String , Object > offlineTaskResult =
taskDefinitionService . releaseTaskDefinition ( user , PROJECT_CODE , TASK_CODE , ReleaseState . OFFLINE ) ;
Assert . assertEquals ( Status . SUCCESS , offlineTaskResult . get ( Constants . STATUS ) ) ;
// process definition online, resource exist
Map < String , Object > onlineTaskResult = taskDefinitionService . releaseTaskDefinition ( loginUser , projectCode , taskCode , ReleaseState . ONLINE ) ;
Map < String , Object > onlineTaskResult =
taskDefinitionService . releaseTaskDefinition ( user , PROJECT_CODE , TASK_CODE , ReleaseState . ONLINE ) ;
Assert . assertEquals ( Status . SUCCESS , onlineTaskResult . get ( Constants . STATUS ) ) ;
// release error code
Map < String , Object > failResult = taskDefinitionService . releaseTaskDefinition ( loginUser , projectCode , taskCode , ReleaseState . getEnum ( 2 ) ) ;
Map < String , Object > failResult =
taskDefinitionService . releaseTaskDefinition ( user , PROJECT_CODE , TASK_CODE , ReleaseState . getEnum ( 2 ) ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , failResult . get ( Constants . STATUS ) ) ;
}
@Test
public void testCreateTaskDefinitionV2 ( ) {
TaskCreateRequest taskCreateRequest = new TaskCreateRequest ( ) ;
taskCreateRequest . setProjectCode ( PROJECT_CODE ) ;
taskCreateRequest . setWorkflowCode ( PROCESS_DEFINITION_CODE ) ;
// error process definition not find
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . createTaskDefinitionV2 ( user , taskCreateRequest ) ) ;
Assertions . assertEquals ( Status . PROCESS_DEFINE_NOT_EXIST . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error project not find
Mockito . when ( processDefinitionMapper . queryByCode ( PROCESS_DEFINITION_CODE ) ) . thenReturn ( getProcessDefinition ( ) ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( getProject ( ) ) ;
Mockito . doThrow ( new ServiceException ( Status . PROJECT_NOT_EXIST ) ) . when ( projectService )
. checkProjectAndAuthThrowException ( user , getProject ( ) , TASK_DEFINITION_CREATE ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . createTaskDefinitionV2 ( user , taskCreateRequest ) ) ;
Assertions . assertEquals ( Status . PROJECT_NOT_EXIST . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition
taskCreateRequest . setTaskParams ( TASK_PARAMETER ) ;
Mockito . doNothing ( ) . when ( projectService ) . checkProjectAndAuthThrowException ( user , getProject ( ) ,
TASK_DEFINITION_CREATE ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . createTaskDefinitionV2 ( user , taskCreateRequest ) ) ;
Assertions . assertEquals ( Status . PROCESS_NODE_S_PARAMETER_INVALID . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// error create task definition object
Mockito . when ( taskPluginManager . checkTaskParameters ( Mockito . any ( ) ) ) . thenReturn ( true ) ;
Mockito . when ( taskDefinitionMapper . insert ( isA ( TaskDefinition . class ) ) ) . thenReturn ( 0 ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . createTaskDefinitionV2 ( user , taskCreateRequest ) ) ;
Assertions . assertEquals ( Status . CREATE_TASK_DEFINITION_ERROR . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// error sync to task definition log
Mockito . when ( taskDefinitionMapper . insert ( isA ( TaskDefinition . class ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionLogMapper . insert ( isA ( TaskDefinitionLog . class ) ) ) . thenReturn ( 0 ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . createTaskDefinitionV2 ( user , taskCreateRequest ) ) ;
Assertions . assertEquals ( Status . CREATE_TASK_DEFINITION_LOG_ERROR . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// success
Mockito . when ( taskDefinitionLogMapper . insert ( isA ( TaskDefinitionLog . class ) ) ) . thenReturn ( 1 ) ;
// we do not test updateUpstreamTaskDefinition, because it should be tested in processTaskRelationService
Mockito . when ( processTaskRelationService . updateUpstreamTaskDefinition ( isA ( User . class ) , isA ( Long . class ) ,
isA ( TaskRelationUpdateUpstreamRequest . class ) ) ) . thenReturn ( getProcessTaskRelationList ( ) ) ;
Mockito . when ( processDefinitionService . updateSingleProcessDefinition ( isA ( User . class ) , isA ( Long . class ) ,
isA ( WorkflowUpdateRequest . class ) ) ) . thenReturn ( getProcessDefinition ( ) ) ;
Assertions . assertDoesNotThrow ( ( ) - > taskDefinitionService . createTaskDefinitionV2 ( user , taskCreateRequest ) ) ;
}
@Test
public void testUpdateTaskDefinitionV2 ( ) {
TaskUpdateRequest taskUpdateRequest = new TaskUpdateRequest ( ) ;
// error task definition not exists
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . TASK_DEFINITION_NOT_EXISTS . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error project not find
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( getTaskDefinition ( ) ) ;
Mockito . when ( projectMapper . queryByCode ( isA ( Long . class ) ) ) . thenReturn ( getProject ( ) ) ;
Mockito . doThrow ( new ServiceException ( Status . PROJECT_NOT_EXIST ) ) . when ( projectService )
. checkProjectAndAuthThrowException ( user , getProject ( ) , TASK_DEFINITION_UPDATE ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . PROJECT_NOT_EXIST . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition
Mockito . doNothing ( ) . when ( projectService ) . checkProjectAndAuthThrowException ( user , getProject ( ) ,
TASK_DEFINITION_UPDATE ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . PROCESS_NODE_S_PARAMETER_INVALID . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition already online
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . PROCESS_NODE_S_PARAMETER_INVALID . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition nothing update
Mockito . when ( processService . isTaskOnline ( TASK_CODE ) ) . thenReturn ( false ) ;
Mockito . when ( taskPluginManager . checkTaskParameters ( Mockito . any ( ) ) ) . thenReturn ( true ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . TASK_DEFINITION_NOT_CHANGE . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition version invalid
taskUpdateRequest . setTaskPriority ( String . valueOf ( Priority . HIGH ) ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . DATA_IS_NOT_VALID . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition update effect number
Mockito . when ( taskDefinitionLogMapper . queryMaxVersionForDefinition ( TASK_CODE ) ) . thenReturn ( VERSION ) ;
Mockito . when ( taskDefinitionMapper . updateById ( isA ( TaskDefinition . class ) ) ) . thenReturn ( 0 ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . UPDATE_TASK_DEFINITION_ERROR . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition log insert
Mockito . when ( taskDefinitionMapper . updateById ( isA ( TaskDefinition . class ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionLogMapper . insert ( isA ( TaskDefinitionLog . class ) ) ) . thenReturn ( 0 ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
Assertions . assertEquals ( Status . CREATE_TASK_DEFINITION_LOG_ERROR . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// success
Mockito . when ( taskDefinitionLogMapper . insert ( isA ( TaskDefinitionLog . class ) ) ) . thenReturn ( 1 ) ;
// we do not test updateUpstreamTaskDefinition, because it should be tested in processTaskRelationService
Mockito . when ( processTaskRelationService . updateUpstreamTaskDefinition ( isA ( User . class ) , isA ( Long . class ) ,
isA ( TaskRelationUpdateUpstreamRequest . class ) ) ) . thenReturn ( getProcessTaskRelationList ( ) ) ;
Assertions . assertDoesNotThrow (
( ) - > taskDefinitionService . updateTaskDefinitionV2 ( user , TASK_CODE , taskUpdateRequest ) ) ;
}
@Test
public void testGetTaskDefinition ( ) {
// error task definition not exists
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . getTaskDefinition ( user , TASK_CODE ) ) ;
Assertions . assertEquals ( Status . TASK_DEFINE_NOT_EXIST . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
// error task definition not exists
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( getTaskDefinition ( ) ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( getProject ( ) ) ;
Mockito . doThrow ( new ServiceException ( Status . USER_NO_OPERATION_PROJECT_PERM ) ) . when ( projectService )
. checkProjectAndAuthThrowException ( user , getProject ( ) , TASK_DEFINITION ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . getTaskDefinition ( user , TASK_CODE ) ) ;
Assertions . assertEquals ( Status . USER_NO_OPERATION_PROJECT_PERM . getCode ( ) ,
( ( ServiceException ) exception ) . getCode ( ) ) ;
// success
Mockito . doNothing ( ) . when ( projectService ) . checkProjectAndAuthThrowException ( user , getProject ( ) , TASK_DEFINITION ) ;
Assertions . assertDoesNotThrow ( ( ) - > taskDefinitionService . getTaskDefinition ( user , TASK_CODE ) ) ;
}
/ * *
* get mock Project
*
* @return Project
* /
private Project getProject ( ) {
Project project = new Project ( ) ;
project . setId ( 1 ) ;
project . setCode ( PROJECT_CODE ) ;
project . setName ( "test" ) ;
project . setUserId ( 1 ) ;
return project ;
}
private ProcessDefinition getProcessDefinition ( ) {
ProcessDefinition processDefinition = new ProcessDefinition ( ) ;
processDefinition . setProjectCode ( PROJECT_CODE ) ;
processDefinition . setCode ( PROCESS_DEFINITION_CODE ) ;
processDefinition . setVersion ( VERSION ) ;
return processDefinition ;
}
private TaskDefinition getTaskDefinition ( ) {
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
taskDefinition . setProjectCode ( PROJECT_CODE ) ;
taskDefinition . setCode ( TASK_CODE ) ;
taskDefinition . setVersion ( VERSION ) ;
taskDefinition . setTaskType ( "SHELL" ) ;
taskDefinition . setTaskParams ( TASK_PARAMETER ) ;
taskDefinition . setFlag ( Flag . YES ) ;
taskDefinition . setCpuQuota ( RESOURCE_RATE ) ;
taskDefinition . setMemoryMax ( RESOURCE_RATE ) ;
return taskDefinition ;
}
private List < ProcessTaskRelation > getProcessTaskRelationList ( ) {
List < ProcessTaskRelation > processTaskRelationList = new ArrayList < > ( ) ;
ProcessTaskRelation processTaskRelation = new ProcessTaskRelation ( ) ;
processTaskRelation . setProjectCode ( PROJECT_CODE ) ;
processTaskRelation . setProcessDefinitionCode ( PROCESS_DEFINITION_CODE ) ;
processTaskRelation . setPreTaskCode ( TASK_CODE ) ;
processTaskRelation . setPostTaskCode ( TASK_CODE + 1L ) ;
processTaskRelationList . add ( processTaskRelation ) ;
return processTaskRelationList ;
}
}