Modify the template model
[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.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.clamp.clds.Application;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.loop.log.LoopLogService;
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     @Test
74     @Transactional
75     public void shouldCreateEmptyLoop() {
76         // given
77         String loopBlueprint = "blueprint";
78         String loopSvg = "representation";
79         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
80         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
81         testLoop.setLastComputedState(LoopState.DESIGN);
82
83         // when
84         Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
85
86         // then
87         assertThat(actualLoop).isNotNull();
88         assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
89         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
90         assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint);
91         assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
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));
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         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
127                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
128                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
129
130         // when
131         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
132                 Lists.newArrayList(microServicePolicy));
133
134         // then
135         assertThat(actualLoop).isNotNull();
136         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
137         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
138         assertThat(savedPolicies).hasSize(1);
139         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
140                 "createdBy", "updatedBy").containsExactly(microServicePolicy);
141         assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
142
143     }
144
145     @Test
146     @Transactional
147     public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
148         // given
149         saveTestLoopToDb();
150
151         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
152                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
153         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
154         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
155                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
156                 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
157
158         // when
159         firstMicroServicePolicy
160                 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
161         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
162                 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
163
164         // then
165         assertThat(actualLoop).isNotNull();
166         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
167         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
168         assertThat(savedPolicies).hasSize(2);
169         assertThat(savedPolicies).contains(firstMicroServicePolicy);
170         assertThat(savedPolicies).contains(secondMicroServicePolicy);
171         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
172                 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
173
174     }
175
176     private void saveTestLoopToDb() {
177         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
178         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
179         loopService.saveOrUpdateLoop(testLoop);
180     }
181
182     @Test
183     @Transactional
184     public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
185         // given
186         saveTestLoopToDb();
187
188         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
189                 "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
190                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
191         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
192
193         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
194                 true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
195
196         // when
197         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
198                 Lists.newArrayList(secondMicroServicePolicy));
199
200         // then
201         assertThat(actualLoop).isNotNull();
202         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
203         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
204         assertThat(savedPolicies).hasSize(1);
205         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
206                 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
207
208     }
209
210     @Test
211     @Transactional
212     public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
213         // given
214         saveTestLoopToDb();
215
216         JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
217
218         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
219                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
220         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
221
222         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
223                 newJsonConfiguration);
224
225         // when
226         firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
227         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
228                 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
229
230         // then
231         assertThat(actualLoop).isNotNull();
232         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
233         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
234         assertThat(savedPolicies).hasSize(2);
235         assertThat(savedPolicies)
236                 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
237                 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
238         Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
239                 .map(Loop::getName).collect(Collectors.toSet());
240         assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
241     }
242
243     @Test
244     @Transactional
245     public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
246         // given
247         saveTestLoopToDb();
248
249         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
250                 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
251         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
252
253         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
254                 JsonUtils.GSON.fromJson("{}", JsonObject.class));
255
256         // when
257         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
258                 Lists.newArrayList(secondOperationalPolicy));
259
260         // then
261         assertThat(actualLoop).isNotNull();
262         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
263         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
264         assertThat(savedPolicies).hasSize(1);
265         assertThat(savedPolicies)
266                 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
267                 .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, except for Microservice
316         assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
317         assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
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 }