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