9805edb8667d974ff1ad4fe6face11dfbfab2bbc
[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.assertNotNull;
27 import static org.junit.Assert.assertNull;
28
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;
46
47 class ControlLoopProviderTest {
48
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";
54
55     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
56     private static AtomicInteger dbNameCounter = new AtomicInteger();
57
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);
64
65     @BeforeEach
66     void beforeSetupDao() throws Exception {
67
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");
76
77         controlLoopProvider = new ControlLoopProvider(parameters);
78
79         inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
80         updateControlLoops = CODER.decode(updateClJson, ControlLoops.class);
81     }
82
83     @AfterEach
84     void teardown() {
85         controlLoopProvider.close();
86     }
87
88     @Test
89     void testControlLoopCreate() throws Exception {
90         assertThatThrownBy(() -> {
91             controlLoopProvider.createControlLoops(null);
92         }).hasMessageMatching(LIST_IS_NULL);
93
94         var createdControlLoops = new ControlLoops();
95         createdControlLoops
96             .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
97
98         assertEquals(inputControlLoops, createdControlLoops);
99     }
100
101     @Test
102     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         var name = inputControlLoops.getControlLoopList().get(0).getName();
112         var version = inputControlLoops.getControlLoopList().get(0).getVersion();
113         assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());
114
115         var 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     @Test
131     void testUpdateControlLoops() throws Exception {
132         assertThatThrownBy(() -> {
133             controlLoopProvider.updateControlLoops(null);
134         }).hasMessageMatching("controlLoops is marked .*ull but is null");
135
136         var existingControlLoops = new ControlLoops();
137         existingControlLoops
138             .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
139         var updateResponse = new ControlLoop();
140         updateResponse = controlLoopProvider.updateControlLoop(updateControlLoops.getControlLoopList().get(0));
141
142         assertEquals(ControlLoopOrderedState.RUNNING, updateResponse.getOrderedState());
143     }
144
145     @Test
146     void testDeleteControlLoop() throws Exception {
147         assertThatThrownBy(() -> {
148             controlLoopProvider.deleteControlLoop("Invalid_name", "1.0.1");
149         }).hasMessageMatching(".*.failed, control loop does not exist");
150
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();
155
156         deletedCl = controlLoopProvider.deleteControlLoop(name, version);
157         assertEquals(clList.get(0), deletedCl);
158     }
159
160     @Test
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();
167     }
168
169     @Test
170     void testSaveAndDeleteInstanceProperties() throws Exception {
171         var toscaServiceTest = testControlLoopRead();
172         controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
173
174         controlLoopProvider.saveInstanceProperties(toscaServiceTest);
175         assertThat(controlLoopProvider.getNodeTemplates(
176                 "org.onap.policy.controlloop.PolicyControlLoopParticipant",
177                 "2.3.1")).isNotEmpty();
178
179         controlLoopProvider.deleteInstanceProperties(
180                 controlLoopProvider.saveInstanceProperties(toscaServiceTest),
181                 controlLoopProvider.getNodeTemplates(
182                         "org.onap.policy.controlloop.PolicyControlLoopParticipant",
183                         "2.3.1"));
184
185         assertThat(controlLoopProvider.getNodeTemplates(
186                 "org.onap.policy.controlloop.PolicyControlLoopParticipant",
187                 "2.3.1")).isEmpty();
188     }
189
190     @Test
191     void testGetNodeTemplates() throws Exception {
192         //Getting all nodes
193         List<ToscaNodeTemplate> listNodes = controlLoopProvider.getNodeTemplates(null, null);
194         assertNotNull(listNodes);
195
196         assertThatThrownBy(() -> {
197             controlLoopProvider.getFilteredNodeTemplates(null);
198         }).hasMessageMatching("filter is marked non-null but is null");
199     }
200
201     private static ToscaServiceTemplate testControlLoopRead() {
202         return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
203     }
204
205     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
206         String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
207         ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
208         return serviceTemplate;
209     }
210 }