You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ji...@apache.org on 2016/03/31 17:23:15 UTC

[52/64] [abbrv] incubator-geode git commit: GEODE-693: refactor security dunit tests

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/22ca5ef8/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientAuthzObjectModDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientAuthzObjectModDUnitTest.java b/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientAuthzObjectModDUnitTest.java
index 346dff8..0e8e57f 100644
--- a/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientAuthzObjectModDUnitTest.java
+++ b/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientAuthzObjectModDUnitTest.java
@@ -16,28 +16,30 @@
  */
 package com.gemstone.gemfire.security;
 
+import static com.gemstone.gemfire.distributed.internal.DistributionConfig.*;
+import static com.gemstone.gemfire.internal.AvailablePort.*;
+import static com.gemstone.gemfire.security.SecurityTestUtils.*;
+import static com.gemstone.gemfire.test.dunit.LogWriterUtils.*;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 import java.util.Random;
 
+import com.gemstone.gemfire.DataSerializable;
+import com.gemstone.gemfire.Instantiator;
+import com.gemstone.gemfire.cache.operations.OperationContext.OperationCode;
 import com.gemstone.gemfire.internal.security.FilterPostAuthorization;
 import com.gemstone.gemfire.internal.security.FilterPreAuthorization;
+import com.gemstone.gemfire.internal.security.ObjectWithAuthz;
 import com.gemstone.gemfire.security.generator.CredentialGenerator;
 import com.gemstone.gemfire.security.generator.DummyAuthzCredentialGenerator;
 import com.gemstone.gemfire.security.generator.DummyCredentialGenerator;
 import com.gemstone.gemfire.security.templates.UserPasswordAuthInit;
-
-import com.gemstone.gemfire.DataSerializable;
-import com.gemstone.gemfire.Instantiator;
-import com.gemstone.gemfire.cache.operations.OperationContext.OperationCode;
-import com.gemstone.gemfire.distributed.internal.DistributionConfig;
-import com.gemstone.gemfire.internal.AvailablePort;
-import com.gemstone.gemfire.internal.security.ObjectWithAuthz;
-import com.gemstone.gemfire.test.dunit.DistributedTestCase;
-import com.gemstone.gemfire.test.dunit.Host;
-import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 import com.gemstone.gemfire.test.dunit.SerializableRunnable;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
 
 /**
  * Tests for authorization callback that modify objects and callbacks from
@@ -55,73 +57,14 @@ import com.gemstone.gemfire.test.dunit.SerializableRunnable;
  * 
  * @since 5.5
  */
