Move PAP database provider to spring boot default
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / startstop / TestPapActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021-2022 Bell Canada. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.startstop;
24
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.nio.charset.StandardCharsets;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
40 import org.onap.policy.common.utils.network.NetworkUtil;
41 import org.onap.policy.common.utils.services.Registry;
42 import org.onap.policy.pap.main.PapConstants;
43 import org.onap.policy.pap.main.PolicyPapException;
44 import org.onap.policy.pap.main.comm.PdpHeartbeatListener;
45 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
46 import org.onap.policy.pap.main.notification.PolicyNotifier;
47 import org.onap.policy.pap.main.parameters.CommonTestData;
48 import org.onap.policy.pap.main.parameters.PapParameterGroup;
49 import org.onap.policy.pap.main.rest.PapStatisticsManager;
50
51
52 /**
53  * Class to perform unit test of {@link PapActivator}}.
54  *
55  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
56  */
57 public class TestPapActivator {
58     private static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
59
60     private static int port;
61
62     private PapActivator activator;
63
64     /**
65      * Allocates a new DB name, server port, and creates a config file.
66      */
67     @BeforeClass
68     public static void setUpBeforeClass() throws Exception {
69         CommonTestData.newDb();
70     }
71
72     /**
73      * Initializes an activator.
74      *
75      * @throws Exception if an error occurs
76      */
77     @Before
78     public void setUp() throws Exception {
79         Registry.newRegistry();
80         HttpServletServerFactoryInstance.getServerFactory().destroy();
81
82         port = NetworkUtil.allocPort();
83
84         String json = new CommonTestData().getPapParameterGroupAsString(port);
85
86         File file = new File(CONFIG_FILE);
87         file.deleteOnExit();
88
89         try (FileOutputStream output = new FileOutputStream(file)) {
90             output.write(json.getBytes(StandardCharsets.UTF_8));
91         }
92
93         final PapParameterGroup parGroup = new CommonTestData().getPapParameterGroup(6969);
94
95         activator = new PapActivator(parGroup, new PolicyNotifier(null), new PdpHeartbeatListener(),
96             new PdpModifyRequestMap(null, null, null, null, null));
97
98     }
99
100     /**
101      * Method for cleanup after each test.
102      *
103      * @throws Exception if an error occurs
104      */
105     @After
106     public void teardown() throws Exception {
107         if (activator != null && activator.isAlive()) {
108             activator.stop();
109         }
110     }
111
112     @Test
113     public void testPapActivator() throws PolicyPapException {
114         assertFalse(activator.isAlive());
115         activator.start();
116         assertTrue(activator.isAlive());
117         assertTrue(activator.getParameterGroup().isValid());
118         assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName());
119
120         // ensure items were added to the registry
121         assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class));
122         assertNotNull(Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class));
123         assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class));
124
125         // repeat - should throw an exception
126         assertThatIllegalStateException().isThrownBy(() -> activator.start());
127         assertTrue(activator.isAlive());
128         assertTrue(activator.getParameterGroup().isValid());
129     }
130
131     @Test
132     public void testTerminate() throws Exception {
133         activator.start();
134         activator.stop();
135         assertFalse(activator.isAlive());
136
137         // ensure items have been removed from the registry
138         assertNull(Registry.getOrDefault(PapConstants.REG_PDP_MODIFY_LOCK, Object.class, null));
139         assertNull(Registry.getOrDefault(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class, null));
140         assertNull(Registry.getOrDefault(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class, null));
141
142         // repeat - should throw an exception
143         assertThatIllegalStateException().isThrownBy(() -> activator.stop());
144         assertFalse(activator.isAlive());
145     }
146 }