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;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.List;
39 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.exception.sdc.controller.BlueprintParserException;
46 import org.onap.clamp.clds.util.ResourceFileUtils;
47 import org.yaml.snakeyaml.Yaml;
49 public class BlueprintParserTest {
50 private static final Gson GSON = new Gson();
51 private static final String FIRST_APPP = "first_app";
52 private static final String SECOND_APPP = "second_app";
53 private static final String THIRD_APPP = "third_app";
54 private static final String MODEL_TYPE1 = "type1";
55 private static final String MODEL_TYPE_TCA = "onap.policies.monitoring.cdap.tca.hi.lo.app";
56 private static final String VERSION = "1.0.0";
58 private static String microServiceTheWholeBlueprintValid;
59 private static String newMicroServiceBlueprint;
60 private static JsonObject jsonObjectBlueprintInvalid;
61 private static JsonObject jsonObjectBlueprintWithoutName;
62 private static JsonObject jsonObjectBlueprintWithoutProperties;
63 private static JsonObject jsonObjectBlueprintWithoutRelationships;
64 private static JsonObject jsonObjectBlueprintValidWithVersion;
67 * Method to load Blueprints before all test.
69 * @throws IOException In case of issues when opening the files
72 public static void loadBlueprints() throws IOException {
73 microServiceTheWholeBlueprintValid = ResourceFileUtils
74 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
76 newMicroServiceBlueprint = ResourceFileUtils.getResourceAsString("clds/new-microservice.yaml");
78 String microServiceBlueprintInvalid = ResourceFileUtils
79 .getResourceAsString("clds/single-microservice-fragment-invalid.yaml");
80 jsonObjectBlueprintInvalid = yamlToJson(microServiceBlueprintInvalid);
81 String microServiceBlueprintWithoutName = ResourceFileUtils
82 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
83 jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
84 String microServiceBlueprintWithoutProperties = ResourceFileUtils
85 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
86 jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
87 String microServiceBlueprintValidWithVersion = ResourceFileUtils
88 .getResourceAsString("clds/single-microservice-fragment-valid-with-version.yaml");
89 jsonObjectBlueprintValidWithVersion = yamlToJson(microServiceBlueprintValidWithVersion);
91 String microServiceBlueprintWithoutRelationships = ResourceFileUtils
92 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
93 jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
98 public void getNameShouldReturnDefinedName() {
99 final JsonObject jsonObject = jsonObjectBlueprintInvalid;
100 String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
101 .getAsJsonObject().get("name").getAsString();
102 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
103 String actualName = BlueprintParser.getName(entry);
105 Assert.assertEquals(expectedName, actualName);
109 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
110 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
112 String expectedName = jsonObject.keySet().iterator().next();
113 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
114 String actualName = BlueprintParser.getName(entry);
116 Assert.assertEquals(expectedName, actualName);
120 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
121 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
123 String expectedName = jsonObject.keySet().iterator().next();
124 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
125 String actualName = BlueprintParser.getName(entry);
127 Assert.assertEquals(expectedName, actualName);
131 public void getInputShouldReturnInputWhenPresent() {
132 final JsonObject jsonObject = jsonObjectBlueprintInvalid;
134 String expected = FIRST_APPP;
135 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
136 String actual = BlueprintParser.getInput(entry);
138 Assert.assertEquals(expected, actual);
142 public void getInputShouldReturnEmptyStringWhenAbsent() {
143 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
145 String expected = "";
146 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
147 String actual = BlueprintParser.getInput(entry);
149 Assert.assertEquals(expected, actual);
152 @Test(expected = BlueprintParserException.class)
153 public void getNodeRepresentationFromIncompleteYaml() throws BlueprintParserException {
154 BlueprintParser.getNodeRepresentation(jsonObjectBlueprintInvalid.entrySet().iterator().next(),
155 jsonObjectBlueprintInvalid, null);
159 public void getNodeRepresentationFromCompleteYamlWithModelVersion() throws BlueprintParserException {
160 final JsonObject jsonObject = jsonObjectBlueprintValidWithVersion;
162 BlueprintMicroService expected = new BlueprintMicroService(SECOND_APPP, MODEL_TYPE1, "", "10.0.0");
163 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
164 BlueprintMicroService actual = BlueprintParser.getNodeRepresentation(entry, jsonObject, null);
166 Assert.assertEquals(expected, actual);
170 public void getMicroServicesFromBlueprintTest() throws BlueprintParserException {
171 BlueprintMicroService thirdApp = new BlueprintMicroService(THIRD_APPP, MODEL_TYPE_TCA, SECOND_APPP, VERSION);
172 BlueprintMicroService firstApp = new BlueprintMicroService(FIRST_APPP, MODEL_TYPE_TCA, "", VERSION);
173 BlueprintMicroService secondApp = new BlueprintMicroService(SECOND_APPP, MODEL_TYPE_TCA, FIRST_APPP, VERSION);
175 Set<BlueprintMicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
176 Set<BlueprintMicroService> actual = BlueprintParser.getMicroServices(microServiceTheWholeBlueprintValid);
178 Assert.assertEquals(expected, actual);
182 public void fallBackToOneMicroServiceTcaTest() {
183 BlueprintMicroService tcaMs = new BlueprintMicroService(BlueprintParser.TCA,
184 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", VERSION);
185 List<BlueprintMicroService> expected = Collections.singletonList(tcaMs);
186 List<BlueprintMicroService> actual = BlueprintParser.fallbackToOneMicroService();
188 Assert.assertEquals(expected, actual);
192 public void newMicroServiceTest() throws BlueprintParserException {
193 List<BlueprintMicroService> microServicesChain = new ChainGenerator()
194 .getChainOfMicroServices(BlueprintParser.getMicroServices(newMicroServiceBlueprint));
195 if (microServicesChain.isEmpty()) {
196 microServicesChain = BlueprintParser.fallbackToOneMicroService();
198 assertThat(microServicesChain.size()).isEqualTo(1);
199 assertThat(microServicesChain.get(0).getName()).isEqualTo("pmsh");
202 private static JsonObject yamlToJson(String yamlString) {
203 Yaml yaml = new Yaml();
204 Map<String, Object> map = yaml.load(yamlString);
205 JSONObject jsonObject = new JSONObject(map);
206 return GSON.fromJson(jsonObject.toString(), JsonObject.class);