0c5db04bbd3f29fccab555a03ebd9db52182f2a1
[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
22 import org.junit.Assert;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.DummyConfigurationManager;
26 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.InterfaceDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
35 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
36 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
37 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
38
39 public class InterfacesOperationsToscaUtilTest {
40
41     @BeforeClass
42     public static void setUp() {
43         new DummyConfigurationManager();
44     }
45
46
47     @Test
48     public void addInterfaceTypeElement() {
49         Component component = new Resource();
50         component.setNormalizedName("normalizedComponentName");
51         InterfaceDefinition addedInterface = new InterfaceDefinition();
52         addedInterface.setToscaResourceName("interface.types.test_resource_name");
53         addOperationsToInterface(addedInterface, 5, 3, 0, true, false);
54         final String interfaceType = "normalizedComponentName-interface";
55         ((Resource) component).setInterfaces(new HashMap<>());
56         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
57         final Map<String, Object> interfaceTypeElement =
58                 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
59
60         ToscaExportHandler handler = new ToscaExportHandler();
61         ToscaTemplate template = new ToscaTemplate("test");
62         template.setInterface_types(interfaceTypeElement);
63         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
64
65         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
66         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
67     }
68
69     @Test
70     public void addInterfaceDefinitionElement() {
71         Component component = new Resource();
72         component.setNormalizedName("normalizedComponentName");
73         InterfaceDefinition addedInterface = new InterfaceDefinition();
74         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
75
76         addOperationsToInterface(addedInterface, 3, 2, 0, true, false);
77         final String interfaceType = "normalizedComponentName-interface";
78         ((Resource) component).setInterfaces(new HashMap<>());
79         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
80         ToscaNodeType nodeType = new ToscaNodeType();
81         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
82
83         ToscaExportHandler handler = new ToscaExportHandler();
84         ToscaTemplate template = new ToscaTemplate("test");
85         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
86         nodeTypes.put("test", nodeType);
87         template.setNode_types(nodeTypes);
88         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
89
90         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
91         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
92         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:"));
93         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
94         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
95         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
96         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
97     }
98
99     @Test
100     public void addInterfaceDefinitionElement_noInputs() {
101         Component component = new Resource();
102         component.setNormalizedName("normalizedComponentName");
103         InterfaceDefinition addedInterface = new InterfaceDefinition();
104         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");
105
106         addOperationsToInterface(addedInterface, 3, 3, 0, false, false);
107         final String interfaceType = "normalizedComponentName-interface";
108         ((Resource) component).setInterfaces(new HashMap<>());
109         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
110         ToscaNodeType nodeType = new ToscaNodeType();
111         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
112
113         ToscaExportHandler handler = new ToscaExportHandler();
114         ToscaTemplate template = new ToscaTemplate("test");
115         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
116         nodeTypes.put("test", nodeType);
117         template.setNode_types(nodeTypes);
118         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
119
120         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
121         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("input_"));
122         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
123         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
124         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
125         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
126     }
127
128     @Test
129     public void addInterfaceDefinitionElementWithOutputs() {
130         Component component = new Resource();
131         component.setNormalizedName("normalizedComponentName");
132         InterfaceDefinition addedInterface = new InterfaceDefinition();
133         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
134
135         addOperationsToInterface(addedInterface, 2, 0, 2, false, true);
136         final String interfaceType = "normalizedComponentName-interface";
137         ((Resource) component).setInterfaces(new HashMap<>());
138         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
139         ToscaNodeType nodeType = new ToscaNodeType();
140         InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
141
142         ToscaExportHandler handler = new ToscaExportHandler();
143         ToscaTemplate template = new ToscaTemplate("test");
144         Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
145         nodeTypes.put("test", nodeType);
146         template.setNode_types(nodeTypes);
147         final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
148
149         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
150         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
151         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("outputs:"));
152         Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
153         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
154         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
155         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
156     }
157
158     private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
159                                           int numOfOutputsPerOp, boolean hasInputs, boolean hasOutputs) {
160
161         addedInterface.setOperations(new HashMap<>());
162         for (int i = 0; i < numOfOps; i++) {
163             final OperationDataDefinition operation = new OperationDataDefinition();
164             operation.setName("name_for_op_" + i);
165             operation.setDescription( "op "+i+" has description");
166             final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
167             implementation.setArtifactName(i + "_createBPMN.bpmn");
168             operation.setImplementation(implementation);
169             if (hasInputs) {
170                 operation.setInputs(createInputs(numOfInputsPerOp));
171             }
172             if (hasOutputs) {
173                 operation.setOutputs(createOutputs(numOfOutputsPerOp));
174             }
175             addedInterface.getOperations().put(operation.getName(), operation);
176         }
177     }
178
179
180     private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
181         ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
182         for (int i = 0; i < numOfInputs; i++) {
183             operationInputDefinitionList.add(createMockOperationInputDefinition("input_" + i,
184                     java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i));
185         }
186         return operationInputDefinitionList;
187     }
188
189     private ListDataDefinition<OperationOutputDefinition> createOutputs(int numOfOutputs) {
190         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
191         for (int i = 0; i < numOfOutputs; i++) {
192             operationOutputDefinitionList.add(createMockOperationOutputDefinition("output_" + i,
193                     java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i, getTestOutputType(i)));
194         }
195         return operationOutputDefinitionList;
196     }
197
198     private String getTestOutputType(int i) {
199         return i%2 == 0 ? "integer" : "boolean";
200     }
201
202     private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
203         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
204         operationInputDefinition.setName(name);
205         operationInputDefinition.setInputId(id);
206         return operationInputDefinition;
207     }
208
209     private OperationOutputDefinition createMockOperationOutputDefinition(String name, String id, String type) {
210         OperationOutputDefinition operationOutputDefinition = new OperationOutputDefinition();
211         operationOutputDefinition.setName(name);
212         operationOutputDefinition.setInputId(id);
213         operationOutputDefinition.setType(type);
214         return operationOutputDefinition;
215     }
216
217 }