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