2  * ============LICENSE_START=======================================================
 
   3  * feature-active-standby-management
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.drools.activestandby;
 
  23 import static org.junit.Assert.assertNotNull;
 
  24 import static org.junit.Assert.assertTrue;
 
  25 import static org.mockito.Mockito.mock;
 
  26 import static org.mockito.Mockito.when;
 
  28 import java.io.FileInputStream;
 
  29 import java.io.IOException;
 
  30 import java.util.ArrayList;
 
  31 import java.util.Date;
 
  32 import java.util.List;
 
  33 import java.util.Properties;
 
  34 import javax.persistence.EntityManager;
 
  35 import javax.persistence.EntityManagerFactory;
 
  36 import javax.persistence.EntityTransaction;
 
  37 import javax.persistence.Persistence;
 
  38 import org.apache.commons.lang3.time.DateUtils;
 
  39 import org.junit.AfterClass;
 
  40 import org.junit.Before;
 
  41 import org.junit.BeforeClass;
 
  42 import org.junit.Test;
 
  43 import org.onap.policy.common.im.AdministrativeStateException;
 
  44 import org.onap.policy.common.im.IntegrityMonitor;
 
  45 import org.onap.policy.common.im.IntegrityMonitorException;
 
  46 import org.onap.policy.common.im.MonitorTime;
 
  47 import org.onap.policy.common.im.StandbyStatusException;
 
  48 import org.onap.policy.common.im.StateManagement;
 
  49 import org.onap.policy.common.utils.time.CurrentTime;
 
  50 import org.onap.policy.common.utils.time.PseudoTimer;
 
  51 import org.onap.policy.common.utils.time.TestTimeMulti;
 
  52 import org.onap.policy.drools.core.PolicySessionFeatureApi;
 
  53 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
 
  54 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
 
  55 import org.powermock.reflect.Whitebox;
 
  56 import org.slf4j.Logger;
 
  57 import org.slf4j.LoggerFactory;
 
  60  * All JUnits are designed to run in the local development environment
 
  61  * where they have write privileges and can execute time-sensitive
 
  64  * These tests can be run as JUnits, but there is some issue with running them
 
  65  * as part of a "mvn install" build.  Also, they take a very long time to run
 
  66  * due to many real time breaks.  Consequently, they are marked as @Ignore and
 
  67  * only run from the desktop.
 
  70 public class StandbyStateManagementTest {
 
  71     private static final Logger  logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
 
  73     private static final String MONITOR_FIELD_NAME = "instance";
 
  74     private static final String HANDLER_INSTANCE_FIELD = "electionHandler";
 
  77      * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
 
  78      * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
 
  79      * to ensure that we wait for the DesignationWaiter to do its job, before
 
  80      * checking the results. Add a few seconds for safety
 
  83     private static final long SLEEP_TIME = 10000;
 
  86      * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
 
  87      * plenty to ensure it has time to re-promote this PDP.
 
  90     private static final long ELECTION_WAIT_SLEEP_TIME = 6000;
 
  93      * Sleep a few seconds after each test to allow interrupt (shutdown) recovery.
 
  96     private static final long INTERRUPT_RECOVERY_TIME = 5000;
 
  98     private static EntityManagerFactory emfx;
 
  99     private static EntityManagerFactory emfd;
 
 100     private static EntityManager emx;
 
 101     private static EntityManager emd;
 
 102     private static EntityTransaction et;
 
 104     private static final String CONFIG_DIR = "src/test/resources";
 
 106     private static CurrentTime saveTime;
 
 107     private static Factory saveFactory;
 
 109     private TestTimeMulti testTime;
 
 112      * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
 
 118      * @throws Exception exception
 
 121     public static void setUpClass() throws Exception {
 
 123         String userDir = System.getProperty("user.dir");
 
 124         logger.debug("setUpClass: userDir={}", userDir);
 
 125         System.setProperty("com.sun.management.jmxremote.port", "9980");
 
 126         System.setProperty("com.sun.management.jmxremote.authenticate","false");
 
 128         saveTime = Whitebox.getInternalState(MonitorTime.class, MONITOR_FIELD_NAME);
 
 129         saveFactory = Factory.getInstance();
 
 131         resetInstanceObjects();
 
 133         //Create the data access for xacml db
 
 134         Properties stateManagementProperties = loadStateManagementProperties();
 
 136         emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
 
 138         // Create an entity manager to use the DB
 
 139         emx = emfx.createEntityManager();
 
 141         //Create the data access for drools db
 
 142         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 144         emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
 
 146         // Create an entity manager to use the DB
 
 147         emd = emfd.createEntityManager();
 
 151      * Restores the system state.
 
 153      * @throws IntegrityMonitorException if the integrity monitor cannot be shut down
 
 156     public static void tearDownClass() throws IntegrityMonitorException {
 
 157         resetInstanceObjects();
 
 159         Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, saveTime);
 
 160         Factory.setInstance(saveFactory);
 
 172      * @throws Exception exception
 
 175     public void setUp() throws Exception {
 
 176         resetInstanceObjects();
 
 179         testTime = new TestTimeMulti();
 
 180         Whitebox.setInternalState(MonitorTime.class, MONITOR_FIELD_NAME, testTime);
 
 182         Factory factory = mock(Factory.class);
 
 183         when(factory.makeTimer()).thenAnswer(ans -> new PseudoTimer(testTime));
 
 184         Factory.setInstance(factory);
 
 187     private static void resetInstanceObjects() throws IntegrityMonitorException {
 
 188         IntegrityMonitor.setUnitTesting(true);
 
 189         IntegrityMonitor.deleteInstance();
 
 190         IntegrityMonitor.setUnitTesting(false);
 
 192         Whitebox.setInternalState(ActiveStandbyFeature.class, HANDLER_INSTANCE_FIELD, (Object) null);
 
 197      * Clean up the xacml database.
 
 200     public void cleanXacmlDb() {
 
 201         et = emx.getTransaction();
 
 204         // Make sure we leave the DB clean
 
 205         emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
 
 206         emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
 
 207         emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
 
 213      * Clean up the drools db.
 
 215     public void cleanDroolsDb() {
 
 216         et = emd.getTransaction();
 
 219         // Make sure we leave the DB clean
 
 220         emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
 
 226      * These JUnit tests must be run one at a time in an eclipse environment
 
 227      * by right-clicking StandbyStateManagementTest and selecting
 
 228      * "Run As" -> "JUnit Test".
 
 230      * They will run successfully when you run all of them under runAllTests(),
 
 231      * however, you will get all sorts of non-fatal errors in the log and on the
 
 232      * console that result from overlapping threads that are not terminated at the
 
 233      * end of each test. The problem is that the JUnit environment does not terminate
 
 234      * all the test threads between tests. This is true even if you break each JUnit
 
 235      * into a separate file. Consequently, all the tests would have to be refactored
 
 236      * so all test object initializations are coordinated.  In other words, you
 
 237      * retrieve the ActiveStandbyFeature instance and other class instances only once
 
 238      * at the beginning of the JUnits and then reuse them throughout the tests.
 
 239      * Initialization of the state of the objects is pretty straight forward as it
 
 240      * just amounts to manipulating the entries in StateManagementEntity and
 
 241      * DroolsPdpEntity tables. However, some thought needs to be given to how to
 
 242      * "pause" the processing in ActiveStandbyFeature class.  I think we could "pause"
 
 243      * it by calling globalInit() which will, I think, restart it. So long as it
 
 244      * does not create a new instance, it will force it to go through an initialization
 
 245      * cycle which includes a "pause" at the beginning of proecessing.  We just must
 
 246      * be sure it does not create another instance - which may mean we need to add
 
 247      * a factory interface instead of calling the constructor directly.
 
 253     public void runAllTests() throws Exception {
 
 259         testPmStandbyStateChangeNotifier();
 
 260         testSanitizeDesignatedList();
 
 261         testComputeMostRecentPrimary();
 
 262         testComputeDesignatedPdp();
 
 266      * Test the standby state change notifier.
 
 268      * @throws Exception exception
 
 272     public void testPmStandbyStateChangeNotifier() throws Exception {
 
 273         logger.debug("\n\ntestPmStandbyStateChangeNotifier: Entering\n\n");
 
 276         logger.debug("testPmStandbyStateChangeNotifier: Reading activeStandbyProperties");
 
 278         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 280         String resourceName = "testPMS";
 
 281         activeStandbyProperties.setProperty("resource.name", resourceName);
 
 282         ActiveStandbyProperties.initProperties(activeStandbyProperties);
 
 284         logger.debug("testPmStandbyStateChangeNotifier: Getting StateManagement instance");
 
 286         StateManagement sm = new StateManagement(emfx, resourceName);
 
 288         //Create an instance of the Observer
 
 289         PmStandbyStateChangeNotifier pmNotifier = new PmStandbyStateChangeNotifier();
 
 291         //Register the PmStandbyStateChangeNotifier Observer
 
 292         sm.addObserver(pmNotifier);
 
 294         //At this point the standbystatus = 'null'
 
 296         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
 
 299         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
 
 301         //Adding standbystatus=hotstandby
 
 303         System.out.println(pmNotifier.getPreviousStandbyStatus());
 
 304         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 305                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 307         //Now making standbystatus=coldstandby
 
 309         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 310                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 312         //standbystatus = hotstandby
 
 314         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 315                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 317         //standbystatus = providingservice
 
 319         //The previousStandbyStatus is not updated until after the delay activation expires
 
 320         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 321                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 323         //Sleep long enough for the delayActivationTimer to run
 
 325         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
 
 327         //standbystatus = providingservice
 
 329         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
 
 331         //standbystatus = coldstandby
 
 333         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 334                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 336         //standbystatus = hotstandby
 
 338         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 339                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 341         //standbystatus = hotstandby
 
 343         assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
 
 344                 PmStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
 
 348      * Test sanitize designated list.
 
 350      * @throws Exception exception
 
 354     public void testSanitizeDesignatedList() throws Exception {
 
 356         logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
 
 358         // Get a DroolsPdpsConnector
 
 360         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 362         logger.debug("testSanitizeDesignatedList: Creating emfDrools");
 
 363         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
 364                 "junitDroolsPU", activeStandbyProperties);
 
 366         final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
 
 368         // Create 4 pdpd all not designated
 
 370         DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate());
 
 371         DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate());
 
 372         DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate());
 
 373         DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate());
 
 375         List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
 
 376         listOfDesignated.add(pdp1);
 
 377         listOfDesignated.add(pdp2);
 
 378         listOfDesignated.add(pdp3);
 
 379         listOfDesignated.add(pdp4);
 
 381         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
 382         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
 384         StateManagementFeatureApi stateManagementFeature = null;
 
 385         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
 386             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 387             stateManagementFeature = feature;
 
 388             logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}",
 
 389                     stateManagementFeature.getResourceName());
 
 392         assertNotNull(stateManagementFeature);
 
 395         DroolsPdpsElectionHandler droolsPdpsElectionHandler =  new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
 
 397         listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
 
 399         logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
 
 401         assertTrue(listOfDesignated.size() == 4);
 
 403         // Now make 2 designated
 
 405         pdp1.setDesignated(true);
 
 406         pdp2.setDesignated(true);
 
 408         listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
 
 410         logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n",
 
 411                 listOfDesignated.size());
 
 413         assertTrue(listOfDesignated.size() == 2);
 
 414         assertTrue(listOfDesignated.contains(pdp1));
 
 415         assertTrue(listOfDesignated.contains(pdp2));
 
 418         // Now all are designated.  But, we have to add back the previously non-designated nodes
 
 420         pdp3.setDesignated(true);
 
 421         pdp4.setDesignated(true);
 
 422         listOfDesignated.add(pdp3);
 
 423         listOfDesignated.add(pdp4);
 
 425         listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
 
 427         logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n",
 
 428                 listOfDesignated.size());
 
 430         assertTrue(listOfDesignated.size() == 4);
 
 435     *  Test Compute most recent primary.
 
 437     * @throws Exception exception
 
 441     public void testComputeMostRecentPrimary() throws Exception {
 
 443         logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
 
 445         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 447         logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
 
 448         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
 449                 "junitDroolsPU", activeStandbyProperties);
 
 451         final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
 
 454         // Create 4 pdpd all not designated
 
 457         long designatedDateMs = testTime.getMillis();
 
 458         DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate());
 
 459         pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
 
 461         DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate());
 
 463         pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
 
 465         DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate());
 
 466         pdp3.setDesignatedDate(new Date(designatedDateMs - 1));
 
 468         DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate());
 
 470         pdp4.setDesignatedDate(new Date(designatedDateMs));
 
 472         ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
 
 473         listOfAllPdps.add(pdp1);
 
 474         listOfAllPdps.add(pdp2);
 
 475         listOfAllPdps.add(pdp3);
 
 476         listOfAllPdps.add(pdp4);
 
 479         ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
 
 480         listOfDesignated.add(pdp1);
 
 481         listOfDesignated.add(pdp2);
 
 482         listOfDesignated.add(pdp3);
 
 483         listOfDesignated.add(pdp4);
 
 485         // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
 
 486         // or all designated members.
 
 488         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
 489         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
 491         StateManagementFeatureApi stateManagementFeature = null;
 
 492         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
 493             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 494             stateManagementFeature = feature;
 
 495             logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}",
 
 496                     stateManagementFeature.getResourceName());
 
 499         assertNotNull(stateManagementFeature);
 
 501         DroolsPdpsElectionHandler droolsPdpsElectionHandler =  new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
 
 503         DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(
 
 504                 listOfAllPdps, listOfDesignated);
 
 506         logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
 
 507                 mostRecentPrimary.getPdpId());
 
 510         // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
 
 511         // the one which has the most recent designated date.
 
 514         assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
 
 517         // Now let's designate all of those on the listOfDesignated.  It will choose the first one designated
 
 520         pdp1.setDesignated(true);
 
 521         pdp2.setDesignated(true);
 
 522         pdp3.setDesignated(true);
 
 523         pdp4.setDesignated(true);
 
 525         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
 
 527         logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, "
 
 528                 + "mostRecentPrimary.getPdpId() = {}\n\n",
 
 529                 mostRecentPrimary.getPdpId());
 
 532         // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
 
 533         // the one which was designated first
 
 536         assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
 
 539         // Now we will designate only 2 and put just them in the listOfDesignated.  The algorithm will now
 
 540         // look for the most recently designated pdp which is not currently designated.
 
 543         pdp3.setDesignated(false);
 
 544         pdp4.setDesignated(false);
 
 546         listOfDesignated.remove(pdp3);
 
 547         listOfDesignated.remove(pdp4);
 
 549         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
 
 551         logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
 
 552                 mostRecentPrimary.getPdpId());
 
 554         assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
 
 558         // Now we will have none designated and put two of them in the listOfDesignated.  The algorithm will now
 
 559         // look for the most recently designated pdp regardless of whether it is currently marked as designated.
 
 562         pdp1.setDesignated(false);
 
 563         pdp2.setDesignated(false);
 
 565         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
 
 567         logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n",
 
 568                 mostRecentPrimary.getPdpId());
 
 570         assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
 
 573         // If we have only one pdp on in the listOfDesignated,
 
 574         // the most recently designated pdp will be chosen, regardless
 
 575         // of its designation status
 
 578         listOfDesignated.remove(pdp1);
 
 580         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
 
 582         logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n",
 
 583                 mostRecentPrimary.getPdpId());
 
 585         assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
 
 588         // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
 
 591         listOfDesignated.remove(pdp2);
 
 593         mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
 
 595         logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n",
 
 596                 mostRecentPrimary.getPdpId());
 
 598         assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
 
 603      * Test compute designated PDP.
 
 605      * @throws Exception exception
 
 609     public void testComputeDesignatedPdp() throws Exception {
 
 611         logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
 
 613         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 616         logger.debug("testComputeDesignatedPdp: Creating emfDrools");
 
 617         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
 618                 "junitDroolsPU", activeStandbyProperties);
 
 620         final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
 
 623         // Create 4 pdpd all not designated.  Two on site1. Two on site2
 
 626         long designatedDateMs = testTime.getMillis();
 
 627         DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, testTime.getDate());
 
 628         pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
 
 629         pdp1.setSite("site1");
 
 631         DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, testTime.getDate());
 
 632         pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
 
 633         pdp2.setSite("site1");
 
 636         DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, testTime.getDate());
 
 637         pdp3.setDesignatedDate(new Date(designatedDateMs - 4));
 
 638         pdp3.setSite("site2");
 
 640         DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, testTime.getDate());
 
 642         pdp4.setDesignatedDate(new Date(designatedDateMs));
 
 643         pdp4.setSite("site2");
 
 645         ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
 
 646         listOfAllPdps.add(pdp1);
 
 647         listOfAllPdps.add(pdp2);
 
 648         listOfAllPdps.add(pdp3);
 
 649         listOfAllPdps.add(pdp4);
 
 652         ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
 
 655         // We will first test an empty listOfDesignated. As we know from the previous JUnit,
 
 656         // the pdp with the most designated date will be chosen for mostRecentPrimary
 
 658         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
 659         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
 661         StateManagementFeatureApi stateManagementFeature = null;
 
 662         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
 663             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 664             stateManagementFeature = feature;
 
 665             logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}",
 
 666                     stateManagementFeature.getResourceName());
 
 669         assertNotNull(stateManagementFeature);
 
 672         DroolsPdpsElectionHandler droolsPdpsElectionHandler =  new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
 
 674         DroolsPdp mostRecentPrimary = pdp4;
 
 676         DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
 
 679         // The designatedPdp should be null
 
 681         assertTrue(designatedPdp == null);
 
 684         // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
 
 686         listOfDesignated.add(pdp2);
 
 688         designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
 
 691         // Now the designatedPdp should be the one and only selection in the listOfDesignated
 
 694         assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
 
 697         // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
 
 700         listOfDesignated.add(pdp1);
 
 702         designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
 
 705         // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
 
 708         assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
 
 711         // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
 
 714         listOfDesignated.remove(pdp1);
 
 715         listOfDesignated.add(pdp3);
 
 717         designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
 
 720         // The designatedPdp should now be the one on the same site as the mostRecentPrimary
 
 723         assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
 
 729      * @throws Exception exception
 
 733     public void testColdStandby() throws Exception {
 
 735         logger.debug("\n\ntestColdStandby: Entering\n\n");
 
 739         Properties stateManagementProperties = loadStateManagementProperties();
 
 741         logger.debug("testColdStandby: Creating emfXacml");
 
 742         final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
 
 743                 "junitXacmlPU", stateManagementProperties);
 
 745         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 746         final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
 
 748         logger.debug("testColdStandby: Creating emfDrools");
 
 749         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
 750                 "junitDroolsPU", activeStandbyProperties);
 
 752         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
 
 754         logger.debug("testColdStandby: Cleaning up tables");
 
 755         conn.deleteAllPdps();
 
 757         logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
 
 758         DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, testTime.getDate());
 
 760         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
 
 761         logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
 
 762                 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
 
 763         assertTrue(droolsPdpEntity.isDesignated() == true);
 
 766          * When the Standby Status changes (from providingservice) to hotstandby
 
 767          * or coldstandby,the Active/Standby selection algorithm must stand down
 
 768          * if thePDP-D is currently the lead/active node and allow another PDP-D
 
 771          * It must also call lock on all engines in the engine management.
 
 776          * Yes, this is kludgy, but we have a chicken and egg problem here: we
 
 777          * need a StateManagement object to invoke the
 
 778          * deleteAllStateManagementEntities method.
 
 780         logger.debug("testColdStandby: Instantiating stateManagement object");
 
 782         StateManagement sm = new StateManagement(emfXacml, "dummy");
 
 783         sm.deleteAllStateManagementEntities();
 
 785         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
 786         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
 788         StateManagementFeatureApi smf = null;
 
 789         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
 790             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 792             logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
 
 797         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
 
 798         // that has been created.
 
 799         ActiveStandbyFeatureApi activeStandbyFeature = null;
 
 800         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
 
 801             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 802             activeStandbyFeature = feature;
 
 803             logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}",
 
 804                     activeStandbyFeature.getResourceName());
 
 807         assertNotNull(activeStandbyFeature);
 
 809         // Artificially putting a PDP into service is really a two step process, 1)
 
 810         // inserting it as designated and 2) promoting it so that its standbyStatus
 
 811         // is providing service.
 
 813         logger.debug("testColdStandby: Runner started; Sleeping "
 
 814                 + INTERRUPT_RECOVERY_TIME + "ms before promoting PDP= {}",
 
 816         sleep(INTERRUPT_RECOVERY_TIME);
 
 818         logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
 
 821         String standbyStatus = sm.getStandbyStatus(thisPdpId);
 
 822         logger.debug("testColdStandby: Before locking, PDP= {}  has standbyStatus= {}",
 
 823                 thisPdpId, standbyStatus);
 
 825         logger.debug("testColdStandby: Locking smf");
 
 828         sleep(INTERRUPT_RECOVERY_TIME);
 
 830         // Verify that the PDP is no longer designated.
 
 832         droolsPdpEntity = conn.getPdp(thisPdpId);
 
 833         logger.debug("testColdStandby: After lock sm.lock() invoked, "
 
 834                 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
 
 835         assertTrue(droolsPdpEntity.isDesignated() == false);
 
 837         logger.debug("\n\ntestColdStandby: Exiting\n\n");
 
 838         sleep(INTERRUPT_RECOVERY_TIME);
 
 842     // Tests hot standby when there is only one PDP.
 
 845      * Test hot standby 1.
 
 847      * @throws Exception exception
 
 851     public void testHotStandby1() throws Exception {
 
 853         logger.debug("\n\ntestHotStandby1: Entering\n\n");
 
 857         Properties stateManagementProperties = loadStateManagementProperties();
 
 859         logger.debug("testHotStandby1: Creating emfXacml");
 
 860         final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
 
 861                 "junitXacmlPU", stateManagementProperties);
 
 863         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 864         final String thisPdpId = activeStandbyProperties
 
 865                 .getProperty(ActiveStandbyProperties.NODE_NAME);
 
 867         logger.debug("testHotStandby1: Creating emfDrools");
 
 868         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
 869                 "junitDroolsPU", activeStandbyProperties);
 
 871         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
 
 873         logger.debug("testHotStandby1: Cleaning up tables");
 
 874         conn.deleteAllPdps();
 
 877          * Insert this PDP as not designated.  Initial standby state will be
 
 878          * either null or cold standby.   Demoting should transit state to
 
 882         logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
 
 883         Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
 
 884         DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
 
 886         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
 
 887         logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
 
 888                 thisPdpId, droolsPdpEntity.isDesignated());
 
 889         assertTrue(droolsPdpEntity.isDesignated() == false);
 
 891         logger.debug("testHotStandby1: Instantiating stateManagement object");
 
 892         StateManagement sm = new StateManagement(emfXacml, "dummy");
 
 893         sm.deleteAllStateManagementEntities();
 
 896         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
 897         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
 899         StateManagementFeatureApi smf = null;
 
 900         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
 901             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 903             logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
 
 908         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
 
 909         // that has been created.
 
 910         ActiveStandbyFeatureApi activeStandbyFeature = null;
 
 911         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
 
 912             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
 913             activeStandbyFeature = feature;
 
 914             logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}",
 
 915                     activeStandbyFeature.getResourceName());
 
 918         assertNotNull(activeStandbyFeature);
 
 921         logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
 
 922         // demoting should cause state to transit to hotstandby
 
 926         logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
 
 927                 + "time to check droolspdpentity table", SLEEP_TIME);
 
 931         // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
 
 933         droolsPdpEntity = conn.getPdp(thisPdpId);
 
 934         logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
 
 935                 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
 
 936         assertTrue(droolsPdpEntity.isDesignated() == true);
 
 937         String standbyStatus = smf.getStandbyStatus(thisPdpId);
 
 938         logger.debug("testHotStandby1: After demotion, PDP= {} "
 
 939                 + "has standbyStatus= {}", thisPdpId, standbyStatus);
 
 940         assertTrue(standbyStatus != null  &&  standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
 
 942         logger.debug("testHotStandby1: Stopping policyManagementRunner");
 
 944         logger.debug("\n\ntestHotStandby1: Exiting\n\n");
 
 945         sleep(INTERRUPT_RECOVERY_TIME);
 
 950      * Tests hot standby when two PDPs are involved.
 
 954      * Test hot standby 2.
 
 956      * @throws Exception exception
 
 960     public void testHotStandby2() throws Exception {
 
 962         logger.info("\n\ntestHotStandby2: Entering\n\n");
 
 966         Properties stateManagementProperties = loadStateManagementProperties();
 
 968         logger.info("testHotStandby2: Creating emfXacml");
 
 969         final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
 
 970                 "junitXacmlPU", stateManagementProperties);
 
 972         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
 973         final String thisPdpId = activeStandbyProperties
 
 974                 .getProperty(ActiveStandbyProperties.NODE_NAME);
 
 976         logger.info("testHotStandby2: Creating emfDrools");
 
 977         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
 978                 "junitDroolsPU", activeStandbyProperties);
 
 980         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
 
 982         logger.info("testHotStandby2: Cleaning up tables");
 
 983         conn.deleteAllPdps();
 
 986         // Insert a PDP that's designated but not current.
 
 988         String activePdpId = "pdp2";
 
 989         logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
 
 990         Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
 
 991         DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
 
 993         DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
 
 994         logger.info("testHotStandby2: After insertion, PDP= {}, which is "
 
 995                 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
 
 996         assertTrue(droolsPdpEntity.isDesignated() == true);
 
 999          * Promote the designated PDP.
 
1001          * We have a chicken and egg problem here: we need a StateManagement
 
1002          * object to invoke the deleteAllStateManagementEntities method.
 
1006         logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
 
1007         StateManagement sm = new StateManagement(emfXacml, "dummy");
 
1008         sm.deleteAllStateManagementEntities();
 
1011         sm = new StateManagement(emfXacml, activePdpId);//pdp2
 
1013         // Artificially putting a PDP into service is really a two step process, 1)
 
1014         // inserting it as designated and 2) promoting it so that its standbyStatus
 
1015         // is providing service.
 
1018          * Insert this PDP as not designated.  Initial standby state will be
 
1019          * either null or cold standby.   Demoting should transit state to
 
1024         logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
 
1025         pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
 
1026         conn.insertPdp(pdp);
 
1027         droolsPdpEntity = conn.getPdp(thisPdpId);
 
1028         logger.info("testHotStandby2: After insertion, PDP={} "
 
1029                 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
 
1030         assertTrue(droolsPdpEntity.isDesignated() == false);
 
1033         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
1034         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
1036         StateManagementFeatureApi sm2 = null;
 
1037         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
1038             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
1040             logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
 
1045         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
 
1046         // that has been created.
 
1047         ActiveStandbyFeatureApi activeStandbyFeature = null;
 
1048         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
 
1049             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
1050             activeStandbyFeature = feature;
 
1051             logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}",
 
1052                     activeStandbyFeature.getResourceName());
 
