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