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