Refactor xacml-pdp to remove various statics
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / startstop / TestXacmlPdpActivator.java
index 7a514f7..b60f458 100644 (file)
 package org.onap.policy.pdpx.main.startstop;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
+import java.io.FileInputStream;
+import java.util.Properties;
 import org.junit.After;
+import org.junit.Before;
 import org.junit.BeforeClass;
-
 import org.junit.Test;
+import org.onap.policy.pdpx.main.CommonRest;
 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
 import org.onap.policy.pdpx.main.parameters.CommonTestData;
 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
@@ -37,33 +42,73 @@ import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler;
  * Class to perform unit test of XacmlPdpActivator.
  *
  */
-public class TestXacmlPdpActivator {
-    private static XacmlPdpActivator activator = null;
+public class TestXacmlPdpActivator extends CommonRest {
+    private static XacmlPdpParameterGroup parGroup;
+    private static Properties props;
+
+    private XacmlPdpActivator activator = null;
 
     /**
-     * Setup the tests.
-     * @throws PolicyXacmlPdpException when Xacml PDP Exceptional condition occurs
+     * Loads properties.
      */
     @BeforeClass
-    public static void setup() throws PolicyXacmlPdpException {
-        final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
+    public static void setUpBeforeClass() throws Exception {
+        CommonRest.setUpBeforeClass();
 
+        final String[] xacmlPdpConfigParameters =
+            {"-c", CommonRest.CONFIG_FILE, "-p", "parameters/topic.properties"};
         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(xacmlPdpConfigParameters);
+        parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
 
-        final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
+        props = new Properties();
+        String propFile = arguments.getFullPropertyFilePath();
+        try (FileInputStream stream = new FileInputStream(propFile)) {
+            props.load(stream);
+        }
+
+        // don't want the common "main" running
+        CommonRest.stopMain();
+    }
 
-        activator = new XacmlPdpActivator(parGroup);
-        activator.initialize();
+    /**
+     * Creates the activator.
+     */
+    @Before
+    public void setUp() {
+        activator = new XacmlPdpActivator(parGroup, props);
     }
 
     @Test
-    public void testXacmlPdpActivator() throws PolicyXacmlPdpException {
+    public void testXacmlPdpActivator() throws Exception {
+        assertFalse(activator.isAlive());
+        activator.start();
+        assertTrue(activator.isAlive());
         assertTrue(activator.getParameterGroup().isValid());
         assertEquals(CommonTestData.PDPX_GROUP_NAME, activator.getParameterGroup().getName());
+
     }
 
+    @Test
+    public void testGetCurrent_testSetCurrent() {
+        XacmlPdpActivator.setCurrent(activator);
+        assertSame(activator, XacmlPdpActivator.getCurrent());
+    }
+
+    @Test
+    public void testTerminate() throws Exception {
+        activator.start();
+        activator.stop();
+        assertFalse(activator.isAlive());
+    }
+
+    /**
+     * Teardown tests.
+     * @throws PolicyXacmlPdpException on termination errors
+     */
     @After
     public void teardown() throws PolicyXacmlPdpException {
-        activator.terminate();
+        if (activator != null && activator.isAlive()) {
+            activator.stop();
+        }
     }
 }