diff options
author | Jim Hahn <jrh3@att.com> | 2019-08-14 17:31:50 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-08-21 13:49:54 -0400 |
commit | dfe8fa8bc3e75c186589d21b619baa55454ef8a2 (patch) | |
tree | ca3c7dbbe53815afdacb1c97e62ee97ace3f867c /feature-active-standby-management/src/test/java | |
parent | 59e9b9a8b56d563814ef21a23716959f772f9194 (diff) |
Use pseudo time for junits
Modified feature-active-standby-management and feature-lifecycle
to be able to use TestTimeMulti, eliminating the need for sleep()
calls in the junit tests and speeding the tests up significantly.
Also modified feature-active-standby-management to use a memory DB
for its junit tests.
Change-Id: I6d7ae61bb73cbb19ff405b8d9fb660e92732edbb
Issue-ID: POLICY-1968
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-active-standby-management/src/test/java')
-rw-r--r-- | feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/AllSeemsWellTest.java (renamed from feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java) | 175 | ||||
-rw-r--r-- | feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/FactoryTest.java | 65 | ||||
-rw-r--r-- | feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java | 35 | ||||
-rw-r--r-- | feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/StandbyStateManagementTest.java (renamed from feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java) | 446 |
4 files changed, 367 insertions, 354 deletions
diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/AllSeemsWellTest.java index 5e9b360f..cb7e4c3f 100644 --- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/AllSeemsWellTest.java @@ -18,37 +18,38 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.drools.controller.test; +package org.onap.policy.drools.activestandby; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.util.Date; import java.util.Properties; -import java.util.function.Supplier; +import java.util.concurrent.Callable; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import org.apache.commons.lang3.time.DateUtils; -import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.policy.common.im.IntegrityMonitor; +import org.onap.policy.common.im.IntegrityMonitorException; +import org.onap.policy.common.im.MonitorTime; import org.onap.policy.common.im.StateManagement; -import org.onap.policy.drools.activestandby.ActiveStandbyFeatureApi; -import org.onap.policy.drools.activestandby.ActiveStandbyFeatureApiConstants; -import org.onap.policy.drools.activestandby.ActiveStandbyProperties; -import org.onap.policy.drools.activestandby.DroolsPdpEntity; -import org.onap.policy.drools.activestandby.DroolsPdpImpl; -import org.onap.policy.drools.activestandby.DroolsPdpsConnector; -import org.onap.policy.drools.activestandby.DroolsPdpsElectionHandler; -import org.onap.policy.drools.activestandby.JpaDroolsPdpsConnector; +import org.onap.policy.common.utils.time.CurrentTime; +import org.onap.policy.common.utils.time.PseudoTimer; +import org.onap.policy.common.utils.time.TestTimeMulti; import org.onap.policy.drools.core.PolicySessionFeatureApi; import org.onap.policy.drools.statemanagement.StateManagementFeatureApi; import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants; +import org.powermock.reflect.Whitebox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,6 +60,10 @@ import org.slf4j.LoggerFactory; public class AllSeemsWellTest { private static final Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class); + + private static final String MONITOR_FIELD_NAME = "instance"; + private static final String HANDLER_INSTANCE_FIELD = "electionHandler"; + /* * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion @@ -66,7 +71,7 @@ public class AllSeemsWellTest { * checking the results. Add a few seconds for safety */ - private static int SLEEP_TIME_SEC = 10; + private static final int SLEEP_TIME_SEC = 10; /* * DroolsPdpsElectionHandler runs every 1 seconds, so it takes 10 seconds for the @@ -74,7 +79,7 @@ public class AllSeemsWellTest { * the forward progress counter to go stale which should add an additional 5 sec. */ - private static int STALLED_ELECTION_HANDLER_SLEEP_TIME_SEC = 15; + private static final int STALLED_ELECTION_HANDLER_SLEEP_TIME_SEC = 15; /* * As soon as the election hander successfully runs, it will resume the forward progress. @@ -82,7 +87,7 @@ public class AllSeemsWellTest { * then fpc is written every 1 sec and then the fpc is checked every 2 sec, that could * take a total of 5 sec to recognize the resumption of progress. So, add 1 for safety. */ - private static int RESUMED_ELECTION_HANDLER_SLEEP_TIME_SEC = 6; + private static final int RESUMED_ELECTION_HANDLER_SLEEP_TIME_SEC = 6; private static EntityManagerFactory emfx; private static EntityManagerFactory emfd; @@ -90,7 +95,12 @@ public class AllSeemsWellTest { private static EntityManager emd; private static EntityTransaction et; - private final String configDir = "src/test/resources/asw"; + private static final String CONFIG_DIR = "src/test/resources/asw"; + + private static CurrentTime saveTime; + private static Factory saveFactory; + + private TestTimeMulti testTime; /* * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing. @@ -108,23 +118,16 @@ public class AllSeemsWellTest { logger.debug("setUpClass: userDir={}", userDir); System.setProperty("com.sun.management.jmxremote.port", "9980"); System.setProperty("com.sun.management.jmxremote.authenticate","false"); - } - @AfterClass - public static void tearDownClass() throws Exception { - } + DroolsPdpsElectionHandler.setIsUnitTesting(true); - /** - * Setup. - * - * @throws Exception exception - */ - @Before - public void setUp() throws Exception { - //Create teh data access for xaml db - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + saveTime = Whitebox.getInternalState(MonitorTime.class, MONITOR_FIELD_NAME); + saveFactory = Factory.getInstance(); + + resetInstanceObjects(); + + //Create the data access for xacml db + Properties stateManagementProperties = loadStateManagementProperties(); emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties); @@ -132,20 +135,59 @@ public class AllSeemsWellTest { emx = emfx.createEntityManager(); //Create the data access for drools db - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties); // Create an entity manager to use the DB emd = emfd.createEntityManager(); + } - DroolsPdpsElectionHandler.setIsUnitTesting(true); + /** + * Restores the system state. + * + * @throws IntegrityMonitorException if the integrity monitor cannot be shut down + */ + @AfterClass + public static void tearDownClass() throws IntegrityMonitorException { + resetInstanceObjects(); + + Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, saveTime); + Factory.setInstance(saveFactory); + + DroolsPdpsElectionHandler.setIsUnitTesting(false); + + emd.close(); + emfd.close(); + + emx.close(); + emfx.close(); } - @After - public void tearDown() throws Exception { + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + resetInstanceObjects(); + + // set test time + testTime = new TestTimeMulti(); + Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, testTime); + + Factory factory = mock(Factory.class); + when(factory.makeTimer()).thenAnswer(ans -> new PseudoTimer(testTime)); + Factory.setInstance(factory); + } + + private static void resetInstanceObjects() throws IntegrityMonitorException { + IntegrityMonitor.setUnitTesting(true); + IntegrityMonitor.deleteInstance(); + IntegrityMonitor.setUnitTesting(false); + + Whitebox.setInternalState(ActiveStandbyFeature.class, HANDLER_INSTANCE_FIELD, (Object) null); } @@ -188,19 +230,13 @@ public class AllSeemsWellTest { cleanXacmlDb(); cleanDroolsDb(); - logger.debug("testAllSeemsWell: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + Properties stateManagementProperties = loadStateManagementProperties(); logger.debug("testAllSeemsWell: Creating emfXacml"); final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( "junitXacmlPU", stateManagementProperties); - logger.debug("testAllSeemsWell: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); final String thisPdpId = activeStandbyProperties .getProperty(ActiveStandbyProperties.NODE_NAME); @@ -220,7 +256,7 @@ public class AllSeemsWellTest { */ logger.debug("testAllSeemsWell: Inserting PDP={} as not designated", thisPdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); + Date yesterday = DateUtils.addDays(testTime.getDate(), -1); DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); conn.insertPdp(pdp); DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); @@ -238,40 +274,27 @@ public class AllSeemsWellTest { StateManagementFeatureApi stateManagementFeatureApi = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); stateManagementFeatureApi = feature; logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}", stateManagementFeatureApi.getResourceName()); break; } - if (stateManagementFeatureApi == null) { - logger.error("testAllSeemsWell failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testAllSeemsWell failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(stateManagementFeatureApi); + final StateManagementFeatureApi smf = stateManagementFeatureApi; // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature // that has been created. ActiveStandbyFeatureApi activeStandbyFeature = null; for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); activeStandbyFeature = feature; logger.debug("testAllSeemsWell activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); break; } - if (activeStandbyFeature == null) { - logger.error("testAllSeemsWell failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testAllSeemsWell failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(activeStandbyFeature); logger.debug("testAllSeemsWell: Demoting PDP={}", thisPdpId); @@ -332,12 +355,24 @@ public class AllSeemsWellTest { } - private void waitForCondition(Supplier<Boolean> testCondition, int timeoutInSeconds) throws InterruptedException { - int maxIterations = timeoutInSeconds * 10; - int iterations = 0; - while (!testCondition.get() && iterations < maxIterations) { - iterations++; - Thread.sleep(100); + private static Properties loadStateManagementProperties() throws IOException { + try (FileInputStream input = new FileInputStream(CONFIG_DIR + "/feature-state-management.properties")) { + Properties props = new Properties(); + props.load(input); + return props; } } + + private static Properties loadActiveStandbyProperties() throws IOException { + try (FileInputStream input = + new FileInputStream(CONFIG_DIR + "/feature-active-standby-management.properties")) { + Properties props = new Properties(); + props.load(input); + return props; + } + } + + private void waitForCondition(Callable<Boolean> testCondition, int timeoutInSeconds) throws InterruptedException { + testTime.waitUntil(testCondition); + } } diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/FactoryTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/FactoryTest.java new file mode 100644 index 00000000..8a166954 --- /dev/null +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/FactoryTest.java @@ -0,0 +1,65 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class FactoryTest { + private static Factory saveFactory; + + private Factory factory; + + @BeforeClass + public static void setUpBeforeClass() { + saveFactory = Factory.getInstance(); + assertNotNull(saveFactory); + } + + @AfterClass + public static void tearDownAfterClass() { + Factory.setInstance(saveFactory); + } + + @Before + public void setUp() { + factory = new Factory(); + } + + @Test + public void testMakeTimer() { + assertNotNull(factory.makeTimer()); + } + + @Test + public void testGetInstance_testSetInstance() { + Factory.setInstance(factory); + assertSame(factory, Factory.getInstance()); + + // repeat - should be the same + assertSame(factory, Factory.getInstance()); + } +} diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java index 28d3b439..4a89d257 100644 --- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/PmStandbyStateChangeNotifierTest.java @@ -32,6 +32,7 @@ import static org.mockito.Mockito.when; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; +import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -47,6 +48,11 @@ public class PmStandbyStateChangeNotifierTest { private static final long UPDATE_INTERVAL = 100; private static final long WAIT_INTERVAL = 2 * UPDATE_INTERVAL + 2000; + private static Factory saveFactory; + + @Mock + private Factory factory; + @Mock private PolicyEngine engmgr; @@ -68,16 +74,25 @@ public class PmStandbyStateChangeNotifierTest { props.setProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL, String.valueOf(UPDATE_INTERVAL)); ActiveStandbyProperties.initProperties(props); + + saveFactory = Factory.getInstance(); + } + + @AfterClass + public static void tearDownAfterClass() { + Factory.setInstance(saveFactory); } /** * Initializes objects, including the notifier. */ - @Before public void setUp() { MockitoAnnotations.initMocks(this); + Factory.setInstance(factory); + when(factory.makeTimer()).thenReturn(timer); + notifier = new MyNotifier(); } @@ -167,12 +182,7 @@ public class PmStandbyStateChangeNotifierTest { @Test public void testHandleStateChange_ProvidingService_Ex() { - notifier = new MyNotifier() { - @Override - protected Timer makeTimer() { - throw new MyException(); - } - }; + when(factory.makeTimer()).thenThrow(new MyException()); when(mgmt.getStandbyStatus()).thenReturn(StateManagement.PROVIDING_SERVICE); notifier.update(mgmt, null); @@ -256,22 +266,11 @@ public class PmStandbyStateChangeNotifierTest { new PmStandbyStateChangeNotifier().getPolicyEngineManager(); } - @Test - public void testMakeTimer() { - // use real object with real method - new PmStandbyStateChangeNotifier().makeTimer().cancel(); - } - private class MyNotifier extends PmStandbyStateChangeNotifier { @Override protected PolicyEngine getPolicyEngineManager() { return engmgr; } - - @Override - protected Timer makeTimer() { - return timer; - } } private static class MyException extends RuntimeException { diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/StandbyStateManagementTest.java index 8bc8489a..143aaf31 100644 --- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java +++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/activestandby/StandbyStateManagementTest.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * feature-active-standby-management * ================================================================================ @@ -18,12 +18,15 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.drools.controller.test; +package org.onap.policy.drools.activestandby; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -32,30 +35,24 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; - import org.apache.commons.lang3.time.DateUtils; -import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.im.AdministrativeStateException; import org.onap.policy.common.im.IntegrityMonitor; +import org.onap.policy.common.im.IntegrityMonitorException; +import org.onap.policy.common.im.MonitorTime; import org.onap.policy.common.im.StandbyStatusException; import org.onap.policy.common.im.StateManagement; -import org.onap.policy.drools.activestandby.ActiveStandbyFeatureApi; -import org.onap.policy.drools.activestandby.ActiveStandbyFeatureApiConstants; -import org.onap.policy.drools.activestandby.ActiveStandbyProperties; -import org.onap.policy.drools.activestandby.DroolsPdp; -import org.onap.policy.drools.activestandby.DroolsPdpEntity; -import org.onap.policy.drools.activestandby.DroolsPdpImpl; -import org.onap.policy.drools.activestandby.DroolsPdpsConnector; -import org.onap.policy.drools.activestandby.DroolsPdpsElectionHandler; -import org.onap.policy.drools.activestandby.JpaDroolsPdpsConnector; -import org.onap.policy.drools.activestandby.PmStandbyStateChangeNotifier; +import org.onap.policy.common.utils.time.CurrentTime; +import org.onap.policy.common.utils.time.PseudoTimer; +import org.onap.policy.common.utils.time.TestTimeMulti; import org.onap.policy.drools.core.PolicySessionFeatureApi; import org.onap.policy.drools.statemanagement.StateManagementFeatureApi; import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants; +import org.powermock.reflect.Whitebox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,6 +69,10 @@ import org.slf4j.LoggerFactory; public class StandbyStateManagementTest { private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class); + + private static final String MONITOR_FIELD_NAME = "instance"; + private static final String HANDLER_INSTANCE_FIELD = "electionHandler"; + /* * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion @@ -79,20 +80,20 @@ public class StandbyStateManagementTest { * checking the results. Add a few seconds for safety */ - long sleepTime = 10000; + private static final long SLEEP_TIME = 10000; /* * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be * plenty to ensure it has time to re-promote this PDP. */ - long electionWaitSleepTime = 6000; + private static final long ELECTION_WAIT_SLEEP_TIME = 6000; /* - * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery. + * Sleep a few seconds after each test to allow interrupt (shutdown) recovery. */ - long interruptRecoveryTime = 5000; + private static final long INTERRUPT_RECOVERY_TIME = 5000; private static EntityManagerFactory emfx; private static EntityManagerFactory emfd; @@ -100,7 +101,12 @@ public class StandbyStateManagementTest { private static EntityManager emd; private static EntityTransaction et; - private final String configDir = "src/test/resources"; + private static final String CONFIG_DIR = "src/test/resources"; + + private static CurrentTime saveTime; + private static Factory saveFactory; + + private TestTimeMulti testTime; /* * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing. @@ -119,23 +125,13 @@ public class StandbyStateManagementTest { System.setProperty("com.sun.management.jmxremote.port", "9980"); System.setProperty("com.sun.management.jmxremote.authenticate","false"); - } + saveTime = Whitebox.getInternalState(MonitorTime.class, MONITOR_FIELD_NAME); + saveFactory = Factory.getInstance(); - @AfterClass - public static void tearDownClass() throws Exception { - } + resetInstanceObjects(); - /** - * Setup. - * - * @throws Exception exception - */ - @Before - public void setUp() throws Exception { - //Create teh data access for xaml db - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - "src/test/resources/feature-state-management.properties"))); + //Create the data access for xacml db + Properties stateManagementProperties = loadStateManagementProperties(); emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties); @@ -143,9 +139,7 @@ public class StandbyStateManagementTest { emx = emfx.createEntityManager(); //Create the data access for drools db - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - "src/test/resources/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties); @@ -153,8 +147,49 @@ public class StandbyStateManagementTest { emd = emfd.createEntityManager(); } - @After - public void tearDown() throws Exception { + /** + * Restores the system state. + * + * @throws IntegrityMonitorException if the integrity monitor cannot be shut down + */ + @AfterClass + public static void tearDownClass() throws IntegrityMonitorException { + resetInstanceObjects(); + + Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, saveTime); + Factory.setInstance(saveFactory); + + emd.close(); + emfd.close(); + + emx.close(); + emfx.close(); + } + + /** + * Setup. + * + * @throws Exception exception + */ + @Before + public void setUp() throws Exception { + resetInstanceObjects(); + + // set test time + testTime = new TestTimeMulti(); + Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, testTime); + + Factory factory = mock(Factory.class); + when(factory.makeTimer()).thenAnswer(ans -> new PseudoTimer(testTime)); + Factory.setInstance(factory); + } + + private static void resetInstanceObjects() throws IntegrityMonitorException { + IntegrityMonitor.setUnitTesting(true); + IntegrityMonitor.deleteInstance(); + IntegrityMonitor.setUnitTesting(false); + + Whitebox.setInternalState(ActiveStandbyFeature.class, HANDLER_INSTANCE_FIELD, (Object) null); } @@ -240,9 +275,7 @@ public class StandbyStateManagementTest { logger.debug("testPmStandbyStateChangeNotifier: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); String resourceName = "testPMS"; activeStandbyProperties.setProperty("resource.name", resourceName); @@ -324,12 +357,7 @@ public class StandbyStateManagementTest { // Get a DroolsPdpsConnector - logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); + Properties activeStandbyProperties = loadActiveStandbyProperties(); logger.debug("testSanitizeDesignatedList: Creating emfDrools"); EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( @@ -339,10 +367,10 @@ public class StandbyStateManagementTest { // Create 4 pdpd all not designated - DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); - DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); - DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); - DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); + DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate()); + DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate()); + DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate()); + DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate()); List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); listOfDesignated.add(pdp1); @@ -355,20 +383,13 @@ public class StandbyStateManagementTest { StateManagementFeatureApi stateManagementFeature = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); stateManagementFeature = feature; logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName()); break; } - if (stateManagementFeature == null) { - logger.error("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(stateManagementFeature); DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); @@ -421,12 +442,7 @@ public class StandbyStateManagementTest { logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n"); - logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); + Properties activeStandbyProperties = loadActiveStandbyProperties(); logger.debug("testComputeMostRecentPrimary: Creating emfDrools"); EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory( @@ -438,18 +454,18 @@ public class StandbyStateManagementTest { // Create 4 pdpd all not designated - long designatedDateMs = new Date().getTime(); - DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); + long designatedDateMs = testTime.getMillis(); + DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate()); pdp1.setDesignatedDate(new Date(designatedDateMs - 2)); - DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); + DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate()); //oldest pdp2.setDesignatedDate(new Date(designatedDateMs - 3)); - DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); + DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate()); pdp3.setDesignatedDate(new Date(designatedDateMs - 1)); - DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); + DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate()); //most recent pdp4.setDesignatedDate(new Date(designatedDateMs)); @@ -474,20 +490,13 @@ public class StandbyStateManagementTest { StateManagementFeatureApi stateManagementFeature = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); stateManagementFeature = feature; logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName()); break; } - if (stateManagementFeature == null) { - logger.error("testComputeMostRecentPrimary failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testComputeMostRecentPrimary failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(stateManagementFeature); DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); @@ -601,12 +610,7 @@ public class StandbyStateManagementTest { logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n"); - logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); - String thisPdpId = activeStandbyProperties - .getProperty(ActiveStandbyProperties.NODE_NAME); + Properties activeStandbyProperties = loadActiveStandbyProperties(); logger.debug("testComputeDesignatedPdp: Creating emfDrools"); @@ -619,21 +623,21 @@ public class StandbyStateManagementTest { // Create 4 pdpd all not designated. Two on site1. Two on site2 - long designatedDateMs = new Date().getTime(); - DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date()); + long designatedDateMs = testTime.getMillis(); + DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate()); pdp1.setDesignatedDate(new Date(designatedDateMs - 2)); pdp1.setSite("site1"); - DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date()); + DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate()); pdp2.setDesignatedDate(new Date(designatedDateMs - 3)); pdp2.setSite("site1"); //oldest - DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date()); + DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate()); pdp3.setDesignatedDate(new Date(designatedDateMs - 4)); pdp3.setSite("site2"); - DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date()); + DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate()); //most recent pdp4.setDesignatedDate(new Date(designatedDateMs)); pdp4.setSite("site2"); @@ -656,20 +660,13 @@ public class StandbyStateManagementTest { StateManagementFeatureApi stateManagementFeature = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); stateManagementFeature = feature; logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName()); break; } - if (stateManagementFeature == null) { - logger.error("testComputeDesignatedPdp failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testComputeDesignatedPdp failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(stateManagementFeature); DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1); @@ -739,19 +736,13 @@ public class StandbyStateManagementTest { cleanXacmlDb(); cleanDroolsDb(); - logger.debug("testColdStandby: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + Properties stateManagementProperties = loadStateManagementProperties(); logger.debug("testColdStandby: Creating emfXacml"); final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( "junitXacmlPU", stateManagementProperties); - logger.debug("testColdStandby: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME); logger.debug("testColdStandby: Creating emfDrools"); @@ -764,7 +755,7 @@ public class StandbyStateManagementTest { conn.deleteAllPdps(); logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId); - DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date()); + DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, testTime.getDate()); conn.insertPdp(pdp); DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); logger.debug("testColdStandby: After insertion, DESIGNATED= {} " @@ -796,47 +787,33 @@ public class StandbyStateManagementTest { StateManagementFeatureApi smf = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); smf = feature; logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName()); break; } - if (smf == null) { - logger.error("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testColdStandby failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(smf); // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature // that has been created. ActiveStandbyFeatureApi activeStandbyFeature = null; for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); activeStandbyFeature = feature; logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); break; } - if (activeStandbyFeature == null) { - logger.error("testColdStandby failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID:{}", thisPdpId); - logger.debug("testColdStandby failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID:{}", thisPdpId); - } + assertNotNull(activeStandbyFeature); // Artificially putting a PDP into service is really a two step process, 1) // inserting it as designated and 2) promoting it so that its standbyStatus // is providing service. logger.debug("testColdStandby: Runner started; Sleeping " - + interruptRecoveryTime + "ms before promoting PDP= {}", + + INTERRUPT_RECOVERY_TIME + "ms before promoting PDP= {}", thisPdpId); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); logger.debug("testColdStandby: Promoting PDP={}", thisPdpId); smf.promote(); @@ -848,7 +825,7 @@ public class StandbyStateManagementTest { logger.debug("testColdStandby: Locking smf"); smf.lock(); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); // Verify that the PDP is no longer designated. @@ -858,7 +835,7 @@ public class StandbyStateManagementTest { assertTrue(droolsPdpEntity.isDesignated() == false); logger.debug("\n\ntestColdStandby: Exiting\n\n"); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); } @@ -877,19 +854,13 @@ public class StandbyStateManagementTest { cleanXacmlDb(); cleanDroolsDb(); - logger.debug("testHotStandby1: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + Properties stateManagementProperties = loadStateManagementProperties(); logger.debug("testHotStandby1: Creating emfXacml"); final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( "junitXacmlPU", stateManagementProperties); - logger.debug("testHotStandby1: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); final String thisPdpId = activeStandbyProperties .getProperty(ActiveStandbyProperties.NODE_NAME); @@ -909,7 +880,7 @@ public class StandbyStateManagementTest { */ logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); + Date yesterday = DateUtils.addDays(testTime.getDate(), -1); DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday); conn.insertPdp(pdp); DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); @@ -927,38 +898,24 @@ public class StandbyStateManagementTest { StateManagementFeatureApi smf = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); smf = feature; logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName()); break; } - if (smf == null) { - logger.error("testHotStandby1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(smf); // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature // that has been created. ActiveStandbyFeatureApi activeStandbyFeature = null; for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); activeStandbyFeature = feature; logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); break; } - if (activeStandbyFeature == null) { - logger.error("testHotStandby1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(activeStandbyFeature); logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId); @@ -967,8 +924,8 @@ public class StandbyStateManagementTest { logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector " - + "time to check droolspdpentity table", sleepTime); - sleep(sleepTime); + + "time to check droolspdpentity table", SLEEP_TIME); + sleep(SLEEP_TIME); // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service. @@ -985,7 +942,7 @@ public class StandbyStateManagementTest { logger.debug("testHotStandby1: Stopping policyManagementRunner"); logger.debug("\n\ntestHotStandby1: Exiting\n\n"); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); } @@ -1006,19 +963,13 @@ public class StandbyStateManagementTest { cleanXacmlDb(); cleanDroolsDb(); - logger.info("testHotStandby2: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + Properties stateManagementProperties = loadStateManagementProperties(); logger.info("testHotStandby2: Creating emfXacml"); final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( "junitXacmlPU", stateManagementProperties); - logger.info("testHotStandby2: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); final String thisPdpId = activeStandbyProperties .getProperty(ActiveStandbyProperties.NODE_NAME); @@ -1036,7 +987,7 @@ public class StandbyStateManagementTest { String activePdpId = "pdp2"; logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); + Date yesterday = DateUtils.addDays(testTime.getDate(), -1); DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday); conn.insertPdp(pdp); DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId); @@ -1084,42 +1035,28 @@ public class StandbyStateManagementTest { StateManagementFeatureApi sm2 = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); sm2 = feature; logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName()); break; } - if (sm2 == null) { - logger.error("testHotStandby2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(sm2); // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature // that has been created. ActiveStandbyFeatureApi activeStandbyFeature = null; for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); activeStandbyFeature = feature; logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); break; } - if (activeStandbyFeature == null) { - logger.error("testHotStandby2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testHotStandby2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(activeStandbyFeature); logger.info("testHotStandby2: Runner started; Sleeping {} " - + "ms before promoting/demoting", interruptRecoveryTime); - sleep(interruptRecoveryTime); + + "ms before promoting/demoting", INTERRUPT_RECOVERY_TIME); + sleep(INTERRUPT_RECOVERY_TIME); logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId); //At this point, the newly created pdp will have set the state to disabled/failed/cold standby @@ -1137,8 +1074,8 @@ public class StandbyStateManagementTest { logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus); logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector " - + "time to check droolspdpentity table", sleepTime); - sleep(sleepTime); + + "time to check droolspdpentity table", SLEEP_TIME); + sleep(SLEEP_TIME); /* * Verify that this PDP, demoted to HOT_STANDBY, is now @@ -1160,7 +1097,7 @@ public class StandbyStateManagementTest { logger.info("testHotStandby2: Stopping policyManagementRunner"); logger.info("\n\ntestHotStandby2: Exiting\n\n"); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); } @@ -1194,19 +1131,13 @@ public class StandbyStateManagementTest { cleanXacmlDb(); cleanDroolsDb(); - logger.debug("testLocking1: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + Properties stateManagementProperties = loadStateManagementProperties(); logger.debug("testLocking1: Creating emfXacml"); final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( "junitXacmlPU", stateManagementProperties); - logger.debug("testLocking1: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); final String thisPdpId = activeStandbyProperties .getProperty(ActiveStandbyProperties.NODE_NAME); @@ -1225,7 +1156,7 @@ public class StandbyStateManagementTest { */ logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId); - DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date()); + DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, testTime.getDate()); conn.insertPdp(pdp); DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}", @@ -1241,51 +1172,37 @@ public class StandbyStateManagementTest { StateManagementFeatureApi sm = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); sm = feature; logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName()); break; } - if (sm == null) { - logger.error("testLocking1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking1 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(sm); // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature // that has been created. ActiveStandbyFeatureApi activeStandbyFeature = null; for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); activeStandbyFeature = feature; logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); break; } - if (activeStandbyFeature == null) { - logger.error("testLocking1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking1 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(activeStandbyFeature); logger.debug("testLocking1: Runner started; Sleeping " - + interruptRecoveryTime + "ms before promoting PDP={}", + + INTERRUPT_RECOVERY_TIME + "ms before promoting PDP={}", thisPdpId); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); logger.debug("testLocking1: Promoting PDP={}", thisPdpId); sm.promote(); logger.debug("testLocking1: Sleeping {} ms, to allow time for " + "policy-management.Main class to come up, designated= {}", - sleepTime, conn.getPdp(thisPdpId).isDesignated()); - sleep(sleepTime); + SLEEP_TIME, conn.getPdp(thisPdpId).isDesignated()); + sleep(SLEEP_TIME); logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}" + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated()); @@ -1312,9 +1229,9 @@ public class StandbyStateManagementTest { logger.debug("testLocking1: demoting PDP={}", thisPdpId); sm.demote(); - logger.debug("testLocking1: sleeping" + electionWaitSleepTime + logger.debug("testLocking1: sleeping" + ELECTION_WAIT_SLEEP_TIME + " to allow election handler to re-promote PDP={}", thisPdpId); - sleep(electionWaitSleepTime); + sleep(ELECTION_WAIT_SLEEP_TIME); logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}" + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); @@ -1365,7 +1282,7 @@ public class StandbyStateManagementTest { // Just to avoid any race conditions, sleep a little after locking logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition"); - sleep(electionWaitSleepTime); + sleep(ELECTION_WAIT_SLEEP_TIME); logger.debug("testLocking1: Invoking startTransaction on unlocked PDP=" + thisPdpId @@ -1413,7 +1330,7 @@ public class StandbyStateManagementTest { } logger.debug("\n\ntestLocking1: Exiting\n\n"); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); } @@ -1441,19 +1358,13 @@ public class StandbyStateManagementTest { cleanXacmlDb(); cleanDroolsDb(); - logger.debug("testLocking2: Reading stateManagementProperties"); - Properties stateManagementProperties = new Properties(); - stateManagementProperties.load(new FileInputStream(new File( - configDir + "/feature-state-management.properties"))); + Properties stateManagementProperties = loadStateManagementProperties(); logger.debug("testLocking2: Creating emfXacml"); final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory( "junitXacmlPU", stateManagementProperties); - logger.debug("testLocking2: Reading activeStandbyProperties"); - Properties activeStandbyProperties = new Properties(); - activeStandbyProperties.load(new FileInputStream(new File( - configDir + "/feature-active-standby-management.properties"))); + Properties activeStandbyProperties = loadActiveStandbyProperties(); final String thisPdpId = activeStandbyProperties .getProperty(ActiveStandbyProperties.NODE_NAME); @@ -1473,7 +1384,7 @@ public class StandbyStateManagementTest { */ logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId); - DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date()); + DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, testTime.getDate()); conn.insertPdp(pdp); DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId); logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}", @@ -1489,38 +1400,24 @@ public class StandbyStateManagementTest { StateManagementFeatureApi sm = null; for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); sm = feature; logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName()); break; } - if (sm == null) { - logger.error("testLocking2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking2 failed to initialize. " - + "Unable to get instance of StateManagementFeatureApi " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(sm); // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature // that has been created. ActiveStandbyFeatureApi activeStandbyFeature = null; for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) { - ((PolicySessionFeatureApi) feature).globalInit(null, configDir); + ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR); activeStandbyFeature = feature; logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName()); break; } - if (activeStandbyFeature == null) { - logger.error("testLocking2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - logger.debug("testLocking2 failed to initialize. " - + "Unable to get instance of ActiveStandbyFeatureAPI " - + "with resourceID: {}", thisPdpId); - } + assertNotNull(activeStandbyFeature); /* * Insert another PDP as not designated. Initial standby state will be @@ -1530,7 +1427,7 @@ public class StandbyStateManagementTest { String standbyPdpId = "pdp2"; logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId); - Date yesterday = DateUtils.addDays(new Date(), -1); + Date yesterday = DateUtils.addDays(testTime.getDate(), -1); pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday); conn.insertPdp(pdp); droolsPdpEntity = conn.getPdp(standbyPdpId); @@ -1542,8 +1439,8 @@ public class StandbyStateManagementTest { final StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId); logger.debug("testLocking2: Runner started; Sleeping {} ms " - + "before promoting/demoting", interruptRecoveryTime); - sleep(interruptRecoveryTime); + + "before promoting/demoting", INTERRUPT_RECOVERY_TIME); + sleep(INTERRUPT_RECOVERY_TIME); logger.debug("testLocking2: Promoting PDP= {}", thisPdpId); sm.promote(); @@ -1552,8 +1449,8 @@ public class StandbyStateManagementTest { logger.debug("testLocking2: Demoting PDP={}", standbyPdpId); sm2.demote(); - logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime); - sleep(sleepTime); + logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", SLEEP_TIME); + sleep(SLEEP_TIME); logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}" + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated()); @@ -1580,8 +1477,8 @@ public class StandbyStateManagementTest { sm.demote(); logger.debug("testLocking2: sleeping {}" - + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId); - sleep(electionWaitSleepTime); + + " to allow election handler to re-promote PDP={}", ELECTION_WAIT_SLEEP_TIME, thisPdpId); + sleep(ELECTION_WAIT_SLEEP_TIME); logger.debug("testLocking2: Waking up and invoking startTransaction " + "on re-promoted PDP= {}, designated= {}", @@ -1606,10 +1503,27 @@ public class StandbyStateManagementTest { assertTrue(standbyPdpDesignated == false); logger.debug("\n\ntestLocking2: Exiting\n\n"); - sleep(interruptRecoveryTime); + sleep(INTERRUPT_RECOVERY_TIME); + } + + private static Properties loadStateManagementProperties() throws IOException { + try (FileInputStream input = new FileInputStream(CONFIG_DIR + "/feature-state-management.properties")) { + Properties props = new Properties(); + props.load(input); + return props; + } + } + + private static Properties loadActiveStandbyProperties() throws IOException { + try (FileInputStream input = + new FileInputStream(CONFIG_DIR + "/feature-active-standby-management.properties")) { + Properties props = new Properties(); + props.load(input); + return props; + } } private void sleep(long sleepms) throws InterruptedException { - Thread.sleep(sleepms); + testTime.waitFor(sleepms); } } |