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;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Test the database models provider implementation.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class DatabasePolicyModelsProviderTest {
49 private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class);
51 PolicyModelsProviderParameters parameters;
54 * Initialize parameters.
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");
67 public void testInitAndClose() throws Exception {
68 PolicyModelsProvider databaseProvider =
69 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
71 parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist");
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());
78 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
81 databaseProvider.init();
82 databaseProvider.close();
83 } catch (Exception pfme) {
84 fail("test shold not throw an exception here");
87 parameters.setPersistenceUnit("WileECoyote");
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());
95 parameters.setPersistenceUnit("ToscaConceptTest");
98 databaseProvider.init();
99 databaseProvider.close();
100 } catch (Exception pfme) {
101 fail("test shold not throw an exception here");
105 databaseProvider.close();
106 } catch (Exception pfme) {
107 fail("test shold not throw an exception here");
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());
122 public void testProviderMethodsNull() throws Exception {
123 PolicyModelsProvider databaseProvider =
124 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
125 databaseProvider.init();
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
252 databaseProvider.close();
256 public void testProviderMethodsNotInit() throws Exception {
257 PolicyModelsProvider databaseProvider =
258 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
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());
268 public void testProviderMethods() {
269 try (PolicyModelsProvider databaseProvider =
270 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
271 databaseProvider.init();
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());
280 databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
281 } catch (Exception npe) {
282 assertEquals("no policy types specified on service template", npe.getMessage());
285 databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
286 } catch (Exception npe) {
287 assertEquals("no policy types specified on service template", npe.getMessage());
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());
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());
303 databaseProvider.createPolicies(new ToscaServiceTemplate());
304 } catch (Exception npe) {
305 assertEquals("topology template not specified on service template", npe.getMessage());
308 databaseProvider.updatePolicies(new ToscaServiceTemplate());
309 } catch (Exception npe) {
310 assertEquals("topology template not specified on service template", npe.getMessage());
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());
319 assertNull(databaseProvider.getOperationalPolicy("policy_id"));
320 assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
321 assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
322 assertNull(databaseProvider.deleteOperationalPolicy("policy_id"));
324 assertNull(databaseProvider.getGuardPolicy("policy_id"));
325 assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy()));
326 assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy()));
327 assertNull(databaseProvider.deleteGuardPolicy("policy_id"));
329 assertNotNull(databaseProvider.getPdpGroups("filter"));
330 assertNotNull(databaseProvider.createPdpGroups(new PdpGroups()));
331 assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups()));
332 assertNotNull(databaseProvider.deletePdpGroups("filter"));
334 } catch (Exception exc) {
335 LOGGER.warn("test should not throw an exception", exc);
336 fail("test should not throw an exception");