2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 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.onap.sdc.tosca.parser.impl;
24 import org.apache.commons.lang3.StringUtils;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.TestInfo;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
32 import org.onap.sdc.tosca.parser.enums.PropertySchemaType;
33 import org.onap.sdc.tosca.parser.utils.PropertyUtils;
34 import org.onap.sdc.toscaparser.api.NodeTemplate;
35 import org.onap.sdc.toscaparser.api.Property;
36 import org.onap.sdc.toscaparser.api.ToscaTemplate;
37 import org.onap.sdc.toscaparser.api.elements.Metadata;
39 import java.util.LinkedHashMap;
40 import java.util.NoSuchElementException;
42 import static org.junit.jupiter.api.Assertions.assertFalse;
43 import static org.junit.jupiter.api.Assertions.assertThrows;
44 import static org.junit.jupiter.api.Assertions.assertTrue;
45 import static org.mockito.Mockito.when;
47 @ExtendWith(MockitoExtension.class)
48 public class ToscaParserNodeTemplateMockTest {
51 private NodeTemplate nodeTemplate;
53 private Metadata metadata;
55 private ToscaTemplate toscaTemplate;
57 private Property property;
59 private static final String dataTypeA = String.format(".%s.aaa", PropertySchemaType.DATATYPE.getSchemaTypeName());
60 private static final String dataTypeB = String.format(".%s.bbb", PropertySchemaType.DATATYPE.getSchemaTypeName());
61 private static final String dataTypeD = String.format(".%s.ddd", PropertySchemaType.DATATYPE.getSchemaTypeName());
62 private static final String dataTypeR = String.format(".%s.rrr", PropertySchemaType.DATATYPE.getSchemaTypeName());
65 public void setUp(TestInfo info) {
66 if (!info.getDisplayName().equals("verifyNodeTypeIsNotSupported()")) {
67 when(property.getType()).thenReturn(dataTypeA);
72 public void verifyCorrectPropertyPath() {
73 String[] path = String.format("%s#%s#%s#%s", "x", "y", "z", "q").split("#");
74 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
75 LinkedHashMap<String, Object> cProp = fillDataTypeEntry(dataTypeB, "z", dataTypeD);
76 LinkedHashMap<String, Object> dProp = fillDataTypeEntry(dataTypeD, "q", PropertySchemaType.STRING.getSchemaTypeName());
78 when(nodeTemplate.getCustomDef())
83 assertTrue(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
87 public void verifyPropertyPathWithMapOfStringsType() {
88 String[] path = String.format("%s#%s#%s#%s", "x", "y", "z", "q").split("#");
89 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
90 LinkedHashMap<String, Object> cProp = fillDataTypeEntry(dataTypeB, "z", PropertySchemaType.MAP.getSchemaTypeName());
91 LinkedHashMap<String, Object> dProp = fillDataTypeEntry(PropertySchemaType.MAP.getSchemaTypeName(), "q",
92 PropertySchemaType.STRING.getSchemaTypeName());
94 when(nodeTemplate.getCustomDef())
99 assertTrue(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
103 public void verifyPropertyPathWithMapType() {
104 String[] path = String.format("%s#%s#%s", "x", "y", "z").split("#");
105 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
106 LinkedHashMap<String, Object> cProp = fillDataTypeEntry(dataTypeB, "z", PropertySchemaType.MAP.getSchemaTypeName());
108 when(nodeTemplate.getCustomDef())
112 assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
117 public void verifyPropertyPathWithListOfDataTypeShouldBeRejected() {
118 String[] path = String.format("%s#%s#%s#%s", "x", "y", "z", "m").split("#");
119 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
120 LinkedHashMap<String, Object> cProp = fillDataTypeEntry(dataTypeB, "z", dataTypeD);
121 LinkedHashMap<String, Object> dProp = fillDataTypeEntry(dataTypeD, "m", PropertySchemaType.LIST.getSchemaTypeName(), dataTypeR);
123 when(nodeTemplate.getCustomDef())
128 assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
132 public void verifyPropertyPathWithListOfIntegersAsType() {
133 String[] path = String.format("%s#%s#%s#%s", "x", "y", "z", "m").split("#");
134 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
135 LinkedHashMap<String, Object> cProp = fillDataTypeEntry(dataTypeB, "z", dataTypeD);
136 LinkedHashMap<String, Object> dProp = fillDataTypeEntry(dataTypeD, "m", PropertySchemaType.LIST.getSchemaTypeName(),
137 PropertySchemaType.INTEGER.getSchemaTypeName());
139 when(nodeTemplate.getCustomDef())
144 assertTrue(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
148 public void propertyPathIsRejectedAsShorterThanExpected() {
149 String[] path = String.format("%s#%s", "x", "y").split("#");
150 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
151 LinkedHashMap<String, Object> dProp = fillDataTypeEntry(dataTypeB, "z", PropertySchemaType.STRING.getSchemaTypeName());
153 when(nodeTemplate.getCustomDef())
157 assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
161 public void propertyPathIsRejectedAsLongerThanExpected() {
162 String[] path = String.format("%s#%s#%s#%s", "x", "y", "z", "q").split("#");
163 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "y", dataTypeB);
164 LinkedHashMap<String, Object> dProp = fillDataTypeEntry(dataTypeB, "z", PropertySchemaType.STRING.getSchemaTypeName());
166 when(nodeTemplate.getCustomDef())
170 assertThrows(NoSuchElementException.class, () -> {
171 PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property);
176 public void propertyPathIsRejectedAsPropertyIsNotFound() {
177 String[] path = String.format("%s#%s", "x", "y").split("#");
178 LinkedHashMap<String, Object> bProp = fillDataTypeEntry(dataTypeA, "t", dataTypeB);
179 when(nodeTemplate.getCustomDef()).thenReturn(bProp);
181 assertThrows(NoSuchElementException.class, () -> {
182 assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
187 public void verifyNodeTypeIsNotSupported() {
188 when(nodeTemplate.getMetaData()).thenReturn(metadata);
189 when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn("VFC-TEST");
191 ISdcCsarHelper sdcCsarHelper = new SdcCsarHelperImpl(toscaTemplate);
192 assertFalse(sdcCsarHelper.isNodeTypeSupported(nodeTemplate));
196 private LinkedHashMap<String, Object> fillDataTypeEntry(String dataTypeName, String propertyName, String type) {
197 return fillDataTypeEntry(dataTypeName, propertyName, type, "");
200 private LinkedHashMap<String, Object> fillDataTypeEntry(String dataTypeName, String propertyName, String type, String entrySchemaType) {
201 LinkedHashMap<String, Object> dataTypes = new LinkedHashMap<>();
202 LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
203 LinkedHashMap<String, Object> property = new LinkedHashMap<>();
204 LinkedHashMap<String, Object> dataType = new LinkedHashMap<>();
205 property.put(SdcPropertyNames.PROPERTY_NAME_TYPE, type);
206 property.put(SdcPropertyNames.PROPERTY_NAME_NAME, propertyName);
208 PropertySchemaType propertySchemaType = PropertySchemaType.getEnumByValue(type);
210 if (!StringUtils.isEmpty(entrySchemaType) &&
211 (propertySchemaType.getSchemaTypeComplexity() == PropertySchemaType.PropertySchemaComplexity.Complex)) {
212 LinkedHashMap<String, Object> entry_schema = new LinkedHashMap<>();
213 entry_schema.put(SdcPropertyNames.PROPERTY_NAME_TYPE, entrySchemaType);
214 property.put(SdcPropertyNames.PROPERTY_NAME_ENTRY_SCHEMA, entry_schema);
216 properties.put(propertyName, property);
217 dataType.put(SdcPropertyNames.PROPERTY_NAME_PROPERTIES, properties);
218 dataTypes.put(dataTypeName, dataType);