9efb6898346d992a5938297741ee88d6e18ec3d7
[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.exception.sdc.controller.BlueprintParserException;
48 import org.onap.clamp.clds.util.ResourceFileUtil;
49 import org.yaml.snakeyaml.Yaml;
50
51 public class BlueprintParserTest {
52     private static final Gson GSON = new Gson();
53     private static final String FIRST_APPP = "first_app";
54     private static final String SECOND_APPP = "second_app";
55     private static final String THIRD_APPP = "third_app";
56     private static final String MODEL_TYPE1 = "type1";
57     private static final String MODEL_TYPE_TCA = "onap.policies.monitoring.cdap.tca.hi.lo.app";
58     private static final String VERSION = "1.0.0";
59
60     private static String microServiceTheWholeBlueprintValid;
61     private static String newMicroServiceBlueprint;
62     private static JsonObject jsonObjectBlueprintInvalid;
63     private static JsonObject jsonObjectBlueprintWithoutName;
64     private static JsonObject jsonObjectBlueprintWithoutProperties;
65     private static JsonObject jsonObjectBlueprintWithoutRelationships;
66     private static JsonObject jsonObjectBlueprintValidWithVersion;
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
78         newMicroServiceBlueprint = ResourceFileUtil.getResourceAsString("clds/new-microservice.yaml");
79
80         String microServiceBlueprintInvalid = ResourceFileUtil
81                 .getResourceAsString("clds/single-microservice-fragment-invalid.yaml");
82         jsonObjectBlueprintInvalid = yamlToJson(microServiceBlueprintInvalid);
83         String microServiceBlueprintWithoutName = ResourceFileUtil
84                 .getResourceAsString("clds/single-microservice-fragment-without-name.yaml");
85         jsonObjectBlueprintWithoutName = yamlToJson(microServiceBlueprintWithoutName);
86         String microServiceBlueprintWithoutProperties = ResourceFileUtil
87                 .getResourceAsString("clds/single-microservice-fragment-without-properties.yaml");
88         jsonObjectBlueprintWithoutProperties = yamlToJson(microServiceBlueprintWithoutProperties);
89         String microServiceBlueprintValidWithVersion = ResourceFileUtil
90                 .getResourceAsString("clds/single-microservice-fragment-valid-with-version.yaml");
91         jsonObjectBlueprintValidWithVersion = yamlToJson(microServiceBlueprintValidWithVersion);
92
93         String microServiceBlueprintWithoutRelationships = ResourceFileUtil
94                 .getResourceAsString("clds/single-microservice-fragment-without-relationships.yaml");
95         jsonObjectBlueprintWithoutRelationships = yamlToJson(microServiceBlueprintWithoutRelationships);
96
97     }
98
99     @Test
100     public void getNameShouldReturnDefinedName() {
101         final JsonObject jsonObject = jsonObjectBlueprintInvalid;
102         String expectedName = jsonObject.get(jsonObject.keySet().iterator().next()).getAsJsonObject().get("properties")
103                 .getAsJsonObject().get("name").getAsString();
104         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
105         String actualName = BlueprintParser.getName(entry);
106
107         Assert.assertEquals(expectedName, actualName);
108     }
109
110     @Test
111     public void getNameShouldReturnServiceNameWhenNoNameDefined() {
112         final JsonObject jsonObject = jsonObjectBlueprintWithoutName;
113
114         String expectedName = jsonObject.keySet().iterator().next();
115         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
116         String actualName = BlueprintParser.getName(entry);
117
118         Assert.assertEquals(expectedName, actualName);
119     }
120
121     @Test
122     public void getNameShouldReturnServiceNameWhenNoPropertiesDefined() {
123         final JsonObject jsonObject = jsonObjectBlueprintWithoutProperties;
124
125         String expectedName = jsonObject.keySet().iterator().next();
126         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
127         String actualName = BlueprintParser.getName(entry);
128
129         Assert.assertEquals(expectedName, actualName);
130     }
131
132     @Test
133     public void getInputShouldReturnInputWhenPresent() {
134         final JsonObject jsonObject = jsonObjectBlueprintInvalid;
135
136         String expected = FIRST_APPP;
137         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
138         String actual = BlueprintParser.getInput(entry);
139
140         Assert.assertEquals(expected, actual);
141     }
142
143     @Test
144     public void getInputShouldReturnEmptyStringWhenAbsent() {
145         final JsonObject jsonObject = jsonObjectBlueprintWithoutRelationships;
146
147         String expected = "";
148         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
149         String actual = BlueprintParser.getInput(entry);
150
151         Assert.assertEquals(expected, actual);
152     }
153
154     @Test(expected = BlueprintParserException.class)
155     public void getNodeRepresentationFromIncompleteYaml() throws BlueprintParserException {
156         BlueprintParser.getNodeRepresentation(jsonObjectBlueprintInvalid.entrySet().iterator().next(),
157                 jsonObjectBlueprintInvalid, null);
158     }
159
160     @Test
161     public void getNodeRepresentationFromCompleteYamlWithModelVersion() throws BlueprintParserException {
162         final JsonObject jsonObject = jsonObjectBlueprintValidWithVersion;
163
164         BlueprintMicroService expected = new BlueprintMicroService(SECOND_APPP, MODEL_TYPE1, "", "10.0.0");
165         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
166         BlueprintMicroService actual = BlueprintParser.getNodeRepresentation(entry, jsonObject, null);
167
168         Assert.assertEquals(expected, actual);
169     }
170
171     @Test
172     public void getMicroServicesFromBlueprintTest() throws BlueprintParserException {
173         BlueprintMicroService thirdApp = new BlueprintMicroService(THIRD_APPP, MODEL_TYPE_TCA, SECOND_APPP, VERSION);
174         BlueprintMicroService firstApp = new BlueprintMicroService(FIRST_APPP, MODEL_TYPE_TCA, "", VERSION);
175         BlueprintMicroService secondApp = new BlueprintMicroService(SECOND_APPP, MODEL_TYPE_TCA, FIRST_APPP, VERSION);
176
177         Set<BlueprintMicroService> expected = new HashSet<>(Arrays.asList(firstApp, secondApp, thirdApp));
178         Set<BlueprintMicroService> actual = BlueprintParser.getMicroServices(microServiceTheWholeBlueprintValid);
179
180         Assert.assertEquals(expected, actual);
181     }
182
183     @Test
184     public void fallBackToOneMicroServiceTcaTest() {
185         BlueprintMicroService tcaMs = new BlueprintMicroService(BlueprintParser.TCA,
186                 "onap.policies.monitoring.cdap.tca.hi.lo.app", "", VERSION);
187         List<BlueprintMicroService> expected = Collections.singletonList(tcaMs);
188         List<BlueprintMicroService> actual = BlueprintParser.fallbackToOneMicroService();
189
190         Assert.assertEquals(expected, actual);
191     }
192
193     @Test
194     public void newMicroServiceTest() throws BlueprintParserException {
195         List<BlueprintMicroService> microServicesChain = new ChainGenerator()
196                 .getChainOfMicroServices(BlueprintParser.getMicroServices(newMicroServiceBlueprint));
197         if (microServicesChain.isEmpty()) {
198             microServicesChain = BlueprintParser.fallbackToOneMicroService();
199         }
200         assertThat(microServicesChain.size()).isEqualTo(1);
201         assertThat(microServicesChain.get(0).getName()).isEqualTo("pmsh");
202     }
203
204     private static JsonObject yamlToJson(String yamlString) {
205         Yaml yaml = new Yaml();
206         Map<String, Object> map = yaml.load(yamlString);
207         JSONObject jsonObject = new JSONObject(map);
208         return GSON.fromJson(jsonObject.toString(), JsonObject.class);
209     }
210 }