Merge "Improved user guide details"
[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 com.google.gson.Gson;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import java.util.Set;
40
41 import org.json.JSONObject;
42 import org.junit.Assert;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.clamp.clds.util.ResourceFileUtil;
46 import org.yaml.snakeyaml.Yaml;
47
48 public class BlueprintParserTest {
49     private static final Gson GSON = new Gson();
50     private static final String FIRST_APPP = "first_app";
51     private static final String SECOND_APPP = "second_app";
52     private static final String THIRD_APPP = "third_app";
53     private static final String MODEL_TYPE1 = "type1";
54     private static final String MODEL_TYPE2 = "type2";
55     private static final String MODEL_TYPE3 = "type3";
56
57     private static String microServiceTheWholeBlueprintValid;
58     private static String microServiceBlueprintOldStyleTCA;
59     private static String microServiceBlueprintOldStyleHolmes;
60
61     private static JsonObject jsonObjectBlueprintValid;
62     private static JsonObject jsonObjectBlueprintWithoutName;
63     private static JsonObject jsonObjectBlueprintWithoutProperties;
64     private static JsonObject jsonObjectBlueprintWithoutRelationships;
65
66     /**
67      * Method to load Blueprints before all test.
68      *
69      * @throws IOException
70      *         In case of issues when opening the files
71      */
72     @BeforeClass
73     public static void loadBlueprints() throws IOException {
74         microServiceTheWholeBlueprintValid = ResourceFileUtil
75             .getResourceAsString("clds/blueprint-with-microservice-chain.yaml");
76         microServiceBlueprintOldStyleTCA = ResourceFileUtil.getResourceAsString("clds/tca-old-style-ms.yaml");
77         microServiceBlueprintOldStyleHolmes = ResourceFileUtil.getResourceAsString("clds/holmes-old-style-ms.yaml");
78
79         String microServiceBlueprintValid = ResourceFileUtil
80             .getResourceAsString("clds/single-microservice-fragment-valid.yaml");
81         String microServiceBlueprintWithoutName = ResourceFileUtil
82             .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
83         String microServiceBlueprintWithoutProperties = ResourceFileUtil
84             .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
85
86         jsonObjectBlueprintValid = yamlToJson(microServiceBlueprintValid);
87         jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
88         jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
89
90         String microServiceBlueprintWithoutRelationships = ResourceFileUtil
91                 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
92         jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
93
94     }
95
96     @Test
97     public void getNameShouldReturnDefinedName() {
98         final JsonObject jsonObject = jsonObjectBlueprintValid;
99         String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
100             .getAsJsonObject().get("name").getAsString();
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 getNameShouldReturnServiceNameWhenNoNameDefined() {
109         final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
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 getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
120         final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
121
122         String expectedName = jsonObject.keySet().iterator().next();
123         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
124         String actualName = new BlueprintParser().getName(entry);
125
126         Assert.assertEquals(expectedName, actualName);
127     }
128
129     @Test
130     public void getInputShouldReturnInputWhenPresent() {
131         final JsonObject jsonObject = jsonObjectBlueprintValid;
132
133         String expected = FIRST_APPP;
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 getInputShouldReturnEmptyStringWhenAbsent() {
142         final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
143
144         String expected = "";
145         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
146         String actual = new BlueprintParser().getInput(entry);
147
148         Assert.assertEquals(expected, actual);
149     }
150
151     @Test
152     public void getNodeRepresentationFromCompleteYaml() {
153         final JsonObject jsonObject = jsonObjectBlueprintValid;
154
155         MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "");
156         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
157         MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject, null);
158
159         Assert.assertEquals(expected, actual);
160     }
161
162     @Test
163     public void getMicroServicesFromBlueprintTest() {
164         MicroService thirdApp = new MicroService(THIRD_APPP, MODEL_TYPE3, "", "");
165         MicroService firstApp = new MicroService(FIRST_APPP, MODEL_TYPE1, THIRD_APPP, "");
166         MicroService secondApp = new MicroService(SECOND_APPP, MODEL_TYPE2, FIRST_APPP, "");
167
168         Set<MicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
169         Set<MicroService> actual = new BlueprintParser().getMicroServices(microServiceTheWholeBlueprintValid);
170
171         Assert.assertEquals(expected, actual);
172     }
173
174     @Test
175
176     public void fallBackToOneMicroServiceTcaTest() {
177         MicroService tcaMs = new MicroService(BlueprintParser.TCA,
178                 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", "");
179         List<MicroService> expected = Collections.singletonList(tcaMs);
180         List<MicroService> actual = new BlueprintParser().fallbackToOneMicroService(microServiceBlueprintOldStyleTCA);
181
182         Assert.assertEquals(expected, actual);
183     }
184
185     @Test
186     public void fallBackToOneMicroServiceHolmesTest() {
187         MicroService holmesMs = new MicroService(BlueprintParser.HOLMES,
188                 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", "");
189
190         List<MicroService> expected = Collections.singletonList(holmesMs);
191         List<MicroService> actual = new BlueprintParser()
192             .fallbackToOneMicroService(microServiceBlueprintOldStyleHolmes);
193
194         Assert.assertEquals(expected, actual);
195     }
196
197     private static JsonObject yamlToJson(String yamlString) {
198         Yaml yaml = new Yaml();
199         Map<String, Object> map = yaml.load(yamlString);
200         JSONObject jsonObject = new JSONObject(map);
201         return GSON.fromJson(jsonObject.toString(), JsonObject.class);
202     }
203 }