2a2ab94e617da13007164f49ca4698277f69f094
[clamp.git] / src / test / java / org / onap / clamp / clds / sdc / controller / installer / BlueprintParserTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23 package org.onap.clamp.clds.sdc.controller.installer;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 import org.json.JSONObject;
37 import org.junit.Assert;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.clamp.clds.util.ResourceFileUtil;
41 import org.yaml.snakeyaml.Yaml;
42
43 public class BlueprintParserTest {
44     private static final Gson GSON = new Gson();
45     private static final String FIRST_APPP = "first_app";
46     private static final String SECOND_APPP = "second_app";
47     private static final String THIRD_APPP = "third_app";
48
49     private static String microServiceTheWholeBlueprintValid;
50     private static String microServiceBlueprintOldStyleTCA;
51     private static String microServiceBlueprintOldStyleHolmes;
52
53     private static JsonObject jsonObjectBlueprintValid;
54     private static JsonObject jsonObjectBlueprintWithoutName;
55     private static JsonObject jsonObjectBlueprintWithoutProperties;
56     private static JsonObject jsonObjectBlueprintWithoutRelationships;
57
58     @BeforeClass
59     public static void loadBlueprints() throws IOException {
60         microServiceTheWholeBlueprintValid = ResourceFileUtil
61             .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
62         microServiceBlueprintOldStyleTCA = ResourceFileUtil
63             .getResourceAsString("clds/tca-old-style-ms.yaml");
64         microServiceBlueprintOldStyleHolmes = ResourceFileUtil
65             .getResourceAsString("clds/holmes-old-style-ms.yaml");
66
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");
75
76         jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
77         jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
78         jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
79         jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
80
81     }
82
83     @Test
84     public void getNameShouldReturnDefinedName() {
85         final JsonObject jsonObject = jsonObjectBlueprintValid;
86         String expectedName = jsonObject.get(jsonObject.keySet().iterator().next())
87             .getAsJsonObject().get("properties")
88             .getAsJsonObject().get("name")
89             .getAsString();
90         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
91         String actualName = new BlueprintParser().getName(entry);
92
93         Assert.assertEquals(expectedName, actualName);
94     }
95
96     @Test
97     public void getNameShouldReturnServiceNameWhenNoNameDefined() {
98         final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
99
100         String expectedName = jsonObject.keySet().iterator().next();
101         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
102         String actualName = new BlueprintParser().getName(entry);
103
104         Assert.assertEquals(expectedName, actualName);
105     }
106
107     @Test
108     public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
109         final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
110
111         String expectedName = jsonObject.keySet().iterator().next();
112         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
113         String actualName = new BlueprintParser().getName(entry);
114
115         Assert.assertEquals(expectedName, actualName);
116     }
117
118     @Test
119     public void getInputShouldReturnInputWhenPresent() {
120         final JsonObject jsonObject = jsonObjectBlueprintValid;
121
122         String expected = FIRST_APPP;
123         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
124         String actual = new BlueprintParser().getInput(entry);
125
126         Assert.assertEquals(expected, actual);
127     }
128
129     @Test
130     public void getInputShouldReturnEmptyStringWhenAbsent() {
131         final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
132
133         String expected = "";
134         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
135         String actual = new BlueprintParser().getInput(entry);
136
137         Assert.assertEquals(expected, actual);
138     }
139
140     @Test
141     public void getNodeRepresentationFromCompleteYaml() {
142         final JsonObject jsonObject = jsonObjectBlueprintValid;
143
144         MicroService expected = new MicroService(SECOND_APPP, FIRST_APPP);
145         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
146         MicroService actual = new BlueprintParser().getNodeRepresentation(entry);
147
148         Assert.assertEquals(expected, actual);
149     }
150
151     @Test
152     public void getMicroServicesFromBlueprintTest() {
153         MicroService thirdApp = new MicroService(THIRD_APPP, "");
154         MicroService firstApp = new MicroService(FIRST_APPP, THIRD_APPP);
155         MicroService secondApp = new MicroService(SECOND_APPP, FIRST_APPP);
156
157         Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
158         Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
159
160         Assert.assertEquals(expected, actual);
161     }
162
163     @Test
164     public void fallBackToOneMicroServiceTCATest() {
165         MicroService tcaMS = new MicroService(BlueprintParser.TCA, "");
166
167         List<MicroService> expected = Collections.singletonList(tcaMS);
168         List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
169
170         Assert.assertEquals(expected, actual);
171     }
172
173     @Test
174     public void fallBackToOneMicroServiceHolmesTest() {
175         MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, "");
176
177         List<MicroService> expected = Collections.singletonList(holmesMS);
178         List<MicroService> actual =
179             new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
180
181         Assert.assertEquals(expected, actual);
182     }
183
184     private static JsonObject yamlToJson(String yamlString) {
185         Yaml yaml = new Yaml();
186         Map<String, Object> map = yaml.load(yamlString);
187         JSONObject jsonObject = new JSONObject(map);
188         return GSON.fromJson(jsonObject.toString(), JsonObject.class);
189     }
190 }