1055         assertNotNull(activeStandbyFeature);
 
1057         logger.info("testHotStandby2: Runner started; Sleeping {} "
 
1058                 + "ms before promoting/demoting", INTERRUPT_RECOVERY_TIME);
 
1059         sleep(INTERRUPT_RECOVERY_TIME);
 
1061         logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
 
1062         //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
 
1063         //because it is stale. So, it cannot be promoted.  We need to call sm.enableNotFailed() so we
 
1064         //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
 
1065         sm.enableNotFailed();//pdp2
 
1067         String standbyStatus = sm.getStandbyStatus(activePdpId);
 
1068         logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
 
1070         // demoting PDP should ensure that state transits to hotstandby
 
1071         logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
 
1073         standbyStatus = sm.getStandbyStatus(thisPdpId);
 
1074         logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
 
1076         logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
 
1077                 + "time to check droolspdpentity table", SLEEP_TIME);
 
1081          * Verify that this PDP, demoted to HOT_STANDBY, is now
 
1082          * re-designated and providing service.
 
1085         droolsPdpEntity = conn.getPdp(thisPdpId);
 
1086         logger.info("testHotStandby2: After demoting PDP={}"
 
1087                 + ", DESIGNATED= {}"
 
1088                 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
 
1089         assertTrue(droolsPdpEntity.isDesignated() == true);
 
1090         standbyStatus = sm2.getStandbyStatus(thisPdpId);
 
1091         logger.info("testHotStandby2: After demoting PDP={}"
 
1092                 + ", PDP={} has standbyStatus= {}",
 
1093                 activePdpId, thisPdpId, standbyStatus);
 
1094         assertTrue(standbyStatus != null
 
1095                 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
 
1097         logger.info("testHotStandby2: Stopping policyManagementRunner");
 
1099         logger.info("\n\ntestHotStandby2: Exiting\n\n");
 
1100         sleep(INTERRUPT_RECOVERY_TIME);
 
1105      * 1) Inserts and designates this PDP, then verifies that startTransaction
 
1108      * 2) Demotes PDP, and verifies that because there is only one PDP, it will
 
