Merge "Update the alpine version"
[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.After;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.clamp.clds.Application;
40 import org.onap.clamp.clds.util.JsonUtils;
41 import org.onap.clamp.loop.log.LogType;
42 import org.onap.clamp.loop.log.LoopLog;
43 import org.onap.clamp.loop.log.LoopLogService;
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     @After
75     public void tearDown() {
76         loopsRepository.deleteAll();
77     }
78
79     @Test
80     @Transactional
81     public void shouldCreateEmptyLoop() {
82         // given
83         String loopBlueprint = "blueprint";
84         String loopSvg = "representation";
85         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
86         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
87         testLoop.setLastComputedState(LoopState.DESIGN);
88
89         // when
90         Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
91
92         // then
93         assertThat(actualLoop).isNotNull();
94         assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
95         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
96         assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint);
97         assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
98         assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
99             .isEqualTo("testValue");
100     }
101
102     @Test
103     @Transactional
104     public void shouldAddOperationalPolicyToLoop() {
105         // given
106         saveTestLoopToDb();
107         OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
108             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
109
110         // when
111         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
112             Lists.newArrayList(operationalPolicy));
113
114         // then
115         assertThat(actualLoop).isNotNull();
116         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
117         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
118         assertThat(savedPolicies).hasSize(1);
119         assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").contains(operationalPolicy);
120         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
121         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
122
123     }
124
125     @Test
126     @Transactional
127     public void shouldAddMicroservicePolicyToLoop() {
128         // given
129         saveTestLoopToDb();
130         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
131             "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
132             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
133
134         // when
135         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
136             Lists.newArrayList(microServicePolicy));
137
138         // then
139         assertThat(actualLoop).isNotNull();
140         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
141         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
142         assertThat(savedPolicies).hasSize(1);
143         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
144             .containsExactly(microServicePolicy);
145         assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
146
147     }
148
149     @Test
150     @Transactional
151     public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
152         // given
153         saveTestLoopToDb();
154
155         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
156             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
157         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
158         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
159             "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, JsonUtils.GSON.fromJson("{}", JsonObject.class),
160             null);
161
162         // when
163         firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
164         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
165             Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
166
167         // then
168         assertThat(actualLoop).isNotNull();
169         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
170         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
171         assertThat(savedPolicies).hasSize(2);
172         assertThat(savedPolicies).contains(firstMicroServicePolicy);
173         assertThat(savedPolicies).contains(secondMicroServicePolicy);
174         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
175             .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
176
177     }
178
179     private void saveTestLoopToDb() {
180         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
181         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
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")
209             .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).usingElementComparatorIgnoringFields("loop")
239             .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
240         Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
241             .map(Loop::getName).collect(Collectors.toSet());
242         assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
243     }
244
245     @Test
246     @Transactional
247     public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
248         // given
249         saveTestLoopToDb();
250
251         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
252             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
253         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
254
255         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
256             JsonUtils.GSON.fromJson("{}", JsonObject.class));
257
258         // when
259         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
260             Lists.newArrayList(secondOperationalPolicy));
261
262         // then
263         assertThat(actualLoop).isNotNull();
264         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
265         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
266         assertThat(savedPolicies).hasSize(1);
267         assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").containsExactly(secondOperationalPolicy);
268         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
269         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
270
271     }
272
273     @Test
274     @Transactional
275     public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
276         // given
277         saveTestLoopToDb();
278         String expectedJson = "{\"test\":\"test\"}";
279         JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
280         JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
281         loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
282
283         // when
284         Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
285
286         // then
287         assertThat(actualLoop).isNotNull();
288         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
289         JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
290         assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
291     }
292
293     @Test
294     @Transactional
295     public void deleteAttempt() {
296         saveTestLoopToDb();
297         // Add log
298         Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
299         loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
300         loop = loopService.saveOrUpdateLoop(loop);
301         // Add op policy
302         OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
303             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
304         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
305
306         // Add Micro service policy
307         MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "",
308             "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
309             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
310         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
311
312         // Verify it's there
313         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
314         loopService.deleteLoop(EXAMPLE_LOOP_NAME);
315         // Verify it's well deleted and has been cascaded
316         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
317         assertThat(microServicePolicyService.isExisting("microPolicy")).isFalse();
318         assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
319         assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
320     }
321
322     @Test
323     @Transactional
324     public void testUpdateLoopState() {
325         saveTestLoopToDb();
326         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
327         loopService.updateLoopState(loop, "SUBMITTED");
328         Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
329         assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
330     }
331
332     @Test
333     @Transactional
334     public void testUpdateDcaeDeploymentFields() {
335         saveTestLoopToDb();
336         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
337         loopService.updateDcaeDeploymentFields(loop,"CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
338                                                "https4://deployment-handler.onap:8443");
339         loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
340         assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
341         assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
342     }
343
344     @Test
345     @Transactional
346     public void testUpdateMicroservicePolicy() {
347         saveTestLoopToDb();
348         assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
349         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
350             "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
351             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
352         loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
353         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
354     }
355
356     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
357         return new Loop(loopName, loopBlueprint, loopSvg);
358     }
359 }