2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds.sdc.controller.installer;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.List;
38 import java.util.Map.Entry;
41 import org.json.JSONObject;
42 import org.junit.Assert;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.clamp.clds.util.ResourceFileUtil;
46 import org.yaml.snakeyaml.Yaml;
48 public class BlueprintParserTest {
49 private static final Gson GSON = new Gson();
50 private static final String FIRST_APPP = "first_app";
51 private static final String SECOND_APPP = "second_app";
52 private static final String THIRD_APPP = "third_app";
53 private static final String MODEL_TYPE1 = "type1";
54 private static final String MODEL_TYPE2 = "type2";
55 private static final String MODEL_TYPE3 = "type3";
57 private static String microServiceTheWholeBlueprintValid;
58 private static String microServiceBlueprintOldStyleTCA;
59 private static String microServiceBlueprintOldStyleHolmes;
61 private static JsonObject jsonObjectBlueprintValid;
62 private static JsonObject jsonObjectBlueprintWithoutName;
63 private static JsonObject jsonObjectBlueprintWithoutProperties;
64 private static JsonObject jsonObjectBlueprintWithoutRelationships;
67 * Method to load Blueprints before all test.
70 * In case of issues when opening the files
73 public static void loadBlueprints() throws IOException {
74 microServiceTheWholeBlueprintValid = ResourceFileUtil
75 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
76 microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
77 microServiceBlueprintOldStyleHolmes = ResourceFileUtil.getResourceAsString("clds/holmes-old-style-ms.yaml");
79 String microServiceBlueprintValid = ResourceFileUtil
80 .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
81 String microServiceBlueprintWithoutName = ResourceFileUtil
82 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
83 String microServiceBlueprintWithoutProperties = ResourceFileUtil
84 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
86 jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
87 jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
88 jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
90 String microServiceBlueprintWithoutRelationships = ResourceFileUtil
91 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
92 jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
97 public void getNameShouldReturnDefinedName() {
98 final JsonObject jsonObject = jsonObjectBlueprintValid;
99 String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
100 .getAsJsonObject().get("name").getAsString();
101 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
102 String actualName = new BlueprintParser().getName(entry);
104 Assert.assertEquals(expectedName, actualName);
108 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
109 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
111 String expectedName = jsonObject.keySet().iterator().next();
112 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
113 String actualName = new BlueprintParser().getName(entry);
115 Assert.assertEquals(expectedName, actualName);
119 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
120 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
122 String expectedName = jsonObject.keySet().iterator().next();
123 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
124 String actualName = new BlueprintParser().getName(entry);
126 Assert.assertEquals(expectedName, actualName);
130 public void getInputShouldReturnInputWhenPresent() {
131 final JsonObject jsonObject = jsonObjectBlueprintValid;
133 String expected = FIRST_APPP;
134 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
135 String actual = new BlueprintParser().getInput(entry);
137 Assert.assertEquals(expected, actual);
141 public void getInputShouldReturnEmptyStringWhenAbsent() {
142 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
144 String expected = "";
145 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
146 String actual = new BlueprintParser().getInput(entry);
148 Assert.assertEquals(expected, actual);
152 public void getNodeRepresentationFromCompleteYaml() {
153 final JsonObject jsonObject = jsonObjectBlueprintValid;
155 MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "");
156 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
157 MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject, null);
159 Assert.assertEquals(expected, actual);
163 public void getMicroServicesFromBlueprintTest() {
164 MicroService thirdApp = new MicroService(THIRD_APPP, MODEL_TYPE3, "", "");
165 MicroService firstApp = new MicroService(FIRST_APPP, MODEL_TYPE1, THIRD_APPP, "");
166 MicroService secondApp = new MicroService(SECOND_APPP, MODEL_TYPE2, FIRST_APPP, "");
168 Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
169 Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
171 Assert.assertEquals(expected, actual);
176 public void fallBackToOneMicroServiceTcaTest() {
177 MicroService tcaMs = new MicroService(BlueprintParser.TCA,
178 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", "");
179 List<MicroService> expected = Collections.singletonList(tcaMs);
180 List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
182 Assert.assertEquals(expected, actual);
186 public void fallBackToOneMicroServiceHolmesTest() {
187 MicroService holmesMs = new MicroService(BlueprintParser.HOLMES,
188 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", "");
190 List<MicroService> expected = Collections.singletonList(holmesMs);
191 List<MicroService> actual = new BlueprintParser()
192 .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
194 Assert.assertEquals(expected, actual);
197 private static JsonObject yamlToJson(String yamlString) {
198 Yaml yaml = new Yaml();
199 Map<String, Object> map = yaml.load(yamlString);
200 JSONObject jsonObject = new JSONObject(map);
201 return GSON.fromJson(jsonObject.toString(), JsonObject.class);