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.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;
75 public void shouldCreateEmptyLoop() {
77 String loopBlueprint = "blueprint";
78 String loopSvg = "representation";
79 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg);
80 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
81 testLoop.setLastComputedState(LoopState.DESIGN);
84 Loop actualLoop = loopService.saveOrUpdateLoop(testLoop);
87 assertThat(actualLoop).isNotNull();
88 assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get());
89 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
90 assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint);
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).usingElementComparatorIgnoringFields("loop").contains(operationalPolicy);
114 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
115 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
121 public void shouldAddMicroservicePolicyToLoop() {
124 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
125 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
126 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
129 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
130 Lists.newArrayList(microServicePolicy));
133 assertThat(actualLoop).isNotNull();
134 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
135 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
136 assertThat(savedPolicies).hasSize(1);
137 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
138 "createdBy", "updatedBy").containsExactly(microServicePolicy);
139 assertThat(savedPolicies).extracting("usedByLoops").hasSize(1);
145 public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() {
149 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false,
150 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
151 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
152 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "",
153 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
154 JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
157 firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class));
158 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
159 Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy));
162 assertThat(actualLoop).isNotNull();
163 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
164 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
165 assertThat(savedPolicies).hasSize(2);
166 assertThat(savedPolicies).contains(firstMicroServicePolicy);
167 assertThat(savedPolicies).contains(secondMicroServicePolicy);
168 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
169 "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy);
173 private void saveTestLoopToDb() {
174 Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation");
175 testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
176 loopService.saveOrUpdateLoop(testLoop);
181 public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() {
185 MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "",
186 "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false,
187 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
188 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy));
190 MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca",
191 true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null);
194 Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME,
195 Lists.newArrayList(secondMicroServicePolicy));
198 assertThat(actualLoop).isNotNull();
199 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
200 Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies();
201 assertThat(savedPolicies).hasSize(1);
202 assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate",
203 "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy);
209 public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() {
213 JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class);
215 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
216 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
217 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
219 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null,
220 newJsonConfiguration);
223 firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration);
224 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
225 Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy));
228 assertThat(actualLoop).isNotNull();
229 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
230 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
231 assertThat(savedPolicies).hasSize(2);
232 assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop")
233 .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy);
234 Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop)
235 .map(Loop::getName).collect(Collectors.toSet());
236 assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME);
241 public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() {
245 OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null,
246 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
247 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy));
249 OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null,
250 JsonUtils.GSON.fromJson("{}", JsonObject.class));
253 Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME,
254 Lists.newArrayList(secondOperationalPolicy));
257 assertThat(actualLoop).isNotNull();
258 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
259 Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies();
260 assertThat(savedPolicies).hasSize(1);
261 assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop").containsExactly(secondOperationalPolicy);
262 OperationalPolicy savedPolicy = savedPolicies.iterator().next();
263 assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME);
269 public void shouldCreateModelPropertiesAndUpdateJsonRepresentationOfOldOne() {
272 String expectedJson = "{\"test\":\"test\"}";
273 JsonObject baseGlobalProperites = JsonUtils.GSON.fromJson("{}", JsonObject.class);
274 JsonObject updatedGlobalProperites = JsonUtils.GSON.fromJson(expectedJson, JsonObject.class);
275 loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, baseGlobalProperites);
278 Loop actualLoop = loopService.updateAndSaveGlobalPropertiesJson(EXAMPLE_LOOP_NAME, updatedGlobalProperites);
281 assertThat(actualLoop).isNotNull();
282 assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME);
283 JsonObject returnedGlobalProperties = actualLoop.getGlobalPropertiesJson();
284 assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites);
289 public void deleteAttempt() {
292 Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null);
293 loop.addLog(new LoopLog("test", LogType.INFO, "CLAMP", loop));
294 loop = loopService.saveOrUpdateLoop(loop);
296 OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null,
297 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class));
298 loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy));
300 // Add Micro service policy
301 MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "",
302 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
303 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
304 loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy));
307 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull();
308 loopService.deleteLoop(EXAMPLE_LOOP_NAME);
309 // Verify it's well deleted and has been cascaded, except for Microservice
310 assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull();
311 assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue();
312 assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse();
313 assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse();
318 public void testUpdateLoopState() {
320 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
321 loopService.updateLoopState(loop, "SUBMITTED");
322 Loop updatedLoop = loopService.getLoop(EXAMPLE_LOOP_NAME);
323 assertThat(updatedLoop.getLastComputedState()).isEqualTo(LoopState.SUBMITTED);
328 public void testUpdateDcaeDeploymentFields() {
330 Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
331 loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85",
332 "https4://deployment-handler.onap:8443");
333 loop = loopService.getLoop(EXAMPLE_LOOP_NAME);
334 assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85");
335 assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443");
340 public void testUpdateMicroservicePolicy() {
342 assertThat(microServicePolicyService.isExisting("policyName")).isFalse();
343 MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "",
344 "tosca_definitions_version: tosca_simple_yaml_1_0_0", false,
345 JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null);
346 loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy);
347 assertThat(microServicePolicyService.isExisting("policyName")).isTrue();
350 private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) {
351 return new Loop(loopName, loopBlueprint, loopSvg);