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