aboutsummaryrefslogtreecommitdiffstats
path: root/deprecated-workflow-designer/sdc-workflow-designer-server/src/test/java/org/onap/sdc/workflowdesigner/parser/Bpmn4ToscaJsonParserTest.java
blob: abadd298cac73373cce5312461ff508e95069eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * Copyright (c) 2017-2018 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the Apache License, Version 2.0
 * and the Eclipse Public License v1.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */
package org.onap.sdc.workflowdesigner.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;
import org.onap.sdc.workflowdesigner.model.Element;
import org.onap.sdc.workflowdesigner.model.Position;
import org.onap.sdc.workflowdesigner.model.Process;
import org.onap.sdc.workflowdesigner.model.ScriptTask;
import org.onap.sdc.workflowdesigner.model.SequenceFlow;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class Bpmn4ToscaJsonParserTest {

    private static String RESOURCES_DIR = "src/test/resources/workflow";
    protected static String PROCESS_NAME = "test1";

    @Test
    public void parseTest() throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
        Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
        URI srcUri = Paths.get(RESOURCES_DIR, "workflow.json").toUri();
        Process actualProcess = parser.parse(PROCESS_NAME, srcUri);
        Process expectedProcess = createReferenceProcess();

        assertElements(expectedProcess.getElementList(), actualProcess.getElementList());
        assertSequenceFlows(expectedProcess.getSequenceFlowList(), actualProcess.getSequenceFlowList());
    }

    private static void assertElements(List<Element> expectedElementList, List<Element> actualElementList) {
        assertEquals(expectedElementList.size(), actualElementList.size());

        for (Iterator<Element> iterator = expectedElementList.iterator(); iterator.hasNext();) {
            Element expectedElement = (Element) iterator.next();
            Element actualElement = getElementById(expectedElement.getId(), actualElementList);
            if (actualElement != null) {
                assertElement(expectedElement, actualElement);
            } else {
                fail("Element with id " + expectedElement.getId() + " could not be found");
            }
        }
    }

    private static Element getElementById(String id, List<Element> actualElementList) {
        Iterator<Element> iter = actualElementList.iterator();
        while (iter.hasNext()) {
            Element element = (Element) iter.next();
            if (element.getId().equals(id)) {
                return element;
            }
        }
        return null;
    }

    public static void assertElement(Element expected, Element actual) {
        assertEquals("element: id ", expected.getId(), actual.getId());
        assertEquals("element: name ", expected.getName(), actual.getName());
    }

    public static void assertSequenceFlows(List<SequenceFlow> expectedSequenceList,
            List<SequenceFlow> actualSequenceFlowList) {
        assertEquals(expectedSequenceList.size(), actualSequenceFlowList.size());

        for (Iterator<SequenceFlow> iterator = expectedSequenceList.iterator(); iterator.hasNext();) {
            SequenceFlow expectedElement = (SequenceFlow) iterator.next();
            SequenceFlow actualElement = getSequenceById(expectedElement.getSourceRef(), expectedElement.getTargetRef(),
                    actualSequenceFlowList);
            if (actualElement != null) {
                assertLink(expectedElement, actualElement);
            } else {
                fail("Element with id " + expectedElement.getId() + " could not be found");
            }
        }
    }

    private static SequenceFlow getSequenceById(String sourceRef, String targetRef,
            List<SequenceFlow> actualSequenceFlowList) {
        Iterator<SequenceFlow> iter = actualSequenceFlowList.iterator();
        while (iter.hasNext()) {
            SequenceFlow element = (SequenceFlow) iter.next();
            if (element.getSourceRef().equals(sourceRef) && element.getTargetRef().equals(targetRef)) {
                return element;
            }
        }
        return null;
    }

    public static void assertLink(SequenceFlow expectedLink, SequenceFlow actualLink) {
        assertEquals("Link source: id", expectedLink.getSourceRef(), actualLink.getSourceRef());
        assertEquals("Link target :id", expectedLink.getTargetRef(), actualLink.getTargetRef());
    }

    private static Process createReferenceProcess() {
        Process process = new Process(PROCESS_NAME);
        
        ScriptTask scriptTask = new ScriptTask();
        scriptTask.setId("scriptTask");
        scriptTask.setName("Script Task");
        Position position = new Position();
        position.setLeft(328);
        position.setTop(134);
        scriptTask.setPosition(position);
        scriptTask.setScript("");
        scriptTask.setScriptFormat("JavaScript");
        process.getElementList().add(scriptTask);

        return process;
    }

}