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;
34 import java.time.Instant;
35 import java.util.HashSet;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.clamp.clds.Application;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.loop.log.LoopLogRepository;
43 import org.onap.clamp.loop.service.Service;
44 import org.onap.clamp.loop.service.ServicesRepository;
45 import org.onap.clamp.loop.template.LoopElementModel;
46 import org.onap.clamp.loop.template.LoopElementModelsRepository;
47 import org.onap.clamp.loop.template.LoopTemplate;
48 import org.onap.clamp.loop.template.LoopTemplatesRepository;
49 import org.onap.clamp.loop.template.PolicyModel;
50 import org.onap.clamp.loop.template.PolicyModelId;
51 import org.onap.clamp.loop.template.PolicyModelsRepository;
52 import org.onap.clamp.policy.microservice.MicroServicePolicy;
53 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
54 import org.onap.clamp.policy.operational.OperationalPolicy;
55 import org.onap.clamp.policy.operational.OperationalPolicyService;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59 import org.springframework.transaction.annotation.Transactional;
61 @RunWith(SpringJUnit4ClassRunner.class)
62 @SpringBootTest(classes = Application.class)
63 public class LoopRepositoriesItCase {
65 private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
68 private LoopsRepository loopRepository;
71 private MicroServicePolicyService microServicePolicyService;
74 private OperationalPolicyService operationalPolicyService;
77 private LoopLogRepository loopLogRepository;
80 private LoopTemplatesRepository loopTemplateRepository;
83 private LoopElementModelsRepository microServiceModelsRepository;
86 private PolicyModelsRepository policyModelsRepository;
89 private ServicesRepository servicesRepository;
91 private Service getService(String serviceDetails, String resourceDetails) {
92 return new Service(serviceDetails, resourceDetails);
95 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
96 return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class));
99 private LoopElementModel getLoopElementModel(String yaml, String name, String policyType, String createdBy,
100 PolicyModel policyModel) {
101 LoopElementModel model = new LoopElementModel(name, policyType, yaml);
102 model.addPolicyModel(policyModel);
106 private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
107 String policyVariant, String createdBy) {
108 return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
111 private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, String createdBy,
112 Integer maxInstancesAllowed) {
113 LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
114 template.addLoopElementModel(getLoopElementModel("yaml", "microService1", "org.onap.policy.drools", createdBy,
115 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1", createdBy)));
119 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
120 String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
121 Loop loop = new Loop();
123 loop.setSvgRepresentation(svgRepresentation);
124 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
125 loop.setLastComputedState(LoopState.DESIGN);
126 loop.setDcaeDeploymentId(dcaeId);
127 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
128 loop.setLoopTemplate(getLoopTemplate("templateName", "yaml", "svg", "toto", 1));
132 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
133 String policyTosca, String jsonProperties, boolean shared) {
134 MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
135 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
136 microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
140 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
141 return new LoopLog(message, type, "CLAMP", loop);
146 public void crudTest() {
148 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
149 "123456789", "https://dcaetest.org", "UUID-blueprint");
150 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
151 loopTest.addOperationalPolicy(opPolicy);
152 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
153 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
154 "{\"param1\":\"value1\"}", true);
155 loopTest.addMicroServicePolicy(microServicePolicy);
156 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
157 loopTest.addLog(loopLog);
158 Service service = getService(
159 "{\"name\": \"vLoadBalancerMS\", \"UUID\": \"63cac700-ab9a-4115-a74f-7eac85e3fce0\"}", "{\"CP\": {}}");
160 loopTest.setModelService(service);
162 // Attempt to save into the database the entire loop
163 Loop loopInDb = loopRepository.save(loopTest);
164 assertThat(loopInDb).isNotNull();
165 assertThat(loopRepository.findById(loopInDb.getName()).get()).isNotNull();
166 assertThat(loopInDb.getCreatedDate()).isNotNull();
167 assertThat(loopInDb.getUpdatedDate()).isNotNull();
168 assertThat(loopInDb.getUpdatedDate()).isEqualTo(loopInDb.getCreatedDate());
169 assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
170 // Autogen id so now set the ID in the previous model so that we can compare the
172 loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
174 assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
175 "createdBy", "updatedBy");
176 assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
177 assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
178 assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
179 assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
180 assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
181 assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
182 assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
183 assertThat(microServiceModelsRepository.existsById(
184 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
186 assertThat(policyModelsRepository.existsById(new PolicyModelId(
187 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
188 .first().getPolicyModelType(),
189 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
190 .first().getVersion()))).isEqualTo(true);
192 // Now attempt to read from database
193 Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
194 assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
195 "createdBy", "updatedBy");
196 assertThat(loopInDbRetrieved).isEqualToComparingOnlyGivenFields(loopInDb, "createdDate", "updatedDate",
197 "createdBy", "updatedBy");
198 assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
199 assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
200 .isEqualToIgnoringGivenFields(opPolicy, "createdDate", "updatedDate", "createdBy", "updatedBy");
201 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedDate())
203 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedDate())
205 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedBy())
207 assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedBy())
210 assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
211 .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy",
215 ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
216 loopInDbRetrieved.setSvgRepresentation("");
217 Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved);
218 // Loop loopInDbRetrievedUpdated =
219 // loopRepository.findById(loopTest.getName()).get();
220 assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
221 .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
222 // UpdatedDate should have been changed
223 assertThat(loopInDbRetrievedUpdated.getUpdatedDate()).isNotEqualTo(loopInDbRetrievedUpdated.getCreatedDate());
224 // createdDate should have NOT been changed
225 assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate());
226 // other audit are the same
227 assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo("");
228 assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo("");
230 // Attempt to delete the object and check it has well been cascaded
232 loopRepository.delete(loopInDbRetrieved);
233 assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
234 assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
235 assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
236 assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
237 assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
238 assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
239 assertThat(microServiceModelsRepository.existsById(
240 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
243 assertThat(policyModelsRepository.existsById(new PolicyModelId(
244 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
245 .first().getPolicyModelType(),
246 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
247 .first().getVersion()))).isEqualTo(true);