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.HashMap;
25 import org.junit.Assert;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.openecomp.sdc.be.DummyConfigurationManager;
29 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.InterfaceDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.Service;
37 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
38 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
39 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
40 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
41 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
42 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
43 import org.openecomp.sdc.common.util.YamlToObjectConverter;
45 public class InterfacesOperationsToscaUtilTest {
47 private static final String MAPPED_PROPERTY_NAME = "mapped_property";
48 private static final String INPUT_NAME_PREFIX = "input_";
49 private static final String NODE_TYPE_NAME = "test";
50 private String[] inputTypes = {"string", "integer", "float", "boolean"};
51 private static ObjectMapper mapper;
54 public static void setUp() {
55 new DummyConfigurationManager();
56 mapper = new ObjectMapper();
57 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
62 public void addInterfaceTypeElementToResource() {
63 Component component = new Resource();
64 component.setNormalizedName("normalizedComponentName");
65 InterfaceDefinition addedInterface = new InterfaceDefinition();
66 addedInterface.setToscaResourceName("interface.types.test_resource_name");
67 addOperationsToInterface(addedInterface, 5, 3, true);
68 final String interfaceType = "normalizedComponentName-interface";
69 component.setInterfaces(new HashMap<>());
70 component.getInterfaces().put(interfaceType, addedInterface);
71 final Map<String, Object> interfaceTypeElement =
72 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
74 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
75 ToscaTemplate template = new ToscaTemplate("test");
76 template.setInterface_types(interfaceTypeElement);
77 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
79 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
80 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_resource_name"));
84 public void addInterfaceTypeElementToService() {
85 Component component = new Service();
86 component.setNormalizedName("normalizedServiceComponentName");
87 InterfaceDefinition addedInterface = new InterfaceDefinition();
88 addedInterface.setToscaResourceName("interface.types.test_service_name");
89 addOperationsToInterface(addedInterface, 5, 3, true);
90 final String interfaceType = "normalizedServiceComponentName-interface";
91 component.setInterfaces(new HashMap<>());
92 component.getInterfaces().put(interfaceType, addedInterface);
93 final Map<String, Object> interfaceTypeElement =
94 InterfacesOperationsToscaUtil.addInterfaceTypeElement(component);
96 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
97 ToscaTemplate template = new ToscaTemplate("testService");
98 template.setInterface_types(interfaceTypeElement);
99 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
101 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
102 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("interface.types.test_service_name"));
106 public void addInterfaceDefinitionElementToResource() {
107 Component component = new Resource();
108 component.setNormalizedName("normalizedComponentName");
109 InterfaceDefinition addedInterface = new InterfaceDefinition();
110 addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
112 addOperationsToInterface(addedInterface, 3, 2, true);
113 final String interfaceType = "normalizedComponentName-interface";
114 component.setInterfaces(new HashMap<>());
115 component.getInterfaces().put(interfaceType, addedInterface);
116 ToscaNodeType nodeType = new ToscaNodeType();
117 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
119 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
120 ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME);
121 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
122 nodeTypes.put(NODE_TYPE_NAME, nodeType);
123 template.setNode_types(nodeTypes);
124 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
126 String mainYaml = toscaRepresentation.getMainYaml();
127 Assert.assertFalse(mainYaml.contains("operations"));
128 Assert.assertTrue(mainYaml.contains("resourceName:"));
129 Assert.assertTrue(mainYaml.contains("inputs:"));
130 validateOperationInputs(mainYaml);
131 Assert.assertFalse(mainYaml.contains("defaultp"));
132 Assert.assertTrue(mainYaml.contains("has description"));
133 Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME));
134 Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName"));
138 public void addInterfaceDefinitionElementToService() {
139 Component component = new Service();
140 component.setNormalizedName("normalizedServiceComponentName");
141 InterfaceDefinition addedInterface = new InterfaceDefinition();
142 addedInterface.setToscaResourceName("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);
151 ToscaExportHandler handler = new ToscaExportHandler(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.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");
175 addOperationsToInterface(addedInterface, 3, 3, false);
176 final String interfaceType = "normalizedComponentName-interface";
177 component.setInterfaces(new HashMap<>());
178 component.getInterfaces().put(interfaceType, addedInterface);
179 ToscaNodeType nodeType = new ToscaNodeType();
180 InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
182 ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null);
183 ToscaTemplate template = new ToscaTemplate("test");
184 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
185 nodeTypes.put("test", nodeType);
186 template.setNode_types(nodeTypes);
187 final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
189 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
190 Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX));
191 Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
192 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:"));
193 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
194 Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
198 private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
201 addedInterface.setOperations(new HashMap<>());
202 for (int i = 0; i < numOfOps; i++) {
203 final OperationDataDefinition operation = new OperationDataDefinition();
204 operation.setName("name_for_op_" + i);
205 operation.setDescription( "op "+i+" has description");
206 final ArtifactDataDefinition implementation = new ArtifactDataDefinition();
207 implementation.setArtifactName(i + "_createBPMN.bpmn");
208 operation.setImplementation(implementation);
210 operation.setInputs(createInputs(numOfInputsPerOp));
212 addedInterface.getOperations().put(operation.getName(), operation);
217 private ListDataDefinition<OperationInputDefinition> createInputs(int numOfInputs) {
218 ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
219 for (int i = 0; i < numOfInputs; i++) {
220 operationInputDefinitionList.add(createMockOperationInputDefinition(
221 INPUT_NAME_PREFIX + inputTypes[i] + "_" + i,
222 java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME, i));
224 return operationInputDefinitionList;
228 private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) {
229 OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
230 operationInputDefinition.setName(name);
231 operationInputDefinition.setInputId(id);
232 operationInputDefinition.setType(inputTypes[index]);
233 operationInputDefinition.setRequired(index % 2 == 0);
234 return operationInputDefinition;
237 private void validateOperationInputs(String mainYaml) {
238 String nodeTypeKey = NODE_TYPE_NAME + ":";
239 String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(),
240 mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length());
241 YamlToObjectConverter objectConverter = new YamlToObjectConverter();
242 ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class);
243 Map<String, Object> interfaces = toscaNodeType.getInterfaces();
244 for (Object interfaceVal : interfaces.values()) {
245 Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class);
246 for (Object operationVal : interfaceDefinition.values()) {
247 if (operationVal instanceof Map) {
248 validateOperationInputDefinition(operationVal);
254 private void validateOperationInputDefinition(Object operationVal) {
255 ToscaLifecycleOperationDefinition operation =
256 mapper.convertValue(operationVal, ToscaLifecycleOperationDefinition.class);
257 Map<String, ToscaProperty> inputs = operation.getInputs();
258 for (Map.Entry<String, ToscaProperty> inputEntry : inputs.entrySet()) {
259 Assert.assertEquals(inputEntry.getKey().split("_")[1], inputEntry.getValue().getType());
260 Boolean expectedIsRequired = Integer.parseInt(inputEntry.getKey().split("_")[2]) % 2 == 0;
261 Assert.assertEquals(expectedIsRequired, inputEntry.getValue().getRequired());