-public class ClientAuthzObjectModDUnitTest extends ClientAuthorizationTestBase {
-
-
-  /** constructor */
-  public ClientAuthzObjectModDUnitTest(String name) {
-    super(name);
-  }
+@Category(DistributedTest.class)
+public class ClientAuthzObjectModDUnitTest extends ClientAuthorizationTestCase {
 
   private static final String preAccessor = FilterPreAuthorization.class.getName() + ".create";
-
   private static final String postAccessor = FilterPostAuthorization.class.getName() + ".create";
 
-  private static class TestPostCredentialGenerator implements
-      TestCredentialGenerator {
-
-    public TestPostCredentialGenerator() {
-    }
-
-    public Properties getAllowedCredentials(OperationCode[] opCodes,
-        String[] regionNames, int[] keyIndices, int num) {
-
-      int userIndex = 1;
-      byte role = DummyAuthzCredentialGenerator.getRequiredRole(opCodes);
-      if (role == DummyAuthzCredentialGenerator.READER_ROLE) {
-        userIndex = keyIndices[0] + 1;
-      }
-      Properties props = new Properties();
-      props.setProperty(UserPasswordAuthInit.USER_NAME, "user" + userIndex);
-      props.setProperty(UserPasswordAuthInit.PASSWORD, "user" + userIndex);
-      return props;
-    }
-
-    public Properties getDisallowedCredentials(OperationCode[] opCodes,
-        String[] regionNames, int[] keyIndices, int num) {
-
-      int userIndex = 0;
-      for (int index = 0; index < keyIndices.length; ++index) {
-        if (keyIndices[index] != index) {
-          userIndex = index + 1;
-          break;
-        }
-      }
-      Properties props = new Properties();
-      props.setProperty(UserPasswordAuthInit.USER_NAME, "gemfire" + userIndex);
-      props.setProperty(UserPasswordAuthInit.PASSWORD, "gemfire" + userIndex);
-      return props;
-    }
-
-    public CredentialGenerator getCredentialGenerator() {
-
-      return null;
-    }
-  }
-
   @Override
-  public final void postSetUp() throws Exception {
-    final Host host = Host.getHost(0);
-    server1 = host.getVM(0);
-    server2 = host.getVM(1);
-    client1 = host.getVM(2);
-    client2 = host.getVM(3);
-
-    server1.invoke(() -> SecurityTestUtil.registerExpectedExceptions( serverExpectedExceptions ));
-    server2.invoke(() -> SecurityTestUtil.registerExpectedExceptions( serverExpectedExceptions ));
-    client2.invoke(() -> SecurityTestUtil.registerExpectedExceptions( clientExpectedExceptions ));
-    SecurityTestUtil.registerExpectedExceptions(clientExpectedExceptions);
-
+  protected final void postSetUpClientAuthorizationTestBase() throws Exception {
     //required by FilterPreAuthorization and FilterPostAuthorization. Normally,
     //this would be automatically registered in the static initializer, but with dunit
     //a previous test may have already loaded these classes. We clear the instantiators
@@ -135,271 +78,208 @@ public class ClientAuthzObjectModDUnitTest extends ClientAuthorizationTestBase {
     server2.invoke(registerInstantiator);
   }
   
-  // Region: Utility and static functions invoked by the tests
-
-  private static Properties buildProperties(String authenticator,
-      Properties extraProps, String preAccessor, String postAccessor) {
+  @Test
+  public void testAllOpsObjectModWithFailover() throws Exception {
+    OperationWithAction[] allOps = allOps();
 
-    Properties authProps = new Properties();
-    if (authenticator != null) {
-      authProps.setProperty(
-          DistributionConfig.SECURITY_CLIENT_AUTHENTICATOR_NAME, authenticator);
-    }
-    if (preAccessor != null) {
-      authProps.setProperty(DistributionConfig.SECURITY_CLIENT_ACCESSOR_NAME,
-          preAccessor);
-    }
-    if (postAccessor != null) {
-      authProps.setProperty(
-          DistributionConfig.SECURITY_CLIENT_ACCESSOR_PP_NAME, postAccessor);
-    }
-    if (extraProps != null) {
-      authProps.putAll(extraProps);
-    }
-    return authProps;
-  }
+    TestPostCredentialGenerator tgen = new TestPostCredentialGenerator();
 
-  public static Integer createCacheServer(Integer mcastPort,
-      Properties authProps) {
+    CredentialGenerator credentialGenerator = new DummyCredentialGenerator();
+    credentialGenerator.init();
+    Properties extraProps = credentialGenerator.getSystemProperties();
+    Properties javaProps = credentialGenerator.getJavaProperties();
+    String authInit = credentialGenerator.getAuthInit();
+    String authenticator = credentialGenerator.getAuthenticator();
 
-    if (mcastPort == null) {
-      mcastPort = new Integer(AvailablePort
-          .getRandomAvailablePort(AvailablePort.SOCKET));
-    }
-    return SecurityTestUtil.createCacheServer(authProps, null, mcastPort, null,
-        null, Boolean.FALSE, new Integer(SecurityTestUtil.NO_EXCEPTION));
-  }
+    getLogWriter().info("testPutsGetsObjectModWithFailover: Using authinit: " + authInit);
+    getLogWriter().info("testPutsGetsObjectModWithFailover: Using authenticator: " + authenticator);
+    getLogWriter().info("testPutsGetsObjectModWithFailover: Using pre-operation accessor: " + preAccessor);
+    getLogWriter().info("testPutsGetsObjectModWithFailover: Using post-operation accessor: " + postAccessor);
 
-  public static void createCacheServer(Integer loctorPort, Integer serverPort,
-      Properties authProps) {
+    // Start servers with all required properties
+    Properties serverProps = buildProperties(authenticator, extraProps, preAccessor, postAccessor);
 
-    if (loctorPort == null) {
-      loctorPort = new Integer(AvailablePort
-          .getRandomAvailablePort(AvailablePort.SOCKET));
-    }
-    SecurityTestUtil.createCacheServer(authProps, null, loctorPort, null,
-        serverPort, Boolean.FALSE, new Integer(SecurityTestUtil.NO_EXCEPTION));
-  }
+    // Get ports for the servers
+    int port1 = getRandomAvailablePort(SOCKET);
+    int port2 =getRandomAvailablePort(SOCKET);
 
-  // End Region: Utility and static functions invoked by the tests
+    // Perform all the ops on the clients
+    List opBlock = new ArrayList();
+    Random rnd = new Random();
 
-  // Region: Tests
+    for (int opNum = 0; opNum < allOps.length; ++opNum) {
+      // Start client with valid credentials as specified in OperationWithAction
+      OperationWithAction currentOp = allOps[opNum];
+      if (currentOp.equals(OperationWithAction.OPBLOCK_END) || currentOp.equals(OperationWithAction.OPBLOCK_NO_FAILOVER)) {
+        // End of current operation block; execute all the operations on the servers with failover
+        if (opBlock.size() > 0) {
+          // Start the first server and execute the operation block
+          server1.invoke(() -> createCacheServer(getLocatorPort(), port1, serverProps, javaProps ));
+          server2.invoke(() -> closeCache());
+          executeOpBlock(opBlock, port1, port2, authInit, extraProps, null, tgen, rnd);
+          if (!currentOp.equals(OperationWithAction.OPBLOCK_NO_FAILOVER)) {
+            // Failover to the second server and run the block again
+            server2.invoke(() -> createCacheServer(getLocatorPort(), port2, serverProps, javaProps ));
+            server1.invoke(() -> closeCache());
+            executeOpBlock(opBlock, port1, port2, authInit, extraProps, null, tgen, rnd);
+          }
+          opBlock.clear();
+        }
 
-  public void testAllOpsObjectModWithFailover() {
+      } else {
+        currentOp.setOpNum(opNum);
+        opBlock.add(currentOp);
+      }
+    }
+  }
 
-    OperationWithAction[] allOps = {
+  private OperationWithAction[] allOps() {
+    return new OperationWithAction[] {
         // Perform CREATE and verify with GET
         new OperationWithAction(OperationCode.PUT, 1, OpFlags.NONE, 8),
-        // For second client connect with valid credentials for key2, key4,
-        // key6, key8 and check that other keys are not accessible
-        new OperationWithAction(OperationCode.GET, 2, OpFlags.CHECK_NOKEY,
-            new int[] { 1, 3, 5, 7 }),
-        new OperationWithAction(OperationCode.GET, 2, OpFlags.CHECK_NOKEY
-            | OpFlags.USE_OLDCONN | OpFlags.CHECK_NOTAUTHZ, new int[] { 0, 2,
-            4, 6 }),
-        // For third client check that key3, key6 are accessible but others are
-        // not
-        new OperationWithAction(OperationCode.GET, 3, OpFlags.CHECK_NOKEY,
-            new int[] { 2, 5 }),
-        new OperationWithAction(OperationCode.GET, 3, OpFlags.CHECK_NOKEY
-            | OpFlags.USE_OLDCONN | OpFlags.CHECK_NOTAUTHZ, new int[] { 0, 1,
-            3, 4, 6, 7 }),
-
-        // OPBLOCK_END indicates end of an operation block that needs to
-        // be executed on each server when doing failover
+        // For second client connect with valid credentials for key2, key4, key6, key8 and check that other KEYS are not accessible
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.CHECK_NOKEY, new int[]{1, 3, 5, 7}),
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.CHECK_NOKEY | OpFlags.USE_OLDCONN | OpFlags.CHECK_NOTAUTHZ, new int[]{0, 2, 4, 6}),
+        // For third client check that key3, key6 are accessible but others are not
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.CHECK_NOKEY, new int[]{2, 5}),
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.CHECK_NOKEY | OpFlags.USE_OLDCONN | OpFlags.CHECK_NOTAUTHZ, new int[]{0, 1, 3, 4, 6, 7}),
+
+        // OPBLOCK_END indicates end of an operation block that needs to be executed on each server when doing failover
         OperationWithAction.OPBLOCK_END,
 
         // Perform UPDATE and verify with GET
-        new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, 8),
-        // For second client check that key2, key4, key6, key8 are accessible
-        // but others are not
-        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, new int[] { 1, 3, 5, 7 }),
-        new OperationWithAction(OperationCode.GET, 2,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY
-                | OpFlags.CHECK_NOTAUTHZ, new int[] { 0, 2, 4, 6 }),
-        // For third client check that key3, key6 are accessible but others are
-        // not
-        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, new int[] { 2, 5 }),
-        new OperationWithAction(OperationCode.GET, 3,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY
-                | OpFlags.CHECK_NOTAUTHZ, new int[] { 0, 1, 3, 4, 6, 7 }),
+        new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, 8),
+        // For second client check that key2, key4, key6, key8 are accessible but others are not
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[]{1, 3, 5, 7}),
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY | OpFlags.CHECK_NOTAUTHZ, new int[]{0, 2, 4, 6}),
+        // For third client check that key3, key6 are accessible but others are not
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[]{2, 5}),
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY | OpFlags.CHECK_NOTAUTHZ, new int[]{0, 1, 3, 4, 6, 7}),
 
         OperationWithAction.OPBLOCK_END,
 
         // Perform UPDATE and verify with GET_ALL
-        new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, 8),
-        // For second client check that key2, key4, key6, key8 are accessible
-        // but others are not; getAll test in doOp uses a combination of local
-        // entries and remote fetches
-        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS,
-            new int[] { 1, 3, 5, 7 }),
-        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS | OpFlags.CHECK_NOKEY
-            | OpFlags.CHECK_FAIL, new int[] { 0, 2, 4, 6 }),
-        // For third client check that key3, key6 are accessible but others are
-        // not
-        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS, new int[] { 2, 5 }),
-        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS | OpFlags.CHECK_NOKEY
-            | OpFlags.CHECK_FAIL, new int[] { 0, 1, 3, 4, 6, 7 }),
-
-        // locally destroy the keys to also test create after failover
-        new OperationWithAction(OperationCode.DESTROY, 1, OpFlags.USE_OLDCONN
-            | OpFlags.LOCAL_OP, 8),
+        new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, 8),
+        // For second client check that key2, key4, key6, key8 are accessible but others are not; getAll test in doOp uses a combination of local entries and remote fetches
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS, new int[]{1, 3, 5, 7}),
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS | OpFlags.CHECK_NOKEY | OpFlags.CHECK_FAIL, new int[]{0, 2, 4, 6}),
+        // For third client check that key3, key6 are accessible but others are not
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS, new int[]{2, 5}),
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.USE_ALL_KEYS | OpFlags.CHECK_NOKEY | OpFlags.CHECK_FAIL, new int[]{0, 1, 3, 4, 6, 7}),
+
+        // locally destroy the KEYS to also test create after failover
+        new OperationWithAction(OperationCode.DESTROY, 1, OpFlags.USE_OLDCONN | OpFlags.LOCAL_OP, 8),
 
         OperationWithAction.OPBLOCK_END,
 
         // Perform PUTALL and verify with GET
