Change for TOSCA v1.3 get_input
[sdc/sdc-tosca.git] / src / test / java / org / onap / sdc / toscaparser / api / JToscaImportTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2017 AT&T Intellectual Property.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  * Modifications copyright (c) 2019 Fujitsu Limited.
18  * ================================================================================
19  */
20 package org.onap.sdc.toscaparser.api;
21
22 import org.junit.Test;
23 import org.onap.sdc.toscaparser.api.common.JToscaException;
24 import org.onap.sdc.toscaparser.api.elements.DataType;
25 import org.onap.sdc.toscaparser.api.elements.PropertyDef;
26 import org.onap.sdc.toscaparser.api.elements.constraints.Schema;
27 import org.onap.sdc.toscaparser.api.parameters.Annotation;
28 import org.onap.sdc.toscaparser.api.parameters.Input;
29 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
30
31 import java.io.File;
32 import java.util.*;
33 import java.util.stream.Collectors;
34
35 import static org.hamcrest.CoreMatchers.containsString;
36 import static org.hamcrest.CoreMatchers.is;
37 import static org.hamcrest.core.IsNull.notNullValue;
38 import static org.junit.Assert.*;
39
40 public class JToscaImportTest {
41
42         @Test
43         public void testNoMissingTypeValidationError() throws JToscaException {
44                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar")
45                                 .getFile();
46                 File file = new File(fileStr);
47                 new ToscaTemplate(file.getAbsolutePath(), null, true, null);
48                 List<String> missingTypeErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream()
49                                 .filter(s -> s.contains("JE136")).collect(Collectors.toList());
50                 assertEquals(0, missingTypeErrors.size());
51         }
52
53         @Test
54         public void testNoStackOverFlowError() {
55                 Exception jte = null;
56                 try {
57                         String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar")
58                                         .getFile();
59                         File file = new File(fileStr);
60                         new ToscaTemplate(file.getAbsolutePath(), null, true, null);
61                 } catch (Exception e) {
62                         jte = e;
63                 }
64                 assertEquals(null, jte);
65         }
66
67         @Test
68         public void testNoInvalidImports() throws JToscaException {
69                 List<String> fileNames = new ArrayList<>();
70                 fileNames.add("csars/tmpCSAR_Huawei_vSPGW_fixed.csar");
71                 fileNames.add("csars/sdc-onboarding_csar.csar");
72                 fileNames.add("csars/resource-Spgw-csar-ZTE.csar");
73
74                 for (String fileName : fileNames) {
75                         String fileStr = JToscaImportTest.class.getClassLoader().getResource(fileName).getFile();
76                         File file = new File(fileStr);
77                         new ToscaTemplate(file.getAbsolutePath(), null, true, null);
78                         List<String> invalidImportErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream()
79                                         .filter(s -> s.contains("JE195")).collect(Collectors.toList());
80                         assertEquals(0, invalidImportErrors.size());
81                 }
82         }
83         
84         @Test
85         public void testParseAnnotations() throws JToscaException {
86
87                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
88                 File file = new File(fileStr);
89                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
90                 
91                 List<Input> inputs = toscaTemplate.getInputs();
92                 assertNotNull(inputs);
93                 assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
94                 
95                 inputs.forEach(Input::parseAnnotations);
96                 assertTrue(!inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
97         }
98
99         @Test
100         public void testGetInputsWithAndWithoutAnnotations() throws JToscaException {
101
102                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
103                 File file = new File(fileStr);
104                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
105                 List<Input> inputs = toscaTemplate.getInputs();
106                 assertNotNull(inputs);
107                 assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
108
109                 inputs = toscaTemplate.getInputs(true);
110                 assertNotNull(inputs);
111                 validateInputsAnnotations(inputs);
112
113                 inputs = toscaTemplate.getInputs(false);
114                 assertNotNull(inputs);
115                 assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
116         }
117
118     @Test
119     public void testGetPropertyNameTest() throws JToscaException {
120
121         String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
122         File file = new File(fileStr);
123         ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
124         NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0);
125
126         ArrayList<String> valueList = (ArrayList<String>)nodeTemplate.getPropertyValueFromTemplatesByName("vmxvpfe_sriov41_0_port_vlanfilter");
127         assertEquals(4, valueList.size());
128
129         assertEquals("vPE", (String) nodeTemplate.getPropertyValueFromTemplatesByName("nf_role"));
130
131         assertNull(nodeTemplate.getPropertyValueFromTemplatesByName("test"));
132     }
133
134         @Test
135         public void testGetParentNodeTemplateTest() throws JToscaException {
136
137                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
138                 File file = new File(fileStr);
139                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
140                 NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0);
141                 //parent of this VF is service (null)
142                 assertNull(nodeTemplate.getParentNodeTemplate());
143                 List<NodeTemplate> children = nodeTemplate.getSubMappingToscaTemplate().getNodeTemplates();
144                 assertFalse(children.isEmpty());
145                 NodeTemplate cVFC = children.get(4);
146                 //parent is the VF above
147                 assertEquals("2017-488_ADIOD-vPE 0", cVFC.getParentNodeTemplate().getName());
148                 List<NodeTemplate> children1 = cVFC.getSubMappingToscaTemplate().getNodeTemplates();
149                 assertFalse(children1.isEmpty());
150                 //parent is the CVFC above
151                 assertEquals(cVFC, children1.get(0).getParentNodeTemplate());
152
153 /*
154
155                 TopologyTemplate tt = nodeTemplate.getOriginComponentTemplate();
156                 List<Group> groups = tt.getGroups();
157                 List<Policy> policies = tt.getPolicies();
158
159                 TopologyTemplate tt1 = cVFC.getOriginComponentTemplate();
160                 groups = tt.getGroups();
161                 policies = tt.getPolicies();
162 */
163
164         }
165
166         @Test
167         public void testNullValueHasNoNullPointerException() throws JToscaException {
168
169                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-JennyVtsbcKarunaSvc-csar.csar").getFile();
170                 File file = new File(fileStr);
171                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
172                 List<Input> inputs = toscaTemplate.getInputs();
173                 assertNotNull(inputs);
174         }
175
176         @Test
177         public void testGetPolicyMetadata() throws JToscaException {
178                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar").getFile();
179                 File file = new File(fileStr);
180                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
181                 ArrayList<Policy> policies = toscaTemplate.getPolicies();
182                 assertNotNull(policies);
183                 assertEquals(1, policies.size());
184                 assertEquals("org.openecomp.policies.External", policies.get(0).getType());
185                 assertEquals("adf03496-bf87-43cf-b20a-450e47cb44bd", policies.get(0).getMetaData().getOrDefault("UUID", "").toString());
186                 assertTrue(policies.get(0).getMetaData().getOrDefault("UUID_test", "").toString().isEmpty());
187         }
188
189         @Test
190         public void testGetPolicyMetadataObj() throws JToscaException {
191                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar").getFile();
192                 File file = new File(fileStr);
193                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
194                 ArrayList<Policy> policies = toscaTemplate.getPolicies();
195                 assertNotNull(policies);
196                 assertEquals(1, policies.size());
197                 assertEquals("adf03496-bf87-43cf-b20a-450e47cb44bd", policies.get(0).getMetaDataObj().getAllProperties().getOrDefault("UUID", "").toString());
198                 assertTrue(policies.get(0).getMetaDataObj().getAllProperties().getOrDefault("name_test", "").toString().isEmpty());
199         }
200
201     private void validateInputsAnnotations(List<Input> inputs) {
202                 List<Input> inputsWithAnnotations = inputs.stream().filter(i -> i.getAnnotations() != null)
203                                 .collect(Collectors.toList());
204                 assertTrue(!inputs.isEmpty());
205                 inputsWithAnnotations.stream().forEach(i -> validateAnnotations(i));
206         }
207
208         private void validateAnnotations(Input input) {
209                 assertNotNull(input.getAnnotations());
210                 assertEquals(input.getAnnotations().size(), 1);
211                 Annotation annotation = input.getAnnotations().get("source");
212                 assertEquals(annotation.getName(), "source");
213                 assertEquals(annotation.getType().toLowerCase(), "org.openecomp.annotations.source");
214                 assertNotNull(annotation.getProperties());
215                 Optional<Property> source_type = annotation.getProperties().stream()
216                                 .filter(p -> p.getName().equals("source_type")).findFirst();
217                 assertTrue(source_type.isPresent());
218                 assertEquals(source_type.get().getValue(), "HEAT");
219         }
220
221         private static final String TEST_DATATYPE_FILENAME ="csars/dataTypes-test-service.csar";
222         private static final String TEST_DATATYPE_TEST1 = "TestType1";
223         private static final String TEST_DATATYPE_TEST2 = "TestType2";
224         private static final String TEST_DATATYPE_PROPERTY_STR = "strdata";
225         private static final String TEST_DATATYPE_PROPERTY_INT = "intdata";
226         private static final String TEST_DATATYPE_PROPERTY_LIST = "listdata";
227         private static final String TEST_DATATYPE_PROPERTY_TYPE = "type";
228         private static final String TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA = "entry_schema";
229         private static final String TEST_DATATYPE_TOSTRING = "data_types=";
230
231         @Test
232         public void testGetDataType() throws JToscaException {
233                 String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_DATATYPE_FILENAME).getFile();
234                 File file = new File(fileStr);
235                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
236                 HashSet<DataType> dataTypes = toscaTemplate.getDataTypes();
237                 assertThat(dataTypes,notNullValue());
238                 assertThat(dataTypes.size(),is(2));
239
240                 for(DataType dataType: dataTypes){
241                         LinkedHashMap<String, PropertyDef> properties;
242                         PropertyDef property;
243                         if(dataType.getType().equals(TEST_DATATYPE_TEST1)){
244                                 properties = dataType.getAllProperties();
245                                 property = properties.get(TEST_DATATYPE_PROPERTY_STR);
246                                 assertThat(property,notNullValue());
247                                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_STR));
248                                 assertThat( property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.STRING));
249                         }
250                         if(dataType.getType().equals(TEST_DATATYPE_TEST2)){
251                                 properties = dataType.getAllProperties();
252                                 property = properties.get(TEST_DATATYPE_PROPERTY_INT);
253                                 assertThat(property,notNullValue());
254                                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_INT));
255                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.INTEGER));
256
257                                 property = properties.get(TEST_DATATYPE_PROPERTY_LIST);
258                                 assertThat(property,notNullValue());
259                                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_LIST));
260                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.LIST));
261                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA),is(TEST_DATATYPE_TEST1));
262
263                                 assertThat((LinkedHashMap<String, Object>) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST1),notNullValue());
264                                 assertThat((LinkedHashMap<String, Object>) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST2),notNullValue());
265                                 assertThat(toscaTemplate.toString(),containsString(TEST_DATATYPE_TOSTRING));
266                         }
267                 }
268
269         }
270
271         @Test
272         public void testGetInputValidate() throws JToscaException {
273                 String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_DATATYPE_FILENAME).getFile();
274                 File file = new File(fileStr);
275                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
276                 HashSet<DataType> dataTypes = toscaTemplate.getDataTypes();
277                 assertThat(dataTypes,notNullValue());
278                 assertThat(dataTypes.size(),is(2));
279
280                 for(DataType dataType: dataTypes) {
281                         LinkedHashMap<String, PropertyDef> properties;
282                         PropertyDef property;
283                         if(dataType.getType().equals(TEST_DATATYPE_TEST1)) {
284                                 properties = dataType.getAllProperties();
285                                 property = properties.get(TEST_DATATYPE_PROPERTY_STR);
286                                 assertThat(property,notNullValue());
287                                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_STR));
288                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.STRING));
289                         }
290                         if(dataType.getType().equals(TEST_DATATYPE_TEST2)) {
291                                 properties = dataType.getAllProperties();
292                                 property = properties.get(TEST_DATATYPE_PROPERTY_INT);
293                                 assertThat(property,notNullValue());
294                                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_INT));
295                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.INTEGER));
296
297                                 property = properties.get(TEST_DATATYPE_PROPERTY_LIST);
298                                 assertThat(property,notNullValue());
299                                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_LIST));
300                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.LIST));
301                                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA),is(TEST_DATATYPE_TEST1));
302
303                                 assertThat((LinkedHashMap<String, Object>) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST1),notNullValue());
304                                 assertThat((LinkedHashMap<String, Object>) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST2),notNullValue());
305                                 assertThat(toscaTemplate.toString(),containsString(TEST_DATATYPE_TOSTRING));
306                         }
307                 }
308         }
309 }