2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 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.assertThatCode;
26 import java.time.Instant;
27 import java.util.Arrays;
28 import org.junit.Test;
29 import org.onap.policy.models.base.PfModelException;
30 import org.onap.policy.models.pdp.concepts.PdpStatistics;
31 import org.onap.policy.models.provider.PolicyModelsProvider;
32 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
36 * Test persistence of PDP statistics to and from the database.
38 public class PolicyStatisticsPersistenceTest {
41 public void testPdpStatiscticsPersistence() throws PfModelException {
42 // Try the test on three providers
43 for (int i = 0; i < 3; i++) {
44 try (PolicyModelsProvider databaseProvider = setupProvider()) {
45 testPdpStatiscticsPersistenceOneProvider(databaseProvider);
50 public void testPdpStatiscticsPersistenceOneProvider(PolicyModelsProvider databaseProvider) {
51 PdpStatistics pdpStatistics = new PdpStatistics();
52 pdpStatistics.setPdpInstanceId("TheInstance");
53 pdpStatistics.setTimeStamp(Instant.now());
55 // Try creating three identical statistics instances
56 for (int i = 0; i < 3; i++) {
57 assertThatCode(() -> databaseProvider.createPdpStatistics(Arrays.asList(pdpStatistics)))
58 .doesNotThrowAnyException();
61 // Try creating three statistics instances with timestams incremented
62 for (int i = 0; i < 3; i++) {
63 pdpStatistics.setTimeStamp(pdpStatistics.getTimeStamp().plusSeconds(1));
65 assertThatCode(() -> databaseProvider.createPdpStatistics(Arrays.asList(pdpStatistics)))
66 .doesNotThrowAnyException();
70 private PolicyModelsProvider setupProvider() throws PfModelException {
71 PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
73 if (System.getProperty("USE-MARIADB") != null) {
74 parameters.setDatabaseDriver("org.mariadb.jdbc.Driver");
75 parameters.setDatabaseUrl("jdbc:mariadb://localhost:3306/policy");
77 parameters.setDatabaseDriver("org.h2.Driver");
78 parameters.setDatabaseUrl("jdbc:h2:mem:PolicyStatisticsPersistenceTest");
81 parameters.setDatabaseUser("policy");
82 parameters.setDatabasePassword("P01icY");
83 parameters.setPersistenceUnit("ToscaConceptTest");
85 return new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);