7e7dee22f02895ffacf5e2e7c5bda4cec3077f5a
[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 java.util.Properties;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.utils.ParameterUtils;
35 import org.onap.policy.pdpx.main.CommonRest;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
37 import org.onap.policy.pdpx.main.parameters.CommonTestData;
38 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
39 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
40
41
42 /**
43  * Class to perform unit test of XacmlPdpActivator.
44  *
45  */
46 public class TestXacmlPdpActivator extends CommonRest {
47     private static XacmlPdpParameterGroup parGroup;
48     private static Properties props;
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         props = ParameterUtils.getTopicProperties(parGroup.getTopicParameterGroup());
64
65         // don't want the common "main" running
66         CommonRest.stopMain();
67     }
68
69     /**
70      * Creates the activator.
71      */
72     @Before
73     public void setUp() {
74         activator = new XacmlPdpActivator(parGroup, props);
75     }
76
77     @Test
78     public void testXacmlPdpActivator() throws Exception {
79         assertFalse(activator.isAlive());
80         activator.start();
81         assertTrue(activator.isAlive());
82         assertTrue(activator.getParameterGroup().isValid());
83         assertEquals(CommonTestData.PDPX_GROUP_NAME, activator.getParameterGroup().getName());
84
85     }
86
87     @Test
88     public void testGetCurrent_testSetCurrent() {
89         XacmlPdpActivator.setCurrent(activator);
90         assertSame(activator, XacmlPdpActivator.getCurrent());
91     }
92
93     @Test
94     public void testTerminate() throws Exception {
95         activator.start();
96         activator.stop();
97         assertFalse(activator.isAlive());
98     }
99
100     /**
101      * Teardown tests.
102      * @throws PolicyXacmlPdpException on termination errors
103      */
104     @After
105     public void teardown() throws PolicyXacmlPdpException {
106         if (activator != null && activator.isAlive()) {
107             activator.stop();
108         }
109     }
110 }