LoopLog repository
[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.After;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.clamp.clds.Application;
40 import org.onap.clamp.clds.util.JsonUtils;
41 import org.onap.clamp.policy.microservice.MicroServicePolicy;
42 import org.onap.clamp.policy.operational.OperationalPolicy;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.test.context.junit4.SpringRunner;
46
47 @RunWith(SpringRunner.class)
48 @SpringBootTest(classes = Application.class)
49 public class LoopServiceTestItCase {
50
51     private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
52     private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
53
54     @Autowired
55     LoopService loopService;
56
57     @Autowired
58     LoopsRepository loopsRepository;
59
60     @After
61     public void tearDown() {
62         loopsRepository.deleteAll();
63     }
64
65     @Test
66     @Transactional
67     public void shouldCreateEmptyLoop() {
68         // given
69         String loopBlueprint = "blueprint";
70         String loopSvg = "representation";
71         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
72         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
73         testLoop.setLastComputedState(LoopState.DESIGN);
74
75         // when
76         Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
77
78         // then
79         assertThat(actualLoop).isNotNull();
80         assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
81         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
82         assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint);
83         assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
84         assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
85             .isEqualTo("testValue");
86     }
87
88     @Test
89     @Transactional
90     public void shouldAddOperationalPolicyToLoop() {
91         // given
92         saveTestLoopToDb();
93         OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
94             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
95
96         // when
97         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
98             Lists.newArrayList(operationalPolicy));
99
100         // then
101         assertThat(actualLoop).isNotNull();
102         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
103         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
104         assertThat(savedPolicies).hasSize(1);
105         assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").contains(operationalPolicy);
106         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
107         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
108
109     }
110
111     @Test
112     @Transactional
113     public void shouldAddMicroservicePolicyToLoop() {
114         // given
115         saveTestLoopToDb();
116         MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
117             "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
118             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
119
120         // when
121         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
122             Lists.newArrayList(microServicePolicy));
123
124         // then
125         assertThat(actualLoop).isNotNull();
126         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
127         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
128         assertThat(savedPolicies).hasSize(1);
129         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
130             .containsExactly(microServicePolicy);
131         assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
132
133     }
134
135     @Test
136     @Transactional
137     public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
138         // given
139         saveTestLoopToDb();
140
141         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
142             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
143         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
144         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
145             "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, JsonUtils.GSON.fromJson("{}", JsonObject.class),
146             null);
147
148         // when
149         firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
150         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
151             Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
152
153         // then
154         assertThat(actualLoop).isNotNull();
155         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
156         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
157         assertThat(savedPolicies).hasSize(2);
158         assertThat(savedPolicies).contains(firstMicroServicePolicy);
159         assertThat(savedPolicies).contains(secondMicroServicePolicy);
160         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
161             .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
162
163     }
164
165     private void saveTestLoopToDb() {
166         Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
167         testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
168         loopService.saveOrUpdateLoop(testLoop);
169     }
170
171     @Test
172     @Transactional
173     public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
174         // given
175         saveTestLoopToDb();
176
177         MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
178             "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
179             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
180         loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
181
182         MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
183             true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
184
185         // when
186         Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
187             Lists.newArrayList(secondMicroServicePolicy));
188
189         // then
190         assertThat(actualLoop).isNotNull();
191         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
192         Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
193         assertThat(savedPolicies).hasSize(1);
194         assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
195             .containsExactly(secondMicroServicePolicy);
196
197     }
198
199     @Test
200     @Transactional
201     public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
202         // given
203         saveTestLoopToDb();
204
205         JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
206
207         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
208             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
209         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
210
211         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
212             newJsonConfiguration);
213
214         // when
215         firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
216         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
217             Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
218
219         // then
220         assertThat(actualLoop).isNotNull();
221         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
222         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
223         assertThat(savedPolicies).hasSize(2);
224         assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop")
225             .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
226         Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
227             .map(Loop::getName).collect(Collectors.toSet());
228         assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
229     }
230
231     @Test
232     @Transactional
233     public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
234         // given
235         saveTestLoopToDb();
236
237         OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
238             JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
239         loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
240
241         OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
242             JsonUtils.GSON.fromJson("{}", JsonObject.class));
243
244         // when
245         Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
246             Lists.newArrayList(secondOperationalPolicy));
247
248         // then
249         assertThat(actualLoop).isNotNull();
250         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
251         Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
252         assertThat(savedPolicies).hasSize(1);
253         assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").containsExactly(secondOperationalPolicy);
254         OperationalPolicy savedPolicy = savedPolicies.iterator().next();
255         assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
256
257     }
258
259     @Test
260     @Transactional
261     public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
262         // given
263         saveTestLoopToDb();
264         String expectedJson = "{\"test\":\"test\"}";
265         JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
266         JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
267         loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
268
269         // when
270         Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
271
272         // then
273         assertThat(actualLoop).isNotNull();
274         assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
275         JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
276         assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
277     }
278
279     private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
280         return new Loop(loopName, loopBlueprint, loopSvg);
281     }
282 }