e58d863968868c367dde450371f173a9a4765a38
[sdc/sdc-tosca.git] / sdc-tosca / src / test / java / org / onap / sdc / tosca / parser / impl / ToscaParserNodeTemplateMockTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdc-tosca
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.tosca.parser.impl;
22
23
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;
38
39 import java.util.LinkedHashMap;
40 import java.util.NoSuchElementException;
41
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;
46
47 @ExtendWith(MockitoExtension.class)
48 public class ToscaParserNodeTemplateMockTest {
49
50     @Mock
51     private NodeTemplate nodeTemplate;
52     @Mock
53     private Metadata metadata;
54     @Mock
55     private ToscaTemplate toscaTemplate;
56     @Mock
57     private Property property;
58
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());
63
64     @BeforeEach
65     public void setUp(TestInfo info) {
66         if (!info.getDisplayName().equals("verifyNodeTypeIsNotSupported()")) {
67             when(property.getType()).thenReturn(dataTypeA);
68         }
69     }
70
71     @Test
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());
77
78         when(nodeTemplate.getCustomDef())
79             .thenReturn(bProp)
80             .thenReturn(cProp)
81             .thenReturn(dProp);
82
83         assertTrue(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
84     }
85
86     @Test
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());
93
94         when(nodeTemplate.getCustomDef())
95             .thenReturn(bProp)
96             .thenReturn(cProp)
97             .thenReturn(dProp);
98
99         assertTrue(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
100     }
101
102     @Test
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());
107
108         when(nodeTemplate.getCustomDef())
109             .thenReturn(bProp)
110             .thenReturn(cProp);
111
112         assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
113     }
114
115
116     @Test
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);
122
123         when(nodeTemplate.getCustomDef())
124             .thenReturn(bProp)
125             .thenReturn(cProp)
126             .thenReturn(dProp);
127
128         assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
129     }
130
131     @Test
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());
138
139         when(nodeTemplate.getCustomDef())
140             .thenReturn(bProp)
141             .thenReturn(cProp)
142             .thenReturn(dProp);
143
144         assertTrue(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
145     }
146
147     @Test
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());
152
153         when(nodeTemplate.getCustomDef())
154             .thenReturn(bProp)
155             .thenReturn(dProp);
156
157         assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
158     }
159
160     @Test
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());
165
166         when(nodeTemplate.getCustomDef())
167             .thenReturn(bProp)
168             .thenReturn(dProp);
169
170         assertThrows(NoSuchElementException.class, () -> {
171             PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property);
172         });
173     }
174
175     @Test
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);
180
181         assertThrows(NoSuchElementException.class, () -> {
182             assertFalse(PropertyUtils.isPropertyTypeSimpleOrListOfSimpleTypes(nodeTemplate, path, property));
183         });
184     }
185
186     @Test
187     public void verifyNodeTypeIsNotSupported() {
188         when(nodeTemplate.getMetaData()).thenReturn(metadata);
189         when(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)).thenReturn("VFC-TEST");
190
191         ISdcCsarHelper sdcCsarHelper = new SdcCsarHelperImpl(toscaTemplate);
192         assertFalse(sdcCsarHelper.isNodeTypeSupported(nodeTemplate));
193     }
194
195
196     private LinkedHashMap<String, Object> fillDataTypeEntry(String dataTypeName, String propertyName, String type) {
197         return fillDataTypeEntry(dataTypeName, propertyName, type, "");
198     }
199
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);
207
208         PropertySchemaType propertySchemaType = PropertySchemaType.getEnumByValue(type);
209
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);
215         }
216         properties.put(propertyName, property);
217         dataType.put(SdcPropertyNames.PROPERTY_NAME_PROPERTIES, properties);
218         dataTypes.put(dataTypeName, dataType);
219         return dataTypes;
220     }
221
222
223 }