Refactor REST server tests
[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.assertSame;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.pap.main.PolicyPapException;
34 import org.onap.policy.pap.main.parameters.CommonTestData;
35 import org.onap.policy.pap.main.parameters.PapParameterGroup;
36 import org.onap.policy.pap.main.parameters.PapParameterHandler;
37
38
39 /**
40  * Class to perform unit test of {@link PapActivator}}.
41  *
42  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43  */
44 public class TestPapActivator {
45
46     private PapActivator activator;
47
48     /**
49      * Initializes an activator.
50      * @throws Exception if an error occurs
51      */
52     @Before
53     public void setUp() throws Exception {
54         final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
55         final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters);
56         final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
57         activator = new PapActivator(parGroup);
58     }
59
60     /**
61      * Method for cleanup after each test.
62      * @throws Exception if an error occurs
63      */
64     @After
65     public void teardown() throws Exception {
66         if (activator != null && activator.isAlive()) {
67             activator.terminate();
68         }
69     }
70
71     @Test
72     public void testPapActivator() throws PolicyPapException {
73         assertFalse(activator.isAlive());
74         activator.initialize();
75         assertTrue(activator.isAlive());
76         assertTrue(activator.getParameterGroup().isValid());
77         assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName());
78
79         // repeat - should throw an exception
80         assertThatIllegalStateException().isThrownBy(() -> activator.initialize());
81         assertTrue(activator.isAlive());
82         assertTrue(activator.getParameterGroup().isValid());
83     }
84
85     @Test
86     public void testGetCurrent_testSetCurrent() {
87         PapActivator.setCurrent(activator);
88         assertSame(activator, PapActivator.getCurrent());
89     }
90
91     @Test
92     public void testTerminate() throws Exception {
93         activator.initialize();
94         activator.terminate();
95         assertFalse(activator.isAlive());
96
97         // repeat - should throw an exception
98         assertThatIllegalStateException().isThrownBy(() -> activator.terminate());
99         assertFalse(activator.isAlive());
100     }
101 }