Changes to PAP infrastructure to support PDP
[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 AT&T Intellectual Property.
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.pap.main.startstop;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.FileInputStream;
31 import java.util.Properties;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.services.Registry;
36 import org.onap.policy.pap.main.PapConstants;
37 import org.onap.policy.pap.main.PolicyPapException;
38 import org.onap.policy.pap.main.parameters.CommonTestData;
39 import org.onap.policy.pap.main.parameters.PapParameterGroup;
40 import org.onap.policy.pap.main.parameters.PapParameterHandler;
41 import org.onap.policy.pap.main.rest.PapStatisticsManager;
42
43
44 /**
45  * Class to perform unit test of {@link PapActivator}}.
46  *
47  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48  */
49 public class TestPapActivator {
50
51     private PapActivator activator;
52
53     /**
54      * Initializes an activator.
55      *
56      * @throws Exception if an error occurs
57      */
58     @Before
59     public void setUp() throws Exception {
60         Registry.newRegistry();
61
62         final String[] papConfigParameters =
63             {"-c", "parameters/PapConfigParameters.json", "-p", "parameters/topic.properties"};
64         final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters);
65         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
66
67         Properties props = new Properties();
68         String propFile = arguments.getFullPropertyFilePath();
69         try (FileInputStream stream = new FileInputStream(propFile)) {
70             props.load(stream);
71         }
72
73         activator = new PapActivator(parGroup, props);
74     }
75
76     /**
77      * Method for cleanup after each test.
78      * @throws Exception if an error occurs
79      */
80     @After
81     public void teardown() throws Exception {
82         if (activator != null && activator.isAlive()) {
83             activator.stop();
84         }
85     }
86
87     @Test
88     public void testPapActivator() throws PolicyPapException {
89         assertFalse(activator.isAlive());
90         activator.start();
91         assertTrue(activator.isAlive());
92         assertTrue(activator.getParameterGroup().isValid());
93         assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName());
94
95         // ensure items were added to the registry
96         assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class));
97         assertNotNull(Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class));
98
99         // repeat - should throw an exception
100         assertThatIllegalStateException().isThrownBy(() -> activator.start());
101         assertTrue(activator.isAlive());
102         assertTrue(activator.getParameterGroup().isValid());
103     }
104
105     @Test
106     public void testTerminate() throws Exception {
107         activator.start();
108         activator.stop();
109         assertFalse(activator.isAlive());
110
111         // repeat - should throw an exception
112         assertThatIllegalStateException().isThrownBy(() -> activator.stop());
113         assertFalse(activator.isAlive());
114     }
115 }