|
26 | 26 | import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.WORKFLOW_TREE_VIEW; |
27 | 27 | import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.WORKFLOW_UPDATE; |
28 | 28 | import static org.apache.dolphinscheduler.common.constants.Constants.EMPTY_STRING; |
| 29 | +import static org.mockito.ArgumentMatchers.any; |
| 30 | +import static org.mockito.ArgumentMatchers.anyList; |
| 31 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 32 | +import static org.mockito.ArgumentMatchers.anyString; |
| 33 | +import static org.mockito.ArgumentMatchers.eq; |
29 | 34 | import static org.mockito.ArgumentMatchers.isA; |
30 | 35 | import static org.mockito.Mockito.doNothing; |
31 | 36 | import static org.mockito.Mockito.doThrow; |
32 | 37 | import static org.mockito.Mockito.times; |
33 | 38 | import static org.mockito.Mockito.when; |
34 | 39 |
|
| 40 | +import org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant; |
35 | 41 | import org.apache.dolphinscheduler.api.dto.workflow.WorkflowCreateRequest; |
36 | 42 | import org.apache.dolphinscheduler.api.dto.workflow.WorkflowFilterRequest; |
37 | 43 | import org.apache.dolphinscheduler.api.dto.workflow.WorkflowUpdateRequest; |
|
63 | 69 | import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper; |
64 | 70 | import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; |
65 | 71 | import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper; |
| 72 | +import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper; |
66 | 73 | import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionMapper; |
67 | 74 | import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
68 | 75 | import org.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionLogMapper; |
|
79 | 86 | import org.apache.commons.lang3.StringUtils; |
80 | 87 |
|
81 | 88 | import java.io.ByteArrayOutputStream; |
| 89 | +import java.io.IOException; |
| 90 | +import java.net.URISyntaxException; |
82 | 91 | import java.nio.charset.StandardCharsets; |
| 92 | +import java.nio.file.Files; |
| 93 | +import java.nio.file.Path; |
| 94 | +import java.nio.file.Paths; |
83 | 95 | import java.text.MessageFormat; |
84 | 96 | import java.util.ArrayList; |
85 | 97 | import java.util.Arrays; |
|
105 | 117 | import org.mockito.Mockito; |
106 | 118 | import org.mockito.junit.jupiter.MockitoExtension; |
107 | 119 | import org.springframework.mock.web.MockMultipartFile; |
| 120 | +import org.springframework.web.multipart.MultipartFile; |
108 | 121 |
|
109 | 122 | import com.google.common.collect.Lists; |
110 | 123 |
|
@@ -182,6 +195,9 @@ public class WorkflowDefinitionServiceTest extends BaseServiceTestTool { |
182 | 195 | @Mock |
183 | 196 | private WorkflowDefinitionLogDao workflowDefinitionLogDao; |
184 | 197 |
|
| 198 | + @Mock |
| 199 | + private TaskDefinitionLogMapper taskDefinitionLogMapper; |
| 200 | + |
185 | 201 | @Mock |
186 | 202 | private UserMapper userMapper; |
187 | 203 |
|
@@ -343,11 +359,11 @@ public void testQueryWorkflowDefinitionListPaging() { |
343 | 359 | .totalCount(30) |
344 | 360 | .build(); |
345 | 361 | when(workflowDefinitionDao.listingWorkflowDefinition( |
346 | | - Mockito.eq(0), |
347 | | - Mockito.eq(10), |
348 | | - Mockito.eq(""), |
349 | | - Mockito.eq(1), |
350 | | - Mockito.eq(projectCode))).thenReturn(pageListingResult); |
| 362 | + eq(0), |
| 363 | + eq(10), |
| 364 | + eq(""), |
| 365 | + eq(1), |
| 366 | + eq(projectCode))).thenReturn(pageListingResult); |
351 | 367 | String user1 = "user1"; |
352 | 368 | String user2 = "user2"; |
353 | 369 | when(userMapper.queryUserWithWorkflowDefinitionCode(processDefinitionCodes)) |
@@ -403,7 +419,7 @@ public void testQueryWorkflowDefinitionByCode() { |
403 | 419 | when(projectService.checkProjectAndAuth(user, project, projectCode, WORKFLOW_DEFINITION)) |
404 | 420 | .thenReturn(result); |
405 | 421 | DagData dagData = new DagData(getWorkflowDefinition(), null, null); |
406 | | - when(processService.genDagData(Mockito.any())).thenReturn(dagData); |
| 422 | + when(processService.genDagData(any())).thenReturn(dagData); |
407 | 423 |
|
408 | 424 | Map<String, Object> instanceNotexitRes = |
409 | 425 | processDefinitionService.queryWorkflowDefinitionByCode(user, projectCode, 1L); |
@@ -743,7 +759,7 @@ public void testGetTaskNodeListByDefinitionCode() { |
743 | 759 | // success |
744 | 760 | WorkflowDefinition workflowDefinition = getWorkflowDefinition(); |
745 | 761 | putMsg(result, Status.SUCCESS, projectCode); |
746 | | - when(processService.genDagData(Mockito.any())).thenReturn(new DagData(workflowDefinition, null, null)); |
| 762 | + when(processService.genDagData(any())).thenReturn(new DagData(workflowDefinition, null, null)); |
747 | 763 | when(workflowDefinitionMapper.queryByCode(46L)).thenReturn(workflowDefinition); |
748 | 764 | Map<String, Object> dataNotValidRes = |
749 | 765 | processDefinitionService.getTaskNodeListByDefinitionCode(user, projectCode, 46L); |
@@ -774,7 +790,7 @@ public void testGetTaskNodeListByDefinitionCodes() { |
774 | 790 | workflowDefinitionList.add(workflowDefinition); |
775 | 791 |
|
776 | 792 | when(workflowDefinitionMapper.queryByCodes(defineCodeSet)).thenReturn(workflowDefinitionList); |
777 | | - when(processService.genDagData(Mockito.any())).thenReturn(new DagData(workflowDefinition, null, null)); |
| 793 | + when(processService.genDagData(any())).thenReturn(new DagData(workflowDefinition, null, null)); |
778 | 794 | Project project1 = getProject(projectCode); |
779 | 795 | List<Project> projects = new ArrayList<>(); |
780 | 796 | projects.add(project1); |
@@ -881,7 +897,7 @@ public void testBatchExportWorkflowDefinitionByCodes() { |
881 | 897 | HttpServletResponse response = Mockito.mock(HttpServletResponse.class); |
882 | 898 |
|
883 | 899 | DagData dagData = new DagData(getWorkflowDefinition(), null, null); |
884 | | - when(processService.genDagData(Mockito.any())).thenReturn(dagData); |
| 900 | + when(processService.genDagData(any())).thenReturn(dagData); |
885 | 901 | processDefinitionService.batchExportWorkflowDefinitionByCodes(user, projectCode, "1", response); |
886 | 902 | Assertions.assertNotNull(processDefinitionService.exportWorkflowDagData(workflowDefinition)); |
887 | 903 | } |
@@ -917,13 +933,13 @@ public void testImportSqlWorkflowDefinition() throws Exception { |
917 | 933 | when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode)); |
918 | 934 | when(projectService.checkProjectAndAuth(user, project, projectCode, WORKFLOW_IMPORT)) |
919 | 935 | .thenReturn(result); |
920 | | - when(processService.saveTaskDefine(Mockito.same(user), Mockito.eq(projectCode), Mockito.notNull(), |
| 936 | + when(processService.saveTaskDefine(Mockito.same(user), eq(projectCode), Mockito.notNull(), |
921 | 937 | Mockito.anyBoolean())).thenReturn(2); |
922 | 938 | when(processService.saveWorkflowDefine(Mockito.same(user), Mockito.notNull(), Mockito.notNull(), |
923 | 939 | Mockito.anyBoolean())).thenReturn(1); |
924 | 940 | when( |
925 | | - processService.saveTaskRelation(Mockito.same(user), Mockito.eq(projectCode), Mockito.anyLong(), |
926 | | - Mockito.eq(1), Mockito.notNull(), Mockito.notNull(), Mockito.anyBoolean())) |
| 941 | + processService.saveTaskRelation(Mockito.same(user), eq(projectCode), anyLong(), |
| 942 | + eq(1), Mockito.notNull(), Mockito.notNull(), Mockito.anyBoolean())) |
927 | 943 | .thenReturn(0); |
928 | 944 | result = processDefinitionService.importSqlWorkflowDefinition(user, projectCode, mockMultipartFile); |
929 | 945 |
|
@@ -991,8 +1007,8 @@ public void testCreateWorkflowDefinitionV2() { |
991 | 1007 | workflowCreateRequest.setReleaseState(releaseState); |
992 | 1008 | workflowCreateRequest.setWarningGroupId(warningGroupId); |
993 | 1009 | workflowCreateRequest.setExecutionType(executionType); |
994 | | - when(workflowDefinitionLogMapper.insert(Mockito.any())).thenReturn(1); |
995 | | - when(workflowDefinitionMapper.insert(Mockito.any())).thenReturn(1); |
| 1010 | + when(workflowDefinitionLogMapper.insert(any())).thenReturn(1); |
| 1011 | + when(workflowDefinitionMapper.insert(any())).thenReturn(1); |
996 | 1012 | WorkflowDefinition workflowDefinition = |
997 | 1013 | processDefinitionService.createSingleWorkflowDefinition(user, workflowCreateRequest); |
998 | 1014 |
|
@@ -1109,7 +1125,7 @@ public void testUpdateWorkflowDefinitionV2() { |
1109 | 1125 | // error update process definition mapper |
1110 | 1126 | workflowUpdateRequest.setName(name); |
1111 | 1127 | when(workflowDefinitionMapper.queryByCode(processDefinitionCode)).thenReturn(workflowDefinition); |
1112 | | - when(workflowDefinitionLogMapper.insert(Mockito.any())).thenReturn(1); |
| 1128 | + when(workflowDefinitionLogMapper.insert(any())).thenReturn(1); |
1113 | 1129 | exception = Assertions.assertThrows(ServiceException.class, () -> processDefinitionService |
1114 | 1130 | .updateSingleWorkflowDefinition(user, processDefinitionCode, workflowUpdateRequest)); |
1115 | 1131 | Assertions.assertEquals(Status.UPDATE_WORKFLOW_DEFINITION_ERROR.getCode(), |
@@ -1254,4 +1270,115 @@ private List<TaskMainInfo> getTaskMainInfo() { |
1254 | 1270 | taskMainInfos.add(taskMainInfo); |
1255 | 1271 | return taskMainInfos; |
1256 | 1272 | } |
| 1273 | + |
| 1274 | + @Test |
| 1275 | + public void testImportWorkflowDefinitionWithoutProjectAuth() { |
| 1276 | + Project project = this.getProject(projectCode); |
| 1277 | + Map<String, Object> successResult = new HashMap<>(); |
| 1278 | + putMsg(successResult, Status.SUCCESS); |
| 1279 | + MultipartFile file = new MockMultipartFile( |
| 1280 | + "file", "", "application/json", "".getBytes()); |
| 1281 | + Map<String, Object> checkProjectPermResult1 = new HashMap<>(); |
| 1282 | + putMsg(checkProjectPermResult1, Status.USER_NO_OPERATION_PROJECT_PERM); |
| 1283 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1284 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), WORKFLOW_IMPORT)) |
| 1285 | + .thenReturn(checkProjectPermResult1); |
| 1286 | + Map<String, Object> checkProjectPermResult = processDefinitionService.importWorkflowDefinition( |
| 1287 | + user, projectCode, file); |
| 1288 | + Assertions.assertEquals( |
| 1289 | + checkProjectPermResult.get(Constants.STATUS), checkProjectPermResult1.get(Constants.STATUS)); |
| 1290 | + } |
| 1291 | + |
| 1292 | + @Test |
| 1293 | + public void testImportWorkflowDefinitionWithEmptyFileContent() { |
| 1294 | + Project project = this.getProject(projectCode); |
| 1295 | + Map<String, Object> successResult = new HashMap<>(); |
| 1296 | + putMsg(successResult, Status.SUCCESS); |
| 1297 | + MultipartFile file = new MockMultipartFile("file", "", "application/json", "".getBytes()); |
| 1298 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1299 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), WORKFLOW_IMPORT)) |
| 1300 | + .thenReturn(successResult); |
| 1301 | + Map<String, Object> result = processDefinitionService.importWorkflowDefinition(user, projectCode, file); |
| 1302 | + Assertions.assertEquals(Status.DATA_IS_NULL, result.get(Constants.STATUS)); |
| 1303 | + } |
| 1304 | + |
| 1305 | + @Test |
| 1306 | + public void testImportWorkflowDefinitionWhenMissImportanceParams() throws URISyntaxException, IOException { |
| 1307 | + Project project = this.getProject(projectCode); |
| 1308 | + Map<String, Object> successResult = new HashMap<>(); |
| 1309 | + putMsg(successResult, Status.SUCCESS); |
| 1310 | + // miss workflowTaskRelationList |
| 1311 | + MultipartFile checkImportanceParamsFile = createMultipartFile("workflowImport/check_importance_params.json"); |
| 1312 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1313 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), WORKFLOW_IMPORT)) |
| 1314 | + .thenReturn(successResult); |
| 1315 | + Map<String, Object> checkImportanceParamsResult = processDefinitionService.importWorkflowDefinition( |
| 1316 | + user, projectCode, checkImportanceParamsFile); |
| 1317 | + Assertions.assertEquals(Status.DATA_IS_NULL, checkImportanceParamsResult.get(Constants.STATUS)); |
| 1318 | + } |
| 1319 | + |
| 1320 | + @Test |
| 1321 | + public void testImportWorkflowDefinitionWhenNameExist() throws URISyntaxException, IOException { |
| 1322 | + Project project = this.getProject(projectCode); |
| 1323 | + Map<String, Object> successResult = new HashMap<>(); |
| 1324 | + putMsg(successResult, Status.SUCCESS); |
| 1325 | + MultipartFile checkDuplicateNameFile = createMultipartFile("workflowImport/check_duplicate_name.json"); |
| 1326 | + Map<String, Object> verifyNameResult = new HashMap<>(); |
| 1327 | + putMsg(verifyNameResult, Status.WORKFLOW_DEFINITION_NAME_EXIST); |
| 1328 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1329 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), WORKFLOW_IMPORT)) |
| 1330 | + .thenReturn(successResult); |
| 1331 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1332 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), WORKFLOW_CREATE)) |
| 1333 | + .thenReturn(successResult); |
| 1334 | + WorkflowDefinition workflowDefinition = new WorkflowDefinition(); |
| 1335 | + workflowDefinition.setCode(2); |
| 1336 | + workflowDefinition.setName("workflow1"); |
| 1337 | + when(workflowDefinitionMapper.verifyByDefineName(eq(projectCode), anyString())) |
| 1338 | + .thenReturn(workflowDefinition); |
| 1339 | + Map<String, Object> checkDuplicateNameResult = processDefinitionService.importWorkflowDefinition( |
| 1340 | + user, projectCode, checkDuplicateNameFile); |
| 1341 | + Assertions.assertEquals(Status.WORKFLOW_DEFINITION_NAME_EXIST, checkDuplicateNameResult.get(Constants.STATUS)); |
| 1342 | + } |
| 1343 | + |
| 1344 | + @Test |
| 1345 | + public void testImportWorkflowDefinitionSuccessful() throws URISyntaxException, IOException { |
| 1346 | + Project project = this.getProject(projectCode); |
| 1347 | + Map<String, Object> successResult = new HashMap<>(); |
| 1348 | + putMsg(successResult, Status.SUCCESS); |
| 1349 | + MultipartFile successfulFile = createMultipartFile("workflowImport/check_successful.json"); |
| 1350 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1351 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), |
| 1352 | + ApiFuncIdentificationConstant.WORKFLOW_IMPORT)) |
| 1353 | + .thenReturn(successResult); |
| 1354 | + when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
| 1355 | + when(projectService.checkProjectAndAuth(user, project, project.getCode(), WORKFLOW_CREATE)) |
| 1356 | + .thenReturn(successResult); |
| 1357 | + when(workflowDefinitionMapper.verifyByDefineName(eq(projectCode), anyString())) |
| 1358 | + .thenReturn(null); |
| 1359 | + when(taskDefinitionMapper.batchInsert(anyList())).thenReturn(1); |
| 1360 | + when(taskDefinitionLogMapper.batchInsert(anyList())).thenReturn(1); |
| 1361 | + WorkflowDefinition successWorkflowDef = new WorkflowDefinition(); |
| 1362 | + successWorkflowDef.setCode(123); |
| 1363 | + when(workflowDefinitionMapper.queryByCode(anyLong())).thenReturn(successWorkflowDef); |
| 1364 | + when(scheduleMapper.insert(any())).thenReturn(1); |
| 1365 | + when(processService.saveWorkflowDefine(eq(user), any(), eq(true), eq(true))) |
| 1366 | + .thenReturn(Constants.VERSION_FIRST); |
| 1367 | + Map<String, Object> successfulResul = processDefinitionService.importWorkflowDefinition( |
| 1368 | + user, 1L, successfulFile); |
| 1369 | + Assertions.assertEquals(Status.SUCCESS, successfulResul.get(Constants.STATUS)); |
| 1370 | + } |
| 1371 | + |
| 1372 | + private MultipartFile createMultipartFile(String filePath) throws URISyntaxException, IOException { |
| 1373 | + Path path = Paths.get(getClass().getClassLoader().getResource(filePath).toURI()); |
| 1374 | + byte[] content = Files.readAllBytes(path); |
| 1375 | + |
| 1376 | + // 2. 创建MockMultipartFile对象 |
| 1377 | + MultipartFile multipartFile = new MockMultipartFile( |
| 1378 | + "file", |
| 1379 | + "", |
| 1380 | + "application/json", |
| 1381 | + content); |
| 1382 | + return multipartFile; |
| 1383 | + } |
1257 | 1384 | } |
0 commit comments