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.getBlueprint()).isEqualTo(loopBlueprint);
91 assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
92 assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
93 .isEqualTo("testValue");
98 public void shouldAddOperationalPolicyToLoop() {
101 OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
102 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
105 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
106 Lists.newArrayList(operationalPolicy));
109 assertThat(actualLoop).isNotNull();
110 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
111 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
112 assertThat(savedPolicies).hasSize(1);
113 assertThat(savedPolicies)
114 .usingElementComparatorIgnoringFields("loop", "createdBy", "createdDate", "updatedBy", "updatedDate")
115 .contains(operationalPolicy);
116 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
117 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
123 public void shouldAddMicroservicePolicyToLoop() {
126 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
127 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
128 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
131 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
132 Lists.newArrayList(microServicePolicy));
135 assertThat(actualLoop).isNotNull();
136 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
137 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
138 assertThat(savedPolicies).hasSize(1);
139 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
140 "createdBy", "updatedBy").containsExactly(microServicePolicy);
141 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
147 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
151 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
152 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
153 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
154 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
155 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
156 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
159 firstMicroServicePolicy
160 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
161 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
162 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
165 assertThat(actualLoop).isNotNull();
166 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
167 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
168 assertThat(savedPolicies).hasSize(2);
169 assertThat(savedPolicies).contains(firstMicroServicePolicy);
170 assertThat(savedPolicies).contains(secondMicroServicePolicy);
171 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
172 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
176 private void saveTestLoopToDb() {
177 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
178 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
179 loopService.saveOrUpdateLoop(testLoop);
184 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
188 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
189 "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
190 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
191 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
193 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
194 true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
197 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
198 Lists.newArrayList(secondMicroServicePolicy));
201 assertThat(actualLoop).isNotNull();
202 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
203 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
204 assertThat(savedPolicies).hasSize(1);
205 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
206 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
212 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
216 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
218 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
219 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
220 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
222 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
223 newJsonConfiguration);
226 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
227 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
228 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
231 assertThat(actualLoop).isNotNull();
232 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
233 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
234 assertThat(savedPolicies).hasSize(2);
235 assertThat(savedPolicies)
236 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
237 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
238 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
239 .map(Loop::getName).collect(Collectors.toSet());
240 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
245 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
249 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
250 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
251 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
253 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
254 JsonUtils.GSON.fromJson("{}", JsonObject.class));
257 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
258 Lists.newArrayList(secondOperationalPolicy));
261 assertThat(actualLoop).isNotNull();
262 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
263 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
264 assertThat(savedPolicies).hasSize(1);
265 assertThat(savedPolicies)
266 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
267 .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, except for Microservice
316 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
317 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
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);