summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java
blob: 67b27f678fd4e3ce7011b53dfb04f661c4c29623 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed 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.
 */

package org.openecomp.sdc.be.tosca.utils;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openecomp.sdc.be.DummyConfigurationManager;
import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.tosca.ToscaRepresentation;
import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
import org.openecomp.sdc.be.tosca.model.ToscaTemplate;

/**
 * @author KATYR
 * @since April 12, 2018
 */

public class InterfacesOperationsToscaUtilTest {

    @BeforeClass
    public static void setUp() {
        new DummyConfigurationManager();
    }


    @Test
    public void addInterfaceTypeElement() {
        Component component = new Resource();
        component.setNormalizedName("normalizedComponentName");
        InterfaceDefinition addedInterface = new InterfaceDefinition();
        addedInterface.setToscaResourceName("interface.types.test_resource_name");
        addOperationsToInterface(addedInterface, 5, 3, true);
        final String interfaceType = "normalizedComponentName-interface";
        ((Resource) component).setInterfaces(new HashMap<>());
        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
        final Map<String, Object> interfaceTypeElement =
                InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);

        ToscaExportHandler handler = new ToscaExportHandler();
        ToscaTemplate template = new ToscaTemplate("test");
        template.setInterface_types(interfaceTypeElement);
        final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);

        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
    }

    @Test
    public void addInterfaceDefinitionElement() {
        Component component = new Resource();
        component.setNormalizedName("normalizedComponentName");
        InterfaceDefinition addedInterface = new InterfaceDefinition();
        addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");

        addOperationsToInterface(addedInterface, 3, 2, true);
        final String interfaceType = "normalizedComponentName-interface";
        ((Resource) component).setInterfaces(new HashMap<>());
        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
        ToscaNodeType nodeType = new ToscaNodeType();
        InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);

        ToscaExportHandler handler = new ToscaExportHandler();
        ToscaTemplate template = new ToscaTemplate("test");
        Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
        nodeTypes.put("test", nodeType);
        template.setNode_types(nodeTypes);
        final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);

        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:"));
        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
    }

    @Test
    public void addInterfaceDefinitionElement_noInputs() {
        Component component = new Resource();
        component.setNormalizedName("normalizedComponentName");
        InterfaceDefinition addedInterface = new InterfaceDefinition();
        addedInterface.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");

        addOperationsToInterface(addedInterface, 3, 3, false);
        final String interfaceType = "normalizedComponentName-interface";
        ((Resource) component).setInterfaces(new HashMap<>());
        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
        ToscaNodeType nodeType = new ToscaNodeType();
        InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);

        ToscaExportHandler handler = new ToscaExportHandler();
        ToscaTemplate template = new ToscaTemplate("test");
        Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
        nodeTypes.put("test", nodeType);
        template.setNode_types(nodeTypes);
        final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);

        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("input_"));
        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
    }


    private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
            boolean hasInputs) {

        addedInterface.setOperations(new HashMap<>());
        for (int i = 0; i < numOfOps; i++) {
            final OperationDataDefinition operation = new OperationDataDefinition();
            operation.setName("name_for_op_" + i);
            operation.setDescription( "op "+i+" has description");
            final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
            implementation.setArtifactName(i + "_createBPMN.bpmn");
            operation.setImplementation(implementation);
            if (hasInputs) {
                operation.setInputs(createInputs(numOfInputsPerOp));
            }
            addedInterface.getOperations().put(operation.getName(), operation);
        }
    }


    private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
        ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
        for (int i = 0; i < numOfInputs; i++) {
            operationInputDefinitionList.add(createMockOperationInputDefinition("input_" + i,
                    java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i));
        }
        return operationInputDefinitionList;
    }


    private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
        OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
        operationInputDefinition.setName(name);
        operationInputDefinition.setInputId(id);
        return operationInputDefinition;
    }
}