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