2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.startstop;
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;
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.PdpModifyRequestMap;
45 import org.onap.policy.pap.main.notification.PolicyNotifier;
46 import org.onap.policy.pap.main.parameters.CommonTestData;
47 import org.onap.policy.pap.main.parameters.PapParameterGroup;
48 import org.onap.policy.pap.main.rest.PapStatisticsManager;
52 * Class to perform unit test of {@link PapActivator}}.
54 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
56 public class TestPapActivator {
57 private static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
59 private static int port;
61 private PapActivator activator;
64 * Allocates a new DB name, server port, and creates a config file.
67 public static void setUpBeforeClass() throws Exception {
68 CommonTestData.newDb();
72 * Initializes an activator.
74 * @throws Exception if an error occurs
77 public void setUp() throws Exception {
78 Registry.newRegistry();
79 HttpServletServerFactoryInstance.getServerFactory().destroy();
81 port = NetworkUtil.allocPort();
83 String json = new CommonTestData().getPapParameterGroupAsString(port);
85 File file = new File(CONFIG_FILE);
88 try (FileOutputStream output = new FileOutputStream(file)) {
89 output.write(json.getBytes(StandardCharsets.UTF_8));
92 final PapParameterGroup parGroup = new CommonTestData().getPapParameterGroup(6969);
94 activator = new PapActivator(parGroup);
98 * Method for cleanup after each test.
100 * @throws Exception if an error occurs
103 public void teardown() throws Exception {
104 if (activator != null && activator.isAlive()) {
110 public void testPapActivator() throws PolicyPapException {
111 assertFalse(activator.isAlive());
113 assertTrue(activator.isAlive());
114 assertTrue(activator.getParameterGroup().isValid());
115 assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName());
117 // ensure items were added to the registry
118 assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class));
119 assertNotNull(Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class));
120 assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class));
121 assertNotNull(Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class));
123 // repeat - should throw an exception
124 assertThatIllegalStateException().isThrownBy(() -> activator.start());
125 assertTrue(activator.isAlive());
126 assertTrue(activator.getParameterGroup().isValid());
130 public void testTerminate() throws Exception {
133 assertFalse(activator.isAlive());
135 // ensure items have been removed from the registry
136 assertNull(Registry.getOrDefault(PapConstants.REG_PDP_MODIFY_LOCK, Object.class, null));
137 assertNull(Registry.getOrDefault(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class, null));
138 assertNull(Registry.getOrDefault(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class, null));
139 assertNull(Registry.getOrDefault(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class, null));
141 // repeat - should throw an exception
142 assertThatIllegalStateException().isThrownBy(() -> activator.stop());
143 assertFalse(activator.isAlive());