2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
23 package org.onap.clamp.clds.sdc.controller.installer;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import org.json.JSONObject;
37 import org.junit.Assert;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.clamp.clds.util.ResourceFileUtil;
41 import org.yaml.snakeyaml.Yaml;
43 public class BlueprintParserTest {
44 private static final Gson GSON = new Gson();
45 private static final String FIRST_APPP = "first_app";
46 private static final String SECOND_APPP = "second_app";
47 private static final String THIRD_APPP = "third_app";
49 private static String microServiceTheWholeBlueprintValid;
50 private static String microServiceBlueprintOldStyleTCA;
51 private static String microServiceBlueprintOldStyleHolmes;
53 private static JsonObject jsonObjectBlueprintValid;
54 private static JsonObject jsonObjectBlueprintWithoutName;
55 private static JsonObject jsonObjectBlueprintWithoutProperties;
56 private static JsonObject jsonObjectBlueprintWithoutRelationships;
59 public static void loadBlueprints() throws IOException {
60 microServiceTheWholeBlueprintValid = ResourceFileUtil
61 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
62 microServiceBlueprintOldStyleTCA = ResourceFileUtil
63 .getResourceAsString("clds/tca-old-style-ms.yaml");
64 microServiceBlueprintOldStyleHolmes = ResourceFileUtil
65 .getResourceAsString("clds/holmes-old-style-ms.yaml");
67 String microServiceBlueprintValid = ResourceFileUtil
68 .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
69 String microServiceBlueprintWithoutName = ResourceFileUtil
70 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
71 String microServiceBlueprintWithoutProperties = ResourceFileUtil
72 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
73 String microServiceBlueprintWithoutRelationships = ResourceFileUtil
74 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
76 jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
77 jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
78 jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
79 jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
84 public void getNameShouldReturnDefinedName() {
85 final JsonObject jsonObject = jsonObjectBlueprintValid;
86 String expectedName = jsonObject.get(jsonObject.keySet().iterator().next())
87 .getAsJsonObject().get("properties")
88 .getAsJsonObject().get("name")
90 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
91 String actualName = new BlueprintParser().getName(entry);
93 Assert.assertEquals(expectedName, actualName);
97 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
98 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
100 String expectedName = jsonObject.keySet().iterator().next();
101 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
102 String actualName = new BlueprintParser().getName(entry);
104 Assert.assertEquals(expectedName, actualName);
108 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
109 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
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 getInputShouldReturnInputWhenPresent() {
120 final JsonObject jsonObject = jsonObjectBlueprintValid;
122 String expected = FIRST_APPP;
123 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
124 String actual = new BlueprintParser().getInput(entry);
126 Assert.assertEquals(expected, actual);
130 public void getInputShouldReturnEmptyStringWhenAbsent() {
131 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
133 String expected = "";
134 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
135 String actual = new BlueprintParser().getInput(entry);
137 Assert.assertEquals(expected, actual);
141 public void getNodeRepresentationFromCompleteYaml() {
142 final JsonObject jsonObject = jsonObjectBlueprintValid;
144 MicroService expected = new MicroService(SECOND_APPP, FIRST_APPP);
145 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
146 MicroService actual = new BlueprintParser().getNodeRepresentation(entry);
148 Assert.assertEquals(expected, actual);
152 public void getMicroServicesFromBlueprintTest() {
153 MicroService thirdApp = new MicroService(THIRD_APPP, "");
154 MicroService firstApp = new MicroService(FIRST_APPP, THIRD_APPP);
155 MicroService secondApp = new MicroService(SECOND_APPP, FIRST_APPP);
157 Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
158 Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
160 Assert.assertEquals(expected, actual);
164 public void fallBackToOneMicroServiceTCATest() {
165 MicroService tcaMS = new MicroService(BlueprintParser.TCA, "");
167 List<MicroService> expected = Collections.singletonList(tcaMS);
168 List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
170 Assert.assertEquals(expected, actual);
174 public void fallBackToOneMicroServiceHolmesTest() {
175 MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, "");
177 List<MicroService> expected = Collections.singletonList(holmesMS);
178 List<MicroService> actual =
179 new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
181 Assert.assertEquals(expected, actual);
184 private static JsonObject yamlToJson(String yamlString) {
185 Yaml yaml = new Yaml();
186 Map<String, Object> map = yaml.load(yamlString);
187 JSONObject jsonObject = new JSONObject(map);
188 return GSON.fromJson(jsonObject.toString(), JsonObject.class);