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