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