2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T 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.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonObject;
32 import java.time.Instant;
34 import java.util.HashSet;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.clamp.clds.Application;
38 import org.onap.clamp.loop.log.LogType;
39 import org.onap.clamp.loop.log.LoopLog;
40 import org.onap.clamp.loop.log.LoopLogRepository;
41 import org.onap.clamp.policy.microservice.MicroServicePolicy;
42 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
43 import org.onap.clamp.policy.operational.OperationalPolicy;
44 import org.onap.clamp.policy.operational.OperationalPolicyService;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 import org.springframework.transaction.annotation.Transactional;
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @SpringBootTest(classes = Application.class)
52 public class LoopRepositoriesItCase {
54 private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
57 private LoopsRepository loopRepository;
60 private MicroservicePolicyService microServicePolicyService;
63 private OperationalPolicyService operationalPolicyService;
66 private LoopLogRepository loopLogRepository;
68 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
69 return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class));
72 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
73 String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
74 Loop loop = new Loop();
76 loop.setSvgRepresentation(svgRepresentation);
77 loop.setBlueprint(blueprint);
78 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
79 loop.setLastComputedState(LoopState.DESIGN);
80 loop.setDcaeDeploymentId(dcaeId);
81 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
82 loop.setDcaeBlueprintId(dcaeBlueprintId);
86 private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca,
87 String jsonProperties, boolean shared) {
88 MicroServicePolicy µService = new MicroServicePolicy(name, policyTosca, shared,
89 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
90 µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
94 private LoopLog getLoopLog(LogType type, String message) {
95 LoopLog log = new LoopLog();
97 log.setMessage(message);
103 public void CrudTest() {
104 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
105 "123456789", "https://dcaetest.org", "UUID-blueprint");
106 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
107 loopTest.addOperationalPolicy(opPolicy);
108 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
109 "YamlContent", "{\"param1\":\"value1\"}", true);
110 loopTest.addMicroServicePolicy(microServicePolicy);
111 LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
112 loopTest.addLog(loopLog);
114 // Attemp to save into the database the entire loop
115 Loop loopInDb = loopRepository.save(loopTest);
116 assertThat(loopInDb).isNotNull();
117 assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
118 // Now set the ID in the previous model so that we can compare the objects
119 loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
121 assertThat(loopInDb).isEqualToComparingFieldByField(loopTest);
122 assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
123 assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
124 assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
125 assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
127 // Now attempt to read from database
128 Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
129 assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest);
130 assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
131 assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
132 .isEqualToComparingFieldByField(opPolicy);
133 assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
134 .isEqualToComparingFieldByField(microServicePolicy);
137 ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
138 loopRepository.save(loopInDbRetrieved);
139 Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get();
140 assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
141 .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
143 // Attempt to delete the object and check it has well been cascaded
144 loopRepository.delete(loopInDbRetrieved);
145 assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
146 assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
147 assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(false);
148 assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);