1109      * be immediately re-promoted, thus allowing startTransaction to be
 
1112      * 3) Locks PDP and verifies that startTransaction results in
 
1113      * AdministrativeStateException.
 
1115      * 4) Unlocks PDP and verifies that startTransaction results in
 
1116      * StandbyStatusException.
 
1118      * 5) Promotes PDP and verifies that startTransaction is once again
 
1125      * @throws Exception exception
 
1129     public void testLocking1() throws Exception {
 
1130         logger.debug("testLocking1: Entry");
 
1134         Properties stateManagementProperties = loadStateManagementProperties();
 
1136         logger.debug("testLocking1: Creating emfXacml");
 
1137         final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
 
1138                 "junitXacmlPU", stateManagementProperties);
 
1140         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
1141         final String thisPdpId = activeStandbyProperties
 
1142                 .getProperty(ActiveStandbyProperties.NODE_NAME);
 
1144         logger.debug("testLocking1: Creating emfDrools");
 
1145         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
1146                 "junitDroolsPU", activeStandbyProperties);
 
1148         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
 
1150         logger.debug("testLocking1: Cleaning up tables");
 
1151         conn.deleteAllPdps();
 
1154          * Insert this PDP as designated.  Initial standby state will be
 
1155          * either null or cold standby.
 
