Merge "Migrate parent from springboot to oparent"
[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 com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.test.context.SpringBootTest;
30 import org.springframework.test.annotation.DirtiesContext;
31 import org.springframework.test.context.ActiveProfiles;
32 import org.springframework.test.context.junit4.SpringRunner;
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(classLoader.getResource("toscafile/service-Sotnvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
164
165     LinkedHashMap response = new LinkedHashMap();
166     response.put("version", "1.0");
167     response.put("name", "Service_vMME");
168     response.put("description", "some service characteristics schema");
169     response.put("id", "7f5e5139-768d-4410-a871-c41430785214");
170
171     List<LinkedHashMap> resources = new ArrayList<>();
172     LinkedHashMap resource1 = new LinkedHashMap();
173     resource1.put("id", "7baa7742-3a13-4288-8330-868015adc340");
174     resources.add(resource1);
175     LinkedHashMap resource2 = new LinkedHashMap();
176     resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
177     resources.add(resource2);
178
179     response.put("resourceSpecification", resources);
180
181     LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
182     serviceSpecCharacteristicValue.put("valueType","Object");
183     serviceSpecCharacteristicValue.put("@schemaLocation","/serviceSpecification/7f5e5139-768d-4410-a871-c41430785214/specificationInputSchema");
184     serviceSpecCharacteristicValue.put("@type","Service_vMME_ServiceCharacteristic");
185
186     LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
187     serviceSpecCharacteristic.put("name","Service_vMME_ServiceCharacteristics");
188     serviceSpecCharacteristic.put("description", "This object describes all the inputs needed from the client to interact with the Service_vMME Service Topology");
189     serviceSpecCharacteristic.put("valueType","Object");
190     serviceSpecCharacteristic.put("@type","ONAPServiceCharacteristic");
191     serviceSpecCharacteristic.put("@schemaLocation","null");
192     serviceSpecCharacteristic.put("serviceSpecCharacteristicValue",serviceSpecCharacteristicValue);
193     try {
194       toscaInfosProcessor.buildAndSaveResponseWithSdcToscaParser(path, response);
195     } catch (SdcToscaParserException ex) {
196       throw new TechnicalException("unable to build response " + ex.getMessage());
197     }
198     assertThat(response.get("serviceSpecCharacteristic")).isEqualTo(serviceSpecCharacteristic);
199   }
200 }