2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import com.google.common.collect.Lists;
29 import fj.data.Either;
30 import java.io.IOException;
31 import java.nio.file.FileSystems;
32 import java.nio.file.Files;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Map.Entry;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.Mockito;
41 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
42 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
43 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
46 import org.openecomp.sdc.be.model.AttributeDefinition;
47 import org.openecomp.sdc.be.model.HeatParameterDefinition;
48 import org.openecomp.sdc.be.model.InputDefinition;
49 import org.openecomp.sdc.be.model.OutputDefinition;
50 import org.openecomp.sdc.be.model.PropertyConstraint;
51 import org.openecomp.sdc.be.model.PropertyDefinition;
52 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
53 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
54 import org.openecomp.sdc.be.utils.TypeUtils;
55 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
56 import org.yaml.snakeyaml.Yaml;
58 public class ImportUtilsTest {
60 public static String loadFileNameToJsonString(String fileName) throws IOException {
61 String sourceDir = "src/test/resources/normativeTypes";
62 return loadFileNameToJsonString(sourceDir, fileName);
65 public static String loadCustomTypeFileNameToJsonString(String fileName) throws IOException {
66 String sourceDir = "src/test/resources/customTypes";
67 return loadFileNameToJsonString(sourceDir, fileName);
70 private static String loadFileNameToJsonString(String sourceDir, String fileName) throws IOException {
71 java.nio.file.Path filePath = FileSystems.getDefault().getPath(sourceDir, fileName);
72 byte[] fileContent = Files.readAllBytes(filePath);
73 return new String(fileContent);
76 private static Object loadJsonFromFile(String fileName) throws IOException {
77 String content = loadFileNameToJsonString(fileName);
78 return new Yaml().load(content);
82 void testStringTypeFindToscaElements() throws IOException {
83 Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(
84 (Map<String, Object>) loadJsonFromFile("normative-types-string-list-test.yml"), "stringTestTag", ToscaElementTypeEnum.STRING,
86 assertTrue(toscaElements.isLeft());
87 List<Object> list = toscaElements.left().value();
88 assertEquals(4, list.size());
90 for (Object element : list) {
91 assertTrue(element instanceof String);
92 String value = (String) element;
93 assertEquals(value, "stringVal" + count);
99 void testBooleanTypeFindToscaElements() throws IOException {
100 Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(
101 (Map<String, Object>) loadJsonFromFile("normative-types-all-map-test.yml"), "required", ToscaElementTypeEnum.BOOLEAN, new ArrayList<>());
102 assertTrue(toscaElements.isLeft());
103 List<Object> list = toscaElements.left().value();
104 assertEquals(3, list.size());
106 for (Object element : list) {
107 assertTrue(element instanceof Boolean);
108 Boolean value = (Boolean) element;
109 if (count == 1 || count == 3) {
111 } else if (count == 2) {
120 void testListTypeFindToscaElements() throws IOException {
121 Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(
122 (Map<String, Object>) loadJsonFromFile("normative-types-string-list-test.yml"), "listTestTag", ToscaElementTypeEnum.LIST,
124 assertTrue(toscaElements.isLeft());
125 List<Object> list = toscaElements.left().value();
126 assertEquals(3, list.size());
128 for (Object element : list) {
129 assertTrue(element instanceof List);
132 verifyListElement1(element);
133 } else if (count == 2) {
134 verifyListElement2(element);
135 } else if (count == 3) {
136 verifyListElement3(element);
143 void testAllTypeFindToscaElements() throws IOException {
144 Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(
145 (Map<String, Object>) loadJsonFromFile("normative-types-all-map-test.yml"), "allTestTag", ToscaElementTypeEnum.ALL, new ArrayList<>());
146 assertTrue(toscaElements.isLeft());
147 List<Object> list = toscaElements.left().value();
148 assertEquals(5, list.size());
150 for (Object element : list) {
152 assertTrue(element instanceof String);
153 assertEquals("tosca.nodes.Root", element);
154 } else if (count == 2) {
155 assertTrue(element instanceof Map);
156 Map<String, Object> mapElement = (Map<String, Object>) element;
157 assertEquals(2, mapElement.size());
158 Iterator<Entry<String, Object>> elementEntries = mapElement.entrySet().iterator();
159 Entry<String, Object> elementEntry = elementEntries.next();
160 assertEquals("mapTestTag", elementEntry.getKey());
161 assertEquals("string", elementEntry.getValue());
163 elementEntry = elementEntries.next();
164 assertEquals("required", elementEntry.getKey());
165 assertTrue(elementEntry.getValue() instanceof Boolean);
166 assertTrue((Boolean) elementEntry.getValue());
167 } else if (count == 3) {
168 assertTrue(element instanceof String);
169 assertEquals("1 MB", element);
170 } else if (count == 4) {
171 assertTrue(element instanceof List);
172 List<Object> listElement = (List<Object>) element;
173 assertEquals(2, listElement.size());
175 assertTrue(listElement.get(0) instanceof Map);
176 Map<String, Object> innerElement = (Map<String, Object>) listElement.get(0);
177 assertEquals(1, innerElement.size());
178 Entry<String, Object> innerEntry = innerElement.entrySet().iterator().next();
179 assertEquals("greater_or_equal", innerEntry.getKey());
180 assertEquals("1 MB", innerEntry.getValue());
182 assertTrue(listElement.get(1) instanceof Map);
183 innerElement = (Map<String, Object>) listElement.get(1);
184 assertEquals(1, innerElement.size());
185 innerEntry = innerElement.entrySet().iterator().next();
186 assertEquals("stringTestTag", innerEntry.getKey());
187 assertEquals("stringVal3", innerEntry.getValue());
188 } else if (count == 5) {
189 assertTrue(element instanceof Boolean);
190 assertFalse((Boolean) element);
197 void testMapTypeFindToscaElements() throws IOException {
198 Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(
199 (Map<String, Object>) loadJsonFromFile("normative-types-all-map-test.yml"), "mapTestTag", ToscaElementTypeEnum.MAP, new ArrayList<>());
200 assertTrue(toscaElements.isLeft());
201 List<Object> list = toscaElements.left().value();
202 assertEquals(2, list.size());
204 for (Object element : list) {
205 assertTrue(element instanceof Map);
208 Map<String, Object> mapElement = (Map<String, Object>) element;
209 assertEquals(2, mapElement.size());
210 Iterator<Entry<String, Object>> iterator = mapElement.entrySet().iterator();
211 Entry<String, Object> inerElementEntry = iterator.next();
212 assertEquals("stringTestTag", inerElementEntry.getKey());
213 assertEquals("stringVal1", inerElementEntry.getValue());
215 inerElementEntry = iterator.next();
216 assertEquals("listTestTag", inerElementEntry.getKey());
217 assertTrue(inerElementEntry.getValue() instanceof List);
218 List<Object> innerValue = (List<Object>) inerElementEntry.getValue();
220 assertEquals(3, innerValue.size());
222 } else if (count == 2) {
223 Map<String, Object> mapElement = (Map<String, Object>) element;
224 assertEquals(2, mapElement.size());
225 Iterator<Entry<String, Object>> entryItr = mapElement.entrySet().iterator();
226 Entry<String, Object> inerElementEntry = entryItr.next();
227 assertEquals("type", inerElementEntry.getKey());
228 assertEquals("tosca.capabilities.Attachment", inerElementEntry.getValue());
229 inerElementEntry = entryItr.next();
230 assertEquals("allTestTag", inerElementEntry.getKey());
231 assertTrue(inerElementEntry.getValue() instanceof Boolean);
239 void testCreateFullHeatParameterModuleWithString() {
241 testCreateFullHeatParameterModule("string", "default value");
246 void testCreateFullHeatParameterModuleWithNumber() {
248 testCreateFullHeatParameterModule("number", "777");
249 testCreateFullHeatParameterModule("number", "777.23");
254 void testCreateFullHeatParameterModuleWithBoolean() {
256 testCreateFullHeatParameterModule("boolean", "true");
257 testCreateFullHeatParameterModule("boolean", "on");
258 testCreateFullHeatParameterModule("boolean", "n");
263 void testCreateFullHeatParameterModuleWithList() {
265 testCreateFullHeatParameterModule("comma_delimited_list", "[one, two]");
270 void testCreateFullHeatParameterModuleWithMissingType() {
272 String name = "fullParameter";
273 String description = "description_text";
275 Map<String, Object> parametersMap = new HashMap<>();
276 Map<String, Object> firstParam = createParameterMap(null, "aaa", name, description);
277 parametersMap.put(TypeUtils.ToscaTagNamesEnum.PARAMETERS.getElementName(), firstParam);
279 Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils.getHeatParameters(parametersMap,
280 ArtifactTypeEnum.HEAT.getType());
281 assertTrue(heatParameters.isRight());
282 assertEquals(ResultStatusEnum.INVALID_PROPERTY_TYPE, heatParameters.right().value());
287 void testCreateFullHeatParameterModuleWithMissingFields() {
289 String name = "fullParameter";
291 Map<String, Object> parametersMap = new HashMap<>();
292 String type = "number";
293 String defValue = "defvalue";
294 // default value cannot be empty in heat in case tag exists
295 Map<String, Object> firstParam = createParameterMap(type, defValue, name, null);
296 parametersMap.put(TypeUtils.ToscaTagNamesEnum.PARAMETERS.getElementName(), firstParam);
298 Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils.getHeatParameters(parametersMap,
299 ArtifactTypeEnum.HEAT.getType());
300 assertTrue(heatParameters.isLeft());
301 List<HeatParameterDefinition> parameterDefList = heatParameters.left().value();
302 assertFalse(parameterDefList.isEmpty());
303 HeatParameterDefinition parameterDefinition = parameterDefList.get(0);
305 assertParameter(parameterDefinition, name, type, null, defValue);
310 void testGetPropertiesFromYml() throws IOException {
312 Map<String, Object> toscaJson = (Map<String, Object>) loadJsonFromFile("importToscaProperties.yml");
313 Either<Map<String, PropertyDefinition>, ResultStatusEnum> actualProperties = ImportUtils.getProperties(toscaJson);
314 assertTrue(actualProperties.isLeft());
315 Map<String, Map<String, Object>> expectedProperties = getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES);
316 compareProperties(expectedProperties, actualProperties.left().value());
321 void testGetPropertiesWithConstraintsFromYml() throws IOException {
323 Map<String, Object> toscaJson = (Map<String, Object>) loadJsonFromFile("propertyConstraintsTest.yml");
324 Either<Map<String, PropertyDefinition>, ResultStatusEnum> actualProperties = ImportUtils.getProperties(toscaJson);
325 assertTrue(actualProperties.isLeft());
326 Map<String, PropertyDefinition> properties = actualProperties.left().value();
327 assertTrue(properties.containsKey("service_type"));
328 PropertyDefinition property = properties.get("service_type");
329 assertTrue(property.getConstraints() != null && property.getConstraints().size() == 1);
330 assertTrue(property.getConstraints().get(0) instanceof ValidValuesConstraint);
331 assertNotNull(((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues());
332 List<String> validValues = ((ValidValuesConstraint) property.getConstraints().get(0)).getValidValues();
333 assertTrue(validValues.containsAll(Lists.newArrayList("firewall", "analyzer", "source-nat", "loadbalancer")));
335 assertTrue(properties.containsKey("service_interface_type_list"));
336 property = properties.get("service_interface_type_list");
337 assertTrue(property.getSchema() != null && property.getSchema().getProperty() != null);
338 PropertyDefinition innerProperty = new PropertyDefinition(property.getSchema().getProperty());
339 List<PropertyConstraint> innerConstraints = innerProperty.getConstraints();
340 assertTrue(innerConstraints.get(0) instanceof ValidValuesConstraint);
341 assertNotNull(((ValidValuesConstraint) innerConstraints.get(0)).getValidValues());
342 validValues = ((ValidValuesConstraint) innerConstraints.get(0)).getValidValues();
343 assertTrue(validValues.containsAll(Lists.newArrayList("management", "left", "right", "other")));
347 void testGetInputsFromYml() throws IOException {
349 Map<String, Object> toscaJson = (Map<String, Object>) loadJsonFromFile("importToscaInputsOutputs.yml");
351 AnnotationTypeOperations annotationTypeOperations = Mockito.mock(AnnotationTypeOperations.class);
352 Mockito.when(annotationTypeOperations.getLatestType(Mockito.anyString())).thenReturn(null);
354 Either<Map<String, InputDefinition>, ResultStatusEnum> actualInputs = ImportUtils.getInputs(toscaJson, annotationTypeOperations);
355 assertTrue(actualInputs.isLeft());
356 Map<String, Map<String, Object>> expectedProperties = getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS);
357 compareProperties(expectedProperties, actualInputs.left().value());
359 actualInputs = ImportUtils.getInputs(toscaJson);
360 assertTrue(actualInputs.isLeft());
361 expectedProperties = getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS);
362 compareProperties(expectedProperties, actualInputs.left().value());
366 void testGetOutputsFromYml() throws IOException {
368 Map<String, Object> toscaJson = (Map<String, Object>) loadJsonFromFile("importToscaInputsOutputs.yml");
370 Either<Map<String, OutputDefinition>, ResultStatusEnum> actualOutputs = ImportUtils.getOutputs(toscaJson);
371 assertTrue(actualOutputs.isLeft());
372 Map<String, Map<String, Object>> expectedProperties = getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.OUTPUTS);
373 compareAttributes(expectedProperties, actualOutputs.left().value());
376 private void compareAttributes(Map<String, Map<String, Object>> expected, Map<String, OutputDefinition> actual) {
378 Map<String, Object> singleExpectedAttribute;
379 AttributeDataDefinition actualAttribute, expectedAttributeModel;
380 // attributes of resource
381 for (Map.Entry<String, Map<String, Object>> expectedAttribute : expected.entrySet()) {
383 singleExpectedAttribute = expectedAttribute.getValue();
384 assertNotNull(singleExpectedAttribute);
385 actualAttribute = actual.get(expectedAttribute.getKey());
386 assertNotNull(actualAttribute);
387 actualAttribute.setName(expectedAttribute.getKey().toString());
388 expectedAttributeModel = ImportUtils.createModuleAttribute(singleExpectedAttribute);
389 expectedAttributeModel.setName(expectedAttribute.getKey().toString());
391 assertEquals(((AttributeDefinition) expectedAttributeModel).getDefaultValue(), ((AttributeDefinition) actualAttribute).getDefaultValue());
392 assertEquals(((AttributeDefinition) expectedAttributeModel).getDescription(), ((AttributeDefinition) actualAttribute).getDescription());
393 assertEquals(((AttributeDefinition) expectedAttributeModel).getName(), ((AttributeDefinition) actualAttribute).getName());
394 assertEquals(((AttributeDefinition) expectedAttributeModel).getStatus(), ((AttributeDefinition) actualAttribute).getStatus());
395 assertEquals(((AttributeDefinition) expectedAttributeModel).getType(), ((AttributeDefinition) actualAttribute).getType());
397 compareSchemas(expectedAttributeModel.getSchema(), actualAttribute.getSchema());
403 private void compareProperties(Map<String, Map<String, Object>> expected, Map<String, ? extends PropertyDefinition> actual) {
405 Map<String, Object> singleExpectedProperty;
406 PropertyDefinition actualProperty, expectedPropertyModel;
408 for (Map.Entry<String, Map<String, Object>> expectedProperty : expected.entrySet()) {
410 singleExpectedProperty = expectedProperty.getValue();
411 assertNotNull(singleExpectedProperty);
412 actualProperty = actual.get(expectedProperty.getKey());
413 assertNotNull(actualProperty);
414 actualProperty.setName(expectedProperty.getKey().toString());
415 expectedPropertyModel = ImportUtils.createModuleProperty(singleExpectedProperty);
416 expectedPropertyModel.setName(expectedProperty.getKey().toString());
418 assertEquals(expectedPropertyModel.getDefaultValue(), actualProperty.getDefaultValue());
419 assertEquals(expectedPropertyModel.getDescription(), actualProperty.getDescription());
420 assertEquals(expectedPropertyModel.getName(), actualProperty.getName());
421 assertEquals(expectedPropertyModel.getStatus(), actualProperty.getStatus());
422 assertEquals(expectedPropertyModel.getType(), actualProperty.getType());
424 compareSchemas(expectedPropertyModel.getSchema(), actualProperty.getSchema());
430 private void compareSchemas(SchemaDefinition expected, SchemaDefinition actual) {
432 if (expected == null && actual == null) {
435 PropertyDataDefinition actualPropertySchema = actual.getProperty();
436 PropertyDataDefinition expectedPropertySchema = expected.getProperty();
437 assertNotNull(actualPropertySchema);
438 assertNotNull(expectedPropertySchema);
439 assertEquals(expectedPropertySchema.getDescription(), actualPropertySchema.getDescription());
440 assertEquals(expectedPropertySchema.getType(), actualPropertySchema.getType());
444 private <T> Map<String, T> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementType) {
446 Either<Map<String, T>, ResultStatusEnum> toscaExpectedElements = ImportUtils.findFirstToscaMapElement(toscaJson, elementType);
447 assertTrue(toscaExpectedElements.isLeft());
449 return toscaExpectedElements.left().value();
453 private void testCreateFullHeatParameterModule(String type, Object defaultVal) {
455 String name = "fullParameter";
456 String description = "description_text";
458 Map<String, Object> parametersMap = new HashMap<>();
459 Map<String, Object> firstParam = createParameterMap(type, defaultVal, name, description);
460 parametersMap.put(TypeUtils.ToscaTagNamesEnum.PARAMETERS.getElementName(), firstParam);
462 Either<List<HeatParameterDefinition>, ResultStatusEnum> heatParameters = ImportUtils.getHeatParameters(parametersMap,
463 ArtifactTypeEnum.HEAT.getType());
464 assertTrue(heatParameters.isLeft());
465 List<HeatParameterDefinition> parameterDefList = heatParameters.left().value();
466 assertFalse(parameterDefList.isEmpty());
467 HeatParameterDefinition parameterDefinition = parameterDefList.get(0);
469 assertParameter(parameterDefinition, name, type, description, defaultVal);
473 private Map<String, Object> createParameterMap(String type, Object defaultVal, String name, String description) {
474 Map<String, Object> firstParam = new HashMap<>();
475 Map<String, Object> valuesMap = new HashMap<>();
477 valuesMap.put(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName(), type);
478 valuesMap.put(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), description);
479 valuesMap.put(TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE.getElementName(), defaultVal);
481 firstParam.put(name, valuesMap);
485 private void assertParameter(HeatParameterDefinition parameterDefinition, String name, String type, String description, Object defaultVal) {
486 assertEquals(name, parameterDefinition.getName());
487 assertEquals(description, parameterDefinition.getDescription());
488 assertEquals(type, parameterDefinition.getType());
489 assertEquals(String.valueOf(defaultVal), parameterDefinition.getDefaultValue());
490 assertEquals(String.valueOf(defaultVal), parameterDefinition.getCurrentValue());
493 private void verifyListElement3(Object element) {
494 List<Object> listElement = (List<Object>) element;
495 assertEquals(2, listElement.size());
497 Map<String, String> innerElement = (Map<String, String>) listElement.get(0);
498 assertEquals(1, innerElement.size());
499 Entry<String, String> innerEntry = innerElement.entrySet().iterator().next();
500 assertEquals("testTag1", innerEntry.getKey());
501 assertEquals("1 MB", innerEntry.getValue());
503 innerElement = (Map<String, String>) listElement.get(1);
504 assertEquals(1, innerElement.size());
505 innerEntry = innerElement.entrySet().iterator().next();
506 assertEquals("type", innerEntry.getKey());
507 assertEquals("stringVal2", innerEntry.getValue());
510 private void verifyListElement2(Object element) {
511 List<Object> listElement = (List<Object>) element;
512 assertEquals(2, listElement.size());
514 Map<String, Object> innerElement = (Map<String, Object>) listElement.get(0);
515 assertEquals(1, innerElement.size());
516 Entry<String, Object> innerEntry = innerElement.entrySet().iterator().next();
517 assertEquals("testTag1", innerEntry.getKey());
518 assertEquals("1 MB", innerEntry.getValue());
520 assertTrue(listElement.get(1) instanceof Map);
521 innerElement = (Map<String, Object>) listElement.get(1);
522 assertEquals(1, innerElement.size());
523 innerEntry = innerElement.entrySet().iterator().next();
524 assertEquals("listTestTag", innerEntry.getKey());
525 assertTrue(innerEntry.getValue() instanceof List);
528 private void verifyListElement1(Object element) {
529 List<Object> listElement = (List<Object>) element;
530 assertEquals(3, listElement.size());
532 Map<String, String> innerElement = (Map<String, String>) listElement.get(0);
533 assertEquals(1, innerElement.size());
534 Entry<String, String> innerEntry = innerElement.entrySet().iterator().next();
535 assertEquals("listTestTag", innerEntry.getKey());
536 assertEquals("1 MB", innerEntry.getValue());
538 innerElement = (Map<String, String>) listElement.get(1);
539 assertEquals(1, innerElement.size());
540 innerEntry = innerElement.entrySet().iterator().next();
541 assertEquals("listTestTag", innerEntry.getKey());
542 assertEquals("2 MB", innerEntry.getValue());
544 innerElement = (Map<String, String>) listElement.get(2);
545 assertEquals(1, innerElement.size());
546 innerEntry = innerElement.entrySet().iterator().next();
547 assertEquals("stringTestTag", innerEntry.getKey());
548 assertEquals("stringVal2", innerEntry.getValue());