2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicInteger;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
47 class ControlLoopProviderTest {
49 private static final String LIST_IS_NULL = "controlLoops is marked .*ull but is null";
50 private static final Coder CODER = new StandardCoder();
51 private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
52 private static final String UPDATE_CL_JSON = "src/test/resources/providers/UpdateControlLoops.json";
53 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
55 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
56 private static AtomicInteger dbNameCounter = new AtomicInteger();
58 private PolicyModelsProviderParameters parameters;
59 private ControlLoopProvider controlLoopProvider;
60 private ControlLoops inputControlLoops;
61 private ControlLoops updateControlLoops;
62 private String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);
63 private String updateClJson = ResourceUtils.getResourceAsString(UPDATE_CL_JSON);
66 void beforeSetupDao() throws Exception {
68 parameters = new PolicyModelsProviderParameters();
69 parameters.setDatabaseDriver("org.h2.Driver");
70 parameters.setName("PolicyProviderParameterGroup");
71 parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
72 parameters.setDatabaseUrl("jdbc:h2:mem:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement());
73 parameters.setDatabaseUser("policy");
74 parameters.setDatabasePassword("P01icY");
75 parameters.setPersistenceUnit("ToscaConceptTest");
77 controlLoopProvider = new ControlLoopProvider(parameters);
79 inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
80 updateControlLoops = CODER.decode(updateClJson, ControlLoops.class);
85 controlLoopProvider.close();
89 void testControlLoopCreate() throws Exception {
90 assertThatThrownBy(() -> {
91 controlLoopProvider.createControlLoops(null);
92 }).hasMessageMatching(LIST_IS_NULL);
94 var createdControlLoops = new ControlLoops();
96 .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
98 assertEquals(inputControlLoops, createdControlLoops);
102 void testGetControlLoops() throws Exception {
104 List<ControlLoop> getResponse;
106 // Return empty list when no data present in db
107 getResponse = controlLoopProvider.getControlLoops(null, null);
108 assertThat(getResponse).isEmpty();
110 controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
111 var name = inputControlLoops.getControlLoopList().get(0).getName();
112 var version = inputControlLoops.getControlLoopList().get(0).getVersion();
113 assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());
115 var cl = new ControlLoop();
116 cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("PMSHInstance1", "1.0.1"));
117 assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
119 assertNull(controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("invalid_name", "1.0.1")));
121 assertThatThrownBy(() -> {
122 controlLoopProvider.getFilteredControlLoops(null);
123 }).hasMessageMatching("filter is marked .*ull but is null");
125 final ToscaTypedEntityFilter<ControlLoop> filter = ToscaTypedEntityFilter.<ControlLoop>builder()
126 .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
127 assertEquals(2, controlLoopProvider.getFilteredControlLoops(filter).size());
131 void testUpdateControlLoops() throws Exception {
132 assertThatThrownBy(() -> {
133 controlLoopProvider.updateControlLoops(null);
134 }).hasMessageMatching("controlLoops is marked .*ull but is null");
136 var existingControlLoops = new ControlLoops();
138 .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
139 var updateResponse = new ControlLoop();
140 updateResponse = controlLoopProvider.updateControlLoop(updateControlLoops.getControlLoopList().get(0));
142 assertEquals(ControlLoopOrderedState.RUNNING, updateResponse.getOrderedState());
146 void testDeleteControlLoop() throws Exception {
147 assertThatThrownBy(() -> {
148 controlLoopProvider.deleteControlLoop("Invalid_name", "1.0.1");
149 }).hasMessageMatching(".*.failed, control loop does not exist");
151 ControlLoop deletedCl;
152 List<ControlLoop> clList = controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
153 var name = inputControlLoops.getControlLoopList().get(0).getName();
154 var version = inputControlLoops.getControlLoopList().get(0).getVersion();
156 deletedCl = controlLoopProvider.deleteControlLoop(name, version);
157 assertEquals(clList.get(0), deletedCl);
161 void testDeleteAllInstanceProperties() throws Exception {
162 var toscaServiceTemplate = testControlLoopRead();
163 controlLoopProvider.deleteInstanceProperties(
164 controlLoopProvider.saveInstanceProperties(toscaServiceTemplate),
165 controlLoopProvider.getNodeTemplates(null, null));
166 assertThat(controlLoopProvider.getControlLoops(null, null)).isEmpty();
170 void testSaveAndDeleteInstanceProperties() throws Exception {
171 var toscaServiceTest = testControlLoopRead();
172 controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
174 controlLoopProvider.saveInstanceProperties(toscaServiceTest);
175 assertThat(controlLoopProvider.getNodeTemplates(
176 "org.onap.policy.controlloop.PolicyControlLoopParticipant",
177 "2.3.1")).isNotEmpty();
179 controlLoopProvider.deleteInstanceProperties(
180 controlLoopProvider.saveInstanceProperties(toscaServiceTest),
181 controlLoopProvider.getNodeTemplates(
182 "org.onap.policy.controlloop.PolicyControlLoopParticipant",
185 assertThat(controlLoopProvider.getNodeTemplates(
186 "org.onap.policy.controlloop.PolicyControlLoopParticipant",
191 void testGetNodeTemplates() throws Exception {
193 List<ToscaNodeTemplate> listNodes = controlLoopProvider.getNodeTemplates(null, null);
194 assertNotNull(listNodes);
196 assertThatThrownBy(() -> {
197 controlLoopProvider.getFilteredNodeTemplates(null);
198 }).hasMessageMatching("filter is marked non-null but is null");
201 private static ToscaServiceTemplate testControlLoopRead() {
202 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
205 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
206 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
207 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
208 return serviceTemplate;