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