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;
30 import java.util.stream.Collectors;
31 import javax.transaction.Transactional;
32 import org.assertj.core.util.Lists;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.clamp.clds.Application;
36 import org.onap.clamp.clds.util.JsonUtils;
37 import org.onap.clamp.loop.log.LogType;
38 import org.onap.clamp.loop.log.LoopLog;
39 import org.onap.clamp.loop.log.LoopLogService;
40 import org.onap.clamp.loop.template.LoopTemplate;
41 import org.onap.clamp.loop.template.PolicyModel;
42 import org.onap.clamp.loop.template.PolicyModelsService;
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.annotation.Commit;
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 PolicyModelsService policyModelsService;
79 public void shouldCreateEmptyLoop() {
81 String loopBlueprint = "blueprint";
82 String loopSvg = "representation";
83 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
84 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
85 testLoop.setLastComputedState(LoopState.DESIGN);
88 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
91 assertThat(actualLoop).isNotNull();
92 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
93 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
94 assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
95 assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
96 .isEqualTo("testValue");
101 public void shouldAddOperationalPolicyToLoop() {
104 OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
105 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
108 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
109 Lists.newArrayList(operationalPolicy));
112 assertThat(actualLoop).isNotNull();
113 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
114 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
115 assertThat(savedPolicies).hasSize(1);
116 assertThat(savedPolicies)
117 .usingElementComparatorIgnoringFields("loop", "createdBy", "createdDate", "updatedBy", "updatedDate")
118 .contains(operationalPolicy);
119 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
120 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
126 public void shouldAddMicroservicePolicyToLoop() {
129 PolicyModel policyModel = new PolicyModel("org.policies.policyModel1",
130 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyModel1");
131 policyModelsService.saveOrUpdatePolicyModel(policyModel);
132 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
133 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
136 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
137 Lists.newArrayList(microServicePolicy));
140 assertThat(actualLoop).isNotNull();
141 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
142 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
143 assertThat(savedPolicies).hasSize(1);
144 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
145 "createdBy", "updatedBy").containsExactly(microServicePolicy);
146 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
153 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
156 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
157 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
158 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
159 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
160 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
161 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
162 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1, false,
163 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
165 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
166 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2, false,
167 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
170 firstMicroServicePolicy
171 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
172 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
173 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
176 assertThat(actualLoop).isNotNull();
177 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
178 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
179 assertThat(savedPolicies).hasSize(2);
180 assertThat(savedPolicies).contains(firstMicroServicePolicy);
181 assertThat(savedPolicies).contains(secondMicroServicePolicy);
182 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
183 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
186 private void saveTestLoopToDb() {
187 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
188 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
189 LoopTemplate template = new LoopTemplate();
190 template.setName("testTemplate");
191 testLoop.setLoopTemplate(template);
192 loopService.saveOrUpdateLoop(testLoop);
197 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
200 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
201 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
202 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
203 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
204 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
205 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
206 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1,
207 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
208 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
210 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2,
211 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
214 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
215 Lists.newArrayList(secondMicroServicePolicy));
218 assertThat(actualLoop).isNotNull();
219 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
220 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
221 assertThat(savedPolicies).hasSize(1);
222 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
223 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
229 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
233 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
235 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
236 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
237 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
239 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
240 newJsonConfiguration, null);
243 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
244 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
245 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
248 assertThat(actualLoop).isNotNull();
249 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
250 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
251 assertThat(savedPolicies).hasSize(2);
252 assertThat(savedPolicies)
253 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
254 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
255 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
256 .map(Loop::getName).collect(Collectors.toSet());
257 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
262 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
266 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
267 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
268 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
270 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
271 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
274 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
275 Lists.newArrayList(secondOperationalPolicy));
278 assertThat(actualLoop).isNotNull();
279 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
280 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
281 assertThat(savedPolicies).hasSize(1);
282 assertThat(savedPolicies)
283 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
284 .containsExactly(secondOperationalPolicy);
285 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
286 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
292 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
295 String expectedJson = "{\"test\":\"test\"}";
296 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
297 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
298 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
301 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
304 assertThat(actualLoop).isNotNull();
305 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
306 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
307 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
312 public void deleteAttempt() {
315 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
316 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
317 LoopTemplate template = new LoopTemplate();
318 template.setName("testTemplate");
319 loop.setLoopTemplate(template);
320 loop = loopService.saveOrUpdateLoop(loop);
322 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
323 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
324 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
326 PolicyModel policyModel = new PolicyModel("org.policies.microPolicy",
327 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "microPolicy");
328 policyModelsService.saveOrUpdatePolicyModel(policyModel);
329 // Add Micro service policy
330 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", policyModel,
331 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
332 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
335 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
336 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
337 // Verify it's well deleted and has been cascaded, except for Microservice
338 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
339 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
340 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
341 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
346 public void testUpdateLoopState() {
348 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
349 loopService.updateLoopState(loop, "SUBMITTED");
350 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
351 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
356 public void testUpdateDcaeDeploymentFields() {
358 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
359 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
360 "https4://deployment-handler.onap:8443");
361 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
362 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
363 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
368 public void testUpdateMicroservicePolicy() {
370 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
371 PolicyModel policyModel = new PolicyModel("org.policies.policyName",
372 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyName");
373 policyModelsService.saveOrUpdatePolicyModel(policyModel);
374 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
375 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
376 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
377 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
380 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
381 return new Loop(loopName, loopSvg);