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 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint);
82 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
83 testLoop.setLastComputedState(LoopState.DESIGN);
86 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
89 assertThat(actualLoop).isNotNull();
90 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
91 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
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), null, null, null, null);
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 PolicyModel policyModel = new PolicyModel("org.policies.policyModel1",
127 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyModel1");
128 policyModelsService.saveOrUpdatePolicyModel(policyModel);
129 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
130 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
133 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
134 Lists.newArrayList(microServicePolicy));
137 assertThat(actualLoop).isNotNull();
138 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
139 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
140 assertThat(savedPolicies).hasSize(1);
141 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
142 "createdBy", "updatedBy").containsExactly(microServicePolicy);
143 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
150 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
153 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
154 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
155 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
156 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
157 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
158 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
159 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1, false,
160 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
162 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
163 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2, false,
164 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
167 firstMicroServicePolicy
168 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
169 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
170 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
173 assertThat(actualLoop).isNotNull();
174 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
175 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
176 assertThat(savedPolicies).hasSize(2);
177 assertThat(savedPolicies).contains(firstMicroServicePolicy);
178 assertThat(savedPolicies).contains(secondMicroServicePolicy);
179 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
180 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
183 private void saveTestLoopToDb() {
184 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint");
185 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
186 LoopTemplate template = new LoopTemplate();
187 template.setName("testTemplate");
188 testLoop.setLoopTemplate(template);
189 loopService.saveOrUpdateLoop(testLoop);
194 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
197 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
198 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
199 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
200 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
201 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
202 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
203 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1,
204 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
205 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
207 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2,
208 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
211 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
212 Lists.newArrayList(secondMicroServicePolicy));
215 assertThat(actualLoop).isNotNull();
216 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
217 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
218 assertThat(savedPolicies).hasSize(1);
219 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
220 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
226 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
230 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
232 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
233 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
234 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
236 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
237 newJsonConfiguration, null, null, null, null);
240 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
241 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
242 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
245 assertThat(actualLoop).isNotNull();
246 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
247 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
248 assertThat(savedPolicies).hasSize(2);
249 assertThat(savedPolicies)
250 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
251 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
252 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
253 .map(Loop::getName).collect(Collectors.toSet());
254 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
259 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
263 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
264 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
265 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
267 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
268 JsonUtils.GSON.fromJson("{}", JsonObject.class), null, null, "pdpGroup1", "pdpSubgroup1");
271 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
272 Lists.newArrayList(secondOperationalPolicy));
275 assertThat(actualLoop).isNotNull();
276 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
277 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
278 assertThat(savedPolicies).hasSize(1);
279 assertThat(savedPolicies)
280 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
281 .containsExactly(secondOperationalPolicy);
282 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
283 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
289 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
292 String expectedJson = "{\"test\":\"test\"}";
293 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
294 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
295 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
298 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
301 assertThat(actualLoop).isNotNull();
302 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
303 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
304 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
309 public void deleteAttempt() {
312 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
313 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
314 LoopTemplate template = new LoopTemplate();
315 template.setName("testTemplate");
316 loop.setLoopTemplate(template);
317 loop = loopService.saveOrUpdateLoop(loop);
319 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
320 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null, null);
321 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
323 PolicyModel policyModel = new PolicyModel("org.policies.microPolicy",
324 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "microPolicy");
325 policyModelsService.saveOrUpdatePolicyModel(policyModel);
326 // Add Micro service policy
327 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", policyModel,
328 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
329 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
332 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
333 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
334 // Verify it's well deleted and has been cascaded, except for Microservice
335 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
336 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
337 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
338 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
343 public void testUpdateLoopState() {
345 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
346 loopService.updateLoopState(loop, "SUBMITTED");
347 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
348 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
353 public void testUpdateDcaeDeploymentFields() {
355 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
356 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
357 "https4://deployment-handler.onap:8443");
358 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
359 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
360 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
365 public void testUpdateMicroservicePolicy() {
367 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
368 PolicyModel policyModel = new PolicyModel("org.policies.policyName",
369 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyName");
370 policyModelsService.saveOrUpdatePolicyModel(policyModel);
371 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
372 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null, null, null);
373 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
374 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
377 private Loop createTestLoop(String loopName, String loopBlueprint) {
378 return new Loop(loopName);