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