Added support for 'Active' and 'Passive' states.
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / startstop / TestXacmlPdpActivator.java
index 51f737a..5223bcd 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,15 +26,11 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.net.UnknownHostException;
-import java.util.Properties;
 import org.junit.After;
+import org.junit.Before;
 import org.junit.BeforeClass;
-
 import org.junit.Test;
-import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
+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;
@@ -44,48 +41,63 @@ 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 XacmlPdpActivator activator = null;
 
     /**
-     * Setup the tests.
+     * Loads properties.
      */
     @BeforeClass
-    public static void setup() throws Exception {
-        final String[] xacmlPdpConfigParameters =
-            {"-c", "parameters/XacmlPdpConfigParameters.json", "-p", "parameters/topic.properties"};
+    public static void setUpBeforeClass() throws Exception {
+        CommonRest.setUpBeforeClass();
+
+        final String[] xacmlPdpConfigParameters = {"-c", CommonRest.CONFIG_FILE};
         final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(xacmlPdpConfigParameters);
-        final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
+        parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
 
-        Properties 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, props);
+    /**
+     * Creates the activator.
+     */
+    @Override
+    @Before
+    public void setUp() {
+        activator = new XacmlPdpActivator(parGroup);
     }
 
     @Test
-    public void testXacmlPdpActivator() throws PolicyXacmlPdpException, TopicSinkClientException, UnknownHostException {
+    public void testXacmlPdpActivator() throws Exception {
         assertFalse(activator.isAlive());
+        assertFalse(activator.isXacmlRestControllerAlive());
         activator.start();
         assertTrue(activator.isAlive());
+
+        // XacmlPdp starts in PASSIVE state so the rest controller should not be alive
+        assertFalse(activator.isXacmlRestControllerAlive());
         assertTrue(activator.getParameterGroup().isValid());
         assertEquals(CommonTestData.PDPX_GROUP_NAME, activator.getParameterGroup().getName());
 
+        activator.startXacmlRestController();
+        assertTrue(activator.isXacmlRestControllerAlive());
+
+        activator.stopXacmlRestController();
+        assertFalse(activator.isXacmlRestControllerAlive());
     }
 
     @Test
     public void testGetCurrent_testSetCurrent() {
+        XacmlPdpActivator.setCurrent(activator);
         assertSame(activator, XacmlPdpActivator.getCurrent());
     }
 
     @Test
     public void testTerminate() throws Exception {
-        if (!activator.isAlive()) {
-            activator.start();
-        }
+        activator.start();
         activator.stop();
         assertFalse(activator.isAlive());
     }