551ac1b30ac47aba70688729bc57dfa212862dcc
[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
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;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37
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;
44
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
51     private static String microServiceTheWholeBlueprintValid;
52     private static String microServiceBlueprintOldStyleTCA;
53     private static String microServiceBlueprintOldStyleHolmes;
54
55     private static JsonObject jsonObjectBlueprintValid;
56     private static JsonObject jsonObjectBlueprintWithoutName;
57     private static JsonObject jsonObjectBlueprintWithoutProperties;
58     private static JsonObject jsonObjectBlueprintWithoutRelationships;
59
60     @BeforeClass
61     public static void loadBlueprints() throws IOException {
62         microServiceTheWholeBlueprintValid = ResourceFileUtil
63             .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
64         microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
65         microServiceBlueprintOldStyleHolmes = ResourceFileUtil.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()).getAsJsonObject().get("properties")
87             .getAsJsonObject().get("name").getAsString();
88         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
89         String actualName = new BlueprintParser().getName(entry);
90
91         Assert.assertEquals(expectedName, actualName);
92     }
93
94     @Test
95     public void getNameShouldReturnServiceNameWhenNoNameDefined() {
96         final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
97
98         String expectedName = jsonObject.keySet().iterator().next();
99         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
100         String actualName = new BlueprintParser().getName(entry);
101
102         Assert.assertEquals(expectedName, actualName);
103     }
104
105     @Test
106     public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
107         final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
108
109         String expectedName = jsonObject.keySet().iterator().next();
110         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
111         String actualName = new BlueprintParser().getName(entry);
112
113         Assert.assertEquals(expectedName, actualName);
114     }
115
116     @Test
117     public void getInputShouldReturnInputWhenPresent() {
118         final JsonObject jsonObject = jsonObjectBlueprintValid;
119
120         String expected = FIRST_APPP;
121         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
122         String actual = new BlueprintParser().getInput(entry);
123
124         Assert.assertEquals(expected, actual);
125     }
126
127     @Test
128     public void getInputShouldReturnEmptyStringWhenAbsent() {
129         final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
130
131         String expected = "";
132         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
133         String actual = new BlueprintParser().getInput(entry);
134
135         Assert.assertEquals(expected, actual);
136     }
137
138     @Test
139     public void getNodeRepresentationFromCompleteYaml() {
140         final JsonObject jsonObject = jsonObjectBlueprintValid;
141
142         MicroService expected = new MicroService(SECOND_APPP, FIRST_APPP, "");
143         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
144         MicroService actual = new BlueprintParser().getNodeRepresentation(entry);
145
146         Assert.assertEquals(expected, actual);
147     }
148
149     @Test
150     public void getMicroServicesFromBlueprintTest() {
151         MicroService thirdApp = new MicroService(THIRD_APPP, "", "");
152         MicroService firstApp = new MicroService(FIRST_APPP, THIRD_APPP, "");
153         MicroService secondApp = new MicroService(SECOND_APPP, FIRST_APPP, "");
154
155         Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
156         Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
157
158         Assert.assertEquals(expected, actual);
159     }
160
161     @Test
162     public void fallBackToOneMicroServiceTCATest() {
163         MicroService tcaMS = new MicroService(BlueprintParser.TCA, "", "");
164
165         List<MicroService> expected = Collections.singletonList(tcaMS);
166         List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
167
168         Assert.assertEquals(expected, actual);
169     }
170
171     @Test
172     public void fallBackToOneMicroServiceHolmesTest() {
173         MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, "", "");
174
175         List<MicroService> expected = Collections.singletonList(holmesMS);
176         List<MicroService> actual = new BlueprintParser()
177             .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
178
179         Assert.assertEquals(expected, actual);
180     }
181
182     private static JsonObject yamlToJson(String yamlString) {
183         Yaml yaml = new Yaml();
184         Map<String, Object> map = yaml.load(yamlString);
185         JSONObject jsonObject = new JSONObject(map);
186         return GSON.fromJson(jsonObject.toString(), JsonObject.class);
187     }
188 }