Merge "Change the Csar installer"
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopServiceTestItCase.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
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import com.google.gson.JsonObject;
29
30 import java.util.Set;
31 import java.util.stream.Collectors;
32
33 import javax.transaction.Transactional;
34
35 import org.assertj.core.util.Lists;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.clamp.clds.Application;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.loop.log.LoopLogService;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
45 import org.onap.clamp.policy.operational.OperationalPolicy;
46 import org.onap.clamp.policy.operational.OperationalPolicyService;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = Application.class)
53 public class LoopServiceTestItCase {
54
55     private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
56     private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
57
58     @Autowired
59     LoopService loopService;
60
61     @Autowired
62     LoopsRepository loopsRepository;
63
64     @Autowired
65     MicroServicePolicyService microServicePolicyService;
66
67     @Autowired
68     OperationalPolicyService operationalPolicyService;
69
70     @Autowired
71     LoopLogService loopLogService;
72
73     @Test
74     @Transactional
75     public void shouldCreateEmptyLoop() {
76         // given
77         String loopBlueprint = "blueprint";
78         String loopSvg = "representation";
79         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
80         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
81         testLoop.setLastComputedState(LoopState.DESIGN);
82
83         // when
84         Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
85
86         // then
87         assertThat(actualLoop).isNotNull();
88         assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
89         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
90         assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
91         assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
92                 .isEqualTo("testValue");
93     }
94
95     @Test
96     @Transactional
97     public void shouldAddOperationalPolicyToLoop() {
98         // given
99         saveTestLoopToDb();
100         OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
101                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
102
103         // when
104         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
105                 Lists.newArrayList(operationalPolicy));
106
107         // then
108         assertThat(actualLoop).isNotNull();
109         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
110         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
111         assertThat(savedPolicies).hasSize(1);
112         assertThat(savedPolicies)
113                 .usingElementComparatorIgnoringFields("loop", "createdBy", "createdDate", "updatedBy", "updatedDate")
114                 .contains(operationalPolicy);
115         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
116         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
117
118     }
119
120     @Test
121     @Transactional
122     public void shouldAddMicroservicePolicyToLoop() {
123         // given
124         saveTestLoopToDb();
125         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
126                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
127                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
128
129         // when
130         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
131                 Lists.newArrayList(microServicePolicy));
132
133         // then
134         assertThat(actualLoop).isNotNull();
135         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
136         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
137         assertThat(savedPolicies).hasSize(1);
138         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
139                 "createdBy", "updatedBy").containsExactly(microServicePolicy);
140         assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
141
142     }
143
144     @Test
145     @Transactional
146     public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
147         // given
148         saveTestLoopToDb();
149
150         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
151                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
152         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
153         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
154                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
155                 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
156
157         // when
158         firstMicroServicePolicy
159                 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
160         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
161                 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
162
163         // then
164         assertThat(actualLoop).isNotNull();
165         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
166         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
167         assertThat(savedPolicies).hasSize(2);
168         assertThat(savedPolicies).contains(firstMicroServicePolicy);
169         assertThat(savedPolicies).contains(secondMicroServicePolicy);
170         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
171                 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
172
173     }
174
175     private void saveTestLoopToDb() {
176         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
177         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
178         loopService.saveOrUpdateLoop(testLoop);
179     }
180
181     @Test
182     @Transactional
183     public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
184         // given
185         saveTestLoopToDb();
186
187         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
188                 "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
189                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
190         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
191
192         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
193                 true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
194
195         // when
196         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
197                 Lists.newArrayList(secondMicroServicePolicy));
198
199         // then
200         assertThat(actualLoop).isNotNull();
201         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
202         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
203         assertThat(savedPolicies).hasSize(1);
204         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
205                 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
206
207     }
208
209     @Test
210     @Transactional
211     public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
212         // given
213         saveTestLoopToDb();
214
215         JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
216
217         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
218                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
219         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
220
221         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
222                 newJsonConfiguration);
223
224         // when
225         firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
226         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
227                 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
228
229         // then
230         assertThat(actualLoop).isNotNull();
231         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
232         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
233         assertThat(savedPolicies).hasSize(2);
234         assertThat(savedPolicies)
235                 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
236                 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
237         Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
238                 .map(Loop::getName).collect(Collectors.toSet());
239         assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
240     }
241
242     @Test
243     @Transactional
244     public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
245         // given
246         saveTestLoopToDb();
247
248         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
249                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
250         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
251
252         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
253                 JsonUtils.GSON.fromJson("{}", JsonObject.class));
254
255         // when
256         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
257                 Lists.newArrayList(secondOperationalPolicy));
258
259         // then
260         assertThat(actualLoop).isNotNull();
261         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
262         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
263         assertThat(savedPolicies).hasSize(1);
264         assertThat(savedPolicies)
265                 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
266                 .containsExactly(secondOperationalPolicy);
267         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
268         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
269
270     }
271
272     @Test
273     @Transactional
274     public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
275         // given
276         saveTestLoopToDb();
277         String expectedJson = "{\"test\":\"test\"}";
278         JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
279         JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
280         loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
281
282         // when
283         Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
284
285         // then
286         assertThat(actualLoop).isNotNull();
287         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
288         JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
289         assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
290     }
291
292     @Test
293     @Transactional
294     public void deleteAttempt() {
295         saveTestLoopToDb();
296         // Add log
297         Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
298         loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
299         loop = loopService.saveOrUpdateLoop(loop);
300         // Add op policy
301         OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
302                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
303         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
304
305         // Add Micro service policy
306         MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "",
307                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
308                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
309         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
310
311         // Verify it's there
312         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
313         loopService.deleteLoop(EXAMPLE_LOOP_NAME);
314         // Verify it's well deleted and has been cascaded, except for Microservice
315         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
316         assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
317         assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
318         assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
319     }
320
321     @Test
322     @Transactional
323     public void testUpdateLoopState() {
324         saveTestLoopToDb();
325         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
326         loopService.updateLoopState(loop, "SUBMITTED");
327         Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
328         assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
329     }
330
331     @Test
332     @Transactional
333     public void testUpdateDcaeDeploymentFields() {
334         saveTestLoopToDb();
335         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
336         loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
337                 "https4://deployment-handler.onap:8443");
338         loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
339         assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
340         assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
341     }
342
343     @Test
344     @Transactional
345     public void testUpdateMicroservicePolicy() {
346         saveTestLoopToDb();
347         assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
348         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
349                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
350                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
351         loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
352         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
353     }
354
355     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
356         return new Loop(loopName, loopSvg);
357     }
358 }