-        new OperationWithAction(OperationCode.PUTALL, 1, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, 8),
-        // For second client check that key2, key4, key6, key8 are accessible
-        // but others are not
-        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, new int[] { 1, 3, 5, 7 }),
-        new OperationWithAction(OperationCode.GET, 2,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY
-                | OpFlags.CHECK_NOTAUTHZ, new int[] { 0, 2, 4, 6 }),
-        // For third client check that key3, key6 are accessible but others are
-        // not
-        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, new int[] { 2, 5 }),
-        new OperationWithAction(OperationCode.GET, 3,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY
-                | OpFlags.CHECK_NOTAUTHZ, new int[] { 0, 1, 3, 4, 6, 7 }),
+        new OperationWithAction(OperationCode.PUTALL, 1, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, 8),
+        // For second client check that key2, key4, key6, key8 are accessible but others are not
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[]{1, 3, 5, 7}),
+        new OperationWithAction(OperationCode.GET, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY | OpFlags.CHECK_NOTAUTHZ, new int[]{0, 2, 4, 6}),
+        // For third client check that key3, key6 are accessible but others are not
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[]{2, 5}),
+        new OperationWithAction(OperationCode.GET, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_NOKEY | OpFlags.CHECK_NOTAUTHZ, new int[]{0, 1, 3, 4, 6, 7}),
 
         OperationWithAction.OPBLOCK_END,
-        
+
         // Test UPDATE and verify with a QUERY
         new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN, 8),
-        // For second client check that key2, key4, key6, key8 are accessible
-        // but others are not
-        new OperationWithAction(OperationCode.QUERY, 2, OpFlags.USE_OLDCONN,
-            new int[] { 1, 3, 5, 7 }),
-        new OperationWithAction(OperationCode.QUERY, 2, OpFlags.USE_OLDCONN
-            | OpFlags.CHECK_FAIL, new int[] { 0, 2, 4, 6 }),
-        // For third client check that key3, key6 are accessible but others are
-        // not
-        new OperationWithAction(OperationCode.QUERY, 3, OpFlags.USE_OLDCONN,
-            new int[] { 2, 5 }),
-        new OperationWithAction(OperationCode.QUERY, 3, OpFlags.USE_OLDCONN
-            | OpFlags.CHECK_FAIL, new int[] { 0, 1, 3, 4, 6, 7 }),
+        // For second client check that key2, key4, key6, key8 are accessible but others are not
+        new OperationWithAction(OperationCode.QUERY, 2, OpFlags.USE_OLDCONN, new int[]{1, 3, 5, 7}),
+        new OperationWithAction(OperationCode.QUERY, 2, OpFlags.USE_OLDCONN | OpFlags.CHECK_FAIL, new int[]{0, 2, 4, 6}),
+        // For third client check that key3, key6 are accessible but others are not
+        new OperationWithAction(OperationCode.QUERY, 3, OpFlags.USE_OLDCONN, new int[]{2, 5}),
+        new OperationWithAction(OperationCode.QUERY, 3, OpFlags.USE_OLDCONN | OpFlags.CHECK_FAIL, new int[]{0, 1, 3, 4, 6, 7}),
 
         OperationWithAction.OPBLOCK_END,
 
         // Test UPDATE and verify with a EXECUTE_CQ initial results
-        new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN
-            | OpFlags.USE_NEWVAL, 8),
-        // For second client check that key2, key4, key6, key8 are accessible
-        // but others are not
-        new OperationWithAction(OperationCode.EXECUTE_CQ, 2,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[] { 1, 3, 5, 7 }),
-        new OperationWithAction(OperationCode.CLOSE_CQ, 2, OpFlags.USE_OLDCONN,
-            1),
-        new OperationWithAction(OperationCode.EXECUTE_CQ, 2,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_FAIL,
-            new int[] { 0, 2, 4, 6 }),
-        new OperationWithAction(OperationCode.CLOSE_CQ, 2, OpFlags.USE_OLDCONN,
-            1),
-        // For third client check that key3, key6 are accessible but others are
-        // not
-        new OperationWithAction(OperationCode.EXECUTE_CQ, 3,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[] { 2, 5 }),
-        new OperationWithAction(OperationCode.CLOSE_CQ, 3, OpFlags.USE_OLDCONN,
-            1),
-        new OperationWithAction(OperationCode.EXECUTE_CQ, 3,
-            OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_FAIL,
-            new int[] { 0, 1, 3, 4, 6, 7 }),
-        new OperationWithAction(OperationCode.CLOSE_CQ, 3, OpFlags.USE_OLDCONN,
-            1),
-
-        OperationWithAction.OPBLOCK_END };
-
-    TestPostCredentialGenerator tgen = new TestPostCredentialGenerator();
-
-    CredentialGenerator gen = new DummyCredentialGenerator();
-    gen.init();
-    Properties extraProps = gen.getSystemProperties();
-    Properties javaProps = gen.getJavaProperties();
-    String authInit = gen.getAuthInit();
-    String authenticator = gen.getAuthenticator();
-
-    LogWriterUtils.getLogWriter().info(
-        "testPutsGetsObjectModWithFailover: Using authinit: " + authInit);
-    LogWriterUtils.getLogWriter().info(
-        "testPutsGetsObjectModWithFailover: Using authenticator: "
-            + authenticator);
-    LogWriterUtils.getLogWriter().info(
-        "testPutsGetsObjectModWithFailover: Using pre-operation accessor: "
-            + preAccessor);
-    LogWriterUtils.getLogWriter().info(
-        "testPutsGetsObjectModWithFailover: Using post-operation accessor: "
-            + postAccessor);
+        new OperationWithAction(OperationCode.PUT, 1, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, 8),
+        // For second client check that key2, key4, key6, key8 are accessible but others are not
+        new OperationWithAction(OperationCode.EXECUTE_CQ, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[]{1, 3, 5, 7}),
+        new OperationWithAction(OperationCode.CLOSE_CQ, 2, OpFlags.USE_OLDCONN, 1),
+        new OperationWithAction(OperationCode.EXECUTE_CQ, 2, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_FAIL, new int[]{0, 2, 4, 6}),
+        new OperationWithAction(OperationCode.CLOSE_CQ, 2, OpFlags.USE_OLDCONN, 1),
+        // For third client check that key3, key6 are accessible but others are not
+        new OperationWithAction(OperationCode.EXECUTE_CQ, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL, new int[]{2, 5}),
+        new OperationWithAction(OperationCode.CLOSE_CQ, 3, OpFlags.USE_OLDCONN, 1),
+        new OperationWithAction(OperationCode.EXECUTE_CQ, 3, OpFlags.USE_OLDCONN | OpFlags.USE_NEWVAL | OpFlags.CHECK_FAIL, new int[]{0, 1, 3, 4, 6, 7}),
+        new OperationWithAction(OperationCode.CLOSE_CQ, 3, OpFlags.USE_OLDCONN, 1),
+
+        OperationWithAction.OPBLOCK_END};
+  }
 
