Fix write failure on PDP statistics
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / PolicyStatisticsPersistenceTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24
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;
33
34 /**
35  * Test persistence of PDP statistics to and from the database.
36  */
37 public class PolicyStatisticsPersistenceTest {
38
39     @Test
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();
46         }
47     }
48
49     public void testPdpStatiscticsPersistenceOneProvider(PolicyModelsProvider databaseProvider) {
50         PdpStatistics pdpStatistics = new PdpStatistics();
51         pdpStatistics.setPdpInstanceId("TheInstance");
52         pdpStatistics.setTimeStamp(Instant.now());
53
54         // Try creating three identical statistics instances
55         for (int i = 0; i < 3; i++) {
56             assertThatCode(() -> databaseProvider.createPdpStatistics(Arrays.asList(pdpStatistics)))
57                 .doesNotThrowAnyException();
58         }
59
60         // Try creating three statistics instances with timestams incremented
61         for (int i = 0; i < 3; i++) {
62             pdpStatistics.setTimeStamp(pdpStatistics.getTimeStamp().plusSeconds(1));
63
64             assertThatCode(() -> databaseProvider.createPdpStatistics(Arrays.asList(pdpStatistics)))
65                 .doesNotThrowAnyException();
66         }
67     }
68
69     private PolicyModelsProvider setupProvider() throws PfModelException {
70         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
71
72         if (System.getProperty("USE-MARIADB") != null) {
73             parameters.setDatabaseDriver("org.mariadb.jdbc.Driver");
74             parameters.setDatabaseUrl("jdbc:mariadb://localhost:3306/policy");
75         } else {
76             parameters.setDatabaseDriver("org.h2.Driver");
77             parameters.setDatabaseUrl("jdbc:h2:mem:testdb");
78         }
79
80         parameters.setDatabaseUser("policy");
81         parameters.setDatabasePassword("P01icY");
82         parameters.setPersistenceUnit("ToscaConceptTest");
83
84         return new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
85     }
86 }