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.exception.sdc.controller.BlueprintParserException;
48 import org.onap.clamp.clds.util.ResourceFileUtil;
49 import org.yaml.snakeyaml.Yaml;
51 public class BlueprintParserTest {
52 private static final Gson GSON = new Gson();
53 private static final String FIRST_APPP = "first_app";
54 private static final String SECOND_APPP = "second_app";
55 private static final String THIRD_APPP = "third_app";
56 private static final String MODEL_TYPE1 = "type1";
57 private static final String MODEL_TYPE_TCA = "onap.policies.monitoring.cdap.tca.hi.lo.app";
58 private static final String VERSION = "1.0.0";
60 private static String microServiceTheWholeBlueprintValid;
61 private static String newMicroServiceBlueprint;
62 private static JsonObject jsonObjectBlueprintInvalid;
63 private static JsonObject jsonObjectBlueprintWithoutName;
64 private static JsonObject jsonObjectBlueprintWithoutProperties;
65 private static JsonObject jsonObjectBlueprintWithoutRelationships;
66 private static JsonObject jsonObjectBlueprintValidWithVersion;
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");
78 newMicroServiceBlueprint = ResourceFileUtil.getResourceAsString("clds/new-microservice.yaml");
80 String microServiceBlueprintInvalid = ResourceFileUtil
81 .getResourceAsString("clds/single-microservice-fragment-invalid.yaml");
82 jsonObjectBlueprintInvalid = yamlToJson(microServiceBlueprintInvalid);
83 String microServiceBlueprintWithoutName = ResourceFileUtil
84 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
85 jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
86 String microServiceBlueprintWithoutProperties = ResourceFileUtil
87 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
88 jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
89 String microServiceBlueprintValidWithVersion = ResourceFileUtil
90 .getResourceAsString("clds/single-microservice-fragment-valid-with-version.yaml");
91 jsonObjectBlueprintValidWithVersion = yamlToJson(microServiceBlueprintValidWithVersion);
93 String microServiceBlueprintWithoutRelationships = ResourceFileUtil
94 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
95 jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
100 public void getNameShouldReturnDefinedName() {
101 final JsonObject jsonObject = jsonObjectBlueprintInvalid;
102 String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
103 .getAsJsonObject().get("name").getAsString();
104 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
105 String actualName = BlueprintParser.getName(entry);
107 Assert.assertEquals(expectedName, actualName);
111 public void getNameShouldReturnServiceNameWhenNoNameDefined() {
112 final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
114 String expectedName = jsonObject.keySet().iterator().next();
115 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
116 String actualName = BlueprintParser.getName(entry);
118 Assert.assertEquals(expectedName, actualName);
122 public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
123 final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
125 String expectedName = jsonObject.keySet().iterator().next();
126 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
127 String actualName = BlueprintParser.getName(entry);
129 Assert.assertEquals(expectedName, actualName);
133 public void getInputShouldReturnInputWhenPresent() {
134 final JsonObject jsonObject = jsonObjectBlueprintInvalid;
136 String expected = FIRST_APPP;
137 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
138 String actual = BlueprintParser.getInput(entry);
140 Assert.assertEquals(expected, actual);
144 public void getInputShouldReturnEmptyStringWhenAbsent() {
145 final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
147 String expected = "";
148 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
149 String actual = BlueprintParser.getInput(entry);
151 Assert.assertEquals(expected, actual);
154 @Test(expected = BlueprintParserException.class)
155 public void getNodeRepresentationFromIncompleteYaml() throws BlueprintParserException {
156 BlueprintParser.getNodeRepresentation(jsonObjectBlueprintInvalid.entrySet().iterator().next(),
157 jsonObjectBlueprintInvalid, null);
161 public void getNodeRepresentationFromCompleteYamlWithModelVersion() throws BlueprintParserException {
162 final JsonObject jsonObject = jsonObjectBlueprintValidWithVersion;
164 BlueprintMicroService expected = new BlueprintMicroService(SECOND_APPP, MODEL_TYPE1, "", "10.0.0");
165 Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
166 BlueprintMicroService actual = BlueprintParser.getNodeRepresentation(entry, jsonObject, null);
168 Assert.assertEquals(expected, actual);
172 public void getMicroServicesFromBlueprintTest() throws BlueprintParserException {
173 BlueprintMicroService thirdApp = new BlueprintMicroService(THIRD_APPP, MODEL_TYPE_TCA, SECOND_APPP, VERSION);
174 BlueprintMicroService firstApp = new BlueprintMicroService(FIRST_APPP, MODEL_TYPE_TCA, "", VERSION);
175 BlueprintMicroService secondApp = new BlueprintMicroService(SECOND_APPP, MODEL_TYPE_TCA, FIRST_APPP, VERSION);
177 Set<BlueprintMicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
178 Set<BlueprintMicroService> actual = BlueprintParser.getMicroServices(microServiceTheWholeBlueprintValid);
180 Assert.assertEquals(expected, actual);
184 public void fallBackToOneMicroServiceTcaTest() {
185 BlueprintMicroService tcaMs = new BlueprintMicroService(BlueprintParser.TCA,
186 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", VERSION);
187 List<BlueprintMicroService> expected = Collections.singletonList(tcaMs);
188 List<BlueprintMicroService> actual = BlueprintParser.fallbackToOneMicroService();
190 Assert.assertEquals(expected, actual);
194 public void newMicroServiceTest() throws BlueprintParserException {
195 List<BlueprintMicroService> microServicesChain = new ChainGenerator()
196 .getChainOfMicroServices(BlueprintParser.getMicroServices(newMicroServiceBlueprint));
197 if (microServicesChain.isEmpty()) {
198 microServicesChain = BlueprintParser.fallbackToOneMicroService();
200 assertThat(microServicesChain.size()).isEqualTo(1);
201 assertThat(microServicesChain.get(0).getName()).isEqualTo("pmsh");
204 private static JsonObject yamlToJson(String yamlString) {
205 Yaml yaml = new Yaml();
206 Map<String, Object> map = yaml.load(yamlString);
207 JSONObject jsonObject = new JSONObject(map);
208 return GSON.fromJson(jsonObject.toString(), JsonObject.class);