8a523fc0f8d58daa27aa63e88e2ceb66d4e3cce3
[clamp.git] / src / test / java / org / onap / clamp / it / dao / model / LoopRepositoriesItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.it.dao.model;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonObject;
31
32 import java.time.Instant;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.clamp.clds.Application;
37 import org.onap.clamp.dao.LoopLogRepository;
38 import org.onap.clamp.dao.LoopsRepository;
39 import org.onap.clamp.dao.MicroServicePolicyRepository;
40 import org.onap.clamp.dao.OperationalPolicyRepository;
41 import org.onap.clamp.dao.model.LogType;
42 import org.onap.clamp.dao.model.Loop;
43 import org.onap.clamp.dao.model.LoopLog;
44 import org.onap.clamp.dao.model.LoopState;
45 import org.onap.clamp.dao.model.MicroServicePolicy;
46 import org.onap.clamp.dao.model.OperationalPolicy;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50 import org.springframework.transaction.annotation.Transactional;
51
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @SpringBootTest(classes = Application.class)
54 public class LoopRepositoriesItCase {
55
56     private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
57
58     @Autowired
59     private LoopsRepository loopRepository;
60
61     @Autowired
62     private MicroServicePolicyRepository microServicePolicyRepository;
63
64     @Autowired
65     private OperationalPolicyRepository operationalPolicyRepository;
66
67     @Autowired
68     private LoopLogRepository loopLogRepository;
69
70     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
71         OperationalPolicy opPolicy = new OperationalPolicy();
72         opPolicy.setName(name);
73         opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class));
74         return opPolicy;
75     }
76
77     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
78         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
79         Loop loop = new Loop();
80         loop.setName(name);
81         loop.setSvgRepresentation(svgRepresentation);
82         loop.setBlueprint(blueprint);
83         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
84         loop.setLastComputedState(LoopState.DESIGN);
85         loop.setDcaeDeploymentId(dcaeId);
86         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
87         loop.setDcaeBlueprintId(dcaeBlueprintId);
88         return loop;
89     }
90
91     private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca,
92         String jsonProperties, boolean shared) {
93         MicroServicePolicy µService = new MicroServicePolicy();
94         µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class));
95         µService.setPolicyTosca(policyTosca);
96         µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
97         µService.setShared(shared);
98
99         µService.setName(name);
100         return µService;
101     }
102
103     private LoopLog getLoopLog(LogType type, String message) {
104         LoopLog log = new LoopLog();
105         log.setLogType(type);
106         log.setMessage(message);
107         return log;
108     }
109
110     @Test
111     @Transactional
112     public void CrudTest() {
113         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
114             "123456789", "https://dcaetest.org", "UUID-blueprint");
115         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
116         loopTest.addOperationalPolicy(opPolicy);
117         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
118             "YamlContent", "{\"param1\":\"value1\"}", true);
119         loopTest.addMicroServicePolicy(microServicePolicy);
120         LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
121         loopTest.addLog(loopLog);
122
123         // Attemp to save into the database the entire loop
124         Loop loopInDb = loopRepository.save(loopTest);
125         assertThat(loopInDb).isNotNull();
126         assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
127         // Now set the ID in the previous model so that we can compare the objects
128         loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
129
130         assertThat(loopInDb).isEqualToComparingFieldByField(loopTest);
131         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
132         assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(true);
133         assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(true);
134         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
135
136         // Now attempt to read from database
137         Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
138         assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest);
139         assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
140         assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
141             .isEqualToComparingFieldByField(opPolicy);
142         assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
143             .isEqualToComparingFieldByField(microServicePolicy);
144
145         // Attempt an update
146         ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
147         loopRepository.save(loopInDbRetrieved);
148         Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get();
149         assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
150             .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
151
152         // Attempt to delete the object and check it has well been cascaded
153         loopRepository.delete(loopInDbRetrieved);
154         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
155         assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(false);
156         assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(false);
157         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
158
159     }
160 }