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 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import com.google.gson.JsonObject;
31 import java.util.stream.Collectors;
33 import javax.transaction.Transactional;
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.loop.log.LogType;
42 import org.onap.clamp.loop.log.LoopLog;
43 import org.onap.clamp.loop.log.LoopLogService;
44 import org.onap.clamp.policy.microservice.MicroServicePolicy;
45 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
46 import org.onap.clamp.policy.operational.OperationalPolicy;
47 import org.onap.clamp.policy.operational.OperationalPolicyService;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.test.context.junit4.SpringRunner;
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(classes = Application.class)
54 public class LoopServiceTestItCase {
56 private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
57 private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
60 LoopService loopService;
63 LoopsRepository loopsRepository;
66 MicroservicePolicyService microServicePolicyService;
69 OperationalPolicyService operationalPolicyService;
72 LoopLogService loopLogService;
75 public void tearDown() {
76 loopsRepository.deleteAll();
81 public void shouldCreateEmptyLoop() {
83 String loopBlueprint = "blueprint";
84 String loopSvg = "representation";
85 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
86 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
87 testLoop.setLastComputedState(LoopState.DESIGN);
90 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
93 assertThat(actualLoop).isNotNull();
94 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
95 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
96 assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint);
97 assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
98 assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
99 .isEqualTo("testValue");
104 public void shouldAddOperationalPolicyToLoop() {
107 OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
108 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
111 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
112 Lists.newArrayList(operationalPolicy));
115 assertThat(actualLoop).isNotNull();
116 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
117 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
118 assertThat(savedPolicies).hasSize(1);
119 assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").contains(operationalPolicy);
120 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
121 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
127 public void shouldAddMicroservicePolicyToLoop() {
130 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
131 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
132 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
135 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
136 Lists.newArrayList(microServicePolicy));
139 assertThat(actualLoop).isNotNull();
140 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
141 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
142 assertThat(savedPolicies).hasSize(1);
143 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
144 .containsExactly(microServicePolicy);
145 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
151 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
155 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
156 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
157 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
158 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
159 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, JsonUtils.GSON.fromJson("{}", JsonObject.class),
163 firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
164 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
165 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
168 assertThat(actualLoop).isNotNull();
169 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
170 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
171 assertThat(savedPolicies).hasSize(2);
172 assertThat(savedPolicies).contains(firstMicroServicePolicy);
173 assertThat(savedPolicies).contains(secondMicroServicePolicy);
174 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
175 .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
179 private void saveTestLoopToDb() {
180 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
181 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
182 loopService.saveOrUpdateLoop(testLoop);
187 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
191 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
192 "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
193 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
194 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
196 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
197 true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
200 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
201 Lists.newArrayList(secondMicroServicePolicy));
204 assertThat(actualLoop).isNotNull();
205 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
206 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
207 assertThat(savedPolicies).hasSize(1);
208 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops")
209 .containsExactly(secondMicroServicePolicy);
215 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
219 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
221 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
222 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
223 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
225 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
226 newJsonConfiguration);
229 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
230 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
231 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
234 assertThat(actualLoop).isNotNull();
235 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
236 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
237 assertThat(savedPolicies).hasSize(2);
238 assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop")
239 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
240 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
241 .map(Loop::getName).collect(Collectors.toSet());
242 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
247 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
251 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
252 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
253 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
255 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
256 JsonUtils.GSON.fromJson("{}", JsonObject.class));
259 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
260 Lists.newArrayList(secondOperationalPolicy));
263 assertThat(actualLoop).isNotNull();
264 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
265 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
266 assertThat(savedPolicies).hasSize(1);
267 assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").containsExactly(secondOperationalPolicy);
268 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
269 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
275 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
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);
284 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
287 assertThat(actualLoop).isNotNull();
288 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
289 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
290 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
295 public void deleteAttempt() {
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);
302 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
303 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
304 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
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));
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
316 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
317 assertThat(microServicePolicyService.isExisting("microPolicy")).isFalse();
318 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
319 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
324 public void testUpdateLoopState() {
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);
334 public void testUpdateDcaeDeploymentFields() {
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");
346 public void testUpdateMicroservicePolicy() {
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();
356 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
357 return new Loop(loopName, loopBlueprint, loopSvg);