2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.loop;
28 import static org.assertj.core.api.Assertions.assertThat;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonObject;
33 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.loop.service.Service;
42 import org.onap.clamp.loop.service.ServicesRepository;
43 import org.onap.clamp.loop.template.LoopElementModel;
44 import org.onap.clamp.loop.template.LoopElementModelsRepository;
45 import org.onap.clamp.loop.template.LoopTemplate;
46 import org.onap.clamp.loop.template.LoopTemplatesRepository;
47 import org.onap.clamp.loop.template.PolicyModel;
48 import org.onap.clamp.loop.template.PolicyModelId;
49 import org.onap.clamp.loop.template.PolicyModelsRepository;
50 import org.onap.clamp.policy.microservice.MicroServicePolicy;
51 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
52 import org.onap.clamp.policy.operational.OperationalPolicy;
53 import org.onap.clamp.policy.operational.OperationalPolicyService;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.context.SpringBootTest;
56 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
57 import org.springframework.transaction.annotation.Transactional;
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @SpringBootTest(classes = Application.class)
61 public class LoopRepositoriesItCase {
63 private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
66 private LoopsRepository loopRepository;
69 private MicroServicePolicyService microServicePolicyService;
72 private OperationalPolicyService operationalPolicyService;
75 private LoopLogRepository loopLogRepository;
78 private LoopTemplatesRepository loopTemplateRepository;
81 private LoopElementModelsRepository microServiceModelsRepository;
84 private PolicyModelsRepository policyModelsRepository;
87 private ServicesRepository servicesRepository;
89 private Service getService(String serviceDetails, String resourceDetails) {
90 return new Service(serviceDetails, resourceDetails);
93 private OperationalPolicy getOperationalPolicy(String configJson, String name, PolicyModel policyModel) {
94 return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class), policyModel);
97 private LoopElementModel getLoopElementModel(String yaml, String name, String policyType, String createdBy,
98 PolicyModel policyModel) {
99 LoopElementModel model = new LoopElementModel(name, policyType, yaml);
100 model.addPolicyModel(policyModel);
104 private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version,
105 String policyAcronym) {
106 return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
109 private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, String createdBy,
110 Integer maxInstancesAllowed) {
111 LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
112 template.addLoopElementModel(getLoopElementModel("yaml", "microService1", "org.onap.policy.drools", createdBy,
113 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools")));
114 loopTemplateRepository.save(template);
118 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
119 String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
120 Loop loop = new Loop();
122 loop.setSvgRepresentation(svgRepresentation);
123 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
124 loop.setLastComputedState(LoopState.DESIGN);
125 loop.setDcaeDeploymentId(dcaeId);
126 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
127 loop.setLoopTemplate(getLoopTemplate("templateName", "yaml", "svg", "toto", 1));
131 private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String jsonProperties,
132 boolean shared, PolicyModel policyModel) {
133 MicroServicePolicy microService = new MicroServicePolicy(name, policyModel, shared,
134 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
135 microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
139 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
140 return new LoopLog(message, type, "CLAMP", loop);
144 * This method does a crud test and save a loop template and a loop object in db.
148 public void crudTest() {
150 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
151 "123456789", "https://dcaetest.org", "UUID-blueprint");
152 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest",
153 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools"));
154 loopTest.addOperationalPolicy(opPolicy);
155 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
156 "{\"param1\":\"value1\"}", true, getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools"));
157 loopTest.addMicroServicePolicy(microServicePolicy);
158 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
159 loopTest.addLog(loopLog);
160 Service service = getService(
161 "{\"name\": \"vLoadBalancerMS\", \"UUID\": \"63cac700-ab9a-4115-a74f-7eac85e3fce0\"}", "{\"CP\": {}}");
162 loopTest.setModelService(service);
164 // Attempt to save into the database the entire loop
165 Loop loopInDb = loopRepository.save(loopTest);
166 assertThat(loopInDb).isNotNull();
167 assertThat(loopRepository.findById(loopInDb.getName()).get()).isNotNull();
168 assertThat(loopInDb.getCreatedDate()).isNotNull();
169 assertThat(loopInDb.getUpdatedDate()).isNotNull();
170 assertThat(loopInDb.getUpdatedDate()).isEqualTo(loopInDb.getCreatedDate());
171 assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
172 // Autogen id so now set the ID in the previous model so that we can compare the
174 loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
176 assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
177 "createdBy", "updatedBy");
178 assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
179 assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
180 assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
181 assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
182 assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
183 assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
184 assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
185 assertThat(microServiceModelsRepository.existsById(
186 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
188 assertThat(policyModelsRepository.existsById(new PolicyModelId(
189 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
190 .first().getPolicyModelType(),
191 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
192 .first().getVersion()))).isEqualTo(true);
194 // Now attempt to read from database
195 Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
196 assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
197 "createdBy", "updatedBy");
198 assertThat(loopInDbRetrieved).isEqualToComparingOnlyGivenFields(loopInDb, "createdDate", "updatedDate",
199 "createdBy", "updatedBy");
200 assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
201 assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
202 .isEqualToIgnoringGivenFields(opPolicy, "createdDate", "updatedDate", "createdBy", "updatedBy");
203 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedDate())
205 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedDate())
207 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedBy())
209 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedBy())
212 assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
213 .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy",
217 ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
218 loopInDbRetrieved.setSvgRepresentation("");
219 Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved);
220 // Loop loopInDbRetrievedUpdated =
221 // loopRepository.findById(loopTest.getName()).get();
222 assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
223 .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
224 // UpdatedDate should have been changed
225 assertThat(loopInDbRetrievedUpdated.getUpdatedDate()).isNotEqualTo(loopInDbRetrievedUpdated.getCreatedDate());
226 // createdDate should have NOT been changed
227 assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate());
228 // other audit are the same
229 assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo("");
230 assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo("");
232 // Attempt to delete the object and check it has well been cascaded
234 loopRepository.delete(loopInDbRetrieved);
235 assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
236 assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
237 assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
238 assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
239 assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
240 assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
241 assertThat(microServiceModelsRepository.existsById(
242 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
245 assertThat(policyModelsRepository.existsById(new PolicyModelId(
246 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
247 .first().getPolicyModelType(),
248 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
249 .first().getVersion()))).isEqualTo(true);