1158         logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
 
1159         DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, testTime.getDate());
 
1160         conn.insertPdp(pdp);
 
1161         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
 
1162         logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
 
1163                 thisPdpId, droolsPdpEntity.isDesignated());
 
1164         assertTrue(droolsPdpEntity.isDesignated() == true);
 
1166         logger.debug("testLocking1: Instantiating stateManagement object");
 
1167         StateManagement smDummy = new StateManagement(emfXacml, "dummy");
 
1168         smDummy.deleteAllStateManagementEntities();
 
1170         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
1171         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
1173         StateManagementFeatureApi sm = null;
 
1174         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
1175             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
1177             logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
 
1182         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
 
1183         // that has been created.
 
1184         ActiveStandbyFeatureApi activeStandbyFeature = null;
 
1185         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
 
1186             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
1187             activeStandbyFeature = feature;
 
1188             logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}",
 
1189                     activeStandbyFeature.getResourceName());
 
1192         assertNotNull(activeStandbyFeature);
 
1194         logger.debug("testLocking1: Runner started; Sleeping "
 
1195                 + INTERRUPT_RECOVERY_TIME + "ms before promoting PDP={}",
 
1197         sleep(INTERRUPT_RECOVERY_TIME);
 
1199         logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
 
1202         logger.debug("testLocking1: Sleeping {} ms, to allow time for "
 
1203                 + "policy-management.Main class to come up, designated= {}",
 
1204                 SLEEP_TIME, conn.getPdp(thisPdpId).isDesignated());
 
1207         logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
 
1208                 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
 
1211         IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
 
1213             droolsPdpIntegrityMonitor.startTransaction();
 
1214             droolsPdpIntegrityMonitor.endTransaction();
 
1215             logger.debug("testLocking1: As expected, transaction successful");
 
1216         } catch (AdministrativeStateException e) {
 
1217             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
 
1219         } catch (StandbyStatusException e) {
 
1220             logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
 
1222         } catch (Exception e) {
 
1223             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
 
1227         // demoting should cause state to transit to hotstandby, followed by re-promotion,
 
1228         // since there is only one PDP.
 
1229         logger.debug("testLocking1: demoting PDP={}", thisPdpId);
 
1232         logger.debug("testLocking1: sleeping" + ELECTION_WAIT_SLEEP_TIME
 
1233                 + " to allow election handler to re-promote PDP={}", thisPdpId);
 
1234         sleep(ELECTION_WAIT_SLEEP_TIME);
 
1236         logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
 
1237                 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
 
1239             droolsPdpIntegrityMonitor.startTransaction();
 
1240             droolsPdpIntegrityMonitor.endTransaction();
 
1241             logger.debug("testLocking1: As expected, transaction successful");
 
1242         } catch (AdministrativeStateException e) {
 
1243             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
 
1245         } catch (StandbyStatusException e) {
 
1246             logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
 
1248         } catch (Exception e) {
 
1249             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
 
1253         // locking should cause state to transit to cold standby
 
1254         logger.debug("testLocking1: locking PDP={}", thisPdpId);
 
1257         // Just to avoid any race conditions, sleep a little after locking
 
1258         logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
 
1261         logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
 
1262                 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
 
1264             droolsPdpIntegrityMonitor.startTransaction();
 
1265             logger.error("testLocking1: startTransaction unexpectedly successful");
 
1267         } catch (AdministrativeStateException e) {
 
1268             logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
 
1269         } catch (StandbyStatusException e) {
 
1270             logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
 
1272         } catch (Exception e) {
 
1273             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
 
1276             droolsPdpIntegrityMonitor.endTransaction();
 
1279         // unlocking should cause state to transit to hot standby and then providing service
 
1280         logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
 
1283         // Just to avoid any race conditions, sleep a little after locking
 
1284         logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
 
1285         sleep(ELECTION_WAIT_SLEEP_TIME);
 
1287         logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
 
1290                 + conn.getPdp(thisPdpId).isDesignated());
 