-    // Start servers with all required properties
-    Properties serverProps = buildProperties(authenticator, extraProps,
-        preAccessor, postAccessor);
-    // Get ports for the servers
-    Integer port1 = new Integer(AvailablePort
-        .getRandomAvailablePort(AvailablePort.SOCKET));
-    Integer port2 = new Integer(AvailablePort
-        .getRandomAvailablePort(AvailablePort.SOCKET));
 
-    // Perform all the ops on the clients
-    List opBlock = new ArrayList();
-    Random rnd = new Random();
-    for (int opNum = 0; opNum < allOps.length; ++opNum) {
-      // Start client with valid credentials as specified in
-      // OperationWithAction
-      OperationWithAction currentOp = allOps[opNum];
-      if (currentOp.equals(OperationWithAction.OPBLOCK_END)
-          || currentOp.equals(OperationWithAction.OPBLOCK_NO_FAILOVER)) {
-        // End of current operation block; execute all the operations
-        // on the servers with failover
-        if (opBlock.size() > 0) {
-          // Start the first server and execute the operation block
-          server1.invoke(() -> ClientAuthorizationTestBase.createCacheServer(
-                  SecurityTestUtil.getLocatorPort(), port1, serverProps,
-                  javaProps ));
-          server2.invoke(() -> SecurityTestUtil.closeCache());
-          executeOpBlock(opBlock, port1, port2, authInit, extraProps, null,
-              tgen, rnd);
-          if (!currentOp.equals(OperationWithAction.OPBLOCK_NO_FAILOVER)) {
-            // Failover to the second server and run the block again
-            server2.invoke(() -> ClientAuthorizationTestBase.createCacheServer(
-                    SecurityTestUtil.getLocatorPort(), port2, serverProps,
-                    javaProps ));
-            server1.invoke(() -> SecurityTestUtil.closeCache());
-            executeOpBlock(opBlock, port1, port2, authInit, extraProps, null,
-                tgen, rnd);
-          }
-          opBlock.clear();
-        }
-      }
-      else {
-        currentOp.setOpNum(opNum);
-        opBlock.add(currentOp);
-      }
+  private Properties buildProperties(final String authenticator, final Properties extraProps, final String preAccessor, final String postAccessor) {
+    Properties authProps = new Properties();
+    if (authenticator != null) {
+      authProps.setProperty(SECURITY_CLIENT_AUTHENTICATOR_NAME, authenticator);
+    }
+    if (preAccessor != null) {
+      authProps.setProperty(SECURITY_CLIENT_ACCESSOR_NAME, preAccessor);
+    }
+    if (postAccessor != null) {
+      authProps.setProperty(SECURITY_CLIENT_ACCESSOR_PP_NAME, postAccessor);
     }
+    if (extraProps != null) {
+      authProps.putAll(extraProps);
+    }
+    return authProps;
   }
 
   private static class MyInstantiator extends Instantiator {
 
-    public MyInstantiator(Class clazz, int classId) {
-      super(clazz, classId);
-    }
     public MyInstantiator() {
       this(ObjectWithAuthz.class, ObjectWithAuthz.CLASSID);
     }
 
+    public MyInstantiator(final Class clazz, final int classId) {
+      super(clazz, classId);
+    }
+
+    @Override
     public DataSerializable newInstance() {
       return new ObjectWithAuthz();
     }
   }
 
+  private static class TestPostCredentialGenerator implements TestCredentialGenerator {
+
+    public TestPostCredentialGenerator() {
+    }
+
+    @Override
+    public Properties getAllowedCredentials(final OperationCode[] opCodes, final String[] regionNames, final int[] keyIndices, final int num) {
+      int userIndex = 1;
+      byte role = DummyAuthzCredentialGenerator.getRequiredRole(opCodes);
+      if (role == DummyAuthzCredentialGenerator.READER_ROLE) {
+        userIndex = keyIndices[0] + 1;
+      }
+      Properties props = new Properties();
+      props.setProperty(UserPasswordAuthInit.USER_NAME, "user" + userIndex);
+      props.setProperty(UserPasswordAuthInit.PASSWORD, "user" + userIndex);
+      return props;
+    }
+
+    @Override
+    public Properties getDisallowedCredentials(final OperationCode[] opCodes, final String[] regionNames, final int[] keyIndices, final int num) {
+      int userIndex = 0;
+      for (int index = 0; index < keyIndices.length; ++index) {
+        if (keyIndices[index] != index) {
+          userIndex = index + 1;
+          break;
+        }
+      }
+      Properties props = new Properties();
+      props.setProperty(UserPasswordAuthInit.USER_NAME, "gemfire" + userIndex);
+      props.setProperty(UserPasswordAuthInit.PASSWORD, "gemfire" + userIndex);
+      return props;
+    }
+
+    @Override
+    public CredentialGenerator getCredentialGenerator() {
+      return null;
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/22ca5ef8/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientCQPostAuthorizationDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientCQPostAuthorizationDUnitTest.java b/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientCQPostAuthorizationDUnitTest.java
index e130601..dfa89f9 100644
--- a/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientCQPostAuthorizationDUnitTest.java
+++ b/geode-cq/src/test/java/com/gemstone/gemfire/security/ClientCQPostAuthorizationDUnitTest.java
@@ -16,6 +16,13 @@
  */
 package com.gemstone.gemfire.security;
 
+import static com.gemstone.gemfire.internal.AvailablePort.*;
+import static com.gemstone.gemfire.security.SecurityTestUtils.*;
+import static com.gemstone.gemfire.test.dunit.Assert.*;
+import static com.gemstone.gemfire.test.dunit.Invoke.*;
+import static com.gemstone.gemfire.test.dunit.LogWriterUtils.*;
+import static com.gemstone.gemfire.test.dunit.Wait.*;
+
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
@@ -27,76 +34,51 @@ import com.gemstone.gemfire.cache.operations.OperationContext.OperationCode;
 import com.gemstone.gemfire.cache.query.CqAttributes;
 import com.gemstone.gemfire.cache.query.CqAttributesFactory;
 import com.gemstone.gemfire.cache.query.CqException;
+import com.gemstone.gemfire.cache.query.CqExistsException;
 import com.gemstone.gemfire.cache.query.CqListener;
 import com.gemstone.gemfire.cache.query.CqQuery;
 import com.gemstone.gemfire.cache.query.QueryService;
+import com.gemstone.gemfire.cache.query.RegionNotFoundException;
 import com.gemstone.gemfire.cache.query.SelectResults;
 import com.gemstone.gemfire.cache.query.cq.dunit.CqQueryTestListener;
 import com.gemstone.gemfire.cache.query.internal.cq.ClientCQImpl;
 import com.gemstone.gemfire.cache.query.internal.cq.CqService;
 import com.gemstone.gemfire.cache.query.internal.cq.InternalCqQuery;
-import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
-import com.gemstone.gemfire.internal.AvailablePort;
 import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
-import com.gemstone.gemfire.internal.logging.InternalLogWriter;
 import com.gemstone.gemfire.security.generator.AuthzCredentialGenerator;
 import com.gemstone.gemfire.security.generator.CredentialGenerator;
-import com.gemstone.gemfire.test.dunit.Host;
-import com.gemstone.gemfire.test.dunit.Invoke;
-import com.gemstone.gemfire.test.dunit.LogWriterUtils;
 import com.gemstone.gemfire.test.dunit.SerializableRunnable;
-import com.gemstone.gemfire.test.dunit.Wait;
 import com.gemstone.gemfire.test.dunit.WaitCriterion;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
 /**
  * This is for multiuser-authentication
- * 
- *
  */
-public class ClientCQPostAuthorizationDUnitTest extends
-    ClientAuthorizationTestBase {
-
-//  public static final String regionName = "ClientCQPostAuthorizationDUnitTest_region";
-
-  public static final Map<String, String> cqNameToQueryStrings = new HashMap<String, String>();
-
-  static {
-    cqNameToQueryStrings.put("CQ_0", "SELECT * FROM ");
-    cqNameToQueryStrings.put("CQ_1", "SELECT * FROM ");
-  }
+@Category(DistributedTest.class)
+public class ClientCQPostAuthorizationDUnitTest extends ClientAuthorizationTestCase {
 
-  public ClientCQPostAuthorizationDUnitTest(String name) {
-    super(name);
-  }
+  private Map<String, String> cqNameToQueryStrings = new HashMap<>();
 
   @Override
-  public final void postSetUp() throws Exception {
+  protected final void preSetUpClientAuthorizationTestBase() throws Exception {
     getSystem();
-    Invoke.invokeInEveryVM(new SerializableRunnable("getSystem") {
+    invokeInEveryVM(new SerializableRunnable("getSystem") {
       public void run() {
         getSystem();
       }
     });
-
-    final Host host = Host.getHost(0);
-    server1 = host.getVM(0);
-    server2 = host.getVM(1);
-    client1 = host.getVM(2);
-    client2 = host.getVM(3);
-
-    server1.invoke(() -> SecurityTestUtil.registerExpectedExceptions( serverExpectedExceptions ));
-    server2.invoke(() -> SecurityTestUtil.registerExpectedExceptions( serverExpectedExceptions ));
-    client2.invoke(() -> SecurityTestUtil.registerExpectedExceptions( clientExpectedExceptions ));
-    SecurityTestUtil.registerExpectedExceptions(clientExpectedExceptions);
+    this.cqNameToQueryStrings.put("CQ_0", "SELECT * FROM ");
+    this.cqNameToQueryStrings.put("CQ_1", "SELECT * FROM ");
   }
 
   @Override
-  public final void preTearDown() throws Exception {
-    client1.invoke(() -> SecurityTestUtil.closeCache());
-    client2.invoke(() -> SecurityTestUtil.closeCache());
-    server1.invoke(() -> SecurityTestUtil.closeCache());
-    server2.invoke(() -> SecurityTestUtil.closeCache());
+  public final void postTearDownClientAuthorizationTestBase() throws Exception {
+    this.cqNameToQueryStrings.clear();
   }
 
+  @Test
   public void testAllowCQForAllMultiusers() throws Exception {
     /*
      * Start a server
@@ -105,10 +87,10 @@ public class ClientCQPostAuthorizationDUnitTest extends
      * Client2 does some operations on the region which satisfies both the CQs
      * Validate that listeners for both the CQs are invoked.
      */
-    doStartUp(Integer.valueOf(2), Integer.valueOf(5), new Boolean[] {true,
-      true});
+    doStartUp(2, 5, new boolean[] {true, true}, false);
   }
 
+  @Test
   public void testDisallowCQForAllMultiusers() throws Exception {
     /*
      * Start a server
@@ -117,10 +99,10 @@ public class ClientCQPostAuthorizationDUnitTest extends
      * Client2 does some operations on the region which satisfies both the CQs
      * Validate that listeners for none of the CQs are invoked.
      */
-    doStartUp(Integer.valueOf(2), Integer.valueOf(5), new Boolean[] {false,
-      false});
+    doStartUp(2, 5, new boolean[] {false, false}, false);
   }
 
+  @Test
   public void testDisallowCQForSomeMultiusers() throws Exception {
     /*
      * Start a server
@@ -130,10 +112,10 @@ public class ClientCQPostAuthorizationDUnitTest extends
      * Client2 does some operations on the region which satisfies both the CQs
      * Validate that listener for User1's CQ is invoked but that for User2's CQ is not invoked.
      */
-    doStartUp(Integer.valueOf(2), Integer.valueOf(5), new Boolean[] {true,
-        false});
+    doStartUp(2, 5, new boolean[] {true, false}, false);
   }
 
+  @Test
   public void testAllowCQForAllMultiusersWithFailover() throws Exception {
     /*
      * Start a server1
@@ -145,204 +127,137 @@ public class ClientCQPostAuthorizationDUnitTest extends
      * Client2 does some operations on the region which satisfies both the CQs
      * Validate that listeners for both the CQs are get updates.
      */
-    doStartUp(Integer.valueOf(2), Integer.valueOf(5), new Boolean[] {true,
-      true}, Boolean.TRUE);
+    doStartUp(2, 5, new boolean[] {true, true}, true);
   }
 
-  public void doStartUp(Integer numOfUsers, Integer numOfPuts,
-      Boolean[] postAuthzAllowed) throws Exception {
-    doStartUp(numOfUsers, numOfPuts, postAuthzAllowed, Boolean.FALSE /* failover */);
-  }
+  private void doStartUp(final int numOfUsers, final int numOfPuts, final boolean[] postAuthzAllowed, final boolean failover) throws Exception {
+    AuthzCredentialGenerator authzGenerator = getXmlAuthzGenerator();
+    CredentialGenerator credentialGenerator = authzGenerator.getCredentialGenerator();
+    Properties extraAuthProps = credentialGenerator.getSystemProperties();
+    Properties javaProps = credentialGenerator.getJavaProperties();
+    Properties extraAuthzProps = authzGenerator.getSystemProperties();
+    String authenticator = credentialGenerator.getAuthenticator();
+    String accessor = authzGenerator.getAuthorizationCallback();
+    String authInit = credentialGenerator.getAuthInit();
+    TestAuthzCredentialGenerator tgen = new TestAuthzCredentialGenerator(authzGenerator);
+
+    Properties serverProps = buildProperties(authenticator, accessor, true, extraAuthProps, extraAuthzProps);
+
+    Properties opCredentials;
+    credentialGenerator = tgen.getCredentialGenerator();
+    final Properties javaProps2 = credentialGenerator == null ? null : credentialGenerator.getJavaProperties();
+
+    int[] indices = new int[numOfPuts];
+    for (int index = 0; index < numOfPuts; ++index) {
+      indices[index] = index;
+    }
 
-  public void doStartUp(Integer numOfUsers, Integer numOfPuts,
-      Boolean[] postAuthzAllowed, Boolean failover) throws Exception {
-      AuthzCredentialGenerator gen = this.getXmlAuthzGenerator();
-      CredentialGenerator cGen = gen.getCredentialGenerator();
-      Properties extraAuthProps = cGen.getSystemProperties();
-      Properties javaProps = cGen.getJavaProperties();
-      Properties extraAuthzProps = gen.getSystemProperties();
-      String authenticator = cGen.getAuthenticator();
-      String accessor = gen.getAuthorizationCallback();
-      String authInit = cGen.getAuthInit();
-      TestAuthzCredentialGenerator tgen = new TestAuthzCredentialGenerator(gen);
-
-      Properties serverProps = buildProperties(authenticator, accessor, true,
-          extraAuthProps, extraAuthzProps);
-
-      Properties opCredentials;
-      cGen = tgen.getCredentialGenerator();
-      Properties javaProps2 = null;
-      if (cGen != null) {
-        javaProps2 = cGen.getJavaProperties();
-      }
+    Random rnd = new Random();
+    Properties[] authProps = new Properties[numOfUsers];
+    for (int i = 0; i < numOfUsers; i++) {
+      int rand = rnd.nextInt(100) + 1;
 
-      int[] indices = new int[numOfPuts];
-      for (int index = 0; index < numOfPuts; ++index) {
-        indices[index] = index;
+      if (postAuthzAllowed[i]) {
+        // For callback, GET should be allowed
+        opCredentials = tgen.getAllowedCredentials(new OperationCode[] {OperationCode.EXECUTE_CQ, OperationCode.GET}, new String[] {REGION_NAME}, indices, rand);
+      } else {
+        // For callback, GET should be disallowed
+        opCredentials = tgen.getDisallowedCredentials(new OperationCode[] { OperationCode.GET}, new String[] {REGION_NAME}, indices, rand);
       }
 
-      Random rnd = new Random();
-      Properties[] authProps = new Properties[numOfUsers];
-      for (int i = 0; i < numOfUsers; i++) {
-        int rand = rnd.nextInt(100) + 1;
-        if (postAuthzAllowed[i]) {
-          opCredentials = tgen.getAllowedCredentials(new OperationCode[] {
-              OperationCode.EXECUTE_CQ, OperationCode.GET}, // For callback, GET should be allowed
-              new String[] {regionName}, indices, rand);
-//          authProps[i] = gen.getAllowedCredentials(
-//              new OperationCode[] {OperationCode.EXECUTE_CQ},
-//              new String[] {regionName}, rnd.nextInt(100) + 1);
-        } else {
-          opCredentials = tgen.getDisallowedCredentials(new OperationCode[] {
-              OperationCode.GET}, // For callback, GET should be disallowed
-              new String[] {regionName}, indices, rand);
-//          authProps[i] = gen.getDisallowedCredentials(
-//              new OperationCode[] {OperationCode.EXECUTE_CQ},
-//              new String[] {regionName}, rnd.nextInt(100) + 1);
-        }
-        authProps[i] = SecurityTestUtil.concatProperties(new Properties[] {
-            opCredentials, extraAuthProps, extraAuthzProps});
-      }
+      authProps[i] = concatProperties(new Properties[] {opCredentials, extraAuthProps, extraAuthzProps});
+    }
 
-      // Get ports for the servers
-      Integer port1 = Integer.valueOf(AvailablePort
-          .getRandomAvailablePort(AvailablePort.SOCKET));
-      Integer port2 = Integer.valueOf(AvailablePort
-          .getRandomAvailablePort(AvailablePort.SOCKET));
-      Integer locatorPort = Integer.valueOf(AvailablePort
-          .getRandomAvailablePort(AvailablePort.SOCKET));
-      // Close down any running servers
-      server1.invoke(() -> SecurityTestUtil.closeCache());
-      server2.invoke(() -> SecurityTestUtil.closeCache());
-
-      server1.invoke(() -> ClientCQPostAuthorizationDUnitTest.createServerCache(serverProps, javaProps, locatorPort, port1));
-      client1.invoke(ClientCQPostAuthorizationDUnitTest.class,
-          "createClientCache", new Object[] {javaProps2, authInit, authProps,
-              new Integer[] {port1, port2}, numOfUsers, postAuthzAllowed});
-      client2.invoke(ClientCQPostAuthorizationDUnitTest.class,
-          "createClientCache", new Object[] {javaProps2, authInit, authProps,
-              new Integer[] {port1, port2}, numOfUsers, postAuthzAllowed});
-
-      client1.invoke(() -> ClientCQPostAuthorizationDUnitTest.createCQ(numOfUsers));
-      client1.invoke(ClientCQPostAuthorizationDUnitTest.class, "executeCQ",
-          new Object[] {numOfUsers, new Boolean[] {false, false}, numOfPuts,
-              new String[numOfUsers], postAuthzAllowed});
-
-      client2.invoke(() -> ClientCQPostAuthorizationDUnitTest.doPuts(numOfPuts, Boolean.TRUE/* put last key */));
-      if (!postAuthzAllowed[0]) {
-        // There is no point waiting as no user is authorized to receive cq events.
-        try {Thread.sleep(1000);} catch (InterruptedException ie) {}
-      } else {
-        client1.invoke(() -> ClientCQPostAuthorizationDUnitTest.waitForLastKey(Integer.valueOf(0)));
-        if (postAuthzAllowed[1]) {
-          client1.invoke(() -> ClientCQPostAuthorizationDUnitTest.waitForLastKey(Integer.valueOf(1)));
-        }
-      }
-      client1.invoke(() -> ClientCQPostAuthorizationDUnitTest.checkCQListeners(numOfUsers, postAuthzAllowed,
-              numOfPuts + 1/* last key */, 0, !failover));
-      if (failover) {
-        server2.invoke(() -> ClientCQPostAuthorizationDUnitTest.createServerCache(serverProps, javaProps, locatorPort, port2));
-        server1.invoke(() -> SecurityTestUtil.closeCache());
-
-        // Allow time for client1 to register its CQs on server2
-        server2.invoke(() -> ClientCQPostAuthorizationDUnitTest.allowCQsToRegister(Integer.valueOf(2)));
-
-        client2.invoke(() -> ClientCQPostAuthorizationDUnitTest.doPuts(numOfPuts, Boolean.TRUE/* put last key */));
-        client1.invoke(() -> ClientCQPostAuthorizationDUnitTest.waitForLastKeyUpdate(Integer.valueOf(0)));
-        client1.invoke(() -> ClientCQPostAuthorizationDUnitTest.checkCQListeners(numOfUsers, postAuthzAllowed,
-                numOfPuts + 1/* last key */, numOfPuts + 1/* last key */,
-                Boolean.TRUE));
+    // Get ports for the servers
+    int port1 = getRandomAvailablePort(SOCKET);
+    int port2 = getRandomAvailablePort(SOCKET);
+    int locatorPort = getRandomAvailablePort(SOCKET);
+
+    // Close down any running servers
+    server1.invoke(() -> closeCache());
+    server2.invoke(() -> closeCache());
+
+    server1.invoke(() -> createTheServerCache(serverProps, javaProps, locatorPort, port1));
+    client1.invoke(() -> createClientCache(javaProps2, authInit, authProps, new int[] {port1, port2}, numOfUsers, postAuthzAllowed));
+    client2.invoke(() -> createClientCache(javaProps2, authInit, authProps, new int[] {port1, port2}, numOfUsers, postAuthzAllowed));
+
+    client1.invoke(() -> createCQ(numOfUsers));
+    client1.invoke(() -> executeCQ(numOfUsers, new boolean[] {false, false}, numOfPuts, new String[numOfUsers], postAuthzAllowed));
+
+    client2.invoke(() -> doPuts(numOfPuts, true/* put last key */));
+
+    if (!postAuthzAllowed[0]) {
+      // There is no point waiting as no user is authorized to receive cq events.
+      try {Thread.sleep(1000);} catch (InterruptedException ie) {} // TODO: replace with Awaitility
+    } else {
+      client1.invoke(() -> waitForLastKey(0));
+      if (postAuthzAllowed[1]) {
+        client1.invoke(() -> waitForLastKey(1));
       }
-  }
+    }
+
+    client1.invoke(() -> checkCQListeners(numOfUsers, postAuthzAllowed, numOfPuts + 1/* last key */, 0, !failover));
 
-  public static void createServerCache(Properties serverProps,
-      Properties javaProps, Integer serverPort) {
-    Integer locatorPort = Integer.valueOf(AvailablePort
-        .getRandomAvailablePort(AvailablePort.SOCKET));
-    SecurityTestUtil.createCacheServer((Properties)serverProps, javaProps,
-        locatorPort, null, serverPort, Boolean.TRUE, Integer.valueOf(
-            SecurityTestUtil.NO_EXCEPTION));
+    if (failover) {
+      server2.invoke(() -> createTheServerCache(serverProps, javaProps, locatorPort, port2));
+      server1.invoke(() -> closeCache());
+
+      // Allow time for client1 to register its CQs on server2
+      server2.invoke(() -> allowCQsToRegister(2));
+
+      client2.invoke(() -> doPuts(numOfPuts, true/* put last key */));
+      client1.invoke(() -> waitForLastKeyUpdate(0));
+      client1.invoke(() -> checkCQListeners(numOfUsers, postAuthzAllowed, numOfPuts + 1/* last key */, numOfPuts + 1/* last key */, true));
+    }
   }
 
-  public static void createServerCache(Properties serverProps,
-        Properties javaProps, Integer locatorPort, Integer serverPort) {
-    SecurityTestUtil.createCacheServer((Properties)serverProps, javaProps,
-        locatorPort, null, serverPort, Boolean.TRUE, Integer.valueOf(
-            SecurityTestUtil.NO_EXCEPTION));
+  private void createTheServerCache(final Properties serverProps, final Properties javaProps, final int locatorPort, final  int serverPort) {
+    SecurityTestUtils.createCacheServer(serverProps, javaProps, locatorPort, (String)null, serverPort, true, NO_EXCEPTION);
   }
 
-  public static void createClientCache(Properties javaProps, String authInit,
-      Properties[] authProps, Integer ports[], Integer numOfUsers,
-      Boolean[] postAuthzAllowed) {
-    SecurityTestUtil.createCacheClientForMultiUserMode(numOfUsers, authInit,
-        authProps, javaProps, ports, null, Boolean.FALSE,
-        SecurityTestUtil.NO_EXCEPTION);
+  private void createClientCache(final Properties javaProps, final String authInit, final Properties[] authProps, final int ports[], final int numOfUsers, final boolean[] postAuthzAllowed) {
+    createCacheClientForMultiUserMode(numOfUsers, authInit, authProps, javaProps, ports, 0, false, NO_EXCEPTION);
   }
 
-  public static void createCQ(Integer num) {
+  private void createCQ(final int num) throws CqException, CqExistsException {
     for (int i = 0; i < num; i++) {
-      QueryService cqService = SecurityTestUtil.proxyCaches[i].getQueryService();
+      QueryService cqService = getProxyCaches(i).getQueryService();
       String cqName = "CQ_" + i;
-      String queryStr = cqNameToQueryStrings.get(cqName)
-          + SecurityTestUtil.proxyCaches[i].getRegion(regionName).getFullPath();
+      String queryStr = cqNameToQueryStrings.get(cqName) + getProxyCaches(i).getRegion(REGION_NAME).getFullPath();
+
       // Create CQ Attributes.
       CqAttributesFactory cqf = new CqAttributesFactory();
-      CqListener[] cqListeners = {new CqQueryTestListener(LogWriterUtils.getLogWriter())};
+      CqListener[] cqListeners = {new CqQueryTestListener(getLogWriter())};
       ((CqQueryTestListener)cqListeners[0]).cqName = cqName;
 
       cqf.initCqListeners(cqListeners);
       CqAttributes cqa = cqf.create();
 
       // Create CQ.
-      try {
-        CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
-        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
-      } catch (Exception ex) {
-        AssertionError err = new AssertionError("Failed to create CQ " + cqName
-            + " . ");
-        err.initCause(ex);
-        LogWriterUtils.getLogWriter().info("CqService is :" + cqService, err);
-        throw err;
-      }
+      CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
+      assertTrue("newCq() state mismatch", cq1.getState().isStopped());
     }
   }
 
-  public static void executeCQ(Integer num, Boolean[] initialResults,
-      Integer expectedResultsSize, String[] expectedErr, Boolean[] postAuthzAllowed) {
-    InternalLogWriter logWriter = InternalDistributedSystem.getStaticInternalLogWriter();
+  private void executeCQ(final int num, final boolean[] initialResults, final int expectedResultsSize, final String[] expectedErr, final boolean[] postAuthzAllowed) throws RegionNotFoundException, CqException {
     for (int i = 0; i < num; i++) {
       try {
         if (expectedErr[i] != null) {
-          logWriter.info(
-              "<ExpectedException action=add>" + expectedErr[i]
-                  + "</ExpectedException>");
+          getLogWriter().info("<ExpectedException action=add>" + expectedErr[i] + "</ExpectedException>");
         }
         CqQuery cq1 = null;
         String cqName = "CQ_" + i;
-        String queryStr = cqNameToQueryStrings.get(cqName)
-            + SecurityTestUtil.proxyCaches[i].getRegion(regionName)
-                .getFullPath();
-        QueryService cqService = SecurityTestUtil.proxyCaches[i]
-            .getQueryService();
+        //String queryStr = cqNameToQueryStrings.get(cqName) + getProxyCaches(i).getRegion(REGION_NAME).getFullPath();
+        QueryService cqService = getProxyCaches(i).getQueryService();
 
         // Get CqQuery object.
-        try {
-          cq1 = cqService.getCq(cqName);
-          if (cq1 == null) {
-            LogWriterUtils.getLogWriter().info(
-                "Failed to get CqQuery object for CQ name: " + cqName);
-            fail("Failed to get CQ " + cqName);
-          } else {
-            LogWriterUtils.getLogWriter().info("Obtained CQ, CQ name: " + cq1.getName());
-            assertTrue("newCq() state mismatch", cq1.getState().isStopped());
-          }
-        } catch (Exception ex) {
-          LogWriterUtils.getLogWriter().info("CqService is :" + cqService);
-          LogWriterUtils.getLogWriter().error(ex);
-          AssertionError err = new AssertionError("Failed to execute CQ "
-              + cqName);
-          err.initCause(ex);
-          throw err;
+        cq1 = cqService.getCq(cqName);
+        if (cq1 == null) {
+          getLogWriter().info("Failed to get CqQuery object for CQ name: " + cqName);
+          fail("Failed to get CQ " + cqName);
+        } else {
+          getLogWriter().info("Obtained CQ, CQ name: " + cq1.getName());
+          assertTrue("newCq() state mismatch", cq1.getState().isStopped());
         }
 
         if (initialResults[i]) {
@@ -352,68 +267,43 @@ public class ClientCQPostAuthorizationDUnitTest extends
             cqResults = cq1.executeWithInitialResults();
           } catch (CqException ce) {
             if (ce.getCause() instanceof NotAuthorizedException && !postAuthzAllowed[i]) {
-              LogWriterUtils.getLogWriter().info("Got expected exception for CQ " + cqName);
+              getLogWriter().info("Got expected exception for CQ " + cqName);
             } else {
-              LogWriterUtils.getLogWriter().info("CqService is: " + cqService);
-              ce.printStackTrace();
-              AssertionError err = new AssertionError("Failed to execute CQ "
-                  + cqName);
-              err.initCause(ce);
-              throw err;
+              getLogWriter().info("CqService is: " + cqService);
+              throw new AssertionError("Failed to execute CQ " + cqName, ce);
             }
-          } catch (Exception ex) {
-            LogWriterUtils.getLogWriter().info("CqService is: " + cqService);
-            ex.printStackTrace();
-            AssertionError err = new AssertionError("Failed to execute CQ "
-                + cqName);
-            err.initCause(ex);
-            throw err;
           }
-          LogWriterUtils.getLogWriter().info("initial result size = " + cqResults.size());
-          assertTrue("executeWithInitialResults() state mismatch", cq1
-              .getState().isRunning());
+
+          getLogWriter().info("initial result size = " + cqResults.size());
+          assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
           if (expectedResultsSize >= 0) {
-            assertEquals("unexpected results size", expectedResultsSize
-                .intValue(), cqResults.size());
+            assertEquals("unexpected results size", expectedResultsSize, cqResults.size());
           }
+
         } else {
+
           try {
             cq1.execute();
           } catch (CqException ce) {
             if (ce.getCause() instanceof NotAuthorizedException && !postAuthzAllowed[i]) {
-              LogWriterUtils.getLogWriter().info("Got expected exception for CQ " + cqName);
+              getLogWriter().info("Got expected exception for CQ " + cqName);
             } else {
-              LogWriterUtils.getLogWriter().info("CqService is: " + cqService);
-              ce.printStackTrace();
-              AssertionError err = new AssertionError("Failed to execute CQ "
-                  + cqName);
-              err.initCause(ce);
-              throw err;
-            }
-          } catch (Exception ex) {
-            AssertionError err = new AssertionError("Failed to execute CQ "
-                + cqName);
-            err.initCause(ex);
-            if (expectedErr == null) {
-              LogWriterUtils.getLogWriter().info("CqService is: " + cqService, err);
+              throw ce;
             }
-            throw err;
           }
+
           assertTrue("execute() state mismatch", cq1.getState().isRunning() == postAuthzAllowed[i]);
         }
       } finally {
         if (expectedErr[i] != null) {
-          logWriter.info(
-              "<ExpectedException action=remove>" + expectedErr[i]
-                  + "</ExpectedException>");
+          getLogWriter().info("<ExpectedException action=remove>" + expectedErr[i] + "</ExpectedException>");
         }
       }
     }
   }
 
-  public static void doPuts(Integer num, Boolean putLastKey) {
-//    Region region = GemFireCache.getInstance().getRegion(regionName);
-    Region region = SecurityTestUtil.proxyCaches[0].getRegion(regionName);
+  private void doPuts(final int num, final boolean putLastKey) {
+    Region region = getProxyCaches(0).getRegion(REGION_NAME);
     for (int i = 0; i < num; i++) {
       region.put("CQ_key"+i, "CQ_value"+i);
     }
@@ -422,46 +312,24 @@ public class ClientCQPostAuthorizationDUnitTest extends
     }
   }
 
-  public static void putLastKey() {
-    Region region = GemFireCacheImpl.getInstance().getRegion(regionName);
-    region.put("LAST_KEY", "LAST_KEY");
-  }
-
-  public static void waitForLastKey(Integer cqIndex) {
+  private void waitForLastKey(final int cqIndex) {
     String cqName = "CQ_" + cqIndex;
-    QueryService qService = SecurityTestUtil.proxyCaches[cqIndex].getQueryService();
+    QueryService qService = getProxyCaches(cqIndex).getQueryService();
     ClientCQImpl cqQuery = (ClientCQImpl)qService.getCq(cqName);
-    ((CqQueryTestListener)cqQuery.getCqListeners()[0])
-        .waitForCreated("LAST_KEY");
-//    WaitCriterion wc = new WaitCriterion() {
-//      public boolean done() {
-//        Region region = GemFireCache.getInstance().getRegion(regionName);
-//        Region.Entry entry = region.getEntry("LAST_KEY");
-//        if (entry != null && entry.getValue() != null) {
-//          return false;
-//        } else if (entry.getValue() != null) {
-//          return true;
-//        }
-//        return false;
-//      }
-//      public String description() {
-//        return "Last key not received.";
-//      }
-//    };
-//    DistributedTestCase.waitForCriterion(wc, 60 * 1000, 100, false);
+    ((CqQueryTestListener)cqQuery.getCqListeners()[0]).waitForCreated("LAST_KEY");
   }
 
-  public static void waitForLastKeyUpdate(Integer cqIndex) {
+  private void waitForLastKeyUpdate(final int cqIndex) {
     String cqName = "CQ_" + cqIndex;
-    QueryService qService = SecurityTestUtil.proxyCaches[cqIndex].getQueryService();
+    QueryService qService = getProxyCaches(cqIndex).getQueryService();
     ClientCQImpl cqQuery = (ClientCQImpl)qService.getCq(cqName);
-    ((CqQueryTestListener)cqQuery.getCqListeners()[0])
-        .waitForUpdated("LAST_KEY");
+    ((CqQueryTestListener)cqQuery.getCqListeners()[0]).waitForUpdated("LAST_KEY");
   }
 
-  public static void allowCQsToRegister(Integer number) {
+  private void allowCQsToRegister(final int number) {
     final int num = number;
     WaitCriterion wc = new WaitCriterion() {
+      @Override
       public boolean done() {
         CqService cqService = GemFireCacheImpl.getInstance().getCqService();
         cqService.start();
@@ -472,28 +340,24 @@ public class ClientCQPostAuthorizationDUnitTest extends
           return false;
         }
       }
-
+      @Override
       public String description() {
-        return num + "Waited for " + num
-            + " CQs to be registered on this server.";
+        return num + "Waited for " + num + " CQs to be registered on this server.";
       }
     };
-    Wait.waitForCriterion(wc, 60 * 1000, 100, false);
+    waitForCriterion(wc, 60 * 1000, 100, false);
   }
 
-  public static void checkCQListeners(Integer numOfUsers,
-      Boolean[] expectedListenerInvocation, Integer createEventsSize,
-      Integer updateEventsSize, Boolean closeCache) {
+  private boolean checkCQListeners(final int numOfUsers, final boolean[] expectedListenerInvocation, final int createEventsSize, final int updateEventsSize, final boolean closeCache) {
     for (int i = 0; i < numOfUsers; i++) {
       String cqName = "CQ_" + i;
-      QueryService qService = SecurityTestUtil.proxyCaches[i].getQueryService();
+      QueryService qService = getProxyCaches(i).getQueryService();
       ClientCQImpl cqQuery = (ClientCQImpl)qService.getCq(cqName);
+
       if (expectedListenerInvocation[i]) {
         for (CqListener listener : cqQuery.getCqListeners()) {
-          assertEquals(createEventsSize.intValue(),
-              ((CqQueryTestListener)listener).getCreateEventCount());
-          assertEquals(updateEventsSize.intValue(),
-              ((CqQueryTestListener)listener).getUpdateEventCount());
+          assertEquals(createEventsSize, ((CqQueryTestListener)listener).getCreateEventCount());
+          assertEquals(updateEventsSize, ((CqQueryTestListener)listener).getUpdateEventCount());
         }
       } else {
         for (CqListener listener : cqQuery.getCqListeners()) {
@@ -501,8 +365,9 @@ public class ClientCQPostAuthorizationDUnitTest extends
         }
       }
       if (closeCache) {
-        SecurityTestUtil.proxyCaches[i].close();
+        getProxyCaches(i).close();
       }
     }
+    return true;
   }
 }