Use new addTopic() method in models
[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  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.models.sim.pdp;
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 org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.services.Registry;
35 import org.onap.policy.models.pdp.concepts.PdpStatus;
36 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
37 import org.onap.policy.models.sim.pdp.parameters.CommonTestData;
38 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
39 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
40
41 /**
42  * Class to perform unit test of {@link PdpSimulatorActivator}}.
43  *
44  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
45  */
46 public class TestPdpSimulatorActivator {
47
48     private PdpSimulatorActivator activator;
49
50     /**
51      * Initializes an activator.
52      *
53      * @throws Exception if an error occurs
54      */
55     @Before
56     public void setUp() throws Exception {
57         Registry.newRegistry();
58         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
59         final PdpSimulatorCommandLineArguments arguments =
60                 new PdpSimulatorCommandLineArguments(pdpSimulatorConfigParameters);
61         final PdpSimulatorParameterGroup parGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
62
63         activator = new PdpSimulatorActivator(parGroup);
64     }
65
66     /**
67      * Method for cleanup after each test.
68      *
69      * @throws Exception if an error occurs
70      */
71     @After
72     public void teardown() throws Exception {
73         if (activator != null && activator.isAlive()) {
74             activator.terminate();
75         }
76     }
77
78     @Test
79     public void testPdpSimulatorActivator() throws PdpSimulatorException {
80         assertFalse(activator.isAlive());
81         activator.initialize();
82         assertTrue(activator.isAlive());
83         assertTrue(activator.getParameterGroup().isValid());
84         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, activator.getParameterGroup().getName());
85
86         // ensure items were added to the registry
87         assertNotNull(Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class));
88
89         // repeat - should throw an exception
90         assertThatIllegalStateException().isThrownBy(() -> activator.initialize());
91         assertTrue(activator.isAlive());
92         assertTrue(activator.getParameterGroup().isValid());
93     }
94
95     @Test
96     public void testTerminate() throws Exception {
97         activator.initialize();
98         activator.terminate();
99         assertFalse(activator.isAlive());
100
101         // ensure items have been removed from the registry
102         assertNull(Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null));
103
104         // repeat - should throw an exception
105         assertThatIllegalStateException().isThrownBy(() -> activator.terminate());
106         assertFalse(activator.isAlive());
107     }
108 }