1292             droolsPdpIntegrityMonitor.startTransaction();
 
1293             logger.error("testLocking1: startTransaction successful as expected");
 
1294         } catch (AdministrativeStateException e) {
 
1295             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
 
1297         } catch (StandbyStatusException e) {
 
1298             logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
 
1300         } catch (Exception e) {
 
1301             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
 
1304             droolsPdpIntegrityMonitor.endTransaction();
 
1307         // demoting should cause state to transit to hot standby
 
1308         logger.debug("testLocking1: demoting PDP={}", thisPdpId);
 
1311         // Just to avoid any race conditions, sleep a little after promoting
 
1312         logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
 
1315         logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
 
1316                 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
 
1318             droolsPdpIntegrityMonitor.startTransaction();
 
1319             droolsPdpIntegrityMonitor.endTransaction();
 
1320             logger.debug("testLocking1: Unexpectedly, transaction successful");
 
1322         } catch (AdministrativeStateException e) {
 
1323             logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
 
1325         } catch (StandbyStatusException e) {
 
1326             logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
 
1327         } catch (Exception e) {
 
1328             logger.error("testLocking1: Unexpectedly caught Exception, ", e);
 
1332         logger.debug("\n\ntestLocking1: Exiting\n\n");
 
1333         sleep(INTERRUPT_RECOVERY_TIME);
 
1339      * 1) Inserts and designates this PDP, then verifies that startTransaction
 
