c524eec39d748533f028f09b76ea0d0f8ecdab09
[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  * 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.loop;
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 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.policy.microservice.MicroServicePolicy;
42 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
43 import org.onap.clamp.policy.operational.OperationalPolicy;
44 import org.onap.clamp.policy.operational.OperationalPolicyService;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 import org.springframework.transaction.annotation.Transactional;
49
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @SpringBootTest(classes = Application.class)
52 public class LoopRepositoriesItCase {
53
54     private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
55
56     @Autowired
57     private LoopsRepository loopRepository;
58
59     @Autowired
60     private MicroservicePolicyService microServicePolicyService;
61
62     @Autowired
63     private OperationalPolicyService operationalPolicyService;
64
65     @Autowired
66     private LoopLogRepository loopLogRepository;
67
68     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
69         return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class));
70     }
71
72     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
73         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
74         Loop loop = new Loop();
75         loop.setName(name);
76         loop.setSvgRepresentation(svgRepresentation);
77         loop.setBlueprint(blueprint);
78         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
79         loop.setLastComputedState(LoopState.DESIGN);
80         loop.setDcaeDeploymentId(dcaeId);
81         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
82         loop.setDcaeBlueprintId(dcaeBlueprintId);
83         return loop;
84     }
85
86     private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca,
87         String jsonProperties, boolean shared) {
88         MicroServicePolicy µService = new MicroServicePolicy(name, policyTosca, shared,
89             gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
90         µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
91         return µService;
92     }
93
94     private LoopLog getLoopLog(LogType type, String message) {
95         LoopLog log = new LoopLog();
96         log.setLogType(type);
97         log.setMessage(message);
98         return log;
99     }
100
101     @Test
102     @Transactional
103     public void CrudTest() {
104         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
105             "123456789", "https://dcaetest.org", "UUID-blueprint");
106         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
107         loopTest.addOperationalPolicy(opPolicy);
108         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}",
109             "YamlContent", "{\"param1\":\"value1\"}", true);
110         loopTest.addMicroServicePolicy(microServicePolicy);
111         LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
112         loopTest.addLog(loopLog);
113
114         // Attemp to save into the database the entire loop
115         Loop loopInDb = loopRepository.save(loopTest);
116         assertThat(loopInDb).isNotNull();
117         assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
118         // Now set the ID in the previous model so that we can compare the objects
119         loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
120
121         assertThat(loopInDb).isEqualToComparingFieldByField(loopTest);
122         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
123         assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
124         assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
125         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
126
127         // Now attempt to read from database
128         Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
129         assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest);
130         assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
131         assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
132             .isEqualToComparingFieldByField(opPolicy);
133         assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
134             .isEqualToComparingFieldByField(microServicePolicy);
135
136         // Attempt an update
137         ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
138         loopRepository.save(loopInDbRetrieved);
139         Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get();
140         assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
141             .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
142
143         // Attempt to delete the object and check it has well been cascaded
144         loopRepository.delete(loopInDbRetrieved);
145         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
146         assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
147         assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(false);
148         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
149
150     }
151 }