Use SDC Tosca Jar to extract and parse SDC CSAR
[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.onap.nbi.exceptions.TechnicalException;
24 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
27
28
29 public class ToscaInfosProcessorTest {
30
31   final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
32
33   ToscaInfosProcessor toscaInfosProcessor = new ToscaInfosProcessor();
34
35
36
37   @Test
38   public void buildResponseWithSdcToscaParser() {
39
40     ClassLoader classLoader = getClass().getClassLoader();
41     Path path = new File(
42         classLoader.getResource("toscafile/service-Sdwanvpninfraservice-csar.csar").getFile())
43             .toPath().toAbsolutePath();
44     List<LinkedHashMap> resources = new ArrayList<>();
45     LinkedHashMap resource1 = new LinkedHashMap();
46     resource1.put("id", "7baa7742-3a13-4288-8330-868015adc340");
47     resources.add(resource1);
48     LinkedHashMap resource2 = new LinkedHashMap();
49     resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
50     resources.add(resource2);
51     LinkedHashMap response = new LinkedHashMap();
52     response.put("resourceSpecification", resources);
53
54     try {
55       toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
56     } catch (SdcToscaParserException e) {
57       throw new TechnicalException("unable to build response from tosca csar using sdc-parser : "
58           + path.toString() + " " + e.getMessage());
59     }
60     resources = (List<LinkedHashMap>) response.get("resourceSpecification");
61     List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
62     serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
63     assertThat(serviceSpecCharacteristic.get(0).get("name"))
64         .isEqualTo("sdwanconnectivity0_topology");
65     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
66     assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
67     assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
68     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
69     assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
70     assertThat(resources.get(0).get("modelCustomizationId"))
71         .isEqualTo("94ec574b-2306-4cbd-8214-09662b040f73");
72     assertThat(resources.get(1).get("modelCustomizationId"))
73         .isEqualTo("a7baba5d-6ac3-42b5-b47d-070841303ab1");
74
75   }
76
77   @Test
78   public void buildResponseWithSdcToscaParserWithDefaultInputs() {
79
80     ClassLoader classLoader = getClass().getClassLoader();
81     Path path = new File(
82         classLoader.getResource("toscafile/service-Sotnvpninfraservice-csar.csar").getFile())
83             .toPath().toAbsolutePath();
84     List<LinkedHashMap> resources = new ArrayList<>();
85     LinkedHashMap resource1 = new LinkedHashMap();
86     resource1.put("id", "218df3c3-50dd-4c26-9e36-4771387bb771");
87     resources.add(resource1);
88     LinkedHashMap resource2 = new LinkedHashMap();
89     resource2.put("id", "81b9430b-8abe-45d6-8bf9-f41a8f5c735f");
90     resources.add(resource2);
91     LinkedHashMap response = new LinkedHashMap();
92     response.put("resourceSpecification", resources);
93
94     try {
95       toscaInfosProcessor.buildResponseWithSdcToscaParser(path, response);
96     } catch (SdcToscaParserException e) {
97       throw new TechnicalException("unable to build response from tosca csar using sdc-parser : "
98           + path.toString() + " " + e.getMessage());
99     }
100     resources = (List<LinkedHashMap>) response.get("resourceSpecification");
101     List<LinkedHashMap> serviceSpecCharacteristic = new ArrayList<>();
102     serviceSpecCharacteristic = (List<LinkedHashMap>) response.get("serviceSpecCharacteristic");
103     assertThat(resources.get(0).get("modelCustomizationId"))
104         .isEqualTo("b44071c8-04fd-4d6b-b6af-772cbfaa1129");
105     assertThat(resources.get(1).get("modelCustomizationId"))
106         .isEqualTo("c3612284-6c67-4d8c-8b41-b699cc90e76d");
107     assertThat(serviceSpecCharacteristic.get(12).get("serviceSpecCharacteristicValue")).isNull();
108     assertThat(serviceSpecCharacteristic.get(13).get("serviceSpecCharacteristicValue")).isNotNull();
109   }
110
111   @Test
112   public void buildResponseWithSdcToscaParserwithMetaDataMisMatch() {
113
114     ClassLoader classLoader = getClass().getClassLoader();
115     Path path = new File(
116         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"))
138         .isEqualTo("sdwanconnectivity0_topology");
139     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
140     assertThat(serviceSpecCharacteristic.get(0).get("required")).isEqualTo(true);
141     assertThat(serviceSpecCharacteristic.get(1).get("name")).isEqualTo("sdwanconnectivity0_name");
142     assertThat(serviceSpecCharacteristic.get(1).get("valueType")).isEqualTo("string");
143     assertThat(serviceSpecCharacteristic.get(1).get("required")).isEqualTo(true);
144     // Check that resources cannot be found in the TOSCA template
145     assertThat(resources.get(0).get("modelCustomizationId")).isNull();
146     assertThat(resources.get(1).get("modelCustomizationId")).isNull();
147
148   }
149 }