Fix model type searching
[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     private static final String MODEL_TYPE1 = "type1";
51     private static final String MODEL_TYPE2 = "type2";
52     private static final String MODEL_TYPE3 = "type3";
53
54     private static String microServiceTheWholeBlueprintValid;
55     private static String microServiceBlueprintOldStyleTCA;
56     private static String microServiceBlueprintOldStyleHolmes;
57
58     private static JsonObject jsonObjectBlueprintValid;
59     private static JsonObject jsonObjectBlueprintWithoutName;
60     private static JsonObject jsonObjectBlueprintWithoutProperties;
61     private static JsonObject jsonObjectBlueprintWithoutRelationships;
62
63     @BeforeClass
64     public static void loadBlueprints() throws IOException {
65         microServiceTheWholeBlueprintValid = ResourceFileUtil
66             .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
67         microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
68         microServiceBlueprintOldStyleHolmes = ResourceFileUtil.getResourceAsString("clds/holmes-old-style-ms.yaml");
69
70         String microServiceBlueprintValid = ResourceFileUtil
71             .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
72         String microServiceBlueprintWithoutName = ResourceFileUtil
73             .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
74         String microServiceBlueprintWithoutProperties = ResourceFileUtil
75             .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
76         String microServiceBlueprintWithoutRelationships = ResourceFileUtil
77             .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
78
79         jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
80         jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
81         jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
82         jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
83
84     }
85
86     @Test
87     public void getNameShouldReturnDefinedName() {
88         final JsonObject jsonObject = jsonObjectBlueprintValid;
89         String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
90             .getAsJsonObject().get("name").getAsString();
91         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
92         String actualName = new BlueprintParser().getName(entry);
93
94         Assert.assertEquals(expectedName, actualName);
95     }
96
97     @Test
98     public void getNameShouldReturnServiceNameWhenNoNameDefined() {
99         final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
100
101         String expectedName = jsonObject.keySet().iterator().next();
102         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
103         String actualName = new BlueprintParser().getName(entry);
104
105         Assert.assertEquals(expectedName, actualName);
106     }
107
108     @Test
109     public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
110         final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
111
112         String expectedName = jsonObject.keySet().iterator().next();
113         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
114         String actualName = new BlueprintParser().getName(entry);
115
116         Assert.assertEquals(expectedName, actualName);
117     }
118
119     @Test
120     public void getInputShouldReturnInputWhenPresent() {
121         final JsonObject jsonObject = jsonObjectBlueprintValid;
122
123         String expected = FIRST_APPP;
124         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
125         String actual = new BlueprintParser().getInput(entry);
126
127         Assert.assertEquals(expected, actual);
128     }
129
130     @Test
131     public void getInputShouldReturnEmptyStringWhenAbsent() {
132         final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
133
134         String expected = "";
135         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
136         String actual = new BlueprintParser().getInput(entry);
137
138         Assert.assertEquals(expected, actual);
139     }
140
141     @Test
142     public void getNodeRepresentationFromCompleteYaml() {
143         final JsonObject jsonObject = jsonObjectBlueprintValid;
144
145         MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "");
146         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
147         MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject, null);
148
149         Assert.assertEquals(expected, actual);
150     }
151
152     @Test
153     public void getMicroServicesFromBlueprintTest() {
154         MicroService thirdApp = new MicroService(THIRD_APPP, MODEL_TYPE3, "", "");
155         MicroService firstApp = new MicroService(FIRST_APPP, MODEL_TYPE1, THIRD_APPP, "");
156         MicroService secondApp = new MicroService(SECOND_APPP, MODEL_TYPE2, FIRST_APPP, "");
157
158         Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
159         Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
160
161         Assert.assertEquals(expected, actual);
162     }
163
164     @Test
165     public void fallBackToOneMicroServiceTCATest() {
166         MicroService tcaMS = new MicroService(BlueprintParser.TCA, "onap.policies.monitoring.cdap.tca.hi.lo.app", "",
167             "");
168
169         List<MicroService> expected = Collections.singletonList(tcaMS);
170         List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
171
172         Assert.assertEquals(expected, actual);
173     }
174
175     @Test
176     public void fallBackToOneMicroServiceHolmesTest() {
177         MicroService holmesMS = new MicroService(BlueprintParser.HOLMES, "onap.policies.monitoring.cdap.tca.hi.lo.app",
178             "", "");
179
180         List<MicroService> expected = Collections.singletonList(holmesMS);
181         List<MicroService> actual = new BlueprintParser()
182             .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
183
184         Assert.assertEquals(expected, actual);
185     }
186
187     private static JsonObject yamlToJson(String yamlString) {
188         Yaml yaml = new Yaml();
189         Map<String, Object> map = yaml.load(yamlString);
190         JSONObject jsonObject = new JSONObject(map);
191         return GSON.fromJson(jsonObject.toString(), JsonObject.class);
192     }
193 }