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";
50 private static final String MODEL_TYPE1 = "type1";
51 private static final String MODEL_TYPE2 = "type2";
52 private static final String MODEL_TYPE3 = "type3";
54 private static String microServiceTheWholeBlueprintValid;
55 private static String microServiceBlueprintOldStyleTCA;
56 private static String microServiceBlueprintOldStyleHolmes;
58 private static JsonObject jsonObjectBlueprintValid;
59 private static JsonObject jsonObjectBlueprintWithoutName;
60 private static JsonObject jsonObjectBlueprintWithoutProperties;
61 private static JsonObject jsonObjectBlueprintWithoutRelationships;
64 public static void loadBlueprints() throws IOException {
65 microServiceTheWholeBlueprintValid = ResourceFileUtil
66 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
67 microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
68 microServiceBlueprintOldStyleHolmes = ResourceFileUtil.getResourceAsString("clds/holmes-old-style-ms.yaml");
70 String microServiceBlueprintValid = ResourceFileUtil
71 .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
72 String microServiceBlueprintWithoutName = ResourceFileUtil
73 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
74 String microServiceBlueprintWithoutProperties = ResourceFileUtil
75 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
76 String microServiceBlueprintWithoutRelationships = ResourceFileUtil
77 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
79 jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
80 jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
81 jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
82 jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
87 public void getNameShouldReturnDefinedName() {
88 final JsonObject jsonObject = jsonObjectBlueprintValid;
89 String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
90 .getAsJsonObject().get("name").getAsString();
91 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
92 String actualName = new BlueprintParser().getName(entry);
94 Assert.assertEquals(expectedName, actualName);
98 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
99 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
101 String expectedName = jsonObject.keySet().iterator().next();
102 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
103 String actualName = new BlueprintParser().getName(entry);
105 Assert.assertEquals(expectedName, actualName);
109 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
110 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
112 String expectedName = jsonObject.keySet().iterator().next();
113 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
114 String actualName = new BlueprintParser().getName(entry);
116 Assert.assertEquals(expectedName, actualName);
120 public void getInputShouldReturnInputWhenPresent() {
121 final JsonObject jsonObject = jsonObjectBlueprintValid;
123 String expected = FIRST_APPP;
124 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
125 String actual = new BlueprintParser().getInput(entry);
127 Assert.assertEquals(expected, actual);
131 public void getInputShouldReturnEmptyStringWhenAbsent() {
132 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
134 String expected = "";
135 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
136 String actual = new BlueprintParser().getInput(entry);
138 Assert.assertEquals(expected, actual);
142 public void getNodeRepresentationFromCompleteYaml() {
143 final JsonObject jsonObject = jsonObjectBlueprintValid;
145 MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "", SECOND_APPP);
146 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
147 MicroService actual = new BlueprintParser().getNodeRepresentation(entry);
149 Assert.assertEquals(expected, actual);
153 public void getMicroServicesFromBlueprintTest() {
154 MicroService thirdApp = new MicroService(THIRD_APPP, MODEL_TYPE3, "", "", THIRD_APPP);
155 MicroService firstApp = new MicroService(FIRST_APPP, MODEL_TYPE1, THIRD_APPP, "", FIRST_APPP);
156 MicroService secondApp = new MicroService(SECOND_APPP, MODEL_TYPE2, FIRST_APPP, "", SECOND_APPP);
158 Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
159 Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
161 Assert.assertEquals(expected, actual);
165 public void fallBackToOneMicroServiceTCATest() {
166 MicroService tcaMS = new MicroService(BlueprintParser.TCA, "onap.policy.monitoring.cdap.tca.hi.lo.ap", "", "",
169 List<MicroService> expected = Collections.singletonList(tcaMS);
170 List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
172 Assert.assertEquals(expected, actual);
176 public void fallBackToOneMicroServiceHolmesTest() {
177 MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, "onap.policy.monitoring.cdap.tca.hi.lo.ap", "",
180 List<MicroService> expected = Collections.singletonList(holmesMS);
181 List<MicroService> actual = new BlueprintParser()
182 .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
184 Assert.assertEquals(expected, actual);
187 private static JsonObject yamlToJson(String yamlString) {
188 Yaml yaml = new Yaml();
189 Map<String, Object> map = yaml.load(yamlString);
190 JSONObject jsonObject = new JSONObject(map);
191 return GSON.fromJson(jsonObject.toString(), JsonObject.class);