2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
13 package org.onap.nbi.apis.servicecatalog;
15 import static org.assertj.core.api.Assertions.assertThat;
16 import static org.junit.Assert.assertNull;
18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import org.assertj.core.api.AbstractBooleanAssert;
28 import org.junit.Test;
29 import org.onap.nbi.exceptions.TechnicalException;
30 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
33 public class ToscaInfosProcessorTest {
35 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
37 ToscaInfosProcessor toscaInfosProcessor = new ToscaInfosProcessor();
40 private LinkedHashMap parseToscaFile(String fileName) {
42 File toscaFile = new File(fileName);
43 if (!toscaFile.exists()) {
44 throw new TechnicalException("unable to find file : " + fileName);
47 return (LinkedHashMap) mapper.readValue(toscaFile, Object.class);
48 } catch (IOException e) {
49 throw new TechnicalException("Unable to parse tosca file : " + fileName);
51 } catch (NullPointerException e) {
52 throw new TechnicalException("unable to find tosca file : " + fileName);
58 public void buildResponseWithToscaInfos() {
60 ClassLoader classLoader = getClass().getClassLoader();
61 File file = new File(classLoader.getResource("toscafile/service-TestNetwork-template.yml").getFile());
62 List<LinkedHashMap> resources = new ArrayList<>();
63 LinkedHashMap resource1 = new LinkedHashMap();
64 resource1.put("id", "e2b12ac6-cbb6-4517-9c58-b846d1f68caf");
65 resources.add(resource1);
66 LinkedHashMap toscaFile = parseToscaFile(file.getPath());
67 LinkedHashMap response = new LinkedHashMap();
68 response.put("resourceSpecification", resources);
69 toscaInfosProcessor.buildResponseWithToscaInfos((LinkedHashMap) toscaFile.get("topology_template"), response);
71 resources = (List<LinkedHashMap>) response.get("resourceSpecification");
72 assertNull(resources.get(0).get("modelCustomizationId"));
73 assertNull(resources.get(0).get("modelCustomizationName"));
78 public void buildResponseWithSdcToscaParser() {
80 ClassLoader classLoader = getClass().getClassLoader();
81 Path path = new File(classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
82 List<LinkedHashMap> resources = new ArrayList<>();
83 LinkedHashMap resource1 = new LinkedHashMap();
84 resource1.put("id", "7baa7742-3a13-4288-8330-868015adc340");
85 resources.add(resource1);
86 LinkedHashMap resource2 = new LinkedHashMap();
87 resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
88 resources.add(resource2);
89 LinkedHashMap response = new LinkedHashMap();
90 response.put("resourceSpecification", resources);
93 toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
95 catch(SdcToscaParserException e) {
96 throw new TechnicalException("unable to build response from tosca csar using sdc-parser : " + path.toString()+" "+e.getMessage());
98 resources = (List<LinkedHashMap>) response.get("resourceSpecification");
99 List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
100 serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
101 assertThat(serviceSpecCharacteristic.get(0).get("name")).isEqualTo("sdwanconnectivity0_topology");
102 assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
103 assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
104 assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
105 assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
106 assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
107 assertThat(resources.get(0).get("modelCustomizationId")).isEqualTo("94ec574b-2306-4cbd-8214-09662b040f73");
108 assertThat(resources.get(1).get("modelCustomizationId")).isEqualTo("a7baba5d-6ac3-42b5-b47d-070841303ab1");
113 public void buildResponseWithSdcToscaParserWithDefaultInputs() {
115 ClassLoader classLoader = getClass().getClassLoader();
116 Path path = new File(classLoader.getResource("toscafile/service-Sotnvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
117 List<LinkedHashMap> resources = new ArrayList<>();
118 LinkedHashMap resource1 = new LinkedHashMap();
119 resource1.put("id", "218df3c3-50dd-4c26-9e36-4771387bb771");
120 resources.add(resource1);
121 LinkedHashMap resource2 = new LinkedHashMap();
122 resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
123 resources.add(resource2);
124 LinkedHashMap response = new LinkedHashMap();
125 response.put("resourceSpecification", resources);
128 toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
130 catch(SdcToscaParserException e) {
131 throw new TechnicalException("unable to build response from tosca csar using sdc-parser : " + path.toString()+" "+e.getMessage());
133 resources = (List<LinkedHashMap>) response.get("resourceSpecification");
134 List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
135 serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
136 assertThat(resources.get(0).get("modelCustomizationId")).isEqualTo("b44071c8-04fd-4d6b-b6af-772cbfaa1129");
137 assertThat(resources.get(1).get("modelCustomizationId")).isEqualTo("c3612284-6c67-4d8c-8b41-b699cc90e76d");
138 assertThat(serviceSpecCharacteristic.get(12).get("serviceSpecCharacteristicValue")).isNull();
139 assertThat(serviceSpecCharacteristic.get(13).get("serviceSpecCharacteristicValue")).isNotNull();
143 public void buildResponseWithSdcToscaParserwithMetaDataMisMatch() {
145 ClassLoader classLoader = getClass().getClassLoader();
146 Path path = new File(classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile()).toPath().toAbsolutePath();
147 List<LinkedHashMap> resources = new ArrayList<>();
148 LinkedHashMap resource1 = new LinkedHashMap();
149 resource1.put("id", "some bad resource id no in TOSCA CSAR");
150 resources.add(resource1);
151 LinkedHashMap resource2 = new LinkedHashMap();
152 resource2.put("id", "some bad resource id no in TOSCA CSAR");
153 resources.add(resource2);
154 LinkedHashMap response = new LinkedHashMap();
155 response.put("resourceSpecification", resources);
158 toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
160 catch(SdcToscaParserException e) {
161 throw new TechnicalException("unable to build response from tosca csar using sdc-parser : " + path.toString()+" "+e.getMessage());
163 resources = (List<LinkedHashMap>) response.get("resourceSpecification");
164 List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
165 serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
166 assertThat(serviceSpecCharacteristic.get(0).get("name")).isEqualTo("sdwanconnectivity0_topology");
167 assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
168 assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
169 assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
170 assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
171 assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
172 // Check that resources cannot be found in the TOSCA template
173 assertThat(resources.get(0).get("modelCustomizationId")).isNull();
174 assertThat(resources.get(1).get("modelCustomizationId")).isNull();
178 public void buildResponseWithToscaInfosOk() {
180 ClassLoader classLoader = getClass().getClassLoader();
181 File file = new File(classLoader.getResource("toscafile/service-VfwService2vfBased-template.yml").getFile());
182 List<LinkedHashMap> resources = new ArrayList<>();
183 LinkedHashMap resource1 = new LinkedHashMap();
184 resource1.put("id", "e2b12ac6-cbb6-4517-9c58-b846d1f68caf");
185 resources.add(resource1);
186 LinkedHashMap toscaFile = parseToscaFile(file.getPath());
187 LinkedHashMap response = new LinkedHashMap();
188 response.put("resourceSpecification", resources);
189 toscaInfosProcessor.buildResponseWithToscaInfos((LinkedHashMap) toscaFile.get("topology_template"), response);
191 ArrayList toscaInfos = (ArrayList) response.get("serviceSpecCharacteristic");
192 assertThat(toscaInfos.size()).isEqualTo(4);
194 for (Object toscaInfo : toscaInfos) {
195 LinkedHashMap info = (LinkedHashMap) toscaInfo;
196 if (((String) info.get("name")).equalsIgnoreCase("fortigate_image_url")) {
197 assertThat(info.get("name")).isEqualTo("fortigate_image_url");
198 assertThat(info.get("description")).isNull();
199 assertThat(info.get("valueType")).isEqualTo("string");
200 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
201 assertThat(info.get("required")).isEqualTo(false);
202 assertThat(info.get("status")).isNull();
203 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(0);
207 if (((String) info.get("name")).equalsIgnoreCase("flavor")) {
208 assertThat(info.get("name")).isEqualTo("flavor");
209 assertThat(info.get("description")).isNull();
210 assertThat(info.get("valueType")).isEqualTo("string");
211 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
212 assertThat(info.get("required")).isNull();
213 assertThat(info.get("status")).isNull();
214 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(0);
218 if (((String) info.get("name")).equalsIgnoreCase("external_network_name")) {
219 assertThat(info.get("name")).isEqualTo("external_network_name");
220 assertThat(info.get("description")).isNull();
221 assertThat(info.get("valueType")).isEqualTo("string");
222 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
223 assertThat(info.get("required")).isNull();
224 assertThat(info.get("status")).isEqualTo("inactive");
226 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(0);
230 if (((String) info.get("name")).equalsIgnoreCase("cpus")) {
231 assertThat(info.get("name")).isEqualTo("cpus");
232 assertThat(info.get("description")).isEqualTo("Number of CPUs for the server.");
233 assertThat(info.get("valueType")).isEqualTo("integer");
234 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
235 assertThat(info.get("required")).isNull();
236 assertThat(info.get("status")).isNull();
238 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(4);
239 ArrayList serviceSpecCharacteristicValues = (ArrayList) info.get("serviceSpecCharacteristicValue");
241 for (Object serviceSpecCharacteristicValue : serviceSpecCharacteristicValues) {
242 LinkedHashMap serviceSpecValue = (LinkedHashMap) serviceSpecCharacteristicValue;
243 if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("1")) {
244 assertThat(serviceSpecValue.get("isDefault")).isEqualTo(false);
245 assertThat(serviceSpecValue.get("value")).isEqualTo("1");
246 assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
248 if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("2")) {
249 assertThat(serviceSpecValue.get("isDefault")).isEqualTo(true);
250 assertThat(serviceSpecValue.get("value")).isEqualTo("2");
251 assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
253 if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("3")) {
254 assertThat(serviceSpecValue.get("isDefault")).isEqualTo(false);
255 assertThat(serviceSpecValue.get("value")).isEqualTo("3");
256 assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
258 if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("4")) {
259 assertThat(serviceSpecValue.get("isDefault")).isEqualTo(false);
260 assertThat(serviceSpecValue.get("value")).isEqualTo("4");
261 assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");