|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +packageorg.apache.dolphinscheduler.api.service.impl; |
| 19 | + |
| 20 | +importstaticorg.assertj.core.api.Assertions.assertThat; |
| 21 | +importstaticorg.mockito.Mockito.verify; |
| 22 | +importstaticorg.mockito.Mockito.verifyNoInteractions; |
| 23 | +importstaticorg.mockito.Mockito.when; |
| 24 | + |
| 25 | +importorg.apache.dolphinscheduler.common.constants.Constants; |
| 26 | +importorg.apache.dolphinscheduler.dao.entity.DependentWorkflowDefinition; |
| 27 | +importorg.apache.dolphinscheduler.dao.entity.TaskDefinition; |
| 28 | +importorg.apache.dolphinscheduler.dao.entity.WorkflowDefinition; |
| 29 | +importorg.apache.dolphinscheduler.dao.entity.WorkflowTaskLineage; |
| 30 | +importorg.apache.dolphinscheduler.dao.mapper.TaskDefinitionMapper; |
| 31 | +importorg.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionMapper; |
| 32 | +importorg.apache.dolphinscheduler.dao.repository.WorkflowTaskLineageDao; |
| 33 | + |
| 34 | +importjava.util.Arrays; |
| 35 | +importjava.util.Collections; |
| 36 | +importjava.util.List; |
| 37 | + |
| 38 | +importorg.junit.jupiter.api.BeforeEach; |
| 39 | +importorg.junit.jupiter.api.Test; |
| 40 | +importorg.junit.jupiter.api.extension.ExtendWith; |
| 41 | +importorg.mockito.InjectMocks; |
| 42 | +importorg.mockito.Mock; |
| 43 | +importorg.mockito.junit.jupiter.MockitoExtension; |
| 44 | +importorg.springframework.test.util.ReflectionTestUtils; |
| 45 | + |
| 46 | +@ExtendWith(MockitoExtension.class) |
| 47 | +classWorkflowLineageServiceImplTest { |
| 48 | + |
| 49 | +@InjectMocks |
| 50 | +privateWorkflowLineageServiceImplworkflowLineageService; |
| 51 | + |
| 52 | +@Mock |
| 53 | +privateWorkflowTaskLineageDaoworkflowTaskLineageDao; |
| 54 | + |
| 55 | +@Mock |
| 56 | +privateWorkflowDefinitionMapperworkflowDefinitionMapper; |
| 57 | + |
| 58 | +@Mock |
| 59 | +privateTaskDefinitionMappertaskDefinitionMapper; |
| 60 | + |
| 61 | +@BeforeEach |
| 62 | +voidsetUp() { |
| 63 | +ReflectionTestUtils.setField(workflowLineageService,"workflowTaskLineageDao",workflowTaskLineageDao); |
| 64 | +ReflectionTestUtils.setField(workflowLineageService,"workflowDefinitionMapper",workflowDefinitionMapper); |
| 65 | +ReflectionTestUtils.setField(workflowLineageService,"taskDefinitionMapper",taskDefinitionMapper); |
| 66 | + } |
| 67 | + |
| 68 | +@Test |
| 69 | +voidshouldReturnEmptyListWhenNoLineageExist() { |
| 70 | +longworkflowCode =100L; |
| 71 | +when(workflowTaskLineageDao |
| 72 | + .queryWorkFlowLineageByDept(Constants.DEFAULT_PROJECT_CODE,workflowCode,Constants.DEPENDENT_ALL_TASK)) |
| 73 | + .thenReturn(Collections.emptyList()); |
| 74 | + |
| 75 | +List<DependentWorkflowDefinition>result = |
| 76 | +workflowLineageService.queryDownstreamDependentWorkflowDefinitions(workflowCode); |
| 77 | + |
| 78 | +assertThat(result).isEmpty(); |
| 79 | +verifyNoInteractions(workflowDefinitionMapper,taskDefinitionMapper); |
| 80 | + } |
| 81 | + |
| 82 | +@Test |
| 83 | +voidshouldBuildDependentWorkflowDefinitions() { |
| 84 | +longupstreamWorkflowCode =1L; |
| 85 | + |
| 86 | +WorkflowTaskLineagetaskLineage =newWorkflowTaskLineage(); |
| 87 | +taskLineage.setWorkflowDefinitionCode(200L); |
| 88 | +taskLineage.setDeptWorkflowDefinitionCode(upstreamWorkflowCode); |
| 89 | +taskLineage.setTaskDefinitionCode(300L); |
| 90 | +taskLineage.setDeptTaskDefinitionCode(0L); |
| 91 | + |
| 92 | +WorkflowTaskLineageworkflowLineage =newWorkflowTaskLineage(); |
| 93 | +workflowLineage.setWorkflowDefinitionCode(201L); |
| 94 | +workflowLineage.setDeptWorkflowDefinitionCode(upstreamWorkflowCode); |
| 95 | +workflowLineage.setTaskDefinitionCode(0L); |
| 96 | + |
| 97 | +when(workflowTaskLineageDao |
| 98 | + .queryWorkFlowLineageByDept(Constants.DEFAULT_PROJECT_CODE,upstreamWorkflowCode, |
| 99 | +Constants.DEPENDENT_ALL_TASK)) |
| 100 | + .thenReturn(Arrays.asList(taskLineage,workflowLineage)); |
| 101 | + |
| 102 | +WorkflowDefinitionworkflowDefinition200 =newWorkflowDefinition(); |
| 103 | +workflowDefinition200.setCode(200L); |
| 104 | +workflowDefinition200.setVersion(3); |
| 105 | + |
| 106 | +WorkflowDefinitionworkflowDefinition201 =newWorkflowDefinition(); |
| 107 | +workflowDefinition201.setCode(201L); |
| 108 | +workflowDefinition201.setVersion(4); |
| 109 | + |
| 110 | +when(workflowDefinitionMapper.queryByCodes(Arrays.asList(200L,201L))) |
| 111 | + .thenReturn(Arrays.asList(workflowDefinition200,workflowDefinition201)); |
| 112 | + |
| 113 | +TaskDefinitiontaskDefinition =newTaskDefinition(); |
| 114 | +taskDefinition.setCode(300L); |
| 115 | +taskDefinition.setTaskParams("task-params"); |
| 116 | +taskDefinition.setWorkerGroup("test-group"); |
| 117 | + |
| 118 | +when(taskDefinitionMapper.queryByCodeList(Collections.singletonList(300L))) |
| 119 | + .thenReturn(Collections.singletonList(taskDefinition)); |
| 120 | + |
| 121 | +List<DependentWorkflowDefinition>result = |
| 122 | +workflowLineageService.queryDownstreamDependentWorkflowDefinitions(upstreamWorkflowCode); |
| 123 | + |
| 124 | +assertThat(result).hasSize(2); |
| 125 | + |
| 126 | +DependentWorkflowDefinitiontaskDependent =result.stream() |
| 127 | + .filter(dependent ->dependent.getWorkflowDefinitionCode() ==200L) |
| 128 | + .findFirst() |
| 129 | + .orElseThrow(() ->newAssertionError("Expected DependentWorkflowDefinition with code 200 not found")); |
| 130 | +assertThat(taskDependent.getTaskDefinitionCode()).isEqualTo(300L); |
| 131 | +assertThat(taskDependent.getTaskParams()).isEqualTo("task-params"); |
| 132 | +assertThat(taskDependent.getWorkerGroup()).isEqualTo("test-group"); |
| 133 | +assertThat(taskDependent.getWorkflowDefinitionVersion()).isEqualTo(3); |
| 134 | + |
| 135 | +DependentWorkflowDefinitionworkflowDependent =result.stream() |
| 136 | + .filter(dependent ->dependent.getWorkflowDefinitionCode() ==201L) |
| 137 | + .findFirst() |
| 138 | + .orElseThrow(() ->newAssertionError("Expected DependentWorkflowDefinition with code 201 not found")); |
| 139 | +assertThat(workflowDependent.getTaskDefinitionCode()).isEqualTo(0L); |
| 140 | +assertThat(workflowDependent.getTaskParams()).isNull(); |
| 141 | +assertThat(workflowDependent.getWorkerGroup()).isNull(); |
| 142 | +assertThat(workflowDependent.getWorkflowDefinitionVersion()).isEqualTo(4); |
| 143 | + |
| 144 | +verify(taskDefinitionMapper).queryByCodeList(Collections.singletonList(300L)); |
| 145 | + } |
| 146 | +} |