Add persistence test for all policy examples
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.models.provider.impl;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.Base64;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.pap.concepts.PdpGroups;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
36 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
38 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
39 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Test the database models provider implementation.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class DatabasePolicyModelsProviderTest {
49     private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class);
50
51     PolicyModelsProviderParameters parameters;
52
53     /**
54      * Initialize parameters.
55      */
56     @Before
57     public void setupParameters() {
58         parameters = new PolicyModelsProviderParameters();
59         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
60         parameters.setDatabaseUser("policy");
61         parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
62         parameters.setPersistenceUnit("ToscaConceptTest");
63
64     }
65
66     @Test
67     public void testInitAndClose() throws Exception {
68         PolicyModelsProvider databaseProvider =
69                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
70
71         parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist");
72         try {
73             databaseProvider.init();
74             fail("test should throw an exception");
75         } catch (Exception pfme) {
76             assertEquals("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\"", pfme.getMessage());
77         }
78         parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
79
80         try {
81             databaseProvider.init();
82             databaseProvider.close();
83         } catch (Exception pfme) {
84             fail("test shold not throw an exception here");
85         }
86
87         parameters.setPersistenceUnit("WileECoyote");
88         try {
89             databaseProvider.init();
90             fail("test should throw an exception");
91         } catch (Exception pfme) {
92             assertEquals("could not create Data Access Object (DAO) using url "
93                     + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\"", pfme.getMessage());
94         }
95         parameters.setPersistenceUnit("ToscaConceptTest");
96
97         try {
98             databaseProvider.init();
99             databaseProvider.close();
100         } catch (Exception pfme) {
101             fail("test shold not throw an exception here");
102         }
103
104         try {
105             databaseProvider.close();
106         } catch (Exception pfme) {
107             fail("test shold not throw an exception here");
108         }
109
110         try {
111             DatabasePolicyModelsProviderImpl databaseProviderImpl = (DatabasePolicyModelsProviderImpl) databaseProvider;
112             databaseProvider.init();
113             databaseProviderImpl.setConnection(new DummyConnection());
114             databaseProvider.close();
115             fail("test should throw an exception");
116         } catch (Exception pfme) {
117             assertEquals("could not close connection to database with URL \"jdbc:h2:mem:testdb\"", pfme.getMessage());
118         }
119     }
120
121     @Test
122     public void testProviderMethodsNull() throws Exception {
123         PolicyModelsProvider databaseProvider =
124                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
125         databaseProvider.init();
126
127         try {
128             databaseProvider.getPolicyTypes(null);
129             fail("test should throw an exception");
130         } catch (Exception npe) {
131             assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage());
132         }
133         try {
134             databaseProvider.createPolicyTypes(null);
135             fail("test should throw an exception");
136         } catch (Exception npe) {
137             assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
138         }
139         try {
140             databaseProvider.updatePolicyTypes(null);
141             fail("test should throw an exception");
142         } catch (Exception npe) {
143             assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
144         }
145         try {
146             databaseProvider.deletePolicyTypes(null);
147             fail("test should throw an exception");
148         } catch (Exception npe) {
149             assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage());
150         }
151
152         try {
153             databaseProvider.getPolicies(null);
154             fail("test should throw an exception");
155         } catch (Exception npe) {
156             assertEquals("policyKey is marked @NonNull but is null", npe.getMessage());
157         }
158         try {
159             databaseProvider.createPolicies(null);
160             fail("test should throw an exception");
161         } catch (Exception npe) {
162             assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
163         }
164         try {
165             databaseProvider.updatePolicies(null);
166             fail("test should throw an exception");
167         } catch (Exception npe) {
168             assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage());
169         }
170         try {
171             databaseProvider.deletePolicies(null);
172             fail("test should throw an exception");
173         } catch (Exception npe) {
174             assertEquals("policyKey is marked @NonNull but is null", npe.getMessage());
175         }
176
177         try {
178             databaseProvider.getOperationalPolicy(null);
179             fail("test should throw an exception");
180         } catch (Exception npe) {
181             assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
182         }
183         try {
184             databaseProvider.createOperationalPolicy(null);
185             fail("test should throw an exception");
186         } catch (Exception npe) {
187             assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage());
188         }
189         try {
190             databaseProvider.updateOperationalPolicy(null);
191             fail("test should throw an exception");
192         } catch (Exception npe) {
193             assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage());
194         }
195         try {
196             databaseProvider.deleteOperationalPolicy(null);
197             fail("test should throw an exception");
198         } catch (Exception npe) {
199             assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
200         }
201
202         try {
203             databaseProvider.getGuardPolicy(null);
204             fail("test should throw an exception");
205         } catch (Exception npe) {
206             assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
207         }
208         try {
209             databaseProvider.createGuardPolicy(null);
210             fail("test should throw an exception");
211         } catch (Exception npe) {
212             assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage());
213         }
214         try {
215             databaseProvider.updateGuardPolicy(null);
216             fail("test should throw an exception");
217         } catch (Exception npe) {
218             assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage());
219         }
220         try {
221             databaseProvider.deleteGuardPolicy(null);
222             fail("test should throw an exception");
223         } catch (Exception npe) {
224             assertEquals("policyId is marked @NonNull but is null", npe.getMessage());
225         }
226
227         try {
228             databaseProvider.getPdpGroups(null);
229             fail("test should throw an exception");
230         } catch (Exception npe) {
231             assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage());
232         }
233         try {
234             databaseProvider.createPdpGroups(null);
235             fail("test should throw an exception");
236         } catch (Exception npe) {
237             assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage());
238         }
239         try {
240             databaseProvider.updatePdpGroups(null);
241             fail("test should throw an exception");
242         } catch (Exception npe) {
243             assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage());
244         }
245         try {
246             databaseProvider.deletePdpGroups(null);
247             fail("test should throw an exception");
248         } catch (Exception npe) {
249             assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage());
250         }
251
252         databaseProvider.close();
253     }
254
255     @Test
256     public void testProviderMethodsNotInit() throws Exception {
257         PolicyModelsProvider databaseProvider =
258                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
259         try {
260             databaseProvider.getPolicyTypes(new PfConceptKey());
261             fail("test should throw an exception");
262         } catch (Exception npe) {
263             assertEquals("policy models provider is not initilaized", npe.getMessage());
264         }
265     }
266
267     @Test
268     public void testProviderMethods() {
269         try (PolicyModelsProvider databaseProvider =
270                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
271             databaseProvider.init();
272
273             try {
274                 databaseProvider.getPolicyTypes(new PfConceptKey());
275                 fail("test should throw an exception");
276             } catch (Exception npe) {
277                 assertEquals("policy type not found: NULL:0.0.0", npe.getMessage());
278             }
279             try {
280                 databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
281             } catch (Exception npe) {
282                 assertEquals("no policy types specified on service template", npe.getMessage());
283             }
284             try {
285                 databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
286             } catch (Exception npe) {
287                 assertEquals("no policy types specified on service template", npe.getMessage());
288             }
289             try {
290                 databaseProvider.deletePolicyTypes(new PfConceptKey());
291                 fail("test should throw an exception");
292             } catch (Exception npe) {
293                 assertEquals("policy type not found: NULL:0.0.0", npe.getMessage());
294             }
295
296             try {
297                 databaseProvider.getPolicies(new PfConceptKey());
298                 fail("test should throw an exception");
299             } catch (Exception npe) {
300                 assertEquals("policy not found: NULL:0.0.0", npe.getMessage());
301             }
302             try {
303                 databaseProvider.createPolicies(new ToscaServiceTemplate());
304             } catch (Exception npe) {
305                 assertEquals("topology template not specified on service template", npe.getMessage());
306             }
307             try {
308                 databaseProvider.updatePolicies(new ToscaServiceTemplate());
309             } catch (Exception npe) {
310                 assertEquals("topology template not specified on service template", npe.getMessage());
311             }
312             try {
313                 databaseProvider.deletePolicies(new PfConceptKey());
314                 fail("test should throw an exception");
315             } catch (Exception npe) {
316                 assertEquals("policy not found: NULL:0.0.0", npe.getMessage());
317             }
318
319             assertNull(databaseProvider.getOperationalPolicy("policy_id"));
320             assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
321             assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
322             assertNull(databaseProvider.deleteOperationalPolicy("policy_id"));
323
324             assertNull(databaseProvider.getGuardPolicy("policy_id"));
325             assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy()));
326             assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy()));
327             assertNull(databaseProvider.deleteGuardPolicy("policy_id"));
328
329             assertNotNull(databaseProvider.getPdpGroups("filter"));
330             assertNotNull(databaseProvider.createPdpGroups(new PdpGroups()));
331             assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups()));
332             assertNotNull(databaseProvider.deletePdpGroups("filter"));
333
334         } catch (Exception exc) {
335             LOGGER.warn("test should not throw an exception", exc);
336             fail("test should not throw an exception");
337         }
338     }
339 }