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.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Test the database models provider implementation.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class DatabasePolicyModelsProviderTest {
50 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
52 PolicyModelsProviderParameters parameters;
55 * Initialize parameters.
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");
68 public void testInitAndClose() throws Exception {
69 PolicyModelsProvider databaseProvider =
70 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
72 parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist");
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());
79 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
82 databaseProvider.init();
83 databaseProvider.close();
84 } catch (Exception pfme) {
85 fail("test shold not throw an exception here");
88 parameters.setPersistenceUnit("WileECoyote");
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());
96 parameters.setPersistenceUnit("ToscaConceptTest");
99 databaseProvider.init();
100 databaseProvider.close();
101 } catch (Exception pfme) {
102 fail("test shold not throw an exception here");
106 databaseProvider.close();
107 } catch (Exception pfme) {
108 fail("test shold not throw an exception here");
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());
123 public void testProviderMethodsNull() throws Exception {
124 PolicyModelsProvider databaseProvider =
125 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
126 databaseProvider.init();
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
253 databaseProvider.close();
257 public void testProviderMethodsNotInit() throws Exception {
258 PolicyModelsProvider databaseProvider =
259 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
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());
269 public void testProviderMethods() {
270 try (PolicyModelsProvider databaseProvider =
271 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
272 databaseProvider.init();
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());
281 databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
282 } catch (Exception npe) {
283 assertEquals("no policy types specified on service template", npe.getMessage());
286 databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
287 } catch (Exception npe) {
288 assertEquals("no policy types specified on service template", npe.getMessage());
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());
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());
304 databaseProvider.createPolicies(new ToscaServiceTemplate());
305 } catch (Exception npe) {
306 assertEquals("topology template not specified on service template", npe.getMessage());
309 databaseProvider.updatePolicies(new ToscaServiceTemplate());
310 } catch (Exception npe) {
311 assertEquals("topology template not specified on service template", npe.getMessage());
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());
320 assertNull(databaseProvider.getOperationalPolicy("policy_id"));
321 assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
322 assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
323 assertNull(databaseProvider.deleteOperationalPolicy("policy_id"));
325 assertNull(databaseProvider.getGuardPolicy("policy_id"));
326 assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy()));
327 assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy()));
328 assertNull(databaseProvider.deleteGuardPolicy("policy_id"));
330 assertNotNull(databaseProvider.getPdpGroups("filter"));
331 assertNotNull(databaseProvider.createPdpGroups(new PdpGroups()));
332 assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups()));
333 assertNotNull(databaseProvider.deletePdpGroups("filter"));
335 } catch (Exception exc) {
336 LOGGER.warn("test should not throw an exception", exc);
337 fail("test should not throw an exception");