WorkFlowLineageServiceTest.java 5.2 KB
Newer Older
L
LiemLin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17

L
LiemLin 已提交
18 19
package org.apache.dolphinscheduler.api.service;

20 21 22
import static org.mockito.Mockito.when;

import org.apache.dolphinscheduler.api.service.impl.WorkFlowLineageServiceImpl;
L
LiemLin 已提交
23
import org.apache.dolphinscheduler.common.Constants;
24 25
import org.apache.dolphinscheduler.dao.entity.ProcessLineage;
import org.apache.dolphinscheduler.dao.entity.Project;
L
LiemLin 已提交
26 27
import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage;
import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation;
28
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
L
LiemLin 已提交
29
import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper;
30 31

import java.util.ArrayList;
32
import java.util.Collection;
33 34 35 36
import java.util.List;
import java.util.Map;
import java.util.Set;

L
LiemLin 已提交
37 38 39 40 41
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
42
import org.mockito.Mockito;
L
LiemLin 已提交
43 44
import org.mockito.junit.MockitoJUnitRunner;

45 46 47
/**
 * work flow lineage service test
 */
L
LiemLin 已提交
48 49 50 51
@RunWith(MockitoJUnitRunner.class)
public class WorkFlowLineageServiceTest {

    @InjectMocks
52
    private WorkFlowLineageServiceImpl workFlowLineageService;
L
LiemLin 已提交
53 54 55 56

    @Mock
    private WorkFlowLineageMapper workFlowLineageMapper;

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    @Mock
    private ProjectMapper projectMapper;

    /**
     * get mock Project
     *
     * @param projectName projectName
     * @return Project
     */
    private Project getProject(String projectName) {
        Project project = new Project();
        project.setCode(1L);
        project.setId(1);
        project.setName(projectName);
        project.setUserId(1);
        return project;
    }

L
LiemLin 已提交
75 76
    @Test
    public void testQueryWorkFlowLineageByName() {
77
        Project project = getProject("test");
78
        String name = "test";
79
        when(projectMapper.queryByCode(1L)).thenReturn(project);
80 81
        when(workFlowLineageMapper.queryWorkFlowLineageByName(Mockito.anyLong(), Mockito.any())).thenReturn(getWorkFlowLineages());
        Map<String, Object> result = workFlowLineageService.queryWorkFlowLineageByName(1L, name);
82 83
        List<WorkFlowLineage> workFlowLineageList = (List<WorkFlowLineage>) result.get(Constants.DATA_LIST);
        Assert.assertTrue(workFlowLineageList.size() > 0);
L
LiemLin 已提交
84 85 86
    }

    @Test
87
    public void testQueryWorkFlowLineage() {
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        Project project = getProject("test");

        List<ProcessLineage> processLineages = new ArrayList<>();
        ProcessLineage processLineage = new ProcessLineage();
        processLineage.setPreTaskVersion(1);
        processLineage.setPreTaskCode(1L);
        processLineage.setPostTaskCode(2L);
        processLineage.setPostTaskVersion(1);
        processLineage.setProcessDefinitionCode(1111L);
        processLineage.setProcessDefinitionVersion(1);
        processLineage.setProjectCode(1111L);
        processLineages.add(processLineage);

        WorkFlowLineage workFlowLineage = new WorkFlowLineage();
102
        workFlowLineage.setSourceWorkFlowCode("");
103

104
        when(projectMapper.queryByCode(1L)).thenReturn(project);
105 106 107 108 109 110 111
        when(workFlowLineageMapper.queryProcessLineage(project.getCode())).thenReturn(processLineages);
        when(workFlowLineageMapper.queryCodeRelation(processLineage.getProjectCode(), processLineage.getProcessDefinitionCode(),
            processLineage.getPostTaskCode(), processLineage.getPreTaskVersion())).thenReturn(processLineages);
        when(workFlowLineageMapper.queryWorkFlowLineageByCode(processLineage.getProjectCode(), processLineage.getProcessDefinitionCode()))
            .thenReturn(workFlowLineage);

        Map<String, Object> result = workFlowLineageService.queryWorkFlowLineage(1L);
112 113 114 115 116 117

        Map<String, Object> workFlowLists = (Map<String, Object>) result.get(Constants.DATA_LIST);
        Collection<WorkFlowLineage> workFlowLineages = (Collection<WorkFlowLineage>) workFlowLists.get(Constants.WORKFLOW_LIST);
        Set<WorkFlowRelation> workFlowRelations = (Set<WorkFlowRelation>) workFlowLists.get(Constants.WORKFLOW_RELATION_LIST);
        Assert.assertTrue(workFlowLineages.size() > 0);
        Assert.assertTrue(workFlowRelations.size() > 0);
L
LiemLin 已提交
118 119 120 121 122
    }

    private List<WorkFlowLineage> getWorkFlowLineages() {
        List<WorkFlowLineage> workFlowLineages = new ArrayList<>();
        WorkFlowLineage workFlowLineage = new WorkFlowLineage();
123
        workFlowLineage.setWorkFlowCode(1);
L
LiemLin 已提交
124 125 126 127 128 129
        workFlowLineage.setWorkFlowName("testdag");
        workFlowLineages.add(workFlowLineage);
        return workFlowLineages;
    }

}