1342      * 2) Inserts another PDP in hotstandby.
 
1344      * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
 
1345      * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
 
1351      * @throws Exception exception
 
1355     public void testLocking2() throws Exception {
 
1357         logger.debug("\n\ntestLocking2: Entering\n\n");
 
1361         Properties stateManagementProperties = loadStateManagementProperties();
 
1363         logger.debug("testLocking2: Creating emfXacml");
 
1364         final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
 
1365                 "junitXacmlPU", stateManagementProperties);
 
1367         Properties activeStandbyProperties = loadActiveStandbyProperties();
 
1368         final String thisPdpId = activeStandbyProperties
 
1369                 .getProperty(ActiveStandbyProperties.NODE_NAME);
 
1371         logger.debug("testLocking2: Creating emfDrools");
 
1372         EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
 
1373                 "junitDroolsPU", activeStandbyProperties);
 
1375         DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
 
1377         logger.debug("testLocking2: Cleaning up tables");
 
1378         conn.deleteAllPdps();
 
1381          * Insert this PDP as designated.  Initial standby state will be
 
1382          * either null or cold standby.   Demoting should transit state to
 
1386         logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
 
1387         DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, testTime.getDate());
 
1388         conn.insertPdp(pdp);
 
1389         DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
 
1390         logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
 
