Create SVG in UI
[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 import java.util.Set;
30 import java.util.stream.Collectors;
31 import javax.transaction.Transactional;
32 import org.assertj.core.util.Lists;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.clamp.clds.Application;
36 import org.onap.clamp.clds.util.JsonUtils;
37 import org.onap.clamp.loop.log.LogType;
38 import org.onap.clamp.loop.log.LoopLog;
39 import org.onap.clamp.loop.log.LoopLogService;
40 import org.onap.clamp.loop.template.LoopTemplate;
41 import org.onap.clamp.loop.template.PolicyModel;
42 import org.onap.clamp.loop.template.PolicyModelsService;
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     @Autowired
74     PolicyModelsService policyModelsService;
75
76     @Test
77     @Transactional
78     public void shouldCreateEmptyLoop() {
79         // given
80         String loopBlueprint = "blueprint";
81         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint);
82         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
83         testLoop.setLastComputedState(LoopState.DESIGN);
84
85         // when
86         Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
87
88         // then
89         assertThat(actualLoop).isNotNull();
90         assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
91         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
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), null, null, null, null);
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         PolicyModel policyModel = new PolicyModel("org.policies.policyModel1",
127                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyModel1");
128         policyModelsService.saveOrUpdatePolicyModel(policyModel);
129         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
130                 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
131
132         // when
133         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
134                 Lists.newArrayList(microServicePolicy));
135
136         // then
137         assertThat(actualLoop).isNotNull();
138         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
139         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
140         assertThat(savedPolicies).hasSize(1);
141         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
142                 "createdBy", "updatedBy").containsExactly(microServicePolicy);
143         assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
144
145     }
146
147     @Test
148     @Transactional
149     //@Commit
150     public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
151         // given
152         saveTestLoopToDb();
153         PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
154                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
155         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
156         PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
157                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
158         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
159         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1, false,
160                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
161
162         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
163         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2, false,
164                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
165
166         // when
167         firstMicroServicePolicy
168                 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
169         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
170                 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
171
172         // then
173         assertThat(actualLoop).isNotNull();
174         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
175         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
176         assertThat(savedPolicies).hasSize(2);
177         assertThat(savedPolicies).contains(firstMicroServicePolicy);
178         assertThat(savedPolicies).contains(secondMicroServicePolicy);
179         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
180                 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
181     }
182
183     private void saveTestLoopToDb() {
184         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint");
185         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
186         LoopTemplate template = new LoopTemplate();
187         template.setName("testTemplate");
188         testLoop.setLoopTemplate(template);
189         loopService.saveOrUpdateLoop(testLoop);
190     }
191
192     @Test
193     @Transactional
194     public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
195         // given
196         saveTestLoopToDb();
197         PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
198                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
199         policyModelsService.saveOrUpdatePolicyModel(policyModel1);
200         PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
201                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
202         policyModelsService.saveOrUpdatePolicyModel(policyModel2);
203         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1,
204                 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
205         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
206
207         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2,
208                 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
209
210         // when
211         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
212                 Lists.newArrayList(secondMicroServicePolicy));
213
214         // then
215         assertThat(actualLoop).isNotNull();
216         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
217         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
218         assertThat(savedPolicies).hasSize(1);
219         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
220                 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
221
222     }
223
224     @Test
225     @Transactional
226     public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
227         // given
228         saveTestLoopToDb();
229
230         JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
231
232         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
233                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
234         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
235
236         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
237                 newJsonConfiguration, null, null, null, null);
238
239         // when
240         firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
241         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
242                 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
243
244         // then
245         assertThat(actualLoop).isNotNull();
246         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
247         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
248         assertThat(savedPolicies).hasSize(2);
249         assertThat(savedPolicies)
250                 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
251                 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
252         Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
253                 .map(Loop::getName).collect(Collectors.toSet());
254         assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
255     }
256
257     @Test
258     @Transactional
259     public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
260         // given
261         saveTestLoopToDb();
262
263         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
264                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
265         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
266
267         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
268                 JsonUtils.GSON.fromJson("{}", JsonObject.class), null, null, "pdpGroup1", "pdpSubgroup1");
269
270         // when
271         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
272                 Lists.newArrayList(secondOperationalPolicy));
273
274         // then
275         assertThat(actualLoop).isNotNull();
276         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
277         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
278         assertThat(savedPolicies).hasSize(1);
279         assertThat(savedPolicies)
280                 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
281                 .containsExactly(secondOperationalPolicy);
282         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
283         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
284
285     }
286
287     @Test
288     @Transactional
289     public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
290         // given
291         saveTestLoopToDb();
292         String expectedJson = "{\"test\":\"test\"}";
293         JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
294         JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
295         loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
296
297         // when
298         Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
299
300         // then
301         assertThat(actualLoop).isNotNull();
302         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
303         JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
304         assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
305     }
306
307     @Test
308     @Transactional
309     public void deleteAttempt() {
310         saveTestLoopToDb();
311         // Add log
312         Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
313         loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
314         LoopTemplate template = new LoopTemplate();
315         template.setName("testTemplate");
316         loop.setLoopTemplate(template);
317         loop = loopService.saveOrUpdateLoop(loop);
318         // Add op policy
319         OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
320                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
321         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
322
323         PolicyModel policyModel = new PolicyModel("org.policies.microPolicy",
324                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "microPolicy");
325         policyModelsService.saveOrUpdatePolicyModel(policyModel);
326         // Add Micro service policy
327         MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", policyModel,
328                 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
329         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
330
331         // Verify it's there
332         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
333         loopService.deleteLoop(EXAMPLE_LOOP_NAME);
334         // Verify it's well deleted and has been cascaded, except for Microservice
335         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
336         assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
337         assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
338         assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
339     }
340
341     @Test
342     @Transactional
343     public void testUpdateLoopState() {
344         saveTestLoopToDb();
345         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
346         loopService.updateLoopState(loop, "SUBMITTED");
347         Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
348         assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
349     }
350
351     @Test
352     @Transactional
353     public void testUpdateDcaeDeploymentFields() {
354         saveTestLoopToDb();
355         Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
356         loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
357                 "https4://deployment-handler.onap:8443");
358         loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
359         assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
360         assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
361     }
362
363     @Test
364     @Transactional
365     public void testUpdateMicroservicePolicy() {
366         saveTestLoopToDb();
367         assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
368         PolicyModel policyModel = new PolicyModel("org.policies.policyName",
369                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyName");
370         policyModelsService.saveOrUpdatePolicyModel(policyModel);
371         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
372                 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
373         loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
374         assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
375     }
376
377     private Loop createTestLoop(String loopName, String loopBlueprint) {
378         return new Loop(loopName);
379     }
380 }