acde66d9a47f85b36a8a8a57757ed983dce11014
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopRepositoriesItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.loop;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29
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 org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.clamp.clds.Application;
37 import org.onap.clamp.loop.log.LogType;
38 import org.onap.clamp.loop.log.LoopLog;
39 import org.onap.clamp.loop.log.LoopLogRepository;
40 import org.onap.clamp.loop.service.Service;
41 import org.onap.clamp.loop.service.ServicesRepository;
42 import org.onap.clamp.loop.template.LoopElementModel;
43 import org.onap.clamp.loop.template.LoopElementModelsRepository;
44 import org.onap.clamp.loop.template.LoopTemplate;
45 import org.onap.clamp.loop.template.LoopTemplatesRepository;
46 import org.onap.clamp.loop.template.PolicyModel;
47 import org.onap.clamp.loop.template.PolicyModelId;
48 import org.onap.clamp.loop.template.PolicyModelsRepository;
49 import org.onap.clamp.policy.microservice.MicroServicePolicy;
50 import org.onap.clamp.policy.microservice.MicroServicePolicyService;
51 import org.onap.clamp.policy.operational.OperationalPolicy;
52 import org.onap.clamp.policy.operational.OperationalPolicyService;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.boot.test.context.SpringBootTest;
55 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
56 import org.springframework.transaction.annotation.Transactional;
57
58 @RunWith(SpringJUnit4ClassRunner.class)
59 @SpringBootTest(classes = Application.class)
60 public class LoopRepositoriesItCase {
61
62     private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
63
64     @Autowired
65     private LoopsRepository loopRepository;
66
67     @Autowired
68     private MicroServicePolicyService microServicePolicyService;
69
70     @Autowired
71     private OperationalPolicyService operationalPolicyService;
72
73     @Autowired
74     private LoopLogRepository loopLogRepository;
75
76     @Autowired
77     private LoopTemplatesRepository loopTemplateRepository;
78
79     @Autowired
80     private LoopElementModelsRepository microServiceModelsRepository;
81
82     @Autowired
83     private PolicyModelsRepository policyModelsRepository;
84
85     @Autowired
86     private ServicesRepository servicesRepository;
87
88     private Service getService(String serviceDetails, String resourceDetails) {
89         return new Service(serviceDetails, resourceDetails);
90     }
91
92     private OperationalPolicy getOperationalPolicy(String configJson, String name, PolicyModel policyModel) {
93         return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class), policyModel, null);
94     }
95
96     private LoopElementModel getLoopElementModel(String yaml, String name, String policyType, String createdBy,
97                                                  PolicyModel policyModel) {
98         LoopElementModel model = new LoopElementModel(name, policyType, yaml);
99         model.addPolicyModel(policyModel);
100         return model;
101     }
102
103     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version,
104                                        String policyAcronym) {
105         return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
106     }
107
108     private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, String createdBy,
109                                          Integer maxInstancesAllowed) {
110         LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
111         template.addLoopElementModel(getLoopElementModel("yaml", "microService1", "org.onap.policy.drools", createdBy,
112                 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools")));
113         loopTemplateRepository.save(template);
114         return template;
115     }
116
117     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
118                          String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
119         Loop loop = new Loop();
120         loop.setName(name);
121         loop.setSvgRepresentation(svgRepresentation);
122         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
123         loop.setLastComputedState(LoopState.DESIGN);
124         loop.setDcaeDeploymentId(dcaeId);
125         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
126         loop.setLoopTemplate(getLoopTemplate("templateName", "yaml", "svg", "toto", 1));
127         return loop;
128     }
129
130     private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String jsonProperties,
131                                                      boolean shared, PolicyModel policyModel) {
132         MicroServicePolicy microService = new MicroServicePolicy(name, policyModel, shared,
133                 gson.fromJson(jsonRepresentation, JsonObject.class), null);
134         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
135         return microService;
136     }
137
138     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
139         return new LoopLog(message, type, "CLAMP", loop);
140     }
141
142     /**
143      * This method does a crud test and save a loop template and a loop object in db.
144      */
145     @Test
146     @Transactional
147     public void crudTest() {
148         // Setup
149         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
150                 "123456789", "https://dcaetest.org", "UUID-blueprint");
151         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest",
152                 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools"));
153         loopTest.addOperationalPolicy(opPolicy);
154         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
155                 "{\"param1\":\"value1\"}", true, getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools"));
156         loopTest.addMicroServicePolicy(microServicePolicy);
157         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
158         loopTest.addLog(loopLog);
159         Service service = getService(
160                 "{\"name\": \"vLoadBalancerMS\", \"UUID\": \"63cac700-ab9a-4115-a74f-7eac85e3fce0\"}", "{\"CP\": {}}");
161         loopTest.setModelService(service);
162
163         // Attempt to save into the database the entire loop
164         Loop loopInDb = loopRepository.save(loopTest);
165         assertThat(loopInDb).isNotNull();
166         assertThat(loopRepository.findById(loopInDb.getName()).get()).isNotNull();
167         assertThat(loopInDb.getCreatedDate()).isNotNull();
168         assertThat(loopInDb.getUpdatedDate()).isNotNull();
169         assertThat(loopInDb.getUpdatedDate()).isEqualTo(loopInDb.getCreatedDate());
170         assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
171         // Autogen id so now set the ID in the previous model so that we can compare the
172         // objects
173         loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
174
175         assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
176                 "createdBy", "updatedBy");
177         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
178         assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
179         assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
180         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
181         assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
182         assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
183         assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
184         assertThat(microServiceModelsRepository.existsById(
185                 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
186                 .isEqualTo(true);
187         assertThat(policyModelsRepository.existsById(new PolicyModelId(
188                 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
189                         .first().getPolicyModelType(),
190                 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
191                         .first().getVersion()))).isEqualTo(true);
192
193         // Now attempt to read from database
194         Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
195         assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate",
196                 "createdBy", "updatedBy");
197         assertThat(loopInDbRetrieved).isEqualToComparingOnlyGivenFields(loopInDb, "createdDate", "updatedDate",
198                 "createdBy", "updatedBy");
199         assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
200         assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
201                 .isEqualToIgnoringGivenFields(opPolicy, "createdDate", "updatedDate", "createdBy", "updatedBy");
202         assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedDate())
203                 .isNotNull();
204         assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedDate())
205                 .isNotNull();
206         assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getCreatedBy())
207                 .isNotNull();
208         assertThat(((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]).getUpdatedBy())
209                 .isNotNull();
210
211         assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
212                 .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy",
213                         "updatedBy");
214
215         // Attempt an update
216         ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
217         loopInDbRetrieved.setSvgRepresentation("");
218         Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved);
219         // Loop loopInDbRetrievedUpdated =
220         // loopRepository.findById(loopTest.getName()).get();
221         assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
222                 .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
223         // UpdatedDate should have been changed
224         assertThat(loopInDbRetrievedUpdated.getUpdatedDate()).isNotEqualTo(loopInDbRetrievedUpdated.getCreatedDate());
225         // createdDate should have NOT been changed
226         assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate());
227         // other audit are the same
228         assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo("Not found");
229         assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo("Not found");
230
231         // Attempt to delete the object and check it has well been cascaded
232
233         loopRepository.delete(loopInDbRetrieved);
234         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
235         assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
236         assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
237         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
238         assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true);
239         assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true);
240         assertThat(microServiceModelsRepository.existsById(
241                 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getName()))
242                 .isEqualTo(true);
243
244         assertThat(policyModelsRepository.existsById(new PolicyModelId(
245                 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
246                         .first().getPolicyModelType(),
247                 loopInDb.getLoopTemplate().getLoopElementModelsUsed().first().getLoopElementModel().getPolicyModels()
248                         .first().getVersion()))).isEqualTo(true);
249
250     }
251 }