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