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