fb443c82e05e483114363ff2ddcb2a1ee46f6dde
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
22
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;
27
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.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;
42
43 public class ControlLoopProviderTest {
44
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";
49
50     private static AtomicInteger dbNameCounter = new AtomicInteger();
51
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);
58
59     /**
60      * Set up test control loop provider.
61      */
62     @Before
63     public void setupDao() throws Exception {
64
65         parameters = new PolicyModelsProviderParameters();
66         parameters.setDatabaseDriver("org.h2.Driver");
67         parameters.setName("PolicyProviderParameterGroup");
68         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
69         parameters.setDatabaseUrl("jdbc:h2:mem:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement());
70         parameters.setDatabaseUser("policy");
71         parameters.setDatabasePassword("P01icY");
72         parameters.setPersistenceUnit("ToscaConceptTest");
73
74         controlLoopProvider = new ControlLoopProvider(parameters);
75
76         inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
77         updateControlLoops = CODER.decode(updateClJson, ControlLoops.class);
78     }
79
80     @After
81     public void teardown() {
82         controlLoopProvider.close();
83     }
84
85     @Test
86     public void testControlLoopCreate() throws Exception {
87         assertThatThrownBy(() -> {
88             controlLoopProvider.createControlLoops(null);
89         }).hasMessageMatching(LIST_IS_NULL);
90
91         ControlLoops createdControlLoops = new ControlLoops();
92         createdControlLoops
93                 .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
94         String createdJson = CODER.encode(createdControlLoops, true);
95
96         System.err.println(originalJson);
97         System.out.println(createdJson);
98         assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", ""));
99     }
100
101     @Test
102     public void testGetControlLoops() throws Exception {
103
104         List<ControlLoop> getResponse;
105
106         // Return empty list when no data present in db
107         getResponse = controlLoopProvider.getControlLoops(null, null);
108         assertThat(getResponse).isEmpty();
109
110         controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
111         String name = inputControlLoops.getControlLoopList().get(0).getName();
112         String version = inputControlLoops.getControlLoopList().get(0).getVersion();
113         assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());
114
115         ControlLoop cl = new ControlLoop();
116         cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("PMSHInstance1", "1.0.1"));
117         assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
118
119         assertNull(controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("invalid_name", "1.0.1")));
120
121         assertThatThrownBy(() -> {
122             controlLoopProvider.getFilteredControlLoops(null);
123         }).hasMessageMatching("filter is marked .*ull but is null");
124
125         final ToscaTypedEntityFilter<ControlLoop> filter = ToscaTypedEntityFilter.<ControlLoop>builder()
126                 .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
127         assertEquals(2, controlLoopProvider.getFilteredControlLoops(filter).size());
128     }
129
130
131     @Test
132     public void testUpdateControlLoops() throws Exception {
133         assertThatThrownBy(() -> {
134             controlLoopProvider.updateControlLoops(null);
135         }).hasMessageMatching("controlLoops is marked .*ull but is null");
136
137         ControlLoops existingControlLoops = new ControlLoops();
138         existingControlLoops
139                 .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
140         ControlLoop updateResponse = new ControlLoop();
141         updateResponse = controlLoopProvider.updateControlLoop(updateControlLoops.getControlLoopList().get(0));
142
143         assertEquals(ControlLoopOrderedState.RUNNING, updateResponse.getOrderedState());
144     }
145
146     @Test
147     public void testDeleteControlLoop() throws Exception {
148         assertThatThrownBy(() -> {
149             controlLoopProvider.deleteControlLoop("Invalid_name", "1.0.1");
150         }).hasMessageMatching(".*.failed, control loop does not exist");
151
152         ControlLoop deletedCl;
153         List<ControlLoop> clList = controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
154         String name = inputControlLoops.getControlLoopList().get(0).getName();
155         String version = inputControlLoops.getControlLoopList().get(0).getVersion();
156
157         deletedCl = controlLoopProvider.deleteControlLoop(name, version);
158         assertEquals(clList.get(0), deletedCl);
159
160     }
161 }
162
163