LoopLog repository
[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 import java.util.HashSet;
34
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 modelType, String jsonRepresentation,
87         String policyTosca, String jsonProperties, boolean shared) {
88         MicroServicePolicy µService = new MicroServicePolicy(name, modelType, 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, Loop loop) {
95         return new LoopLog(message, type, loop);
96     }
97
98     @Test
99     @Transactional
100     public void CrudTest() {
101         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
102             "123456789", "https://dcaetest.org", "UUID-blueprint");
103         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
104         loopTest.addOperationalPolicy(opPolicy);
105         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
106             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
107             "{\"param1\":\"value1\"}", true);
108         loopTest.addMicroServicePolicy(microServicePolicy);
109         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
110         loopTest.addLog(loopLog);
111
112         // Attemp to save into the database the entire loop
113         Loop loopInDb = loopRepository.save(loopTest);
114         assertThat(loopInDb).isNotNull();
115         assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest");
116         // Now set the ID in the previous model so that we can compare the objects
117         loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId());
118
119         assertThat(loopInDb).isEqualToComparingFieldByField(loopTest);
120         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true);
121         assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true);
122         assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true);
123         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true);
124
125         // Now attempt to read from database
126         Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get();
127         assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest);
128         assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog);
129         assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0])
130             .isEqualToComparingFieldByField(opPolicy);
131         assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0])
132             .isEqualToComparingFieldByField(microServicePolicy);
133
134         // Attempt an update
135         ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now());
136         loopRepository.save(loopInDbRetrieved);
137         Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get();
138         assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0])
139             .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]);
140
141         // Attempt to delete the object and check it has well been cascaded
142         loopRepository.delete(loopInDbRetrieved);
143         assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false);
144         assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false);
145         assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(false);
146         assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false);
147
148     }
149 }