2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.provider.impl;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import java.util.ArrayList;
31 import java.util.Base64;
32 import java.util.List;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.models.pdp.concepts.Pdp;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
41 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
42 import org.onap.policy.models.pdp.enums.PdpState;
43 import org.onap.policy.models.provider.PolicyModelsProvider;
44 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
45 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
51 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Test the database models provider implementation.
58 * @author Liam Fallon (liam.fallon@est.tech)
60 public class DatabasePolicyModelsProviderTest {
61 private static final String NAME = "name";
63 private static final String TEMPLATE_IS_NULL = "serviceTemplate is marked @NonNull but is null";
65 private static final String POLICY_ID_IS_NULL = "policyId is marked @NonNull but is null";
67 private static final String PDP_TYPE_IS_NULL = "pdpType is marked @NonNull but is null";
69 private static final String SUBGROUP_IS_NULL = "pdpSubGroup is marked @NonNull but is null";
71 private static final String GROUP_IS_NULL = "pdpGroupName is marked @NonNull but is null";
73 private static final String NAME_IS_NULL = "name is marked @NonNull but is null";
75 private static final String FILTER_IS_NULL = "filter is marked @NonNull but is null";
77 private static final String INSTANCE = "Instance";
79 private static final String POLICY_ID = "policy_id";
81 private static final String GROUP = "group";
83 private static final String VERSION_100 = "1.0.0";
85 private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class);
87 PolicyModelsProviderParameters parameters;
90 * Initialize parameters.
93 public void setupParameters() {
94 parameters = new PolicyModelsProviderParameters();
95 parameters.setDatabaseDriver("org.h2.Driver");
96 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
97 parameters.setDatabaseUser("policy");
98 parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
99 parameters.setPersistenceUnit("ToscaConceptTest");
103 public void testInitAndClose() throws Exception {
104 assertThatThrownBy(() -> {
105 new DatabasePolicyModelsProviderImpl(null);
106 }).hasMessage("parameters is marked @NonNull but is null");
108 PolicyModelsProvider databaseProvider =
109 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
111 parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist");
113 databaseProvider.close();
114 databaseProvider.init();
116 databaseProvider.close();
118 parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
120 parameters.setPersistenceUnit("WileECoyote");
122 assertThatThrownBy(databaseProvider::init).hasMessageContaining("could not create Data Access Object (DAO)");
124 parameters.setPersistenceUnit("ToscaConceptTest");
126 databaseProvider.init();
127 databaseProvider.close();
129 assertThatThrownBy(() -> {
130 databaseProvider.init();
131 databaseProvider.init();
132 }).hasMessage("provider is already initialized");
134 databaseProvider.close();
136 databaseProvider.close();
140 public void testProviderMethodsNull() throws Exception {
142 try (PolicyModelsProvider databaseProvider =
143 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
145 assertThatThrownBy(() -> {
146 databaseProvider.getFilteredPolicyTypes(null);
147 }).hasMessage(FILTER_IS_NULL);
149 assertThatThrownBy(() -> {
150 databaseProvider.getFilteredPolicyTypeList(null);
151 }).hasMessage(FILTER_IS_NULL);
153 assertThatThrownBy(() -> {
154 databaseProvider.createPolicyTypes(null);
155 }).hasMessage(TEMPLATE_IS_NULL);
157 assertThatThrownBy(() -> {
158 databaseProvider.updatePolicyTypes(null);
159 }).hasMessage(TEMPLATE_IS_NULL);
161 assertThatThrownBy(() -> {
162 databaseProvider.deletePolicyType(null, null);
163 }).hasMessage(NAME_IS_NULL);
165 assertThatThrownBy(() -> {
166 databaseProvider.deletePolicyType("aaa", null);
167 }).hasMessage("version is marked @NonNull but is null");
169 assertThatThrownBy(() -> {
170 databaseProvider.deletePolicyType(null, "aaa");
171 }).hasMessage(NAME_IS_NULL);
173 assertThatThrownBy(() -> {
174 databaseProvider.getFilteredPolicies(null);
175 }).hasMessage(FILTER_IS_NULL);
177 assertThatThrownBy(() -> {
178 databaseProvider.getFilteredPolicyList(null);
179 }).hasMessage(FILTER_IS_NULL);
181 assertThatThrownBy(() -> {
182 databaseProvider.createPolicies(null);
183 }).hasMessage(TEMPLATE_IS_NULL);
185 assertThatThrownBy(() -> {
186 databaseProvider.updatePolicies(null);
187 }).hasMessage(TEMPLATE_IS_NULL);
189 assertThatThrownBy(() -> {
190 databaseProvider.deletePolicy(null, null);
191 }).hasMessage(NAME_IS_NULL);
193 assertThatThrownBy(() -> {
194 databaseProvider.deletePolicy(null, "aaa");
195 }).hasMessage(NAME_IS_NULL);
197 assertThatThrownBy(() -> {
198 databaseProvider.deletePolicy("aaa", null);
199 }).hasMessage("version is marked @NonNull but is null");
201 assertThatThrownBy(() -> {
202 databaseProvider.getOperationalPolicy(null, null);
203 }).hasMessage(POLICY_ID_IS_NULL);
205 assertThatThrownBy(() -> {
206 databaseProvider.getOperationalPolicy(null, "");
207 }).hasMessage(POLICY_ID_IS_NULL);
209 assertThatThrownBy(() -> {
210 databaseProvider.getOperationalPolicy("", null);
211 }).hasMessage("no policy found for policy: :null");
213 assertThatThrownBy(() -> {
214 databaseProvider.createOperationalPolicy(null);
215 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
217 assertThatThrownBy(() -> {
218 databaseProvider.updateOperationalPolicy(null);
219 }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
221 assertThatThrownBy(() -> {
222 databaseProvider.deleteOperationalPolicy(null, null);
223 }).hasMessage(POLICY_ID_IS_NULL);
225 assertThatThrownBy(() -> {
226 databaseProvider.deleteOperationalPolicy(null, "");
227 }).hasMessage(POLICY_ID_IS_NULL);
229 assertThatThrownBy(() -> {
230 databaseProvider.deleteOperationalPolicy("", null);
231 }).hasMessage("policyVersion is marked @NonNull but is null");
233 assertThatThrownBy(() -> {
234 databaseProvider.getGuardPolicy(null, null);
235 }).hasMessage(POLICY_ID_IS_NULL);
237 assertThatThrownBy(() -> {
238 databaseProvider.getGuardPolicy(null, "");
239 }).hasMessage(POLICY_ID_IS_NULL);
241 assertThatThrownBy(() -> {
242 databaseProvider.getGuardPolicy("", null);
243 }).hasMessage("no policy found for policy: :null");
245 assertThatThrownBy(() -> {
246 databaseProvider.createGuardPolicy(null);
247 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
249 assertThatThrownBy(() -> {
250 databaseProvider.updateGuardPolicy(null);
251 }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
253 assertThatThrownBy(() -> {
254 databaseProvider.deleteGuardPolicy(null, null);
255 }).hasMessage(POLICY_ID_IS_NULL);
257 assertThatThrownBy(() -> {
258 databaseProvider.deleteGuardPolicy(null, "");
259 }).hasMessage(POLICY_ID_IS_NULL);
261 assertThatThrownBy(() -> {
262 databaseProvider.deleteGuardPolicy("", null);
263 }).hasMessage("policyVersion is marked @NonNull but is null");
265 assertThatThrownBy(() -> {
266 databaseProvider.getFilteredPdpGroups(null);
267 }).hasMessage(FILTER_IS_NULL);
269 assertThatThrownBy(() -> {
270 databaseProvider.createPdpGroups(null);
271 }).hasMessage("pdpGroups is marked @NonNull but is null");
273 assertThatThrownBy(() -> {
274 databaseProvider.updatePdpGroups(null);
275 }).hasMessage("pdpGroups is marked @NonNull but is null");
277 assertThatThrownBy(() -> {
278 databaseProvider.updatePdpSubGroup(null, null);
279 }).hasMessage(GROUP_IS_NULL);
281 assertThatThrownBy(() -> {
282 databaseProvider.updatePdpSubGroup(null, new PdpSubGroup());
283 }).hasMessage(GROUP_IS_NULL);
285 assertThatThrownBy(() -> {
286 databaseProvider.updatePdpSubGroup(NAME, null);
287 }).hasMessage(SUBGROUP_IS_NULL);
289 assertThatThrownBy(() -> {
290 databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup());
291 }).hasMessage("parameter \"localName\" is null");
293 assertThatThrownBy(() -> {
294 databaseProvider.updatePdp(null, null, null);
295 }).hasMessage(GROUP_IS_NULL);
297 assertThatThrownBy(() -> {
298 databaseProvider.updatePdp(null, null, new Pdp());
299 }).hasMessage(GROUP_IS_NULL);
301 assertThatThrownBy(() -> {
302 databaseProvider.updatePdp(null, "sub", null);
303 }).hasMessage(GROUP_IS_NULL);
305 assertThatThrownBy(() -> {
306 databaseProvider.updatePdp(null, "sub", new Pdp());
307 }).hasMessage(GROUP_IS_NULL);
309 assertThatThrownBy(() -> {
310 databaseProvider.updatePdp(NAME, null, null);
311 }).hasMessage(SUBGROUP_IS_NULL);
313 assertThatThrownBy(() -> {
314 databaseProvider.updatePdp(NAME, null, new Pdp());
315 }).hasMessage(SUBGROUP_IS_NULL);
317 assertThatThrownBy(() -> {
318 databaseProvider.updatePdp(NAME, "sub", null);
319 }).hasMessage("pdp is marked @NonNull but is null");
321 assertThatThrownBy(() -> {
322 databaseProvider.updatePdp(NAME, "sub", new Pdp());
323 }).hasMessage("parameter \"localName\" is null");
325 assertThatThrownBy(() -> {
326 databaseProvider.deletePdpGroup(null);
327 }).hasMessage(NAME_IS_NULL);
329 assertThatThrownBy(() -> {
330 databaseProvider.updatePdpStatistics(null, null, null, null);
331 }).hasMessage(GROUP_IS_NULL);
333 assertThatThrownBy(() -> {
334 databaseProvider.updatePdpStatistics(null, null, null, new PdpStatistics());
335 }).hasMessage(GROUP_IS_NULL);
337 assertThatThrownBy(() -> {
338 databaseProvider.updatePdpStatistics(null, null, INSTANCE, null);
339 }).hasMessage(GROUP_IS_NULL);
341 assertThatThrownBy(() -> {
342 databaseProvider.updatePdpStatistics(null, null, INSTANCE, new PdpStatistics());
343 }).hasMessage(GROUP_IS_NULL);
345 assertThatThrownBy(() -> {
346 databaseProvider.updatePdpStatistics(null, "type", null, null);
347 }).hasMessage(GROUP_IS_NULL);
349 assertThatThrownBy(() -> {
350 databaseProvider.updatePdpStatistics(null, "type", null, new PdpStatistics());
351 }).hasMessage(GROUP_IS_NULL);
353 assertThatThrownBy(() -> {
354 databaseProvider.updatePdpStatistics(null, "type", INSTANCE, null);
355 }).hasMessage(GROUP_IS_NULL);
357 assertThatThrownBy(() -> {
358 databaseProvider.updatePdpStatistics(null, "type", INSTANCE, new PdpStatistics());
359 }).hasMessage(GROUP_IS_NULL);
361 assertThatThrownBy(() -> {
362 databaseProvider.updatePdpStatistics(NAME, null, null, null);
363 }).hasMessage(PDP_TYPE_IS_NULL);
365 assertThatThrownBy(() -> {
366 databaseProvider.updatePdpStatistics(NAME, null, null, new PdpStatistics());
367 }).hasMessage(PDP_TYPE_IS_NULL);
369 assertThatThrownBy(() -> {
370 databaseProvider.updatePdpStatistics(NAME, null, INSTANCE, null);
371 }).hasMessage(PDP_TYPE_IS_NULL);
373 assertThatThrownBy(() -> {
374 databaseProvider.updatePdpStatistics(NAME, null, INSTANCE, new PdpStatistics());
375 }).hasMessage(PDP_TYPE_IS_NULL);
377 assertThatThrownBy(() -> {
378 databaseProvider.updatePdpStatistics(NAME, "type", null, null);
379 }).hasMessage("pdpInstanceId is marked @NonNull but is null");
381 assertThatThrownBy(() -> {
382 databaseProvider.updatePdpStatistics(NAME, "type", null, new PdpStatistics());
383 }).hasMessage("pdpInstanceId is marked @NonNull but is null");
385 assertThatThrownBy(() -> {
386 databaseProvider.updatePdpStatistics(NAME, "type", INSTANCE, null);
387 }).hasMessage("pdpStatistics is marked @NonNull but is null");
389 databaseProvider.updatePdpStatistics(NAME, "type", INSTANCE, new PdpStatistics());
394 public void testProviderMethodsNotInit() throws Exception {
395 PolicyModelsProvider databaseProvider =
396 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
398 databaseProvider.close();
400 assertThatThrownBy(() -> {
401 databaseProvider.getPolicyTypes(NAME, "version");
402 }).hasMessage("policy models provider is not initilaized");
406 public void testProviderMethods() {
407 try (PolicyModelsProvider databaseProvider =
408 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
410 assertTrue(databaseProvider.getPolicyTypes(NAME, VERSION_100).getPolicyTypes().isEmpty());
411 assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty());
412 assertEquals(0, databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())
413 .getPolicyTypes().get(0).size());
414 assertEquals(0, databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).size());
416 assertThatThrownBy(() -> {
417 databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
418 }).hasMessage("no policy types specified on service template");
420 assertThatThrownBy(() -> {
421 databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
422 }).hasMessage("no policy types specified on service template");
424 assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
426 assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
429 databaseProvider.getPolicies(NAME, VERSION_100).getToscaTopologyTemplate().getPolicies().isEmpty());
430 assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty());
431 assertEquals(0, databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())
432 .getToscaTopologyTemplate().getPolicies().get(0).size());
433 assertEquals(0, databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).size());
435 assertThatThrownBy(() -> {
436 databaseProvider.createPolicies(new ToscaServiceTemplate());
437 }).hasMessage("topology template not specified on service template");
439 assertThatThrownBy(() -> {
440 databaseProvider.updatePolicies(new ToscaServiceTemplate());
441 }).hasMessage("topology template not specified on service template");
443 assertTrue(databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate().getPolicies()
446 assertThatThrownBy(() -> {
447 databaseProvider.getOperationalPolicy(POLICY_ID, null);
448 }).hasMessage("no policy found for policy: policy_id:null");
450 assertThatThrownBy(() -> {
451 databaseProvider.getOperationalPolicy(POLICY_ID, "10");
452 }).hasMessage("no policy found for policy: policy_id:10");
454 assertThatThrownBy(() -> {
455 databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
456 }).hasMessage(NAME_IS_NULL);
458 assertThatThrownBy(() -> {
459 databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
460 }).hasMessage(NAME_IS_NULL);
462 assertThatThrownBy(() -> {
463 databaseProvider.deleteOperationalPolicy(POLICY_ID, "55");
464 }).hasMessage("no policy found for policy: policy_id:55");
466 assertThatThrownBy(() -> {
467 databaseProvider.getGuardPolicy(POLICY_ID, null);
468 }).hasMessage("no policy found for policy: policy_id:null");
470 assertThatThrownBy(() -> {
471 databaseProvider.getGuardPolicy(POLICY_ID, "6");
472 }).hasMessage("no policy found for policy: policy_id:6");
474 assertThatThrownBy(() -> {
475 databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput());
476 }).hasMessage("policy type for guard policy \"null\" unknown");
478 assertThatThrownBy(() -> {
479 databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput());
480 }).hasMessage("policy type for guard policy \"null\" unknown");
482 assertThatThrownBy(() -> {
483 databaseProvider.deleteGuardPolicy(POLICY_ID, "33");
484 }).hasMessage("no policy found for policy: policy_id:33");
486 assertEquals(0, databaseProvider.getPdpGroups(NAME).size());
487 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
489 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
490 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
492 PdpGroup pdpGroup = new PdpGroup();
493 pdpGroup.setName(GROUP);
494 pdpGroup.setVersion("1.2.3");
495 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
496 pdpGroup.setPdpSubgroups(new ArrayList<>());
497 List<PdpGroup> groupList = new ArrayList<>();
498 groupList.add(pdpGroup);
500 PdpSubGroup pdpSubGroup = new PdpSubGroup();
501 pdpSubGroup.setPdpType("type");
502 pdpSubGroup.setDesiredInstanceCount(123);
503 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
504 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9"));
505 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
508 pdp.setInstanceId("type-0");
509 pdp.setMessage("Hello");
510 pdp.setPdpState(PdpState.ACTIVE);
511 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
512 pdpSubGroup.setPdpInstances(new ArrayList<>());
513 pdpSubGroup.getPdpInstances().add(pdp);
515 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
516 .getDesiredInstanceCount());
517 assertEquals(1, databaseProvider.getPdpGroups(GROUP).size());
519 pdpSubGroup.setDesiredInstanceCount(234);
520 databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup);
521 assertEquals(234, databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups()
522 .get(0).getDesiredInstanceCount());
524 assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups()
525 .get(0).getPdpInstances().get(0).getMessage());
526 pdp.setMessage("Howdy");
527 databaseProvider.updatePdp(GROUP, "type", pdp);
528 assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups()
529 .get(0).getPdpInstances().get(0).getMessage());
531 assertThatThrownBy(() -> {
532 databaseProvider.deletePdpGroup(NAME);
533 }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
535 assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName());
537 assertEquals(0, databaseProvider.getPdpStatistics(null).size());
539 databaseProvider.updatePdpStatistics(GROUP, "type", "type-0", new PdpStatistics());
540 } catch (Exception exc) {
541 LOGGER.warn("test should not throw an exception", exc);
542 fail("test should not throw an exception");