sonar clean code
[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 in compliance with
5  * 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 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.
12  */
13 package org.onap.nbi.apis.servicecatalog;
14
15 import static org.assertj.core.api.Assertions.assertThat;
16 import static org.junit.Assert.assertNull;
17
18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import org.junit.Test;
26 import org.onap.nbi.exceptions.TechnicalException;
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     private LinkedHashMap parseToscaFile(String fileName) {
37
38         File toscaFile = new File(fileName);
39         if (!toscaFile.exists()) {
40             throw new TechnicalException("unable to find  file : " + fileName);
41         }
42         try {
43             return (LinkedHashMap) mapper.readValue(toscaFile, Object.class);
44         } catch (IOException e) {
45             throw new TechnicalException("Unable to parse tosca file : " + fileName);
46
47         } catch (NullPointerException e) {
48             throw new TechnicalException("unable to find tosca file : " + fileName);
49         }
50     }
51
52
53     @Test
54     public void buildResponseWithToscaInfos() {
55
56         ClassLoader classLoader = getClass().getClassLoader();
57         File file = new File(classLoader.getResource("toscafile/service-TestNetwork-template.yml").getFile());
58         List<LinkedHashMap> resources = new ArrayList<>();
59         LinkedHashMap resource1 = new LinkedHashMap();
60         resource1.put("id", "e2b12ac6-cbb6-4517-9c58-b846d1f68caf");
61         resources.add(resource1);
62         LinkedHashMap toscaFile = parseToscaFile(file.getPath());
63         LinkedHashMap response = new LinkedHashMap();
64         response.put("resourceSpecification", resources);
65         toscaInfosProcessor.buildResponseWithToscaInfos((LinkedHashMap) toscaFile.get("topology_template"), response);
66
67         resources = (List<LinkedHashMap>) response.get("resourceSpecification");
68         assertNull(resources.get(0).get("modelCustomizationId"));
69         assertNull(resources.get(0).get("modelCustomizationName"));
70
71     }
72
73
74     @Test
75     public void buildResponseWithToscaInfosOk() {
76
77         ClassLoader classLoader = getClass().getClassLoader();
78         File file = new File(classLoader.getResource("toscafile/service-VfwService2vfBased-template.yml").getFile());
79         List<LinkedHashMap> resources = new ArrayList<>();
80         LinkedHashMap resource1 = new LinkedHashMap();
81         resource1.put("id", "e2b12ac6-cbb6-4517-9c58-b846d1f68caf");
82         resources.add(resource1);
83         LinkedHashMap toscaFile = parseToscaFile(file.getPath());
84         LinkedHashMap response = new LinkedHashMap();
85         response.put("resourceSpecification", resources);
86         toscaInfosProcessor.buildResponseWithToscaInfos((LinkedHashMap) toscaFile.get("topology_template"), response);
87
88         ArrayList toscaInfos = (ArrayList) response.get("serviceSpecCharacteristic");
89         assertThat(toscaInfos.size()).isEqualTo(4);
90
91         for (Object toscaInfo : toscaInfos) {
92             LinkedHashMap info = (LinkedHashMap) toscaInfo;
93             if (((String) info.get("name")).equalsIgnoreCase("fortigate_image_url")) {
94                 assertThat(info.get("name")).isEqualTo("fortigate_image_url");
95                 assertThat(info.get("description")).isNull();
96                 assertThat(info.get("valueType")).isEqualTo("string");
97                 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
98                 assertThat(info.get("required")).isEqualTo(false);
99                 assertThat(info.get("status")).isNull();
100                 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(0);
101
102             }
103
104             if (((String) info.get("name")).equalsIgnoreCase("flavor")) {
105                 assertThat(info.get("name")).isEqualTo("flavor");
106                 assertThat(info.get("description")).isNull();
107                 assertThat(info.get("valueType")).isEqualTo("string");
108                 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
109                 assertThat(info.get("required")).isNull();
110                 assertThat(info.get("status")).isNull();
111                 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(0);
112
113             }
114
115             if (((String) info.get("name")).equalsIgnoreCase("external_network_name")) {
116                 assertThat(info.get("name")).isEqualTo("external_network_name");
117                 assertThat(info.get("description")).isNull();
118                 assertThat(info.get("valueType")).isEqualTo("string");
119                 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
120                 assertThat(info.get("required")).isNull();
121                 assertThat(info.get("status")).isEqualTo("inactive");
122                 ;
123                 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(0);
124
125             }
126
127             if (((String) info.get("name")).equalsIgnoreCase("cpus")) {
128                 assertThat(info.get("name")).isEqualTo("cpus");
129                 assertThat(info.get("description")).isEqualTo("Number of CPUs for the server.");
130                 assertThat(info.get("valueType")).isEqualTo("integer");
131                 assertThat(info.get("@type")).isEqualTo("ONAPserviceCharacteristic");
132                 assertThat(info.get("required")).isNull();
133                 assertThat(info.get("status")).isNull();
134                 ;
135                 assertThat(((ArrayList) info.get("serviceSpecCharacteristicValue")).size()).isEqualTo(4);
136                 ArrayList serviceSpecCharacteristicValues = (ArrayList) info.get("serviceSpecCharacteristicValue");
137
138                 for (Object serviceSpecCharacteristicValue : serviceSpecCharacteristicValues) {
139                     LinkedHashMap serviceSpecValue = (LinkedHashMap) serviceSpecCharacteristicValue;
140                     if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("1")) {
141                         assertThat(serviceSpecValue.get("isDefault")).isEqualTo(false);
142                         assertThat(serviceSpecValue.get("value")).isEqualTo("1");
143                         assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
144                     }
145                     if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("2")) {
146                         assertThat(serviceSpecValue.get("isDefault")).isEqualTo(true);
147                         assertThat(serviceSpecValue.get("value")).isEqualTo("2");
148                         assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
149                     }
150                     if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("3")) {
151                         assertThat(serviceSpecValue.get("isDefault")).isEqualTo(false);
152                         assertThat(serviceSpecValue.get("value")).isEqualTo("3");
153                         assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
154                     }
155                     if (((String) serviceSpecValue.get("value")).equalsIgnoreCase("4")) {
156                         assertThat(serviceSpecValue.get("isDefault")).isEqualTo(false);
157                         assertThat(serviceSpecValue.get("value")).isEqualTo("4");
158                         assertThat(serviceSpecValue.get("valueType")).isEqualTo("integer");
159                     }
160
161                 }
162
163
164             }
165
166         }
167
168
169     }
170 }