1391                 thisPdpId, droolsPdpEntity.isDesignated());
 
1392         assertTrue(droolsPdpEntity.isDesignated() == true);
 
1394         logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
 
1395         StateManagement smDummy = new StateManagement(emfXacml, "dummy");
 
1396         smDummy.deleteAllStateManagementEntities();
 
1398         // Now we want to create a StateManagementFeature and initialize it.  It will be
 
1399         // discovered by the ActiveStandbyFeature when the election handler initializes.
 
1401         StateManagementFeatureApi sm = null;
 
1402         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
 
1403             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
1405             logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
 
1410         // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
 
1411         // that has been created.
 
1412         ActiveStandbyFeatureApi activeStandbyFeature = null;
 
1413         for (ActiveStandbyFeatureApi feature : ActiveStandbyFeatureApiConstants.getImpl().getList()) {
 
1414             ((PolicySessionFeatureApi) feature).globalInit(null, CONFIG_DIR);
 
1415             activeStandbyFeature = feature;
 
1416             logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}",
 
1417                     activeStandbyFeature.getResourceName());
 
1420         assertNotNull(activeStandbyFeature);
 
1423          * Insert another PDP as not designated.  Initial standby state will be
 
1424          * either null or cold standby.   Demoting should transit state to
 
1428         String standbyPdpId = "pdp2";
 
