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);
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);
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);
151 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
154 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
155 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
156 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
157 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
158 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
159 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
160 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1, false,
161 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
163 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
164 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2, false,
165 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
168 firstMicroServicePolicy
169 .setConfigurationsJson(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
170 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
171 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
174 assertThat(actualLoop).isNotNull();
175 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
176 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
177 assertThat(savedPolicies).hasSize(2);
178 assertThat(savedPolicies).contains(firstMicroServicePolicy);
179 assertThat(savedPolicies).contains(secondMicroServicePolicy);
180 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
181 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
184 private void saveTestLoopToDb() {
185 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
186 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
187 LoopTemplate template = new LoopTemplate();
188 template.setName("testTemplate");
189 testLoop.setLoopTemplate(template);
190 loopService.saveOrUpdateLoop(testLoop);
195 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
198 PolicyModel policyModel1 = new PolicyModel("org.policies.firstPolicyName",
199 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "firstPolicyName");
200 policyModelsService.saveOrUpdatePolicyModel(policyModel1);
201 PolicyModel policyModel2 = new PolicyModel("org.policies.secondPolicyName",
202 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "secondPolicyName");
203 policyModelsService.saveOrUpdatePolicyModel(policyModel2);
204 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", policyModel1,
205 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
206 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
208 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", policyModel2,
209 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
212 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
213 Lists.newArrayList(secondMicroServicePolicy));
216 assertThat(actualLoop).isNotNull();
217 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
218 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
219 assertThat(savedPolicies).hasSize(1);
220 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
221 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
227 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
231 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
233 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
234 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
235 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
237 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
238 newJsonConfiguration, null);
241 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
242 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
243 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
246 assertThat(actualLoop).isNotNull();
247 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
248 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
249 assertThat(savedPolicies).hasSize(2);
250 assertThat(savedPolicies)
251 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
252 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
253 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
254 .map(Loop::getName).collect(Collectors.toSet());
255 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
260 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
264 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
265 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
266 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
268 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
269 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
272 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
273 Lists.newArrayList(secondOperationalPolicy));
276 assertThat(actualLoop).isNotNull();
277 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
278 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
279 assertThat(savedPolicies).hasSize(1);
280 assertThat(savedPolicies)
281 .usingElementComparatorIgnoringFields("loop", "createdDate", "updatedDate", "createdBy", "updatedBy")
282 .containsExactly(secondOperationalPolicy);
283 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
284 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
290 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
293 String expectedJson = "{\"test\":\"test\"}";
294 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
295 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
296 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
299 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
302 assertThat(actualLoop).isNotNull();
303 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
304 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
305 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
310 public void deleteAttempt() {
313 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
314 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
315 LoopTemplate template = new LoopTemplate();
316 template.setName("testTemplate");
317 loop.setLoopTemplate(template);
318 loop = loopService.saveOrUpdateLoop(loop);
320 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
321 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
322 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
324 PolicyModel policyModel = new PolicyModel("org.policies.microPolicy",
325 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "microPolicy");
326 policyModelsService.saveOrUpdatePolicyModel(policyModel);
327 // Add Micro service policy
328 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", policyModel,
329 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
330 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
333 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
334 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
335 // Verify it's well deleted and has been cascaded, except for Microservice
336 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
337 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
338 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
339 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
344 public void testUpdateLoopState() {
346 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
347 loopService.updateLoopState(loop, "SUBMITTED");
348 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
349 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
354 public void testUpdateDcaeDeploymentFields() {
356 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
357 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
358 "https4://deployment-handler.onap:8443");
359 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
360 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
361 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
366 public void testUpdateMicroservicePolicy() {
368 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
369 PolicyModel policyModel = new PolicyModel("org.policies.policyName",
370 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0", "policyName");
371 policyModelsService.saveOrUpdatePolicyModel(policyModel);
372 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", policyModel,
373 false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
374 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
375 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
378 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
379 return new Loop(loopName, loopSvg);