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.loop.template.LoopTemplate;
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;
76 public void shouldCreateEmptyLoop() {
78 String loopBlueprint = "blueprint";
79 String loopSvg = "representation";
80 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
81 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
82 testLoop.setLastComputedState(LoopState.DESIGN);
85 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
88 assertThat(actualLoop).isNotNull();
89 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
90 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
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 LoopTemplate template = new LoopTemplate();
180 template.setName("testTemplate");
181 testLoop.setLoopTemplate(template);
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", "createdDate", "updatedDate",
209 "createdBy", "updatedBy").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)
239 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
240 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
241 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
242 .map(Loop::getName).collect(Collectors.toSet());
243 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
248 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
252 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
253 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
254 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
256 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
257 JsonUtils.GSON.fromJson("{}", JsonObject.class));
260 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
261 Lists.newArrayList(secondOperationalPolicy));
264 assertThat(actualLoop).isNotNull();
265 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
266 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
267 assertThat(savedPolicies).hasSize(1);
268 assertThat(savedPolicies)
269 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
270 .containsExactly(secondOperationalPolicy);
271 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
272 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
278 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
281 String expectedJson = "{\"test\":\"test\"}";
282 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
283 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
284 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
287 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
290 assertThat(actualLoop).isNotNull();
291 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
292 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
293 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
298 public void deleteAttempt() {
301 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
302 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
303 LoopTemplate template = new LoopTemplate();
304 template.setName("testTemplate");
305 loop.setLoopTemplate(template);
306 loop = loopService.saveOrUpdateLoop(loop);
308 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
309 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
310 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
312 // Add Micro service policy
313 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "",
314 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
315 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
316 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
319 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
320 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
321 // Verify it's well deleted and has been cascaded, except for Microservice
322 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
323 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
324 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
325 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
330 public void testUpdateLoopState() {
332 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
333 loopService.updateLoopState(loop, "SUBMITTED");
334 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
335 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
340 public void testUpdateDcaeDeploymentFields() {
342 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
343 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
344 "https4://deployment-handler.onap:8443");
345 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
346 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
347 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
352 public void testUpdateMicroservicePolicy() {
354 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
355 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
356 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
357 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
358 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
359 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
362 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
363 return new Loop(loopName, loopSvg);