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;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
35 import java.util.Map.Entry;
38 import org.json.JSONObject;
39 import org.junit.Assert;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43 import org.yaml.snakeyaml.Yaml;
45 public class BlueprintParserTest {
46 private static final Gson GSON = new Gson();
47 private static final String FIRST_APPP = "first_app";
48 private static final String SECOND_APPP = "second_app";
49 private static final String THIRD_APPP = "third_app";
51 private static String microServiceTheWholeBlueprintValid;
52 private static String microServiceBlueprintOldStyleTCA;
53 private static String microServiceBlueprintOldStyleHolmes;
55 private static JsonObject jsonObjectBlueprintValid;
56 private static JsonObject jsonObjectBlueprintWithoutName;
57 private static JsonObject jsonObjectBlueprintWithoutProperties;
58 private static JsonObject jsonObjectBlueprintWithoutRelationships;
61 public static void loadBlueprints() throws IOException {
62 microServiceTheWholeBlueprintValid = ResourceFileUtil
63 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
64 microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
65 microServiceBlueprintOldStyleHolmes = ResourceFileUtil.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()).getAsJsonObject().get("properties")
87 .getAsJsonObject().get("name").getAsString();
88 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
89 String actualName = new BlueprintParser().getName(entry);
91 Assert.assertEquals(expectedName, actualName);
95 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
96 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
98 String expectedName = jsonObject.keySet().iterator().next();
99 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
100 String actualName = new BlueprintParser().getName(entry);
102 Assert.assertEquals(expectedName, actualName);
106 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
107 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
109 String expectedName = jsonObject.keySet().iterator().next();
110 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
111 String actualName = new BlueprintParser().getName(entry);
113 Assert.assertEquals(expectedName, actualName);
117 public void getInputShouldReturnInputWhenPresent() {
118 final JsonObject jsonObject = jsonObjectBlueprintValid;
120 String expected = FIRST_APPP;
121 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
122 String actual = new BlueprintParser().getInput(entry);
124 Assert.assertEquals(expected, actual);
128 public void getInputShouldReturnEmptyStringWhenAbsent() {
129 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
131 String expected = "";
132 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
133 String actual = new BlueprintParser().getInput(entry);
135 Assert.assertEquals(expected, actual);
139 public void getNodeRepresentationFromCompleteYaml() {
140 final JsonObject jsonObject = jsonObjectBlueprintValid;
142 MicroService expected = new MicroService(SECOND_APPP, FIRST_APPP, "");
143 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
144 MicroService actual = new BlueprintParser().getNodeRepresentation(entry);
146 Assert.assertEquals(expected, actual);
150 public void getMicroServicesFromBlueprintTest() {
151 MicroService thirdApp = new MicroService(THIRD_APPP, "", "");
152 MicroService firstApp = new MicroService(FIRST_APPP, THIRD_APPP, "");
153 MicroService secondApp = new MicroService(SECOND_APPP, FIRST_APPP, "");
155 Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
156 Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
158 Assert.assertEquals(expected, actual);
162 public void fallBackToOneMicroServiceTCATest() {
163 MicroService tcaMS = new MicroService(BlueprintParser.TCA, "", "");
165 List<MicroService> expected = Collections.singletonList(tcaMS);
166 List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
168 Assert.assertEquals(expected, actual);
172 public void fallBackToOneMicroServiceHolmesTest() {
173 MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, "", "");
175 List<MicroService> expected = Collections.singletonList(holmesMS);
176 List<MicroService> actual = new BlueprintParser()
177 .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
179 Assert.assertEquals(expected, actual);
182 private static JsonObject yamlToJson(String yamlString) {
183 Yaml yaml = new Yaml();
184 Map<String, Object> map = yaml.load(yamlString);
185 JSONObject jsonObject = new JSONObject(map);
186 return GSON.fromJson(jsonObject.toString(), JsonObject.class);