e48bfc44a8855344b78148ea2ca86d7f00af9b89
[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  * 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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.sdc.controller.installer;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29
30 import com.google.gson.Gson;
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonObject;
33
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;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 import java.util.Set;
42
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.util.ResourceFileUtil;
48 import org.yaml.snakeyaml.Yaml;
49
50 public class BlueprintParserTest {
51     private static final Gson GSON = new Gson();
52     private static final String FIRST_APPP = "first_app";
53     private static final String SECOND_APPP = "second_app";
54     private static final String THIRD_APPP = "third_app";
55     private static final String MODEL_TYPE1 = "type1";
56     private static final String MODEL_TYPE2 = "type2";
57     private static final String MODEL_TYPE3 = "type3";
58
59     private static String microServiceTheWholeBlueprintValid;
60     private static String microServiceBlueprintOldStyleTCA;
61     private static String microServiceBlueprintOldStyleHolmes;
62     private static String newMicroServiceBlueprint;
63     private static JsonObject jsonObjectBlueprintValid;
64     private static JsonObject jsonObjectBlueprintWithoutName;
65     private static JsonObject jsonObjectBlueprintWithoutProperties;
66     private static JsonObject jsonObjectBlueprintWithoutRelationships;
67
68     /**
69      * Method to load Blueprints before all test.
70      *
71      * @throws IOException In case of issues when opening the files
72      */
73     @BeforeClass
74     public static void loadBlueprints() throws IOException {
75         microServiceTheWholeBlueprintValid = ResourceFileUtil
76                 .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
77         microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
78         newMicroServiceBlueprint = ResourceFileUtil.getResourceAsString("clds/new-microservice.yaml");
79         microServiceBlueprintOldStyleHolmes = ResourceFileUtil.getResourceAsString("clds/holmes-old-style-ms.yaml");
80
81         String microServiceBlueprintValid = ResourceFileUtil
82                 .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
83         String microServiceBlueprintWithoutName = ResourceFileUtil
84                 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
85         String microServiceBlueprintWithoutProperties = ResourceFileUtil
86                 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
87
88         jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
89         jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
90         jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
91
92         String microServiceBlueprintWithoutRelationships = ResourceFileUtil
93                 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
94         jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
95
96     }
97
98     @Test
99     public void getNameShouldReturnDefinedName() {
100         final JsonObject jsonObject = jsonObjectBlueprintValid;
101         String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
102                 .getAsJsonObject().get("name").getAsString();
103         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
104         String actualName = new BlueprintParser().getName(entry);
105
106         Assert.assertEquals(expectedName, actualName);
107     }
108
109     @Test
110     public void getNameShouldReturnServiceNameWhenNoNameDefined() {
111         final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
112
113         String expectedName = jsonObject.keySet().iterator().next();
114         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
115         String actualName = new BlueprintParser().getName(entry);
116
117         Assert.assertEquals(expectedName, actualName);
118     }
119
120     @Test
121     public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
122         final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
123
124         String expectedName = jsonObject.keySet().iterator().next();
125         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
126         String actualName = new BlueprintParser().getName(entry);
127
128         Assert.assertEquals(expectedName, actualName);
129     }
130
131     @Test
132     public void getInputShouldReturnInputWhenPresent() {
133         final JsonObject jsonObject = jsonObjectBlueprintValid;
134
135         String expected = FIRST_APPP;
136         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
137         String actual = new BlueprintParser().getInput(entry);
138
139         Assert.assertEquals(expected, actual);
140     }
141
142     @Test
143     public void getInputShouldReturnEmptyStringWhenAbsent() {
144         final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
145
146         String expected = "";
147         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
148         String actual = new BlueprintParser().getInput(entry);
149
150         Assert.assertEquals(expected, actual);
151     }
152
153     @Test
154     public void getNodeRepresentationFromCompleteYaml() {
155         final JsonObject jsonObject = jsonObjectBlueprintValid;
156
157         MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "");
158         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
159         MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject, null);
160
161         Assert.assertEquals(expected, actual);
162     }
163
164     @Test
165     public void getMicroServicesFromBlueprintTest() {
166         MicroService thirdApp = new MicroService(THIRD_APPP, MODEL_TYPE3, "", "");
167         MicroService firstApp = new MicroService(FIRST_APPP, MODEL_TYPE1, THIRD_APPP, "");
168         MicroService secondApp = new MicroService(SECOND_APPP, MODEL_TYPE2, FIRST_APPP, "");
169
170         Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
171         Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
172
173         Assert.assertEquals(expected, actual);
174     }
175
176     @Test
177
178     public void fallBackToOneMicroServiceTcaTest() {
179         MicroService tcaMs = new MicroService(BlueprintParser.TCA, "onap.policies.monitoring.cdap.tca.hi.lo.app", "",
180                 "");
181         List<MicroService> expected = Collections.singletonList(tcaMs);
182         List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
183
184         Assert.assertEquals(expected, actual);
185     }
186
187     @Test
188     public void fallBackToOneMicroServiceHolmesTest() {
189         MicroService holmesMs = new MicroService(BlueprintParser.HOLMES, "onap.policies.monitoring.cdap.tca.hi.lo.app",
190                 "", "");
191
192         List<MicroService> expected = Collections.singletonList(holmesMs);
193         List<MicroService> actual = new BlueprintParser()
194                 .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
195
196         Assert.assertEquals(expected, actual);
197     }
198
199     @Test
200     public void newMicroServiceTest() {
201         List<MicroService> microServicesChain = new ChainGenerator()
202                 .getChainOfMicroServices(new BlueprintParser().getMicroServices(newMicroServiceBlueprint));
203         if (microServicesChain.isEmpty()) {
204             microServicesChain = new BlueprintParser().fallbackToOneMicroService(newMicroServiceBlueprint);
205         }
206         assertThat(microServicesChain.size()).isEqualTo(1);
207         assertThat(microServicesChain.get(0).getName()).isEqualTo("pmsh");
208     }
209
210     private static JsonObject yamlToJson(String yamlString) {
211         Yaml yaml = new Yaml();
212         Map<String, Object> map = yaml.load(yamlString);
213         JSONObject jsonObject = new JSONObject(map);
214         return GSON.fromJson(jsonObject.toString(), JsonObject.class);
215     }
216 }