Tosca for Workflow operations
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / utils / InterfacesOperationsToscaUtilTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.tosca.utils;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import org.junit.Assert;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.openecomp.sdc.be.DummyConfigurationManager;
25 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.InterfaceDefinition;
31 import org.openecomp.sdc.be.model.Resource;
32 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
33 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
34 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
35 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
36
37 /**
38  * @author KATYR
39  * @since April 12, 2018
40  */
41
42 public class InterfacesOperationsToscaUtilTest {
43
44     @BeforeClass
45     public static void setUp() throws Exception {
46         new DummyConfigurationManager();
47     }
48
49
50     @Test
51     public void addInterfaceTypeElement() {
52         Component component = new Resource();
53         component.setNormalizedName("normalizedComponentName");
54         InterfaceDefinition addedInterface = new InterfaceDefinition();
55         addedInterface.setToscaResourceName("interface.types.test_resource_name");
56         addOperationsToInterface(addedInterface, 5, 3, true);
57         final String interfaceType = "normalizedComponentName-interface";
58         ((Resource) component).setInterfaces(new HashMap<>());
59         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
60         final Map<String, Object> interfaceTypeElement =
61                 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
62
63         ToscaExportHandler handler = new ToscaExportHandler();
64         ToscaTemplate template = new ToscaTemplate("test");
65         template.setInterface_types(interfaceTypeElement);
66         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
67
68         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
69         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
70     }
71
72     @Test
73     public void addInterfaceDefinitionElement() {
74         Component component = new Resource();
75         component.setNormalizedName("normalizedComponentName");
76         InterfaceDefinition addedInterface = new InterfaceDefinition();
77         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
78
79         addOperationsToInterface(addedInterface, 3, 2, true);
80         final String interfaceType = "normalizedComponentName-interface";
81         ((Resource) component).setInterfaces(new HashMap<>());
82         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
83         ToscaNodeType nodeType = new ToscaNodeType();
84         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
85
86         ToscaExportHandler handler = new ToscaExportHandler();
87         ToscaTemplate template = new ToscaTemplate("test");
88         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
89         nodeTypes.put("test", nodeType);
90         template.setNode_types(nodeTypes);
91         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
92
93         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
94         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
95         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:"));
96         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
97         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
98     }
99
100     @Test
101     public void addInterfaceDefinitionElement_noInputs() {
102         Component component = new Resource();
103         component.setNormalizedName("normalizedComponentName");
104         InterfaceDefinition addedInterface = new InterfaceDefinition();
105         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");
106
107         addOperationsToInterface(addedInterface, 3, 3, false);
108         final String interfaceType = "normalizedComponentName-interface";
109         ((Resource) component).setInterfaces(new HashMap<>());
110         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
111         ToscaNodeType nodeType = new ToscaNodeType();
112         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
113
114         ToscaExportHandler handler = new ToscaExportHandler();
115         ToscaTemplate template = new ToscaTemplate("test");
116         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
117         nodeTypes.put("test", nodeType);
118         template.setNode_types(nodeTypes);
119         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
120
121         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
122         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("input_"));
123         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
124         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
125         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
126     }
127
128
129     private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
130             boolean hasInputs) {
131
132         addedInterface.setOperations(new HashMap<>());
133         for (int i = 0; i < numOfOps; i++) {
134             final OperationDataDefinition operation = new OperationDataDefinition();
135             operation.setName("name_for_op_" + i);
136             final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
137             implementation.setArtifactName(i + "_createBPMN.bpmn");
138             operation.setImplementation(implementation);
139             if (hasInputs) {
140                 operation.setInputs(createInputs(numOfInputsPerOp));
141             }
142             addedInterface.getOperations().put(operation.getName(), operation);
143         }
144     }
145
146
147     private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
148         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
149         for (int i = 0; i < numOfInputs; i++) {
150             operationInputDefinitionList.add(createMockOperationInputDefinition("input_" + i,
151                     java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i));
152         }
153         return operationInputDefinitionList;
154     }
155
156
157     private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
158         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
159         operationInputDefinition.setName(name);
160         operationInputDefinition.setInputId(id);
161         return operationInputDefinition;
162     }
163 }