2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.openstack.utils;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
37 import org.junit.Test;
38 import org.onap.so.db.catalog.beans.HeatTemplate;
39 import org.onap.so.db.catalog.beans.HeatTemplateParam;
40 import org.skyscreamer.jsonassert.JSONAssert;
42 import com.fasterxml.jackson.core.JsonParseException;
43 import com.fasterxml.jackson.core.type.TypeReference;
44 import com.fasterxml.jackson.databind.JsonMappingException;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class MsoHeatUtilsUnitTest {
51 private ObjectMapper mapper = new ObjectMapper();
53 public void convertInputMapTest() throws JsonParseException, JsonMappingException, IOException {
54 MsoHeatUtils utils = new MsoHeatUtils();
56 Map<String, Object> input = new HashMap<>();
57 HeatTemplate template = new HeatTemplate();
58 template.setArtifactUuid("my-uuid");
59 Set<HeatTemplateParam> parameters = template.getParameters();
60 HeatTemplateParam paramNum = new HeatTemplateParam();
61 paramNum.setParamType("number");
62 paramNum.setParamName("my-number");
63 input.put("my-number", "3");
65 HeatTemplateParam paramString = new HeatTemplateParam();
66 paramString.setParamType("string");
67 paramString.setParamName("my-string");
68 input.put("my-string", "hello");
70 HeatTemplateParam paramJson = new HeatTemplateParam();
71 paramJson.setParamType("json");
72 paramJson.setParamName("my-json");
74 HeatTemplateParam paramJsonEscaped = new HeatTemplateParam();
75 paramJsonEscaped.setParamType("json");
76 paramJsonEscaped.setParamName("my-json-escaped");
78 Map<String, Object> jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference<Map<String, Object>>(){});
79 input.put("my-json", jsonMap);
81 input.put("my-json-escaped", getJson("free-form.json"));
83 parameters.add(paramNum);
84 parameters.add(paramString);
85 parameters.add(paramJson);
86 parameters.add(paramJsonEscaped);
88 Map<String, Object> output = utils.convertInputMap(input, template);
90 assertEquals(3, output.get("my-number"));
91 assertEquals("hello", output.get("my-string"));
92 assertTrue("expect no change in type", output.get("my-json") instanceof Map);
93 assertTrue("expect string to become jsonNode", output.get("my-json-escaped") instanceof JsonNode);
95 JSONAssert.assertEquals(getJson("free-form.json"), mapper.writeValueAsString(output.get("my-json-escaped")), false);
99 public final void convertInputMapValuesTest() {
100 MsoHeatUtils utils = new MsoHeatUtils();
101 Map<String, Object> inputs = new HashMap<>();
102 Set<HeatTemplateParam> params = new HashSet<>();
103 HeatTemplate ht = new HeatTemplate();
104 HeatTemplateParam htp = new HeatTemplateParam();
105 htp.setParamName("vnf_name");
106 htp.setParamType("string");
108 inputs.put("vnf_name", "a_vnf_name");
109 htp = new HeatTemplateParam();
110 htp.setParamName("image_size");
111 htp.setParamType("number");
113 inputs.put("image_size", "1024");
114 htp = new HeatTemplateParam();
115 htp.setParamName("external");
116 htp.setParamType("boolean");
118 inputs.put("external", "false");
119 htp = new HeatTemplateParam();
120 htp.setParamName("oam_ips");
121 htp.setParamType("comma_delimited_list");
123 inputs.put("oam_ips", "a,b");
124 htp = new HeatTemplateParam();
125 htp.setParamName("oam_prefixes");
126 htp.setParamType("json");
128 String jsonEscInput = "[{\"prefix\": \"aValue\"}, {\"prefix\": \"aValue2\"}]";
129 inputs.put("oam_prefixes", jsonEscInput);
130 ht.setParameters(params);
132 Map<String, Object> output = utils.convertInputMap(inputs, ht);
134 assertEquals("a_vnf_name", output.get("vnf_name"));
135 assertEquals(1024, output.get("image_size"));
136 assertEquals(false, output.get("external"));
137 List<String> cdl = new ArrayList<>();
140 assertEquals(cdl, output.get("oam_ips"));
141 ObjectMapper JSON_MAPPER = new ObjectMapper();
144 jn = JSON_MAPPER.readTree(jsonEscInput);
145 } catch (Exception e) {
147 assertEquals(jn, output.get("oam_prefixes"));
151 public final void convertInputMapNullsTest() {
152 MsoHeatUtils utils = new MsoHeatUtils();
153 Map<String, Object> inputs = new HashMap<>();
154 Set<HeatTemplateParam> params = new HashSet<>();
155 HeatTemplate ht = new HeatTemplate();
156 HeatTemplateParam htp = new HeatTemplateParam();
157 htp.setParamName("vnf_name");
158 htp.setParamType("string");
160 inputs.put("vnf_name", null);
161 htp = new HeatTemplateParam();
162 htp.setParamName("image_size");
163 htp.setParamType("number");
165 inputs.put("image_size", null);
166 htp = new HeatTemplateParam();
167 htp.setParamName("external");
168 htp.setParamType("boolean");
170 inputs.put("external", null);
171 htp = new HeatTemplateParam();
172 htp.setParamName("oam_ips");
173 htp.setParamType("comma_delimited_list");
175 inputs.put("oam_ips", null);
176 htp = new HeatTemplateParam();
177 htp.setParamName("oam_prefixes");
178 htp.setParamType("json");
180 inputs.put("oam_prefixes", null);
181 ht.setParameters(params);
183 Map<String, Object> output = utils.convertInputMap(inputs, ht);
185 assertNull(output.get("vnf_name"));
186 assertNull(output.get("image_size"));
187 assertEquals(false, output.get("external"));
188 assertNull(output.get("oam_ips"));
189 assertNull(output.get("oam_prefixes"));
192 private String getJson(String filename) throws IOException {
193 return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/MsoHeatUtils/" + filename)));