1429         logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
 
1430         Date yesterday = DateUtils.addDays(testTime.getDate(), -1);
 
1431         pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
 
1432         conn.insertPdp(pdp);
 
1433         droolsPdpEntity = conn.getPdp(standbyPdpId);
 
1434         logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
 
1435                 standbyPdpId, droolsPdpEntity.isDesignated());
 
1436         assertTrue(droolsPdpEntity.isDesignated() == false);
 
1438         logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
 
1439         final StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
 
1441         logger.debug("testLocking2: Runner started; Sleeping {} ms "
 
1442                 + "before promoting/demoting", INTERRUPT_RECOVERY_TIME);
 
1443         sleep(INTERRUPT_RECOVERY_TIME);
 
1445         logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
 
1448         // demoting PDP should ensure that state transits to hotstandby
 
1449         logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
 
1452         logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", SLEEP_TIME);
 
1455         logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
 
1456                 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
 
1458         IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
 
1461             droolsPdpIntegrityMonitor.startTransaction();
 
1462             droolsPdpIntegrityMonitor.endTransaction();
 
1463             logger.debug("testLocking2: As expected, transaction successful");
 
1464         } catch (AdministrativeStateException e) {
 
1465             logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
 
1467         } catch (StandbyStatusException e) {
 
1468             logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
 
1470         } catch (Exception e) {
 
1471             logger.error("testLocking2: Unexpectedly caught Exception, ", e);
 
1475         // demoting should cause state to transit to hotstandby followed by re-promotion.
 
1476         logger.debug("testLocking2: demoting PDP={}", thisPdpId);
 
1479         logger.debug("testLocking2: sleeping {}"
 
1480                 + " to allow election handler to re-promote PDP={}", ELECTION_WAIT_SLEEP_TIME, thisPdpId);
 
1481         sleep(ELECTION_WAIT_SLEEP_TIME);
 
1483         logger.debug("testLocking2: Waking up and invoking startTransaction "
 
1484                 + "on re-promoted PDP= {}, designated= {}",
 
1485                 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
 
1487             droolsPdpIntegrityMonitor.startTransaction();
 
1488             droolsPdpIntegrityMonitor.endTransaction();
 
1489             logger.debug("testLocking2: As expected, transaction successful");
 
1490         } catch (AdministrativeStateException e) {
 
1491             logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
 
1493         } catch (StandbyStatusException e) {
 
1494             logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
 
1496         } catch (Exception e) {
 
1497             logger.error("testLocking2: Unexpectedly caught Exception, ", e);
 
1501         logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
 
1502         boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
 
1503         assertTrue(standbyPdpDesignated == false);
 
1505         logger.debug("\n\ntestLocking2: Exiting\n\n");
 
1506         sleep(INTERRUPT_RECOVERY_TIME);
 
1509     private static Properties loadStateManagementProperties() throws IOException {
 
1510         try (FileInputStream input = new FileInputStream(CONFIG_DIR + "/feature-state-management.properties")) {
 
1511             Properties props = new Properties();
 
1517     private static Properties loadActiveStandbyProperties() throws IOException {
 
1518         try (FileInputStream input =
 
1519                         new FileInputStream(CONFIG_DIR + "/feature-active-standby-management.properties")) {
 
1520             Properties props = new Properties();
 
1526     private void sleep(long sleepms) throws InterruptedException {
 
1527         testTime.waitFor(sleepms);