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