2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.be.tosca.utils;
19 import com.fasterxml.jackson.databind.DeserializationFeature;
20 import com.fasterxml.jackson.databind.ObjectMapper;
22 import java.util.ArrayList;
23 import java.util.HashMap;
26 import org.junit.Assert;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.openecomp.sdc.be.DummyConfigurationManager;
30 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
34 import org.openecomp.sdc.be.model.Component;
35 import org.openecomp.sdc.be.model.InterfaceDefinition;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.Service;
38 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
39 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
40 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
41 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
42 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
43 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
44 import org.openecomp.sdc.common.util.YamlToObjectConverter;
46 public class InterfacesOperationsToscaUtilTest {
48 private static final String MAPPED_PROPERTY_NAME = "mapped_property";
49 private static final String INPUT_NAME_PREFIX = "input_";
50 private static final String NODE_TYPE_NAME = "test";
51 private String[] inputTypes = {"string", "integer", "float", "boolean"};
52 private static ObjectMapper mapper;
55 public static void setUp() {
56 new DummyConfigurationManager();
57 mapper = new ObjectMapper();
58 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
63 public void addInterfaceTypeElementToResource() {
64 Component component = new Resource();
65 component.setNormalizedName("normalizedComponentName");
66 InterfaceDefinition addedInterface = new InterfaceDefinition();
67 addedInterface.setType("interface.types.test_resource_name");
68 addOperationsToInterface(addedInterface, 5, 3, true);
69 final String interfaceType = "normalizedComponentName-interface";
70 component.setInterfaces(new HashMap<>());
71 component.getInterfaces().put(interfaceType, addedInterface);
72 final Map<String, Object> interfaceTypeElement =
73 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component, new ArrayList<>());
75 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null);
76 ToscaTemplate template = new ToscaTemplate("test");
77 template.setInterface_types(interfaceTypeElement);
78 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
80 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
81 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
85 public void addInterfaceTypeElementToService() {
86 Component component = new Service();
87 component.setNormalizedName("normalizedServiceComponentName");
88 InterfaceDefinition addedInterface = new InterfaceDefinition();
89 addedInterface.setType("interface.types.test_service_name");
90 addOperationsToInterface(addedInterface, 5, 3, true);
91 final String interfaceType = "normalizedServiceComponentName-interface";
92 component.setInterfaces(new HashMap<>());
93 component.getInterfaces().put(interfaceType, addedInterface);
94 final Map<String, Object> interfaceTypeElement =
95 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component, new ArrayList<>());
97 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null);
98 ToscaTemplate template = new ToscaTemplate("testService");
99 template.setInterface_types(interfaceTypeElement);
100 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
102 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
103 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_service_name"));
107 public void addInterfaceDefinitionElementToResource() {
108 Component component = new Resource();
109 component.setNormalizedName("normalizedComponentName");
110 InterfaceDefinition addedInterface = new InterfaceDefinition();
111 addedInterface.setType("com.some.resource.or.other.resourceName");
113 addOperationsToInterface(addedInterface, 3, 2, true);
114 final String interfaceType = "normalizedComponentName-interface";
115 component.setInterfaces(new HashMap<>());
116 component.getInterfaces().put(interfaceType, addedInterface);
117 ToscaNodeType nodeType = new ToscaNodeType();
118 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType, false);
120 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null);
121 ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
122 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
123 nodeTypes.put(NODE_TYPE_NAME, nodeType);
124 template.setNode_types(nodeTypes);
125 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
127 String mainYaml = toscaRepresentation.getMainYaml();
128 Assert.assertFalse(mainYaml.contains("operations"));
129 Assert.assertTrue(mainYaml.contains("resourceName:"));
130 Assert.assertTrue(mainYaml.contains("inputs:"));
131 validateOperationInputs(mainYaml);
132 Assert.assertFalse(mainYaml.contains("defaultp"));
133 Assert.assertTrue(mainYaml.contains("has description"));
134 Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
135 Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
139 public void addInterfaceDefinitionElementToService() {
140 Component component = new Service();
141 component.setNormalizedName("normalizedServiceComponentName");
142 InterfaceDefinition addedInterface = new InterfaceDefinition();
143 addedInterface.setType("com.some.service.or.other.serviceName");
144 addOperationsToInterface(addedInterface, 3, 2, true);
145 final String interfaceType = "normalizedServiceComponentName-interface";
146 component.setInterfaces(new HashMap<>());
147 component.getInterfaces().put(interfaceType, addedInterface);
148 ToscaNodeType nodeType = new ToscaNodeType();
149 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType, false);
151 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null);
152 ToscaTemplate template = new ToscaTemplate("testService");
153 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
154 nodeTypes.put(NODE_TYPE_NAME, nodeType);
155 template.setNode_types(nodeTypes);
156 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
157 String mainYaml = toscaRepresentation.getMainYaml();
158 Assert.assertFalse(mainYaml.contains("operations"));
159 Assert.assertTrue(mainYaml.contains("serviceName:"));
160 Assert.assertTrue(mainYaml.contains("inputs:"));
161 validateOperationInputs(mainYaml);
162 Assert.assertFalse(mainYaml.contains("defaultp"));
163 Assert.assertTrue(mainYaml.contains("has description"));
164 Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
165 Assert.assertTrue(mainYaml.contains("com.some.service.or.other.serviceName"));
169 public void addInterfaceDefinitionElement_noInputs() {
170 Component component = new Resource();
171 component.setNormalizedName("normalizedComponentName");
172 InterfaceDefinition addedInterface = new InterfaceDefinition();
173 addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs");
174 addOperationsToInterface(addedInterface, 3, 3, false);
175 final String interfaceType = "normalizedComponentName-interface";
176 component.setInterfaces(new HashMap<>());
177 component.getInterfaces().put(interfaceType, addedInterface);
178 ToscaNodeType nodeType = new ToscaNodeType();
179 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType, false);
181 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null);
182 ToscaTemplate template = new ToscaTemplate("test");
183 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
184 nodeTypes.put("test", nodeType);
185 template.setNode_types(nodeTypes);
186 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
188 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
189 Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX));
190 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
191 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
192 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
193 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
197 private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
200 addedInterface.setOperations(new HashMap<>());
201 for (int i = 0; i < numOfOps; i++) {
202 final OperationDataDefinition operation = new OperationDataDefinition();
203 operation.setName("name_for_op_" + i);
204 operation.setDescription( "op "+i+" has description");
205 final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
206 implementation.setArtifactName(i + "_createBPMN.bpmn");
207 operation.setImplementation(implementation);
209 operation.setInputs(createInputs(numOfInputsPerOp));
211 addedInterface.getOperations().put(operation.getName(), operation);
216 private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
217 ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
218 for (int i = 0; i < numOfInputs; i++) {
219 operationInputDefinitionList.add(createMockOperationInputDefinition(
220 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i,
221 java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME, i));
223 return operationInputDefinitionList;
227 private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
228 OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
229 operationInputDefinition.setName(name);
230 operationInputDefinition.setInputId(id);
231 operationInputDefinition.setType(inputTypes[index]);
232 operationInputDefinition.setRequired(index % 2 == 0);
233 return operationInputDefinition;
236 private void validateOperationInputs(String mainYaml) {
237 String nodeTypeKey = NODE_TYPE_NAME + ":";
238 String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
239 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
240 YamlToObjectConverter objectConverter = new YamlToObjectConverter();
241 ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
242 Map<String, Object> interfaces = toscaNodeType.getInterfaces();
243 for (Object interfaceVal : interfaces.values()) {
244 Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
245 for (Object operationVal : interfaceDefinition.values()) {
246 if (operationVal instanceof Map) {
247 validateOperationInputDefinition(operationVal);
253 private void validateOperationInputDefinition(Object operationVal) {
254 ToscaLifecycleOperationDefinition operation =
255 mapper.convertValue(operationVal, ToscaLifecycleOperationDefinition.class);
256 Map<String, ToscaProperty> inputs = operation.getInputs();
257 for (Map.Entry<String, ToscaProperty> inputEntry : inputs.entrySet()) {
258 Assert.assertEquals(inputEntry.getKey().split("_")[1], inputEntry.getValue().getType());
259 Boolean expectedIsRequired = Integer.parseInt(inputEntry.getKey().split("_")[2]) % 2 == 0;
260 Assert.assertEquals(expectedIsRequired, inputEntry.getValue().getRequired());