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.assertNull;
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
43 class ControlLoopProviderTest {
45 private static final String LIST_IS_NULL = "controlLoops is marked .*ull but is null";
46 private static final Coder CODER = new StandardCoder();
47 private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
48 private static final String UPDATE_CL_JSON = "src/test/resources/providers/UpdateControlLoops.json";
50 private static AtomicInteger dbNameCounter = new AtomicInteger();
52 private PolicyModelsProviderParameters parameters;
53 private ControlLoopProvider controlLoopProvider;
54 private ControlLoops inputControlLoops;
55 private ControlLoops updateControlLoops;
56 private String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);
57 private String updateClJson = ResourceUtils.getResourceAsString(UPDATE_CL_JSON);
60 void beforeSetupDao() throws Exception {
62 parameters = new PolicyModelsProviderParameters();
63 parameters.setDatabaseDriver("org.h2.Driver");
64 parameters.setName("PolicyProviderParameterGroup");
65 parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
66 parameters.setDatabaseUrl("jdbc:h2:mem:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement());
67 parameters.setDatabaseUser("policy");
68 parameters.setDatabasePassword("P01icY");
69 parameters.setPersistenceUnit("ToscaConceptTest");
71 controlLoopProvider = new ControlLoopProvider(parameters);
73 inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
74 updateControlLoops = CODER.decode(updateClJson, ControlLoops.class);
79 controlLoopProvider.close();
83 void testControlLoopCreate() throws Exception {
84 assertThatThrownBy(() -> {
85 controlLoopProvider.createControlLoops(null);
86 }).hasMessageMatching(LIST_IS_NULL);
88 ControlLoops createdControlLoops = new ControlLoops();
90 .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
92 assertEquals(inputControlLoops, createdControlLoops);
96 void testGetControlLoops() throws Exception {
98 List<ControlLoop> getResponse;
100 // Return empty list when no data present in db
101 getResponse = controlLoopProvider.getControlLoops(null, null);
102 assertThat(getResponse).isEmpty();
104 controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
105 String name = inputControlLoops.getControlLoopList().get(0).getName();
106 String version = inputControlLoops.getControlLoopList().get(0).getVersion();
107 assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());
109 ControlLoop cl = new ControlLoop();
110 cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("PMSHInstance1", "1.0.1"));
111 assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
113 assertNull(controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("invalid_name", "1.0.1")));
115 assertThatThrownBy(() -> {
116 controlLoopProvider.getFilteredControlLoops(null);
117 }).hasMessageMatching("filter is marked .*ull but is null");
119 final ToscaTypedEntityFilter<ControlLoop> filter = ToscaTypedEntityFilter.<ControlLoop>builder()
120 .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
121 assertEquals(2, controlLoopProvider.getFilteredControlLoops(filter).size());
125 void testUpdateControlLoops() throws Exception {
126 assertThatThrownBy(() -> {
127 controlLoopProvider.updateControlLoops(null);
128 }).hasMessageMatching("controlLoops is marked .*ull but is null");
130 ControlLoops existingControlLoops = new ControlLoops();
132 .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
133 ControlLoop updateResponse = new ControlLoop();
134 updateResponse = controlLoopProvider.updateControlLoop(updateControlLoops.getControlLoopList().get(0));
136 assertEquals(ControlLoopOrderedState.RUNNING, updateResponse.getOrderedState());
140 void testDeleteControlLoop() throws Exception {
141 assertThatThrownBy(() -> {
142 controlLoopProvider.deleteControlLoop("Invalid_name", "1.0.1");
143 }).hasMessageMatching(".*.failed, control loop does not exist");
145 ControlLoop deletedCl;
146 List<ControlLoop> clList = controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
147 String name = inputControlLoops.getControlLoopList().get(0).getName();
148 String version = inputControlLoops.getControlLoopList().get(0).getVersion();
150 deletedCl = controlLoopProvider.deleteControlLoop(name, version);
151 assertEquals(clList.get(0), deletedCl);