c874761df883eb5c552748dd9f877492fe6a1b8d
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / startstop / TestXacmlPdpActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2019 Nordix Foundation.
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.pdpx.main.startstop;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.pdpx.main.CommonRest;
34 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
35 import org.onap.policy.pdpx.main.parameters.CommonTestData;
36 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
37 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
38 import org.powermock.reflect.Whitebox;
39
40
41 /**
42  * Class to perform unit test of XacmlPdpActivator.
43  *
44  */
45 public class TestXacmlPdpActivator extends CommonRest {
46     private static final String PROBE_FIELD_NAME = "probeHeartbeatTopicSec";
47
48     private static XacmlPdpParameterGroup parGroup;
49
50     private XacmlPdpActivator activator = null;
51
52     /**
53      * Loads properties.
54      */
55     @BeforeClass
56     public static void setUpBeforeClass() throws Exception {
57         CommonRest.setUpBeforeClass();
58
59         final String[] xacmlPdpConfigParameters = {"-c", CommonRest.CONFIG_FILE};
60         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(xacmlPdpConfigParameters);
61         parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
62
63         // don't want the common "main" running
64         CommonRest.stopMain();
65     }
66
67     /**
68      * Creates the activator.
69      */
70     @Override
71     @Before
72     public void setUp() {
73         Whitebox.setInternalState(parGroup, PROBE_FIELD_NAME, 4);
74         activator = new XacmlPdpActivator(parGroup);
75     }
76
77     @Test
78     public void testXacmlPdpActivator() throws Exception {
79         assertFalse(activator.isAlive());
80         assertFalse(activator.isXacmlRestControllerAlive());
81         activator.start();
82         assertTrue(activator.isAlive());
83
84         // XacmlPdp starts in PASSIVE state so the rest controller should not be alive
85         assertFalse(activator.isXacmlRestControllerAlive());
86         assertTrue(activator.getParameterGroup().isValid());
87         assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, activator.getParameterGroup().getName());
88         assertEquals(CommonTestData.PDPX_GROUP, activator.getParameterGroup().getPdpGroup());
89
90         activator.startXacmlRestController();
91         assertTrue(activator.isXacmlRestControllerAlive());
92
93         activator.stopXacmlRestController();
94         assertFalse(activator.isXacmlRestControllerAlive());
95     }
96
97     @Test
98     public void testXacmlPdpActivator_NoProbe() throws Exception {
99         Whitebox.setInternalState(parGroup, PROBE_FIELD_NAME, 0);
100         activator = new XacmlPdpActivator(parGroup);
101         activator.start();
102         assertTrue(activator.isAlive());
103     }
104
105     @Test
106     public void testGetCurrent_testSetCurrent() {
107         XacmlPdpActivator.setCurrent(activator);
108         assertSame(activator, XacmlPdpActivator.getCurrent());
109     }
110
111     @Test
112     public void testTerminate() throws Exception {
113         activator.start();
114         activator.stop();
115         assertFalse(activator.isAlive());
116     }
117
118     /**
119      * Teardown tests.
120      * @throws PolicyXacmlPdpException on termination errors
121      */
122     @After
123     public void teardown() throws PolicyXacmlPdpException {
124         if (activator != null && activator.isAlive()) {
125             activator.stop();
126         }
127     }
128 }