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.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;
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = Application.class)
53 public class LoopServiceTestItCase {
55 private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest";
56 private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}";
59 LoopService loopService;
62 LoopsRepository loopsRepository;
65 MicroServicePolicyService microServicePolicyService;
68 OperationalPolicyService operationalPolicyService;
71 LoopLogService loopLogService;
75 public void shouldCreateEmptyLoop() {
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);
84 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
87 assertThat(actualLoop).isNotNull();
88 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
89 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
90 assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
91 assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
92 .isEqualTo("testValue");
97 public void shouldAddOperationalPolicyToLoop() {
100 OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
101 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
104 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
105 Lists.newArrayList(operationalPolicy));
108 assertThat(actualLoop).isNotNull();
109 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
110 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
111 assertThat(savedPolicies).hasSize(1);
112 assertThat(savedPolicies)
113 .usingElementComparatorIgnoringFields("loop", "createdBy", "createdDate", "updatedBy", "updatedDate")
114 .contains(operationalPolicy);
115 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
116 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
122 public void shouldAddMicroservicePolicyToLoop() {
125 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
126 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
127 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
130 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
131 Lists.newArrayList(microServicePolicy));
134 assertThat(actualLoop).isNotNull();
135 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
136 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
137 assertThat(savedPolicies).hasSize(1);
138 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
139 "createdBy", "updatedBy").containsExactly(microServicePolicy);
140 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
146 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
150 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
151 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
152 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
153 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
154 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
155 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
158 firstMicroServicePolicy
159 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
160 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
161 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
164 assertThat(actualLoop).isNotNull();
165 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
166 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
167 assertThat(savedPolicies).hasSize(2);
168 assertThat(savedPolicies).contains(firstMicroServicePolicy);
169 assertThat(savedPolicies).contains(secondMicroServicePolicy);
170 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
171 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
175 private void saveTestLoopToDb() {
176 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
177 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
178 loopService.saveOrUpdateLoop(testLoop);
183 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
187 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
188 "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
189 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
190 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
192 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
193 true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
196 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
197 Lists.newArrayList(secondMicroServicePolicy));
200 assertThat(actualLoop).isNotNull();
201 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
202 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
203 assertThat(savedPolicies).hasSize(1);
204 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
205 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
211 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
215 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
217 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
218 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
219 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
221 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
222 newJsonConfiguration);
225 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
226 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
227 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
230 assertThat(actualLoop).isNotNull();
231 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
232 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
233 assertThat(savedPolicies).hasSize(2);
234 assertThat(savedPolicies)
235 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
236 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
237 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
238 .map(Loop::getName).collect(Collectors.toSet());
239 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
244 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
248 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
249 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
250 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
252 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
253 JsonUtils.GSON.fromJson("{}", JsonObject.class));
256 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
257 Lists.newArrayList(secondOperationalPolicy));
260 assertThat(actualLoop).isNotNull();
261 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
262 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
263 assertThat(savedPolicies).hasSize(1);
264 assertThat(savedPolicies)
265 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
266 .containsExactly(secondOperationalPolicy);
267 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
268 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
274 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
277 String expectedJson = "{\"test\":\"test\"}";
278 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
279 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
280 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
283 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
286 assertThat(actualLoop).isNotNull();
287 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
288 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
289 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
294 public void deleteAttempt() {
297 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
298 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
299 loop = loopService.saveOrUpdateLoop(loop);
301 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
302 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
303 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
305 // Add Micro service policy
306 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "",
307 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
308 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
309 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
312 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
313 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
314 // Verify it's well deleted and has been cascaded, except for Microservice
315 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
316 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
317 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
318 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
323 public void testUpdateLoopState() {
325 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
326 loopService.updateLoopState(loop, "SUBMITTED");
327 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
328 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
333 public void testUpdateDcaeDeploymentFields() {
335 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
336 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
337 "https4://deployment-handler.onap:8443");
338 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
339 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
340 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
345 public void testUpdateMicroservicePolicy() {
347 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
348 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
349 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
350 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
351 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
352 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
355 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
356 return new Loop(loopName, loopSvg);