Fix issue with null Pointer for Inputs of type list/map
[externalapi/nbi.git] / src / test / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessorTest.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.nbi.apis.servicecatalog;
15
16 import static org.assertj.core.api.Assertions.assertThat;
17 import java.io.File;
18 import java.nio.file.Path;
19 import java.util.ArrayList;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.onap.nbi.exceptions.TechnicalException;
25 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.boot.test.context.SpringBootTest;
28 import org.springframework.test.annotation.DirtiesContext;
29 import org.springframework.test.context.ActiveProfiles;
30 import org.springframework.test.context.junit4.SpringRunner;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
33
34
35 @RunWith(SpringRunner.class)
36 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
37 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
38 @ActiveProfiles("test")
39 public class ToscaInfosProcessorTest {
40
41   @Autowired
42   ToscaInfosProcessor toscaInfosProcessor;
43   final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
44
45
46   @Test
47   public void buildResponseWithSdcToscaParser() {
48
49     ClassLoader classLoader = getClass().getClassLoader();
50     Path path = new File(
51         classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile())
52             .toPath().toAbsolutePath();
53     List<LinkedHashMap> resources = new ArrayList<>();
54     LinkedHashMap resource1 = new LinkedHashMap();
55     resource1.put("id", "7baa7742-3a13-4288-8330-868015adc340");
56     resources.add(resource1);
57     LinkedHashMap resource2 = new LinkedHashMap();
58     resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
59     resources.add(resource2);
60     LinkedHashMap response = new LinkedHashMap();
61     response.put("resourceSpecification", resources);
62
63     try {
64       toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
65     } catch (SdcToscaParserException e) {
66       throw new TechnicalException("unable to build response from tosca csar using sdc-parser : "
67           + path.toString() + " " + e.getMessage());
68     }
69     resources = (List<LinkedHashMap>) response.get("resourceSpecification");
70     List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
71     serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
72     assertThat(serviceSpecCharacteristic.get(0).get("name"))
73         .isEqualTo("sdwanconnectivity0_topology");
74     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
75     assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
76     assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
77     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
78     assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
79     assertThat(resources.get(0).get("modelCustomizationId"))
80         .isEqualTo("94ec574b-2306-4cbd-8214-09662b040f73");
81     assertThat(resources.get(1).get("modelCustomizationId"))
82         .isEqualTo("a7baba5d-6ac3-42b5-b47d-070841303ab1");
83
84   }
85
86   @Test
87   public void buildResponseWithSdcToscaParserWithDefaultInputs() {
88
89     ClassLoader classLoader = getClass().getClassLoader();
90     Path path = new File(
91         classLoader.getResource("toscafile/service-Sotnvpninfraservice-csar.csar").getFile())
92             .toPath().toAbsolutePath();
93     List<LinkedHashMap> resources = new ArrayList<>();
94     LinkedHashMap resource1 = new LinkedHashMap();
95     resource1.put("id", "218df3c3-50dd-4c26-9e36-4771387bb771");
96     resources.add(resource1);
97     LinkedHashMap resource2 = new LinkedHashMap();
98     resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
99     resources.add(resource2);
100     LinkedHashMap response = new LinkedHashMap();
101     response.put("resourceSpecification", resources);
102
103     try {
104       toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
105     } catch (SdcToscaParserException e) {
106       throw new TechnicalException("unable to build response from tosca csar using sdc-parser : "
107           + path.toString() + " " + e.getMessage());
108     }
109     resources = (List<LinkedHashMap>) response.get("resourceSpecification");
110     List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
111     serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
112     assertThat(resources.get(0).get("modelCustomizationId"))
113         .isEqualTo("b44071c8-04fd-4d6b-b6af-772cbfaa1129");
114     assertThat(resources.get(1).get("modelCustomizationId"))
115         .isEqualTo("c3612284-6c67-4d8c-8b41-b699cc90e76d");
116     assertThat(serviceSpecCharacteristic.get(12).get("serviceSpecCharacteristicValue")).isNull();
117     assertThat(serviceSpecCharacteristic.get(13).get("serviceSpecCharacteristicValue")).isNotNull();
118   }
119
120   @Test
121   public void buildResponseWithSdcToscaParserwithMetaDataMisMatch() {
122
123     ClassLoader classLoader = getClass().getClassLoader();
124     Path path = new File(
125         classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile())
126             .toPath().toAbsolutePath();
127     List<LinkedHashMap> resources = new ArrayList<>();
128     LinkedHashMap resource1 = new LinkedHashMap();
129     resource1.put("id", "some bad resource id no in TOSCA CSAR");
130     resources.add(resource1);
131     LinkedHashMap resource2 = new LinkedHashMap();
132     resource2.put("id", "some bad resource id no in TOSCA CSAR");
133     resources.add(resource2);
134     LinkedHashMap response = new LinkedHashMap();
135     response.put("resourceSpecification", resources);
136
137     try {
138       toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
139     } catch (SdcToscaParserException e) {
140       throw new TechnicalException("unable to build response from tosca csar using sdc-parser : "
141           + path.toString() + " " + e.getMessage());
142     }
143     resources = (List<LinkedHashMap>) response.get("resourceSpecification");
144     List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
145     serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
146     assertThat(serviceSpecCharacteristic.get(0).get("name"))
147         .isEqualTo("sdwanconnectivity0_topology");
148     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
149     assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
150     assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
151     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
152     assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
153     // Check that resources cannot be found in the TOSCA template
154     assertThat(resources.get(0).get("modelCustomizationId")).isNull();
155     assertThat(resources.get(1).get("modelCustomizationId")).isNull();
156
157   }
158
159   @Test
160   public void testBuildAndSaveResponseWithSdcToscaParser() {
161
162     ClassLoader classLoader = getClass().getClassLoader();
163     Path path = new File(
164         classLoader.getResource("toscafile/service-Sotnvpninfraservice-csar.csar").getFile())
165             .toPath().toAbsolutePath();
166
167     LinkedHashMap response = new LinkedHashMap();
168     response.put("version", "1.0");
169     response.put("name", "Service_vMME");
170     response.put("description", "some service characteristics schema");
171     response.put("id", "7f5e5139-768d-4410-a871-c41430785214");
172
173     List<LinkedHashMap> resources = new ArrayList<>();
174     LinkedHashMap resource1 = new LinkedHashMap();
175     resource1.put("id", "7baa7742-3a13-4288-8330-868015adc340");
176     resources.add(resource1);
177     LinkedHashMap resource2 = new LinkedHashMap();
178     resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
179     resources.add(resource2);
180
181     response.put("resourceSpecification", resources);
182
183     LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
184     serviceSpecCharacteristicValue.put("valueType", "object");
185     serviceSpecCharacteristicValue.put("@schemaLocation",
186         "/serviceSpecification/7f5e5139-768d-4410-a871-c41430785214/specificationInputSchema");
187     serviceSpecCharacteristicValue.put("@type", "Service_vMME_ServiceCharacteristic");
188
189     LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
190     serviceSpecCharacteristic.put("name", "Service_vMME_ServiceCharacteristics");
191     serviceSpecCharacteristic.put("description",
192         "This object describes all the inputs needed from the client to interact with the Service_vMME Service Topology");
193     // using object to match examples in specifications
194     serviceSpecCharacteristic.put("valueType", "object");
195     serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic");
196     serviceSpecCharacteristic.put("@schemaLocation", "null");
197     serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValue);
198     try {
199       toscaInfosProcessor.buildAndSaveResponseWithSdcToscaParser(path, response);
200     } catch (SdcToscaParserException ex) {
201       throw new TechnicalException("unable to build response " + ex.getMessage());
202     }
203     assertThat(response.get("serviceSpecCharacteristic")).isEqualTo(serviceSpecCharacteristic);
204   }
205   
206   
207   @Test
208   public void testBuildAndSaveResponseWithSdcToscaParserWithInputListType() {
209
210     ClassLoader classLoader = getClass().getClassLoader();
211     Path path = new File(
212         classLoader.getResource("toscafile/service-MscmEvplService-csar.csar").getFile())
213             .toPath().toAbsolutePath();
214
215     LinkedHashMap response = new LinkedHashMap();
216     response.put("version", "1.0");
217     response.put("name", "MSCM-EVPL-Service");
218     response.put("description", "MSCM EVPL Service");
219     response.put("id", "66a66cc3-178c-45fd-82c2-494336cb3665");
220
221     List<LinkedHashMap> resources = new ArrayList<>();
222     LinkedHashMap resource1 = new LinkedHashMap();
223     resource1.put("id", "f5f487df-8c02-4485-81d4-695c50e24b22");
224     resources.add(resource1);
225     LinkedHashMap resource2 = new LinkedHashMap();
226     resource2.put("id", "65c34b35-e8ab-426a-b048-d707467f68b2");
227     resources.add(resource2);
228
229     response.put("resourceSpecification", resources);
230
231     LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
232     serviceSpecCharacteristicValue.put("valueType", "object");
233     serviceSpecCharacteristicValue.put("@schemaLocation",
234         "/serviceSpecification/66a66cc3-178c-45fd-82c2-494336cb3665/specificationInputSchema");
235     serviceSpecCharacteristicValue.put("@type", "MSCM-EVPL-Service_ServiceCharacteristic");
236
237     LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
238     serviceSpecCharacteristic.put("name", "MSCM-EVPL-Service_ServiceCharacteristics");
239     serviceSpecCharacteristic.put("description",
240         "This object describes all the inputs needed from the client to interact with the MSCM-EVPL-Service Service Topology");
241     // using object to match examples in specifications
242     serviceSpecCharacteristic.put("valueType", "object");
243     serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic");
244     serviceSpecCharacteristic.put("@schemaLocation", "null");
245     serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValue);
246     try {
247       toscaInfosProcessor.buildAndSaveResponseWithSdcToscaParser(path, response);
248     } catch (SdcToscaParserException ex) {
249       throw new TechnicalException("unable to build response " + ex.getMessage());
250     }
251     assertThat(response.get("serviceSpecCharacteristic")).isEqualTo(serviceSpecCharacteristic);
252   }
253 }