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.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;
74 PolicyModelsService policyModelsService;
78 public void shouldCreateEmptyLoop() {
80 String loopBlueprint = "blueprint";
81 String loopSvg = "representation";
82 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
83 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
84 testLoop.setLastComputedState(LoopState.DESIGN);
87 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
90 assertThat(actualLoop).isNotNull();
91 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
92 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
93 assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg);
94 assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString())
95 .isEqualTo("testValue");
100 public void shouldAddOperationalPolicyToLoop() {
103 OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null,
104 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
107 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
108 Lists.newArrayList(operationalPolicy));
111 assertThat(actualLoop).isNotNull();
112 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
113 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
114 assertThat(savedPolicies).hasSize(1);
115 assertThat(savedPolicies)
116 .usingElementComparatorIgnoringFields("loop", "createdBy", "createdDate", "updatedBy", "updatedDate")
117 .contains(operationalPolicy);
118 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
119 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
125 public void shouldAddMicroservicePolicyToLoop() {
128 PolicyModel policyModel = new PolicyModel("org.policies.policyModel1",
129 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyModel1");
130 policyModelsService.saveOrUpdatePolicyModel(policyModel);
131 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
132 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, 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", "createdDate", "updatedDate",
144 "createdBy", "updatedBy").containsExactly(microServicePolicy);
145 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
152 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
155 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
156 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
157 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
158 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
159 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
160 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
161 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1, false,
162 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
164 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
165 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2, false,
166 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
169 firstMicroServicePolicy
170 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
171 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
172 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
175 assertThat(actualLoop).isNotNull();
176 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
177 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
178 assertThat(savedPolicies).hasSize(2);
179 assertThat(savedPolicies).contains(firstMicroServicePolicy);
180 assertThat(savedPolicies).contains(secondMicroServicePolicy);
181 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
182 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
185 private void saveTestLoopToDb() {
186 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
187 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
188 LoopTemplate template = new LoopTemplate();
189 template.setName("testTemplate");
190 testLoop.setLoopTemplate(template);
191 loopService.saveOrUpdateLoop(testLoop);
196 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
199 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
200 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
201 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
202 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
203 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
204 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
205 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1,
206 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
207 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
209 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2,
210 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
213 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
214 Lists.newArrayList(secondMicroServicePolicy));
217 assertThat(actualLoop).isNotNull();
218 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
219 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
220 assertThat(savedPolicies).hasSize(1);
221 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
222 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
228 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
232 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
234 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
235 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
236 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
238 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
239 newJsonConfiguration, null, null, null, null);
242 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
243 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
244 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
247 assertThat(actualLoop).isNotNull();
248 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
249 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
250 assertThat(savedPolicies).hasSize(2);
251 assertThat(savedPolicies)
252 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
253 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
254 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
255 .map(Loop::getName).collect(Collectors.toSet());
256 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
261 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
265 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
266 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
267 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
269 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
270 JsonUtils.GSON.fromJson("{}", JsonObject.class), null, null, "pdpGroup1", "pdpSubgroup1");
273 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
274 Lists.newArrayList(secondOperationalPolicy));
277 assertThat(actualLoop).isNotNull();
278 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
279 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
280 assertThat(savedPolicies).hasSize(1);
281 assertThat(savedPolicies)
282 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
283 .containsExactly(secondOperationalPolicy);
284 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
285 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
291 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
294 String expectedJson = "{\"test\":\"test\"}";
295 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
296 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
297 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
300 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
303 assertThat(actualLoop).isNotNull();
304 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
305 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
306 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
311 public void deleteAttempt() {
314 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
315 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
316 LoopTemplate template = new LoopTemplate();
317 template.setName("testTemplate");
318 loop.setLoopTemplate(template);
319 loop = loopService.saveOrUpdateLoop(loop);
321 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
322 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
323 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
325 PolicyModel policyModel = new PolicyModel("org.policies.microPolicy",
326 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "microPolicy");
327 policyModelsService.saveOrUpdatePolicyModel(policyModel);
328 // Add Micro service policy
329 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", policyModel,
330 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
331 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
334 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
335 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
336 // Verify it's well deleted and has been cascaded, except for Microservice
337 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
338 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
339 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
340 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
345 public void testUpdateLoopState() {
347 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
348 loopService.updateLoopState(loop, "SUBMITTED");
349 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
350 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
355 public void testUpdateDcaeDeploymentFields() {
357 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
358 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
359 "https4://deployment-handler.onap:8443");
360 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
361 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
362 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
367 public void testUpdateMicroservicePolicy() {
369 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
370 PolicyModel policyModel = new PolicyModel("org.policies.policyName",
371 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyName");
372 policyModelsService.saveOrUpdatePolicyModel(policyModel);
373 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
374 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
375 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
376 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
379 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
380 return new Loop(loopName, loopSvg);