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