2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.Date;
33 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 .*on.*ull but is null$";
65 private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$";
67 private static final String SUBGROUP_IS_NULL = "^pdpSubGroup is marked .*on.*ull but is null$";
69 private static final String GROUP_IS_NULL = "^pdpGroupName is marked .*on.*ull but is null$";
71 private static final String NAME_IS_NULL = "^name is marked .*on.*ull but is null$";
73 private static final String FILTER_IS_NULL = "^filter is marked .*on.*ull but is null$";
75 private static final String POLICY_ID = "policy_id";
77 private static final String GROUP = "group";
79 private static final String VERSION_100 = "1.0.0";
81 private static final Date TIMESTAMP = new Date();
83 private static final String ORDER = "DESC";
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 }).hasMessageMatching("^parameters is marked .*on.*ull 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 }).hasMessageMatching(FILTER_IS_NULL);
149 assertThatThrownBy(() -> {
150 databaseProvider.getFilteredPolicyTypeList(null);
151 }).hasMessageMatching(FILTER_IS_NULL);
153 assertThatThrownBy(() -> {
154 databaseProvider.createPolicyTypes(null);
155 }).hasMessageMatching(TEMPLATE_IS_NULL);
157 assertThatThrownBy(() -> {
158 databaseProvider.updatePolicyTypes(null);
159 }).hasMessageMatching(TEMPLATE_IS_NULL);
161 assertThatThrownBy(() -> {
162 databaseProvider.deletePolicyType(null, null);
163 }).hasMessageMatching(NAME_IS_NULL);
165 assertThatThrownBy(() -> {
166 databaseProvider.deletePolicyType("aaa", null);
167 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
169 assertThatThrownBy(() -> {
170 databaseProvider.deletePolicyType(null, "aaa");
171 }).hasMessageMatching(NAME_IS_NULL);
173 assertThatThrownBy(() -> {
174 databaseProvider.getFilteredPolicies(null);
175 }).hasMessageMatching(FILTER_IS_NULL);
177 assertThatThrownBy(() -> {
178 databaseProvider.getFilteredPolicyList(null);
179 }).hasMessageMatching(FILTER_IS_NULL);
181 assertThatThrownBy(() -> {
182 databaseProvider.createPolicies(null);
183 }).hasMessageMatching(TEMPLATE_IS_NULL);
185 assertThatThrownBy(() -> {
186 databaseProvider.updatePolicies(null);
187 }).hasMessageMatching(TEMPLATE_IS_NULL);
189 assertThatThrownBy(() -> {
190 databaseProvider.deletePolicy(null, null);
191 }).hasMessageMatching(NAME_IS_NULL);
193 assertThatThrownBy(() -> {
194 databaseProvider.deletePolicy(null, "aaa");
195 }).hasMessageMatching(NAME_IS_NULL);
197 assertThatThrownBy(() -> {
198 databaseProvider.deletePolicy("aaa", null);
199 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
201 assertThatThrownBy(() -> {
202 databaseProvider.getOperationalPolicy(null, null);
203 }).hasMessageMatching(POLICY_ID_IS_NULL);
205 assertThatThrownBy(() -> {
206 databaseProvider.getOperationalPolicy(null, "");
207 }).hasMessageMatching(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 }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
217 assertThatThrownBy(() -> {
218 databaseProvider.updateOperationalPolicy(null);
219 }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
221 assertThatThrownBy(() -> {
222 databaseProvider.deleteOperationalPolicy(null, null);
223 }).hasMessageMatching(POLICY_ID_IS_NULL);
225 assertThatThrownBy(() -> {
226 databaseProvider.deleteOperationalPolicy(null, "");
227 }).hasMessageMatching(POLICY_ID_IS_NULL);
229 assertThatThrownBy(() -> {
230 databaseProvider.deleteOperationalPolicy("", null);
231 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
233 assertThatThrownBy(() -> {
234 databaseProvider.getGuardPolicy(null, null);
235 }).hasMessageMatching(POLICY_ID_IS_NULL);
237 assertThatThrownBy(() -> {
238 databaseProvider.getGuardPolicy(null, "");
239 }).hasMessageMatching(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 }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$");
249 assertThatThrownBy(() -> {
250 databaseProvider.updateGuardPolicy(null);
251 }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$");
253 assertThatThrownBy(() -> {
254 databaseProvider.deleteGuardPolicy(null, null);
255 }).hasMessageMatching(POLICY_ID_IS_NULL);
257 assertThatThrownBy(() -> {
258 databaseProvider.deleteGuardPolicy(null, "");
259 }).hasMessageMatching(POLICY_ID_IS_NULL);
261 assertThatThrownBy(() -> {
262 databaseProvider.deleteGuardPolicy("", null);
263 }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
265 assertThatThrownBy(() -> {
266 databaseProvider.getFilteredPdpGroups(null);
267 }).hasMessageMatching(FILTER_IS_NULL);
269 assertThatThrownBy(() -> {
270 databaseProvider.createPdpGroups(null);
271 }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
273 assertThatThrownBy(() -> {
274 databaseProvider.updatePdpGroups(null);
275 }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
277 assertThatThrownBy(() -> {
278 databaseProvider.updatePdpSubGroup(null, null);
279 }).hasMessageMatching(GROUP_IS_NULL);
281 assertThatThrownBy(() -> {
282 databaseProvider.updatePdpSubGroup(null, new PdpSubGroup());
283 }).hasMessageMatching(GROUP_IS_NULL);
285 assertThatThrownBy(() -> {
286 databaseProvider.updatePdpSubGroup(NAME, null);
287 }).hasMessageMatching(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 }).hasMessageMatching(GROUP_IS_NULL);
297 assertThatThrownBy(() -> {
298 databaseProvider.updatePdp(null, null, new Pdp());
299 }).hasMessageMatching(GROUP_IS_NULL);
301 assertThatThrownBy(() -> {
302 databaseProvider.updatePdp(null, "sub", null);
303 }).hasMessageMatching(GROUP_IS_NULL);
305 assertThatThrownBy(() -> {
306 databaseProvider.updatePdp(null, "sub", new Pdp());
307 }).hasMessageMatching(GROUP_IS_NULL);
309 assertThatThrownBy(() -> {
310 databaseProvider.updatePdp(NAME, null, null);
311 }).hasMessageMatching(SUBGROUP_IS_NULL);
313 assertThatThrownBy(() -> {
314 databaseProvider.updatePdp(NAME, null, new Pdp());
315 }).hasMessageMatching(SUBGROUP_IS_NULL);
317 assertThatThrownBy(() -> {
318 databaseProvider.updatePdp(NAME, "sub", null);
319 }).hasMessageMatching("^pdp is marked .*on.*ull 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 }).hasMessageMatching(NAME_IS_NULL);
329 assertThatThrownBy(() -> {
330 databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0);
331 }).hasMessageMatching(GROUP_IS_NULL);
333 assertThatThrownBy(() -> {
334 databaseProvider.createPdpStatistics(null);
335 }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
337 assertThatThrownBy(() -> {
338 databaseProvider.updatePdpStatistics(null);
339 }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
341 assertThatThrownBy(() -> {
342 databaseProvider.deletePdpStatistics(null, TIMESTAMP);
343 }).hasMessageMatching(NAME_IS_NULL);
349 public void testProviderMethodsNotInit() throws Exception {
350 PolicyModelsProvider databaseProvider =
351 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
353 databaseProvider.close();
355 assertThatThrownBy(() -> {
356 databaseProvider.getPolicyTypes(NAME, "version");
357 }).hasMessage("policy models provider is not initilaized");
361 public void testProviderMethods() {
362 try (PolicyModelsProvider databaseProvider =
363 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
365 assertTrue(databaseProvider.getPolicyTypes(NAME, VERSION_100).getPolicyTypes().isEmpty());
366 assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty());
367 assertEquals(0, databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())
368 .getPolicyTypes().size());
369 assertEquals(0, databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).size());
371 assertThatThrownBy(() -> {
372 databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
373 }).hasMessage("no policy types specified on service template");
375 assertThatThrownBy(() -> {
376 databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
377 }).hasMessage("no policy types specified on service template");
379 assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
381 assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
384 databaseProvider.getPolicies(NAME, VERSION_100).getToscaTopologyTemplate().getPolicies().isEmpty());
385 assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty());
386 assertEquals(0, databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())
387 .getToscaTopologyTemplate().getPolicies().size());
388 assertEquals(0, databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).size());
390 assertThatThrownBy(() -> {
391 databaseProvider.createPolicies(new ToscaServiceTemplate());
392 }).hasMessage("topology template not specified on service template");
394 assertThatThrownBy(() -> {
395 databaseProvider.updatePolicies(new ToscaServiceTemplate());
396 }).hasMessage("topology template not specified on service template");
398 assertTrue(databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate().getPolicies()
401 assertThatThrownBy(() -> {
402 databaseProvider.getOperationalPolicy(POLICY_ID, null);
403 }).hasMessage("no policy found for policy: policy_id:null");
405 assertThatThrownBy(() -> {
406 databaseProvider.getOperationalPolicy(POLICY_ID, "10");
407 }).hasMessage("no policy found for policy: policy_id:10");
409 assertThatThrownBy(() -> {
410 databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
411 }).hasMessageMatching(NAME_IS_NULL);
413 assertThatThrownBy(() -> {
414 databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
415 }).hasMessageMatching(NAME_IS_NULL);
417 assertThatThrownBy(() -> {
418 databaseProvider.deleteOperationalPolicy(POLICY_ID, "55");
419 }).hasMessage("no policy found for policy: policy_id:55");
421 assertThatThrownBy(() -> {
422 databaseProvider.getGuardPolicy(POLICY_ID, null);
423 }).hasMessage("no policy found for policy: policy_id:null");
425 assertThatThrownBy(() -> {
426 databaseProvider.getGuardPolicy(POLICY_ID, "6");
427 }).hasMessage("no policy found for policy: policy_id:6");
429 assertThatThrownBy(() -> {
430 databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput());
431 }).hasMessage("policy type for guard policy \"null\" unknown");
433 assertThatThrownBy(() -> {
434 databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput());
435 }).hasMessage("policy type for guard policy \"null\" unknown");
437 assertThatThrownBy(() -> {
438 databaseProvider.deleteGuardPolicy(POLICY_ID, "33");
439 }).hasMessage("no policy found for policy: policy_id:33");
441 assertEquals(0, databaseProvider.getPdpGroups(NAME).size());
442 assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
444 assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
445 assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
447 PdpGroup pdpGroup = new PdpGroup();
448 pdpGroup.setName(GROUP);
449 pdpGroup.setVersion("1.2.3");
450 pdpGroup.setPdpGroupState(PdpState.ACTIVE);
451 pdpGroup.setPdpSubgroups(new ArrayList<>());
452 List<PdpGroup> groupList = new ArrayList<>();
453 groupList.add(pdpGroup);
455 PdpSubGroup pdpSubGroup = new PdpSubGroup();
456 pdpSubGroup.setPdpType("type");
457 pdpSubGroup.setDesiredInstanceCount(123);
458 pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
459 pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9"));
460 pdpGroup.getPdpSubgroups().add(pdpSubGroup);
463 pdp.setInstanceId("type-0");
464 pdp.setMessage("Hello");
465 pdp.setPdpState(PdpState.ACTIVE);
466 pdp.setHealthy(PdpHealthStatus.UNKNOWN);
467 pdpSubGroup.setPdpInstances(new ArrayList<>());
468 pdpSubGroup.getPdpInstances().add(pdp);
470 PdpStatistics pdpStatistics = new PdpStatistics();
471 pdpStatistics.setPdpInstanceId(NAME);
472 pdpStatistics.setTimeStamp(new Date());
473 pdpStatistics.setPdpGroupName(GROUP);
474 pdpStatistics.setPdpSubGroupName("type");
475 ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>();
476 statisticsArrayList.add(pdpStatistics);
478 assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
479 .getDesiredInstanceCount());
480 assertEquals(1, databaseProvider.getPdpGroups(GROUP).size());
482 pdpSubGroup.setDesiredInstanceCount(234);
483 databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup);
485 databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
487 assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
488 .get(0).getMessage());
489 pdp.setMessage("Howdy");
490 databaseProvider.updatePdp(GROUP, "type", pdp);
491 assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
492 .get(0).getMessage());
494 assertThatThrownBy(() -> {
495 databaseProvider.deletePdpGroup(NAME);
496 }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
498 assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName());
500 assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
501 assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size());
502 assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size());
504 assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId());
505 assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0)
506 .getPdpInstanceId());
508 databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size());
509 assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0)
510 .get(0).getPdpInstanceId());
511 assertEquals(0, databaseProvider
512 .getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size());
514 assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0)
515 .getPdpInstanceId());
516 assertEquals(0, databaseProvider
517 .getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size());
519 assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0)
520 .get(0).getPdpInstanceId());
521 assertEquals(0, databaseProvider
522 .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0).size());
524 assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1)
525 .get(0).getPdpInstanceId());
526 assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5)
527 .get(0).getPdpInstanceId());
528 assertEquals(0, databaseProvider
529 .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5).size());
531 assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId());
532 assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
533 } catch (Exception exc) {
534 LOGGER.warn("test should not throw an exception", exc);
535 fail("test should not throw an exception");