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