Skip policy preload if DB already contains data
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / startstop / ApiDatabaseInitializerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.api.main.startstop;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.api.main.exception.PolicyApiException;
31 import org.onap.policy.api.main.parameters.ApiParameterGroup;
32 import org.onap.policy.api.main.parameters.CommonTestData;
33 import org.onap.policy.common.parameters.GroupValidationResult;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
37
38 public class ApiDatabaseInitializerTest {
39     private static final String PARAM_FILE = "src/test/resources/parameters/ApiConfigParameters_Https.json";
40     private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
41     private static ApiParameterGroup params;
42     private static PolicyModelsProvider provider;
43
44     /**
45      * Creates the DB and keeps it open.
46      */
47     @BeforeClass
48     public static void setUpBeforeClass() throws Exception {
49         COMMON_TEST_DATA.makeParameters(PARAM_FILE, "src/test/resources/parameters/ApiConfigParametersXXX.json", 6969);
50
51         params = new StandardCoder().decode(new File(PARAM_FILE), ApiParameterGroup.class);
52         GroupValidationResult result = params.validate();
53         if (!result.isValid()) {
54             fail(result.getResult());
55         }
56
57         // keep the DB open until the test completes
58         provider = new PolicyModelsProviderFactory().createPolicyModelsProvider(params.getDatabaseProviderParameters());
59     }
60
61     @AfterClass
62     public static void tearDownAfterClass() throws Exception {
63         provider.close();
64     }
65
66     @Test
67     public void testInitializeApiDatabase() throws PolicyApiException {
68         ApiDatabaseInitializer adi = new ApiDatabaseInitializer();
69         assertThatCode(() -> adi.initializeApiDatabase(params)).doesNotThrowAnyException();
70
71         // invoke it again - should still be OK
72         assertThatCode(() -> adi.initializeApiDatabase(params)).doesNotThrowAnyException();
73     }
74 }