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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.provider.impl;
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;
28 import java.util.Base64;
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;
42 * Test the database models provider implementation.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class DatabasePolicyModelsProviderTest {
47 PolicyModelsProviderParameters parameters;
50 * Initialize parameters.
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");
63 public void testInitAndClose() throws Exception {
64 PolicyModelsProvider databaseProvider =
65 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
67 parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist");
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());
74 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
77 databaseProvider.init();
78 databaseProvider.close();
79 } catch (Exception pfme) {
80 fail("test shold not throw an exception here");
83 parameters.setPersistenceUnit("WileECoyote");
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());
91 parameters.setPersistenceUnit("ToscaConceptTest");
94 databaseProvider.init();
95 databaseProvider.close();
96 } catch (Exception pfme) {
97 fail("test shold not throw an exception here");
101 databaseProvider.close();
102 } catch (Exception pfme) {
103 fail("test shold not throw an exception here");
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());
118 public void testProviderMethodsNull() throws Exception {
119 PolicyModelsProvider databaseProvider =
120 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
121 databaseProvider.init();
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
248 databaseProvider.close();
252 public void testProviderMethodsNotInit() throws Exception {
253 PolicyModelsProvider databaseProvider =
254 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
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());
264 public void testProviderMethods() {
265 try (PolicyModelsProvider databaseProvider =
266 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
267 databaseProvider.init();
269 assertNull(databaseProvider.getPolicyTypes(new PfConceptKey()));
270 assertNull(databaseProvider.createPolicyTypes(new ToscaServiceTemplate()));
271 assertNull(databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()));
272 assertNull(databaseProvider.deletePolicyTypes(new PfConceptKey()));
274 assertNull(databaseProvider.getPolicies(new PfConceptKey()));
275 assertNull(databaseProvider.createPolicies(new ToscaServiceTemplate()));
276 assertNull(databaseProvider.updatePolicies(new ToscaServiceTemplate()));
277 assertNull(databaseProvider.deletePolicies(new PfConceptKey()));
279 assertNull(databaseProvider.getOperationalPolicy("policy_id"));
280 assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
281 assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
282 assertNull(databaseProvider.deleteOperationalPolicy("policy_id"));
284 assertNull(databaseProvider.getGuardPolicy("policy_id"));
285 assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy()));
286 assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy()));
287 assertNull(databaseProvider.deleteGuardPolicy("policy_id"));
289 assertNotNull(databaseProvider.getPdpGroups("filter"));
290 assertNotNull(databaseProvider.createPdpGroups(new PdpGroups()));
291 assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups()));
292 assertNotNull(databaseProvider.deletePdpGroups("filter"));
294 } catch (Exception exc) {
295 fail("test should not throw an exception");