2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
26 package org.onap.clamp.loop;
28 import static org.assertj.core.api.Assertions.assertThat;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonObject;
34 import java.time.Instant;
35 import java.util.HashSet;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.clamp.clds.Application;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.loop.log.LoopLogRepository;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44 import org.onap.clamp.policy.microservice.MicroservicePolicyService;
45 import org.onap.clamp.policy.operational.OperationalPolicy;
46 import org.onap.clamp.policy.operational.OperationalPolicyService;
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;
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @SpringBootTest(classes = Application.class)
54 public class LoopRepositoriesItCase {
56 private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
59 private LoopsRepository loopRepository;
62 private MicroservicePolicyService microServicePolicyService;
65 private OperationalPolicyService operationalPolicyService;
68 private LoopLogRepository loopLogRepository;
70 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
71 return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class));
74 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
75 String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
76 Loop loop = new Loop();
78 loop.setSvgRepresentation(svgRepresentation);
79 loop.setBlueprint(blueprint);
80 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
81 loop.setLastComputedState(LoopState.DESIGN);
82 loop.setDcaeDeploymentId(dcaeId);
83 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
84 loop.setDcaeBlueprintId(dcaeBlueprintId);
88 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
89 String policyTosca, String jsonProperties, boolean shared) {
90 MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
91 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
92 microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
96 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
97 return new LoopLog(message, type, loop);
102 public void crudTest() {
103 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
104 "123456789", "https://dcaetest.org", "UUID-blueprint");
105 OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
106 loopTest.addOperationalPolicy(opPolicy);
107 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
108 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
109 "{\"param1\":\"value1\"}", true);
110 loopTest.addMicroServicePolicy(microServicePolicy);
111 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
112 loopTest.addLog(loopLog);
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());
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);
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);
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]);
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);