Improve tests
[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
31 import java.time.Instant;
32 import java.util.Map;
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, Map.class));
74         return opPolicy;
75     }
76
77     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
78         String dcaeId, String dcaeUrl) {
79         Loop loop = new Loop();
80         loop.setName(name);
81         loop.setSvgRepresentation(svgRepresentation);
82         loop.setBlueprint(blueprint);
83         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class));
84         loop.setLastComputedState(LoopState.DESIGN);
85         loop.setDcaeDeploymentId(dcaeId);
86         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
87         return loop;
88     }
89
90     private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca,
91         String jsonProperties, boolean shared) {
92         MicroServicePolicy µService = new MicroServicePolicy();
93         µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, Map.class));
94         µService.setPolicyTosca(policyTosca);
95         µService.setProperties(new Gson().fromJson(jsonProperties, Map.class));
96         µService.setShared(shared);
97
98         µService.setName(name);
99         return µService;
100     }
101
102     private LoopLog getLoopLog(LogType type, String message) {
103         LoopLog log = new LoopLog();
104         log.setLogType(type);
105         log.setMessage(message);
106         return log;
107     }
108
109     @Test
110     @Transactional
111     public void CrudTest() {
112         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
113             "123456789", "https://dcaetest.org");
114         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
115         loopTest.addOperationalPolicy(opPolicy);
116         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
117             "YamlContent", "{\"param1\":\"value1\"}", true);
118         loopTest.addMicroServicePolicy(microServicePolicy);
119         LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
120         loopTest.addLog(loopLog);
121
122         // Attemp to save into the database the entire loop
123         Loop loopInDb = loopRepository.save(loopTest);
124         assertThat(loopInDb).isNotNull();
125         assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
126         // Now set the ID in the previous model so that we can compare the objects
127         loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
128
129         assertThat(loopInDb).isEqualToComparingFieldByField(loopTest);
130         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
131         assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(true);
132         assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(true);
133         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
134
135         // Now attempt to read from database
136         Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
137         assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest);
138         assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
139         assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
140             .isEqualToComparingFieldByField(opPolicy);
141         assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
142             .isEqualToComparingFieldByField(microServicePolicy);
143
144         // Attempt an update
145         ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
146         loopRepository.save(loopInDbRetrieved);
147         Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get();
148         assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
149             .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
150
151         // Attempt to delete the object and check it has well been cascaded
152         loopRepository.delete(loopInDbRetrieved);
153         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
154         assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(false);
155         assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(false);
156         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
157
158     }
159 }