@ -17,17 +17,34 @@
package org.apache.dolphinscheduler.api.service ;
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.enums.Status ;
import org.apache.dolphinscheduler.api.exceptions.ServiceException ;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl ;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl ;
import org.apache.dolphinscheduler.api.service.impl.TaskDefinitionServiceImpl ;
import org.apache.dolphinscheduler.api.service.impl.TaskDefinitionServiceImpl ;
import org.apache.dolphinscheduler.common.Constants ;
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.ReleaseState ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
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.Project ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog ;
import org.apache.dolphinscheduler.dao.entity.User ;
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.ProcessTaskRelationMapper ;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper ;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper ;
import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper ;
import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper ;
@ -42,15 +59,15 @@ import java.util.List;
import java.util.Map ;
import java.util.Map ;
import org.junit.Assert ;
import org.junit.Assert ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.jupiter.api.Assertions ;
import org.junit.runner.RunWith ;
import org.junit.runner.RunWith ;
import org.mockito.InjectMocks ;
import org.mockito.InjectMocks ;
import org.mockito.Mock ;
import org.mockito.Mock ;
import org.mockito.Mockito ;
import org.mockito.Mockito ;
import org.mockito.junit.MockitoJUnitRunner ;
import org.mockito.junit.MockitoJUnitRunner ;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.* ;
@RunWith ( MockitoJUnitRunner . class )
@RunWith ( MockitoJUnitRunner . class )
public class TaskDefinitionServiceImplTest {
public class TaskDefinitionServiceImplTest {
@ -78,148 +95,156 @@ public class TaskDefinitionServiceImplTest {
@Mock
@Mock
private TaskPluginManager taskPluginManager ;
private TaskPluginManager taskPluginManager ;
@Test
@Mock
public void createTaskDefinition ( ) {
private ProcessTaskRelationService processTaskRelationService ;
long projectCode = 1L ;
Project project = getProject ( projectCode ) ;
@Mock
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
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 ( ) ;
User loginUser = new User ( ) ;
loginUser . setId ( - 1 ) ;
loginUser . setId ( 1 ) ;
loginUser . setTenantId ( 2 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
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 < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION_CREATE ) ) . thenReturn ( result ) ;
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\\\","
String createTaskDefinitionJson =
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
"[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
+ "\\\\\\\"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 ) ;
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 ) ;
Mockito . when ( taskPluginManager . checkTaskParameters ( Mockito . any ( ) ) ) . thenReturn ( true ) ;
Map < String , Object > relation = taskDefinitionService
Map < String , Object > relation = taskDefinitionService
. createTaskDefinition ( loginUser , projectCode , createTaskDefinitionJson ) ;
. createTaskDefinition ( user , PROJECT_CODE , createTaskDefinitionJson ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
}
}
@Test
@Test
public void updateTaskDefinition ( ) {
public void updateTaskDefinition ( ) {
String taskDefinitionJson = "{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
String taskDefinitionJson =
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
"{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}" ;
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
long projectCode = 1L ;
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}" ;
long taskCode = 1L ;
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
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 < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION_UPDATE ) ) . thenReturn ( result ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , TASK_DEFINITION_UPDATE ) )
. thenReturn ( result ) ;
Mockito . when ( processService . isTaskOnline ( taskCode ) ) . thenReturn ( Boolean . FALSE ) ;
Mockito . when ( processService . isTaskOnline ( TASK_CODE ) ) . thenReturn ( Boolean . FALSE ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) ) . thenReturn ( new TaskDefinition ( ) ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( new TaskDefinition ( ) ) ;
Mockito . when ( taskDefinitionMapper . updateById ( Mockito . any ( TaskDefinitionLog . class ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionMapper . updateById ( Mockito . any ( TaskDefinitionLog . class ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionLogMapper . insert ( 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 ) ;
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 ) ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
}
@Test
@Test
public void queryTaskDefinitionByName ( ) {
public void queryTaskDefinitionByName ( ) {
String taskName = "task" ;
String taskName = "task" ;
long projectCode = 1L ;
Project project = getProject ( ) ;
long processCode = 1L ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
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 < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION ) ) . thenReturn ( result ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , TASK_DEFINITION ) )
. thenReturn ( result ) ;
Mockito . when ( taskDefinitionMapper . queryByName ( project . getCode ( ) , processCode , taskName ) )
Mockito . when ( taskDefinitionMapper . queryByName ( project . getCode ( ) , PROCESS_DEFINITION_CODE , taskName ) )
. thenReturn ( new TaskDefinition ( ) ) ;
. thenReturn ( new TaskDefinition ( ) ) ;
Map < String , Object > relation = taskDefinitionService
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 ) ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
}
}
@Test
@Test
public void deleteTaskDefinitionByCode ( ) {
public void deleteTaskDefinitionByCode ( ) {
long projectCode = 1L ;
Project project = getProject ( ) ;
long taskCode = 1L ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
Project project = getProject ( projectCode ) ;
// error task definition not find
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( project ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
( ) - > taskDefinitionService . deleteTaskDefinitionByCode ( user , TASK_CODE ) ) ;
User loginUser = new User ( ) ;
Assertions . assertEquals ( Status . TASK_DEFINE_NOT_EXIST . getCode ( ) , ( ( ServiceException ) exception ) . getCode ( ) ) ;
loginUser . setId ( - 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
// error delete single task definition object
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( getTaskDefinition ( ) ) ;
Map < String , Object > result = new HashMap < > ( ) ;
Mockito . when ( taskDefinitionMapper . deleteByCode ( TASK_CODE ) ) . thenReturn ( 0 ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
exception = Assertions . assertThrows ( ServiceException . class ,
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , TASK_DEFINITION_DELETE ) ) . thenReturn ( result ) ;
( ) - > taskDefinitionService . deleteTaskDefinitionByCode ( user , TASK_CODE ) ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) ) . thenReturn ( getTaskDefinition ( ) ) ;
Assertions . assertEquals ( Status . DELETE_TASK_DEFINE_BY_CODE_MSG_ERROR . getCode ( ) ,
Mockito . when ( processTaskRelationMapper . queryDownstreamByTaskCode ( taskCode ) )
( ( ServiceException ) exception ) . getCode ( ) ) ;
. thenReturn ( new ArrayList < > ( ) ) ;
Mockito . when ( taskDefinitionMapper . deleteByCode ( taskCode ) )
// success
. thenReturn ( 1 ) ;
Mockito . doNothing ( ) . when ( projectService ) . checkProjectAndAuthThrowException ( user , project ,
TASK_DEFINITION_DELETE ) ;
Map < String , Object > relation = taskDefinitionService
Mockito . when ( processTaskRelationMapper . queryDownstreamByTaskCode ( TASK_CODE ) ) . thenReturn ( new ArrayList < > ( ) ) ;
. deleteTaskDefinitionByCode ( loginUser , projectCode , taskCode ) ;
Mockito . when ( taskDefinitionMapper . deleteByCode ( TASK_CODE ) ) . thenReturn ( 1 ) ;
Assertions . assertDoesNotThrow ( ( ) - > taskDefinitionService . deleteTaskDefinitionByCode ( user , TASK_CODE ) ) ;
Assert . assertEquals ( Status . SUCCESS , relation . get ( Constants . STATUS ) ) ;
}
}
@Test
@Test
public void switchVersion ( ) {
public void switchVersion ( ) {
int version = 1 ;
Project project = getProject ( ) ;
long taskCode = 11L ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( project ) ;
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 ) ;
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , projectCode ) ;
putMsg ( result , Status . SUCCESS , PROJECT_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , WORKFLOW_SWITCH_TO_THIS_VERSION ) ) . thenReturn ( result ) ;
Mockito . when (
projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , WORKFLOW_SWITCH_TO_THIS_VERSION ) )
. thenReturn ( result ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( taskCode , version ) )
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( TASK_CODE , VERSION ) )
. thenReturn ( new TaskDefinitionLog ( ) ) ;
. thenReturn ( new TaskDefinitionLog ( ) ) ;
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
taskDefinition . setProjectCode ( projectCode ) ;
taskDefinition . setProjectCode ( PROJECT_CODE ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) )
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) )
. thenReturn ( taskDefinition ) ;
. thenReturn ( taskDefinition ) ;
Mockito . when ( taskDefinitionMapper . updateById ( new TaskDefinitionLog ( ) ) ) . thenReturn ( 1 ) ;
Mockito . when ( taskDefinitionMapper . updateById ( new TaskDefinitionLog ( ) ) ) . thenReturn ( 1 ) ;
Map < String , Object > relation = taskDefinitionService
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 ) ) ;
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
@Test
public void checkJson ( ) {
public void checkJson ( ) {
String taskDefinitionJson = "[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
String taskDefinitionJson =
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
"[{\"name\":\"detail_up\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":"
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\"{\\\"resourceList\\\":[],\\\"localParams\\\":[{\\\"prop\\\":\\\"datetime\\\",\\\"direct\\\":\\\"IN\\\","
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\\\"type\\\":\\\"VARCHAR\\\",\\\"value\\\":\\\"${system.datetime}\\\"}],\\\"rawScript\\\":"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\\\"echo ${datetime}\\\",\\\"conditionResult\\\":\\\"{\\\\\\\"successNode\\\\\\\":[\\\\\\\"\\\\\\\"],"
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\\\\\\\"failedNode\\\\\\\":[\\\\\\\"\\\\\\\"]}\\\",\\\"dependence\\\":{}}\",\"flag\":0,\"taskPriority\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
+ "\"workerGroup\":\"default\",\"failRetryTimes\":0,\"failRetryInterval\":0,\"timeoutFlag\":0,"
+ "\"timeoutNotifyStrategy\":0,\"timeout\":0,\"delayTime\":0,\"resourceIds\":\"\"}]" ;
List < TaskDefinitionLog > taskDefinitionLogs = JSONUtils . toList ( taskDefinitionJson , TaskDefinitionLog . class ) ;
List < TaskDefinitionLog > taskDefinitionLogs = JSONUtils . toList ( taskDefinitionJson , TaskDefinitionLog . class ) ;
Assert . assertFalse ( taskDefinitionLogs . isEmpty ( ) ) ;
Assert . assertFalse ( taskDefinitionLogs . isEmpty ( ) ) ;
String taskJson = "[{\"name\":\"shell1\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],"
String taskJson =
+ "\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},"
"[{\"name\":\"shell1\",\"description\":\"\",\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],"
+ "\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\",\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\","
+ "\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},"
+ "\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"},{\"name\":\"shell2\",\"description\":\"\","
+ "\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\",\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\","
+ "\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 2\",\"conditionResult\":{\"successNode\""
+ "\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"},{\"name\":\"shell2\",\"description\":\"\","
+ ":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}},\"flag\":\"NORMAL\",\"taskPriority\":\"MEDIUM\",\"workerGroup\":\"default\","
+ "\"taskType\":\"SHELL\",\"taskParams\":{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 2\",\"conditionResult\":{\"successNode\""
+ "\"failRetryTimes\":\"0\",\"failRetryInterval\":\"1\",\"timeoutFlag\":\"CLOSE\",\"timeoutNotifyStrategy\":\"\",\"timeout\":null,\"delayTime\":\"0\"}]" ;
+ ":[\"\"],\"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 ) ;
taskDefinitionLogs = JSONUtils . toList ( taskJson , TaskDefinitionLog . class ) ;
Assert . assertFalse ( taskDefinitionLogs . isEmpty ( ) ) ;
Assert . assertFalse ( taskDefinitionLogs . isEmpty ( ) ) ;
String taskParams = "{\"resourceList\":[],\"localParams\":[{\"prop\":\"datetime\",\"direct\":\"IN\",\"type\":\"VARCHAR\","
String taskParams =
+ "\"value\":\"${system.datetime}\"}],\"rawScript\":\"echo ${datetime}\",\"conditionResult\":\"{\\\"successNode\\\":[\\\"\\\"],"
"{\"resourceList\":[],\"localParams\":[{\"prop\":\"datetime\",\"direct\":\"IN\",\"type\":\"VARCHAR\","
+ "\\\"failedNode\\\":[\\\"\\\"]}\",\"dependence\":{}}" ;
+ "\"value\":\"${system.datetime}\"}],\"rawScript\":\"echo ${datetime}\",\"conditionResult\":\"{\\\"successNode\\\":[\\\"\\\"],"
+ "\\\"failedNode\\\":[\\\"\\\"]}\",\"dependence\":{}}" ;
Map parameters = JSONUtils . parseObject ( taskParams , Map . class ) ;
Map parameters = JSONUtils . parseObject ( taskParams , Map . class ) ;
Assert . assertNotNull ( parameters ) ;
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 ) ;
Map parameters1 = JSONUtils . parseObject ( params , Map . class ) ;
Assert . assertNotNull ( parameters1 ) ;
Assert . assertNotNull ( parameters1 ) ;
}
}
@ -295,43 +300,238 @@ public class TaskDefinitionServiceImplTest {
@Test
@Test
public void testReleaseTaskDefinition ( ) {
public void testReleaseTaskDefinition ( ) {
long projectCode = 1L ;
Mockito . when ( projectMapper . queryByCode ( PROJECT_CODE ) ) . thenReturn ( getProject ( ) ) ;
long taskCode = 1L ;
Project project = getProject ( ) ;
Mockito . when ( projectMapper . queryByCode ( projectCode ) ) . thenReturn ( getProject ( projectCode ) ) ;
Project project = getProject ( projectCode ) ;
User loginUser = new User ( ) ;
loginUser . setId ( 1 ) ;
loginUser . setUserType ( UserType . GENERAL_USER ) ;
// check task dose not exist
// check task dose not exist
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . TASK_DEFINE_NOT_EXIST , taskCode ) ;
putMsg ( result , Status . TASK_DEFINE_NOT_EXIST , TASK_CODE ) ;
Mockito . when ( projectService . checkProjectAndAuth ( loginUser , project , projectCode , null ) ) . thenReturn ( result ) ;
Mockito . when ( projectService . checkProjectAndAuth ( user , project , PROJECT_CODE , null ) ) . thenReturn ( result ) ;
Map < String , Object > map = taskDefinitionService . releaseTaskDefinition ( loginUser , projectCode , taskCode , ReleaseState . OFFLINE ) ;
Map < String , Object > map =
taskDefinitionService . releaseTaskDefinition ( user , PROJECT_CODE , TASK_CODE , ReleaseState . OFFLINE ) ;
Assert . assertEquals ( Status . TASK_DEFINE_NOT_EXIST , map . get ( Constants . STATUS ) ) ;
Assert . assertEquals ( Status . TASK_DEFINE_NOT_EXIST , map . get ( Constants . STATUS ) ) ;
// process definition offline
// process definition offline
putMsg ( result , Status . SUCCESS ) ;
putMsg ( result , Status . SUCCESS ) ;
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
TaskDefinition taskDefinition = new TaskDefinition ( ) ;
taskDefinition . setProjectCode ( projectCode ) ;
taskDefinition . setProjectCode ( PROJECT_CODE ) ;
taskDefinition . setVersion ( 1 ) ;
taskDefinition . setVersion ( 1 ) ;
taskDefinition . setCode ( taskCode ) ;
taskDefinition . setCode ( TASK_CODE ) ;
String params = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ;
String params =
"{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"echo 1\",\"conditionResult\":{\"successNode\":[\"\"],\"failedNode\":[\"\"]},\"dependence\":{}}" ;
taskDefinition . setTaskParams ( params ) ;
taskDefinition . setTaskParams ( params ) ;
taskDefinition . setTaskType ( "SHELL" ) ;
taskDefinition . setTaskType ( "SHELL" ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( taskCode ) ) . thenReturn ( taskDefinition ) ;
Mockito . when ( taskDefinitionMapper . queryByCode ( TASK_CODE ) ) . thenReturn ( taskDefinition ) ;
TaskDefinitionLog taskDefinitionLog = new TaskDefinitionLog ( taskDefinition ) ;
TaskDefinitionLog taskDefinitionLog = new TaskDefinitionLog ( taskDefinition ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( taskCode , taskDefinition . getVersion ( ) ) ) . thenReturn ( taskDefinitionLog ) ;
Mockito . when ( taskDefinitionLogMapper . queryByDefinitionCodeAndVersion ( TASK_CODE , taskDefinition . getVersion ( ) ) )
Map < String , Object > offlineTaskResult = taskDefinitionService . releaseTaskDefinition ( loginUser , projectCode , taskCode , ReleaseState . OFFLINE ) ;
. thenReturn ( taskDefinitionLog ) ;
Map < String , Object > offlineTaskResult =
taskDefinitionService . releaseTaskDefinition ( user , PROJECT_CODE , TASK_CODE , ReleaseState . OFFLINE ) ;
Assert . assertEquals ( Status . SUCCESS , offlineTaskResult . get ( Constants . STATUS ) ) ;
Assert . assertEquals ( Status . SUCCESS , offlineTaskResult . get ( Constants . STATUS ) ) ;
// process definition online, resource exist
// 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 ) ) ;
Assert . assertEquals ( Status . SUCCESS , onlineTaskResult . get ( Constants . STATUS ) ) ;
// release error code
// 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 ) ) ;
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 ;
}
}
}