2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.loop;
27 import static org.assertj.core.api.Assertions.assertThat;
29 import com.google.gson.JsonObject;
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;
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @SpringBootTest(classes = Application.class)
47 public class LoopServiceTestItCase {
49 private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
50 private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
53 LoopService loopService;
56 LoopsRepository loopsRepository;
59 public void tearDown() {
60 loopsRepository.deleteAll();
64 public void shouldCreateEmptyLoop() {
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);
73 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
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");
86 public void shouldAddOperationalPolicyToLoop() {
89 JsonObject confJson = JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class);
90 String policyName = "policyName";
91 OperationalPolicy operationalPolicy = new OperationalPolicy(policyName, null, confJson);
94 Loop actualLoop = loopService
95 .updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
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);
111 public void shouldAddMicroservicePolicyToLoop() {
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, "");
120 Loop actualLoop = loopService
121 .updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
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")
137 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
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));
148 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy(secondPolicyName, "", secondPolicyTosca, true,
149 newJsonRepresentation, null, "");
152 firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
153 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
154 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
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);
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);
173 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
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));
185 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy(secondPolicyName, "", secondPolicyTosca, true,
186 jsonRepresentation, null, "");
189 Loop actualLoop = loopService
190 .updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(secondMicroServicePolicy));
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);
204 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
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));
215 OperationalPolicy secondOperationalPolicy = new OperationalPolicy(secondPolicyName, null, newJsonConfiguration);
218 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
219 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
220 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
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)
232 .collect(Collectors.toSet());
233 assertThat(policiesLoops)
234 .containsExactly(EXAMPLE_LOOP_NAME);
238 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
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));
249 OperationalPolicy secondOperationalPolicy = new OperationalPolicy(secondPolicyName, null, jsonRepresentation);
252 Loop actualLoop = loopService
253 .updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(secondOperationalPolicy));
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);
268 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
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);
277 Loop actualLoop = loopService
278 .updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
281 assertThat(actualLoop).isNotNull();
282 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
283 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
284 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
287 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
288 return new Loop(loopName, loopBlueprint, loopSvg);