Changed Xacml-pdp to report pdp group defined in XacmlPdpParameters config file
[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
39
40 /**
41  * Class to perform unit test of XacmlPdpActivator.
42  *
43  */
44 public class TestXacmlPdpActivator extends CommonRest {
45     private static XacmlPdpParameterGroup parGroup;
46
47     private XacmlPdpActivator activator = null;
48
49     /**
50      * Loads properties.
51      */
52     @BeforeClass
53     public static void setUpBeforeClass() throws Exception {
54         CommonRest.setUpBeforeClass();
55
56         final String[] xacmlPdpConfigParameters = {"-c", CommonRest.CONFIG_FILE};
57         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(xacmlPdpConfigParameters);
58         parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
59
60         // don't want the common "main" running
61         CommonRest.stopMain();
62     }
63
64     /**
65      * Creates the activator.
66      */
67     @Override
68     @Before
69     public void setUp() {
70         activator = new XacmlPdpActivator(parGroup);
71     }
72
73     @Test
74     public void testXacmlPdpActivator() throws Exception {
75         assertFalse(activator.isAlive());
76         assertFalse(activator.isXacmlRestControllerAlive());
77         activator.start();
78         assertTrue(activator.isAlive());
79
80         // XacmlPdp starts in PASSIVE state so the rest controller should not be alive
81         assertFalse(activator.isXacmlRestControllerAlive());
82         assertTrue(activator.getParameterGroup().isValid());
83         assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, activator.getParameterGroup().getName());
84         assertEquals(CommonTestData.PDPX_GROUP, activator.getParameterGroup().getPdpGroup());
85
86         activator.startXacmlRestController();
87         assertTrue(activator.isXacmlRestControllerAlive());
88
89         activator.stopXacmlRestController();
90         assertFalse(activator.isXacmlRestControllerAlive());
91     }
92
93     @Test
94     public void testGetCurrent_testSetCurrent() {
95         XacmlPdpActivator.setCurrent(activator);
96         assertSame(activator, XacmlPdpActivator.getCurrent());
97     }
98
99     @Test
100     public void testTerminate() throws Exception {
101         activator.start();
102         activator.stop();
103         assertFalse(activator.isAlive());
104     }
105
106     /**
107      * Teardown tests.
108      * @throws PolicyXacmlPdpException on termination errors
109      */
110     @After
111     public void teardown() throws PolicyXacmlPdpException {
112         if (activator != null && activator.isAlive()) {
113             activator.stop();
114         }
115     }
116 }