Remove topic.properties and incorporate into overall config file for pdp-simulator
[policy/models.git] / models-sim / policy-models-sim-pdp / src / test / java / org / onap / policy / models / sim / pdp / TestPdpSimulatorActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.sim.pdp;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.FileInputStream;
31 import java.util.Properties;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.utils.ParameterUtils;
37 import org.onap.policy.common.utils.services.Registry;
38 import org.onap.policy.models.pdp.concepts.PdpStatus;
39 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
40 import org.onap.policy.models.sim.pdp.parameters.CommonTestData;
41 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
42 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
43
44 /**
45  * Class to perform unit test of {@link PdpSimulatorActivator}}.
46  *
47  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
48  */
49 public class TestPdpSimulatorActivator {
50
51     private PdpSimulatorActivator 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         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
62         final PdpSimulatorCommandLineArguments arguments =
63                 new PdpSimulatorCommandLineArguments(pdpSimulatorConfigParameters);
64         final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
65
66         final Properties props = ParameterUtils.getTopicProperties(parGroup.getTopicParameterGroup());
67         activator = new PdpSimulatorActivator(parGroup, props);
68     }
69
70     /**
71      * Method for cleanup after each test.
72      *
73      * @throws Exception if an error occurs
74      */
75     @After
76     public void teardown() throws Exception {
77         if (activator != null && activator.isAlive()) {
78             activator.terminate();
79         }
80     }
81
82     @Test
83     public void testPdpSimulatorActivator() throws PdpSimulatorException {
84         assertFalse(activator.isAlive());
85         activator.initialize();
86         assertTrue(activator.isAlive());
87         assertTrue(activator.getParameterGroup().isValid());
88         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, activator.getParameterGroup().getName());
89
90         // ensure items were added to the registry
91         assertNotNull(Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class));
92
93         // repeat - should throw an exception
94         assertThatIllegalStateException().isThrownBy(() -> activator.initialize());
95         assertTrue(activator.isAlive());
96         assertTrue(activator.getParameterGroup().isValid());
97     }
98
99     @Test
100     public void testTerminate() throws Exception {
101         activator.initialize();
102         activator.terminate();
103         assertFalse(activator.isAlive());
104
105         // ensure items have been removed from the registry
106         assertNull(Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null));
107
108         // repeat - should throw an exception
109         assertThatIllegalStateException().isThrownBy(() -> activator.terminate());
110         assertFalse(activator.isAlive());
111     }
112 }