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 static org.assertj.core.api.Assertions.assertThat;
30 import com.google.gson.Gson;
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonObject;
34 import java.io.IOException;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashSet;
38 import java.util.List;
40 import java.util.Map.Entry;
43 import org.json.JSONObject;
44 import org.junit.Assert;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.clamp.clds.util.ResourceFileUtil;
48 import org.yaml.snakeyaml.Yaml;
50 public class BlueprintParserTest {
51 private static final Gson GSON = new Gson();
52 private static final String FIRST_APPP = "first_app";
53 private static final String SECOND_APPP = "second_app";
54 private static final String THIRD_APPP = "third_app";
55 private static final String MODEL_TYPE1 = "type1";
56 private static final String MODEL_TYPE2 = "type2";
57 private static final String MODEL_TYPE3 = "type3";
59 private static String microServiceTheWholeBlueprintValid;
60 private static String microServiceBlueprintOldStyleTCA;
61 private static String microServiceBlueprintOldStyleHolmes;
62 private static String newMicroServiceBlueprint;
63 private static JsonObject jsonObjectBlueprintValid;
64 private static JsonObject jsonObjectBlueprintWithoutName;
65 private static JsonObject jsonObjectBlueprintWithoutProperties;
66 private static JsonObject jsonObjectBlueprintWithoutRelationships;
69 * Method to load Blueprints before all test.
71 * @throws IOException In case of issues when opening the files
74 public static void loadBlueprints() throws IOException {
75 microServiceTheWholeBlueprintValid = ResourceFileUtil
76 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
77 microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
78 newMicroServiceBlueprint = ResourceFileUtil.getResourceAsString("clds/new-microservice.yaml");
79 microServiceBlueprintOldStyleHolmes = ResourceFileUtil.getResourceAsString("clds/holmes-old-style-ms.yaml");
81 String microServiceBlueprintValid = ResourceFileUtil
82 .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
83 String microServiceBlueprintWithoutName = ResourceFileUtil
84 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
85 String microServiceBlueprintWithoutProperties = ResourceFileUtil
86 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
88 jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
89 jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
90 jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
92 String microServiceBlueprintWithoutRelationships = ResourceFileUtil
93 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
94 jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
99 public void getNameShouldReturnDefinedName() {
100 final JsonObject jsonObject = jsonObjectBlueprintValid;
101 String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
102 .getAsJsonObject().get("name").getAsString();
103 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
104 String actualName = new BlueprintParser().getName(entry);
106 Assert.assertEquals(expectedName, actualName);
110 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
111 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
113 String expectedName = jsonObject.keySet().iterator().next();
114 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
115 String actualName = new BlueprintParser().getName(entry);
117 Assert.assertEquals(expectedName, actualName);
121 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
122 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
124 String expectedName = jsonObject.keySet().iterator().next();
125 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
126 String actualName = new BlueprintParser().getName(entry);
128 Assert.assertEquals(expectedName, actualName);
132 public void getInputShouldReturnInputWhenPresent() {
133 final JsonObject jsonObject = jsonObjectBlueprintValid;
135 String expected = FIRST_APPP;
136 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
137 String actual = new BlueprintParser().getInput(entry);
139 Assert.assertEquals(expected, actual);
143 public void getInputShouldReturnEmptyStringWhenAbsent() {
144 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
146 String expected = "";
147 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
148 String actual = new BlueprintParser().getInput(entry);
150 Assert.assertEquals(expected, actual);
154 public void getNodeRepresentationFromCompleteYaml() {
155 final JsonObject jsonObject = jsonObjectBlueprintValid;
157 MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "");
158 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
159 MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject, null);
161 Assert.assertEquals(expected, actual);
165 public void getMicroServicesFromBlueprintTest() {
166 MicroService thirdApp = new MicroService(THIRD_APPP, MODEL_TYPE3, "", "");
167 MicroService firstApp = new MicroService(FIRST_APPP, MODEL_TYPE1, THIRD_APPP, "");
168 MicroService secondApp = new MicroService(SECOND_APPP, MODEL_TYPE2, FIRST_APPP, "");
170 Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
171 Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
173 Assert.assertEquals(expected, actual);
178 public void fallBackToOneMicroServiceTcaTest() {
179 MicroService tcaMs = new MicroService(BlueprintParser.TCA, "onap.policies.monitoring.cdap.tca.hi.lo.app", "",
181 List<MicroService> expected = Collections.singletonList(tcaMs);
182 List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
184 Assert.assertEquals(expected, actual);
188 public void fallBackToOneMicroServiceHolmesTest() {
189 MicroService holmesMs = new MicroService(BlueprintParser.HOLMES, "onap.policies.monitoring.cdap.tca.hi.lo.app",
192 List<MicroService> expected = Collections.singletonList(holmesMs);
193 List<MicroService> actual = new BlueprintParser()
194 .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
196 Assert.assertEquals(expected, actual);
200 public void newMicroServiceTest() {
201 List<MicroService> microServicesChain = new ChainGenerator()
202 .getChainOfMicroServices(new BlueprintParser().getMicroServices(newMicroServiceBlueprint));
203 if (microServicesChain.isEmpty()) {
204 microServicesChain = new BlueprintParser().fallbackToOneMicroService(newMicroServiceBlueprint);
206 assertThat(microServicesChain.size()).isEqualTo(1);
207 assertThat(microServicesChain.get(0).getName()).isEqualTo("pmsh");
210 private static JsonObject yamlToJson(String yamlString) {
211 Yaml yaml = new Yaml();
212 Map<String, Object> map = yaml.load(yamlString);
213 JSONObject jsonObject = new JSONObject(map);
214 return GSON.fromJson(jsonObject.toString(), JsonObject.class);