IGNITE-12533 Remote security context tests refactoring (#7276)

diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractRemoteSecurityContextCheckTest.java
index 40e23b5..bf722cd 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractRemoteSecurityContextCheckTest.java
@@ -19,13 +19,12 @@
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Map;
-import java.util.Objects;
 import java.util.UUID;
-import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import javax.cache.Cache;
 import javax.cache.processor.EntryProcessor;
@@ -40,7 +39,7 @@
 import org.apache.ignite.lang.IgniteClosure;
 import org.apache.ignite.lang.IgniteRunnable;
 
-import static org.apache.ignite.Ignition.localIgnite;
+import static org.apache.ignite.internal.IgnitionEx.localIgnite;
 
 /**
  *
@@ -73,6 +72,15 @@
     /** Verifier to check results of tests. */
     protected static final Verifier VERIFIER = new Verifier();
 
+    /** Start operation. */
+    protected static final String OPERATION_START = "start";
+
+    /** Check operation. */
+    protected static final String OPERATION_CHECK = "check";
+
+    /** Endpoint operation. */
+    protected static final String OPERATION_ENDPOINT = "endpoint";
+
     /**
      * @return IgniteCompute is produced by passed node for cluster group that contains nodes with ids from collection.
      */
@@ -81,24 +89,52 @@
     }
 
     /**
+     * @return Collection of initiator nodes names.
+     */
+    protected Collection<IgniteEx> initiators() {
+        return Arrays.asList(grid(SRV_INITIATOR), grid(CLNT_INITIATOR));
+    }
+
+    /**
      * @return Collection of feature call nodes ids.
      */
-    protected Collection<UUID> nodesToRun() {
-        return Arrays.asList(nodeId(SRV_RUN), nodeId(CLNT_RUN));
+    protected final Collection<UUID> nodesToRunIds() {
+        return nodesToRun().stream().map(this::nodeId).collect(Collectors.toList());
+    }
+
+    /**
+     * @return Collection of feature call nodes names.
+     */
+    protected Collection<String> nodesToRun() {
+        return Arrays.asList(SRV_RUN, CLNT_RUN);
     }
 
     /**
      * @return Collection of feature transit nodes ids.
      */
-    protected Collection<UUID> nodesToCheck() {
-        return Arrays.asList(nodeId(SRV_CHECK), nodeId(CLNT_CHECK));
+    protected final Collection<UUID> nodesToCheckIds() {
+        return nodesToCheck().stream().map(this::nodeId).collect(Collectors.toList());
+    }
+
+    /**
+     * @return Collection of feature transit nodes names.
+     */
+    protected Collection<String> nodesToCheck() {
+        return Arrays.asList(SRV_CHECK, CLNT_CHECK);
     }
 
     /**
      * @return Collection of endpont nodes ids.
      */
-    protected Collection<UUID> endpoints() {
-        return Arrays.asList(nodeId(SRV_ENDPOINT), nodeId(CLNT_ENDPOINT));
+    protected final Collection<UUID> endpointIds() {
+        return endpoints().stream().map(this::nodeId).collect(Collectors.toList());
+    }
+
+    /**
+     * @return Collection of endpont nodes names.
+     */
+    protected Collection<String> endpoints() {
+        return Arrays.asList(SRV_ENDPOINT, CLNT_ENDPOINT);
     }
 
     /**
@@ -112,32 +148,43 @@
     /**
      * Setups expected behavior to passed verifier.
      */
-    protected abstract void setupVerifier(Verifier verifier);
+    protected void setupVerifier(final Verifier verifier) {
+        Collection<String> toRuns = nodesToRun();
+
+        Collection<String> toChecks = nodesToCheck();
+
+        toRuns.forEach(n -> verifier.expect(n, OPERATION_START, 1));
+        toChecks.forEach(n -> verifier.expect(n, OPERATION_CHECK, toRuns.size()));
+        endpoints().forEach(n -> verifier.expect(n, OPERATION_ENDPOINT, toChecks.size() * toRuns.size()));
+    }
 
     /**
-     * @param initiator Node that initiates an execution.
      * @param op Operation.
      */
-    protected void runAndCheck(IgniteEx initiator, IgniteRunnable op) {
-        runAndCheck(initiator, Stream.of(op));
+    protected void runAndCheck(IgniteRunnable op) {
+        runAndCheck(Stream.of(op));
     }
 
     /**
      * Sets up VERIFIER, performs the runnable and checks the result.
      *
-     * @param initiator Node that initiates an execution.
      * @param ops Operations.
      */
-    protected void runAndCheck(IgniteEx initiator, Stream<IgniteRunnable> ops) {
-        ops.forEach(r -> {
-            VERIFIER.initiator(initiator);
+    protected void runAndCheck(Stream<IgniteRunnable> ops) {
+        ops.forEach(
+            r -> initiators().forEach(
+                initiator -> {
+                    VERIFIER.initiator(initiator);
 
-            setupVerifier(VERIFIER);
+                    setupVerifier(VERIFIER);
 
-            compute(initiator, nodesToRun()).broadcast(r);
+                    compute(initiator, nodesToRunIds())
+                        .broadcast((IgniteRunnable)new RegisterExecAndForward<>(OPERATION_START, r, endpointIds()));
 
-            VERIFIER.checkResult();
-        });
+                    VERIFIER.checkResult();
+                }
+            )
+        );
     }
 
     /**
@@ -152,7 +199,7 @@
         /**
          * Checked errors.
          */
-        private final Collection<String> errors = new ArrayBlockingQueue<>(10);
+        private final Collection<String> errors = new ConcurrentLinkedQueue<>();
 
         /**
          * Expected security subject id.
@@ -160,6 +207,11 @@
         private UUID expSecSubjId;
 
         /** */
+        private Verifier() {
+            // No-op.
+        }
+
+        /** */
         private void clear() {
             expInvokes.clear();
 
@@ -170,17 +222,6 @@
 
         /**
          * Adds expected behaivior the method {@link #register} will be invoke expected times on the node with passed
-         * name.
-         *
-         * @param nodeName Node name.
-         * @param num Expected number of invokes.
-         */
-        public Verifier expect(String nodeName, int num) {
-            return expect(nodeName, null, num);
-        }
-
-        /**
-         * Adds expected behaivior the method {@link #register} will be invoke expected times on the node with passed
          * name and the passed operation name.
          *
          * @param nodeName Node name.
@@ -194,45 +235,20 @@
         }
 
         /**
-         * Registers a security subject referred for {@code localIgnite} and increments invoke counter.
-         */
-        public void register() {
-            register((IgniteEx)localIgnite(), null);
-        }
-
-        /**
          * Registers a security subject referred for {@code localIgnite} with the passed operation name and increments
          * invoke counter.
          *
          * @param opName Operation name.
          */
         public void register(String opName) {
-            register((IgniteEx)localIgnite(), opName);
-        }
-
-        /**
-         * Registers a security subject referred for the passed {@code ignite} and increments invoke counter.
-         *
-         * @param ignite Instance of ignite.
-         */
-        public void register(IgniteEx ignite) {
-            register(ignite, null);
-        }
-
-        /**
-         * Registers a security subject referred for the passed {@code ignite} with the passed operation name and
-         * increments invoke counter.
-         *
-         * @param ignite Instance of ignite.
-         * @param opName Operation name.
-         */
-        public void register(IgniteEx ignite, String opName) {
             if (expSecSubjId == null) {
                 error("SubjectId cannot be null.");
 
                 return;
             }
 
+            IgniteEx ignite = localIgnite();
+
             UUID actualSubjId = secSubjectId(ignite);
 
             if (!expSecSubjId.equals(actualSubjId)) {
@@ -290,10 +306,11 @@
         private RegisterExecAndForward<K, V> instance;
 
         /**
+         * @param opName Operation name.
          * @param endpoints Collection of endpont nodes ids.
          */
-        public ExecRegisterAndForwardAdapter(Collection<UUID> endpoints) {
-            instance = new RegisterExecAndForward<>(endpoints);
+        public ExecRegisterAndForwardAdapter(String opName, Collection<UUID> endpoints) {
+            instance = new RegisterExecAndForward<>(null, opName, endpoints);
         }
 
         /** {@inheritDoc} */
@@ -303,13 +320,13 @@
     }
 
     /** */
-    protected <K, V> RegisterExecAndForward<K, V> createRunner(String srvName) {
-        return new RegisterExecAndForward<>(srvName, endpoints());
+    protected <K, V> RegisterExecAndForward<K, V> operationCheck(String srvName) {
+        return new RegisterExecAndForward<>(srvName, OPERATION_CHECK, endpointIds());
     }
 
     /** */
-    protected <K, V> RegisterExecAndForward<K, V> createRunner() {
-        return new RegisterExecAndForward<>(endpoints());
+    protected <K, V> RegisterExecAndForward<K, V> operationCheck() {
+        return new RegisterExecAndForward<>(null, OPERATION_CHECK, endpointIds());
     }
 
     /** */
@@ -321,35 +338,32 @@
         /** Expected local node name. */
         private final String node;
 
+        /** Operation name. */
+        private final String opName;
+
         /** Collection of endpoint node ids. */
         private final Collection<UUID> endpoints;
 
         /**
-         * @param runnable Runnable.
-         */
-        public RegisterExecAndForward(IgniteRunnable runnable) {
-            this.runnable = Objects.requireNonNull(runnable);
-            node = null;
-            endpoints = Collections.emptyList();
-        }
-
-        /**
          * @param node Expected local node name.
+         * @param opName Operation name.
          * @param endpoints Collection of endpont nodes ids.
          */
-        public RegisterExecAndForward(String node, Collection<UUID> endpoints) {
-            this.node = node;
-            this.endpoints = endpoints;
-            runnable = null;
+        private RegisterExecAndForward(String node, String opName, Collection<UUID> endpoints) {
+            this(node, opName, null, endpoints);
         }
 
-        /**
-         * @param endpoints Collection of endpont nodes ids.
-         */
-        public RegisterExecAndForward(Collection<UUID> endpoints) {
+        /** */
+        private RegisterExecAndForward(String opName, IgniteRunnable runnable, Collection<UUID> endpoints) {
+            this(null, opName, runnable, endpoints);
+        }
+
+        /** */
+        private RegisterExecAndForward(String node, String opName, IgniteRunnable runnable, Collection<UUID> endpoints) {
+            this.node = node;
+            this.opName = opName;
+            this.runnable = runnable;
             this.endpoints = endpoints;
-            runnable = null;
-            node = null;
         }
 
         /** {@inheritDoc} */
@@ -364,12 +378,12 @@
             Ignite loc = localIgnite();
 
             if (node == null || node.equals(loc.name())) {
-                VERIFIER.register();
+                VERIFIER.register(opName);
 
                 if (runnable != null)
                     runnable.run();
                 else
-                    compute(loc, endpoints).broadcast(() -> VERIFIER.register());
+                    compute(loc, endpoints).broadcast(() -> VERIFIER.register(OPERATION_ENDPOINT));
             }
         }
 
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractSecurityTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractSecurityTest.java
index 5907952..737ba34 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractSecurityTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/AbstractSecurityTest.java
@@ -25,8 +25,6 @@
 import javax.cache.Cache;
 import javax.cache.configuration.Factory;
 import org.apache.ignite.cache.store.CacheStoreAdapter;
-import org.apache.ignite.configuration.DataRegionConfiguration;
-import org.apache.ignite.configuration.DataStorageConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.internal.IgniteEx;
 import org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider;
@@ -65,13 +63,6 @@
         AbstractTestSecurityPluginProvider pluginProv) throws Exception {
 
         return getConfiguration(instanceName)
-            .setDataStorageConfiguration(
-                new DataStorageConfiguration()
-                    .setDefaultDataRegionConfiguration(
-                        new DataRegionConfiguration().setPersistenceEnabled(true)
-                    )
-            )
-            .setAuthenticationEnabled(true)
             .setPluginProviders(pluginProv);
     }
 
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/AbstractContinuousQueryRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/AbstractContinuousQueryRemoteSecurityContextCheckTest.java
index 090cb4b..1d0ed95 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/AbstractContinuousQueryRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/AbstractContinuousQueryRemoteSecurityContextCheckTest.java
@@ -31,6 +31,7 @@
 import org.apache.ignite.internal.processors.security.AbstractCacheOperationRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.processors.security.SecurityContext;
 import org.apache.ignite.lang.IgniteBiPredicate;
+import org.apache.ignite.lang.IgniteRunnable;
 
 import static org.apache.ignite.Ignition.localIgnite;
 
@@ -90,11 +91,17 @@
     }
 
     /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, OPERATION_OPEN_CQ, 1)
-            .expect(CLNT_RUN, OPERATION_OPEN_CQ, 1)
-            .expect(SRV_CHECK, OPERATION_CQ_COMPONENT, 2);
+    @Override protected void runAndCheck(IgniteRunnable op) {
+        initiators().forEach(initiator -> {
+            VERIFIER.initiator(initiator)
+                .expect(SRV_RUN, OPERATION_OPEN_CQ, 1)
+                .expect(CLNT_RUN, OPERATION_OPEN_CQ, 1)
+                .expect(SRV_CHECK, OPERATION_CQ_COMPONENT, 2);
+
+            compute(initiator, nodesToRunIds()).broadcast(op);
+
+            VERIFIER.checkResult();
+        });
     }
 
     /**
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/CacheLoadRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/CacheLoadRemoteSecurityContextCheckTest.java
index fea44fd..57b1bf0 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/CacheLoadRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/CacheLoadRemoteSecurityContextCheckTest.java
@@ -19,12 +19,11 @@
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.UUID;
 import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.internal.processors.security.AbstractCacheOperationRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
-import org.apache.ignite.lang.IgniteRunnable;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -57,7 +56,7 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** {@inheritDoc} */
@@ -74,37 +73,20 @@
         };
     }
 
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(SRV_CHECK, 1)
-            .expect(SRV_ENDPOINT, 1)
-            .expect(CLNT_ENDPOINT, 1);
-    }
-
     /** */
     @Test
     public void test() {
-        IgniteRunnable operation = () -> {
-            VERIFIER.register();
-
-            localIgnite().<Integer, Integer>cache(CACHE_NAME).loadCache(
-                new RegisterExecAndForward<>(SRV_CHECK, endpoints())
-            );
-        };
-
-        runAndCheck(grid(SRV_INITIATOR), operation);
-        runAndCheck(grid(CLNT_INITIATOR), operation);
+        runAndCheck(() ->
+            localIgnite().<Integer, Integer>cache(CACHE_NAME).loadCache(operationCheck(SRV_CHECK)));
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToRun() {
-        return Collections.singletonList(nodeId(SRV_RUN));
+    @Override protected Collection<String> nodesToRun() {
+        return Collections.singletonList(SRV_RUN);
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToCheck() {
-        return Collections.singletonList(nodeId(SRV_CHECK));
+    @Override protected Collection<String> nodesToCheck() {
+        return Collections.singletonList(SRV_CHECK);
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryRemoteSecurityContextCheckTest.java
index 767e2ad..0e2c1f1 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryRemoteSecurityContextCheckTest.java
@@ -45,8 +45,7 @@
             }
         };
 
-        runAndCheck(grid(SRV_INITIATOR), operation(consumer, true));
-        runAndCheck(grid(CLNT_INITIATOR), operation(consumer, true));
+        runAndCheck(operation(consumer, true));
     }
 
     /**
@@ -60,8 +59,7 @@
             }
         };
 
-        runAndCheck(grid(SRV_INITIATOR), operation(consumer));
-        runAndCheck(grid(CLNT_INITIATOR), operation(consumer));
+        runAndCheck(operation(consumer));
     }
 
     /**
@@ -75,8 +73,7 @@
             }
         };
 
-        runAndCheck(grid(SRV_INITIATOR), operation(consumer));
-        runAndCheck(grid(CLNT_INITIATOR), operation(consumer));
+        runAndCheck(operation(consumer));
     }
 
     /**
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryWithTransformerRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryWithTransformerRemoteSecurityContextCheckTest.java
index f9ed65f..4095fc6 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryWithTransformerRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ContinuousQueryWithTransformerRemoteSecurityContextCheckTest.java
@@ -48,8 +48,7 @@
                 }
             };
 
-        runAndCheck(grid(SRV_INITIATOR), operation(consumer, true));
-        runAndCheck(grid(CLNT_INITIATOR), operation(consumer, true));
+        runAndCheck(operation(consumer, true));
     }
 
     /**
@@ -65,8 +64,7 @@
                 }
             };
 
-        runAndCheck(grid(SRV_INITIATOR), operation(consumer));
-        runAndCheck(grid(CLNT_INITIATOR), operation(consumer));
+        runAndCheck(operation(consumer));
     }
 
     /**
@@ -86,8 +84,7 @@
                 }
             };
 
-        runAndCheck(grid(SRV_INITIATOR), operation(consumer));
-        runAndCheck(grid(CLNT_INITIATOR), operation(consumer));
+        runAndCheck(operation(consumer));
     }
 
     /**
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/EntryProcessorRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/EntryProcessorRemoteSecurityContextCheckTest.java
index facd7ec..630211f 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/EntryProcessorRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/EntryProcessorRemoteSecurityContextCheckTest.java
@@ -19,8 +19,8 @@
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.UUID;
 import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.internal.processors.security.AbstractCacheOperationRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
 import org.apache.ignite.lang.IgniteRunnable;
@@ -54,33 +54,23 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
-    }
-
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(SRV_CHECK, 1)
-            .expect(SRV_ENDPOINT, 1)
-            .expect(CLNT_ENDPOINT, 1);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** */
     @Test
     public void test() {
-        runAndCheck(grid(SRV_INITIATOR), operations());
-        runAndCheck(grid(CLNT_INITIATOR), operations());
+        runAndCheck(operations());
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToRun() {
-        return Collections.singletonList(nodeId(SRV_RUN));
+    @Override protected Collection<String> nodesToRun() {
+        return Collections.singletonList(SRV_RUN);
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToCheck() {
-        return Collections.singletonList(nodeId(SRV_CHECK));
+    @Override protected Collection<String> nodesToCheck() {
+        return Collections.singletonList(SRV_CHECK);
     }
 
     /**
@@ -89,15 +79,15 @@
     private Stream<IgniteRunnable> operations() {
         final Integer key = primaryKey(grid(SRV_CHECK));
 
-        return Stream.<IgniteRunnable>of(
-            () -> localIgnite().<Integer, Integer>cache(CACHE_NAME).invoke(key, createRunner()),
+        return Stream.of(
+            () -> localIgnite().<Integer, Integer>cache(CACHE_NAME).invoke(key, operationCheck()),
 
-            () -> localIgnite().<Integer, Integer>cache(CACHE_NAME).invokeAll(singleton(key), createRunner()),
+            () -> localIgnite().<Integer, Integer>cache(CACHE_NAME).invokeAll(singleton(key), operationCheck()),
 
-            () -> localIgnite().<Integer, Integer>cache(CACHE_NAME).invokeAsync(key, createRunner()).get(),
+            () -> localIgnite().<Integer, Integer>cache(CACHE_NAME).invokeAsync(key, operationCheck()).get(),
 
             () -> localIgnite().<Integer, Integer>cache(CACHE_NAME)
-                .invokeAllAsync(singleton(key), createRunner()).get()
-        ).map(RegisterExecAndForward::new);
+                .invokeAllAsync(singleton(key), operationCheck()).get()
+        );
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ScanQueryRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ScanQueryRemoteSecurityContextCheckTest.java
index 24035ad..6cb61c0 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ScanQueryRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/cache/closure/ScanQueryRemoteSecurityContextCheckTest.java
@@ -19,9 +19,9 @@
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.UUID;
 import java.util.stream.Stream;
 import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.internal.processors.security.AbstractCacheOperationRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
 import org.apache.ignite.lang.IgniteRunnable;
@@ -56,17 +56,7 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
-    }
-
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(CLNT_RUN, 1)
-            .expect(SRV_CHECK, 2)
-            .expect(SRV_ENDPOINT, 2)
-            .expect(CLNT_ENDPOINT, 2);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** */
@@ -77,13 +67,12 @@
 
         awaitPartitionMapExchange();
 
-        runAndCheck(grid(SRV_INITIATOR), operations());
-        runAndCheck(grid(CLNT_INITIATOR), operations());
+        runAndCheck(operations());
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToCheck() {
-        return Collections.singletonList(nodeId(SRV_CHECK));
+    @Override protected Collection<String> nodesToCheck() {
+        return Collections.singletonList(SRV_CHECK);
     }
 
     /**
@@ -91,19 +80,8 @@
      */
     private Stream<IgniteRunnable> operations() {
         return Stream.of(
-            () -> {
-                VERIFIER.register();
-
-                localIgnite().cache(CACHE_NAME).query(new ScanQuery<>(createRunner(SRV_CHECK))).getAll();
-            },
-            () -> {
-                VERIFIER.register();
-
-                localIgnite().cache(CACHE_NAME).query(
-                    new ScanQuery<>((k, v) -> true),
-                    createRunner(SRV_CHECK)
-                ).getAll();
-            }
+            () -> localIgnite().cache(CACHE_NAME).query(new ScanQuery<>(operationCheck(SRV_CHECK))).getAll(),
+            () -> localIgnite().cache(CACHE_NAME).query(new ScanQuery<>((k, v) -> true), operationCheck(SRV_CHECK)).getAll()
         );
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskCancelRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskCancelRemoteSecurityContextCheckTest.java
index c79245d..92fd82a 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskCancelRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskCancelRemoteSecurityContextCheckTest.java
@@ -24,19 +24,17 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
-import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.compute.ComputeJob;
 import org.apache.ignite.compute.ComputeJobResult;
 import org.apache.ignite.compute.ComputeJobResultPolicy;
 import org.apache.ignite.compute.ComputeTask;
 import org.apache.ignite.internal.IgniteEx;
-import org.apache.ignite.internal.IgnitionEx;
 import org.apache.ignite.internal.processors.security.AbstractRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
 import org.apache.ignite.lang.IgniteFuture;
-import org.apache.ignite.resources.IgniteInstanceResource;
 import org.apache.ignite.testframework.GridTestUtils;
 import org.jetbrains.annotations.Nullable;
 import org.junit.Test;
@@ -109,12 +107,12 @@
 
             IgniteEx rmt = isClientRmt ? startClientAllowAll("clnt_rmt") : startGridAllowAll("srv_rmt");
 
-            srv.cluster().active(true);
+            srv.cluster().state(ClusterState.ACTIVE);
 
             //Checks the case when IgniteFuture#cancel is called.
             checkCancel(initator, rmt, IgniteFuture::cancel);
             //Checks the case when rmt node leaves the cluster.
-            checkCancel(initator, rmt, f -> IgnitionEx.stop(rmt.name(), true, false));
+            checkCancel(initator, rmt, f -> stopGrid(rmt.name(), true));
         }
         finally {
             G.stopAll(true);
@@ -127,7 +125,7 @@
     private void checkCancel(IgniteEx initator, IgniteEx rmt, Consumer<IgniteFuture> consumer) throws Exception {
         VERIFIER
             .initiator(initator)
-            .expect(rmt.name(), 1);
+            .expect(rmt.name(), OPERATION_START, 1);
 
         BARRIER.reset();
         CANCELED.set(false);
@@ -153,11 +151,8 @@
             Integer arg) throws IgniteException {
             return Collections.singletonMap(
                 new ComputeJob() {
-                    @IgniteInstanceResource
-                    private Ignite loc;
-
                     @Override public void cancel() {
-                        VERIFIER.register((IgniteEx)loc);
+                        VERIFIER.register(OPERATION_START);
 
                         CANCELED.set(true);
                     }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskRemoteSecurityContextCheckTest.java
index 2dd31b3..358e0dc 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ComputeTaskRemoteSecurityContextCheckTest.java
@@ -25,6 +25,7 @@
 import java.util.stream.Stream;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.compute.ComputeJob;
 import org.apache.ignite.compute.ComputeJobResult;
 import org.apache.ignite.compute.ComputeJobResultPolicy;
@@ -42,8 +43,8 @@
  * Testing operation security context when the compute task is executed on remote nodes.
  * <p>
  * The initiator node broadcasts a task to 'run' nodes that starts compute task. That compute task is executed on
- * 'check' nodes and broadcasts a task to 'endpoint' nodes. On every step, it is performed verification that
- * operation security context is the initiator context.
+ * 'check' nodes and broadcasts a task to 'endpoint' nodes. On every step, it is performed verification that operation
+ * security context is the initiator context.
  */
 public class ComputeTaskRemoteSecurityContextCheckTest extends AbstractRemoteSecurityContextCheckTest {
     /** {@inheritDoc} */
@@ -64,25 +65,13 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
-    }
-
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(CLNT_RUN, 1)
-            .expect(SRV_CHECK, 2)
-            .expect(CLNT_CHECK, 2)
-            .expect(SRV_ENDPOINT, 4)
-            .expect(CLNT_ENDPOINT, 4);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** */
     @Test
     public void test() {
-        runAndCheck(grid(SRV_INITIATOR), operations());
-        runAndCheck(grid(CLNT_INITIATOR), operations());
+        runAndCheck(operations());
     }
 
     /**
@@ -90,16 +79,8 @@
      */
     private Stream<IgniteRunnable> operations() {
         return Stream.of(
-            () -> {
-                VERIFIER.register();
-
-                localIgnite().compute().execute(new ComputeTaskClosure(nodesToCheck(), endpoints()), 0);
-            },
-            () -> {
-                VERIFIER.register();
-
-                localIgnite().compute().executeAsync(new ComputeTaskClosure(nodesToCheck(), endpoints()), 0).get();
-            }
+            () -> localIgnite().compute().execute(new ComputeTaskClosure(nodesToCheckIds(), endpointIds()), 0),
+            () -> localIgnite().compute().executeAsync(new ComputeTaskClosure(nodesToCheckIds(), endpointIds()), 0).get()
         );
     }
 
@@ -127,7 +108,7 @@
         }
 
         /** {@inheritDoc} */
-        @Override public @Nullable Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid,
+        @Override public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid,
             @Nullable Integer arg) {
             Map<ComputeJob, ClusterNode> res = new HashMap<>();
 
@@ -142,9 +123,9 @@
                         }
 
                         @Override public Object execute() {
-                            VERIFIER.register();
+                            VERIFIER.register(OPERATION_CHECK);
 
-                            compute(loc, endpoints).broadcast(() -> VERIFIER.register());
+                            compute(loc, endpoints).broadcast(() -> VERIFIER.register(OPERATION_ENDPOINT));
 
                             return null;
                         }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/DistributedClosureRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/DistributedClosureRemoteSecurityContextCheckTest.java
index 6a6ca0f..f6b288b 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/DistributedClosureRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/DistributedClosureRemoteSecurityContextCheckTest.java
@@ -21,6 +21,7 @@
 import java.util.stream.Stream;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteCompute;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.internal.processors.security.AbstractRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
 import org.apache.ignite.lang.IgniteRunnable;
@@ -54,25 +55,13 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
-    }
-
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(CLNT_RUN, 1)
-            .expect(SRV_CHECK, 2)
-            .expect(CLNT_CHECK, 2)
-            .expect(SRV_ENDPOINT, 4)
-            .expect(CLNT_ENDPOINT, 4);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** */
     @Test
     public void test() {
-        runAndCheck(grid(SRV_INITIATOR), operations());
-        runAndCheck(grid(CLNT_INITIATOR), operations());
+        runAndCheck(operations());
     }
 
     /** */
@@ -86,33 +75,33 @@
      * @return Stream of check cases.
      */
     private Stream<IgniteRunnable> operations() {
-        return Stream.<IgniteRunnable>of(
-            () -> compute(localIgnite(), nodesToCheck()).broadcast((IgniteRunnable) createRunner()),
-            () -> compute(localIgnite(), nodesToCheck()).broadcastAsync((IgniteRunnable) createRunner()).get(),
+        return Stream.of(
+            () -> compute(localIgnite(), nodesToCheckIds()).broadcast((IgniteRunnable) operationCheck()),
+            () -> compute(localIgnite(), nodesToCheckIds()).broadcastAsync((IgniteRunnable) operationCheck()).get(),
             () -> {
-                for (UUID id : nodesToCheck())
-                    compute(id).call(createRunner());
+                for (UUID id : nodesToCheckIds())
+                    compute(id).call(operationCheck());
             },
             () -> {
-                for (UUID id : nodesToCheck())
-                    compute(id).callAsync(createRunner()).get();
+                for (UUID id : nodesToCheckIds())
+                    compute(id).callAsync(operationCheck()).get();
             },
             () -> {
-                for (UUID id : nodesToCheck())
-                    compute(id).run(createRunner());
+                for (UUID id : nodesToCheckIds())
+                    compute(id).run(operationCheck());
             },
             () -> {
-                for (UUID id : nodesToCheck())
-                    compute(id).runAsync(createRunner()).get();
+                for (UUID id : nodesToCheckIds())
+                    compute(id).runAsync(operationCheck()).get();
             },
             () -> {
-                for (UUID id : nodesToCheck())
-                    compute(id).apply(createRunner(), new Object());
+                for (UUID id : nodesToCheckIds())
+                    compute(id).apply(operationCheck(), new Object());
             },
             () -> {
-                for (UUID id : nodesToCheck())
-                    compute(id).applyAsync(createRunner(), new Object()).get();
+                for (UUID id : nodesToCheckIds())
+                    compute(id).applyAsync(operationCheck(), new Object()).get();
             }
-        ).map(RegisterExecAndForward::new);
+        );
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ExecutorServiceRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ExecutorServiceRemoteSecurityContextCheckTest.java
index 3f450ed..7d230d3 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ExecutorServiceRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/compute/closure/ExecutorServiceRemoteSecurityContextCheckTest.java
@@ -21,6 +21,7 @@
 import java.util.concurrent.ExecutorService;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.Ignition;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.internal.processors.security.AbstractRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
 import org.apache.ignite.testframework.GridTestUtils.IgniteRunnableX;
@@ -55,36 +56,21 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
-    }
-
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(CLNT_RUN, 1)
-            .expect(SRV_CHECK, 2)
-            .expect(CLNT_CHECK, 2)
-            .expect(SRV_ENDPOINT, 4)
-            .expect(CLNT_ENDPOINT, 4);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** */
     @Test
     public void test() {
-        IgniteRunnableX operation = () -> {
-            VERIFIER.register();
+        runAndCheck((IgniteRunnableX)() -> {
+                Ignite loc = Ignition.localIgnite();
 
-            Ignite loc = Ignition.localIgnite();
+                for (UUID nodeId : nodesToCheckIds()) {
+                    ExecutorService svc = loc.executorService(loc.cluster().forNodeId(nodeId));
 
-            for (UUID nodeId : nodesToCheck()) {
-                ExecutorService svc = loc.executorService(loc.cluster().forNodeId(nodeId));
-
-                svc.submit((Runnable) createRunner()).get();
+                    svc.submit((Runnable)operationCheck()).get();
+                }
             }
-        };
-
-        runAndCheck(grid(SRV_INITIATOR), operation);
-        runAndCheck(grid(CLNT_INITIATOR), operation);
+        );
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/datastreamer/closure/DataStreamerRemoteSecurityContextCheckTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/datastreamer/closure/DataStreamerRemoteSecurityContextCheckTest.java
index fa7bb8f..b74f9dc 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/datastreamer/closure/DataStreamerRemoteSecurityContextCheckTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/datastreamer/closure/DataStreamerRemoteSecurityContextCheckTest.java
@@ -19,12 +19,11 @@
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.UUID;
 import org.apache.ignite.IgniteDataStreamer;
 import org.apache.ignite.Ignition;
+import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.internal.processors.security.AbstractCacheOperationRemoteSecurityContextCheckTest;
 import org.apache.ignite.internal.util.typedef.G;
-import org.apache.ignite.lang.IgniteRunnable;
 import org.apache.ignite.stream.StreamVisitor;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -53,42 +52,29 @@
 
         startClientAllowAll(CLNT_ENDPOINT);
 
-        G.allGrids().get(0).cluster().active(true);
-    }
-
-    /** {@inheritDoc} */
-    @Override protected void setupVerifier(Verifier verifier) {
-        verifier
-            .expect(SRV_RUN, 1)
-            .expect(SRV_CHECK, 1)
-            .expect(SRV_ENDPOINT, 1)
-            .expect(CLNT_ENDPOINT, 1);
+        G.allGrids().get(0).cluster().state(ClusterState.ACTIVE);
     }
 
     /** */
     @Test
     public void testDataStreamer() {
-        IgniteRunnable op = () -> {
-            VERIFIER.register();
+        runAndCheck(() -> {
+                try (IgniteDataStreamer<Integer, Integer> strm = Ignition.localIgnite().dataStreamer(CACHE_NAME)) {
+                    strm.receiver(StreamVisitor.from(new ExecRegisterAndForwardAdapter<>(OPERATION_CHECK, endpointIds())));
 
-            try (IgniteDataStreamer<Integer, Integer> strm = Ignition.localIgnite().dataStreamer(CACHE_NAME)) {
-                strm.receiver(StreamVisitor.from(new ExecRegisterAndForwardAdapter<>(endpoints())));
-
-                strm.addData(primaryKey(grid(SRV_CHECK)), 100);
+                    strm.addData(primaryKey(grid(SRV_CHECK)), 100);
+                }
             }
-        };
-
-        runAndCheck(grid(SRV_INITIATOR), op);
-        runAndCheck(grid(CLNT_INITIATOR), op);
+        );
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToRun() {
-        return Collections.singletonList(nodeId(SRV_RUN));
+    @Override protected Collection<String> nodesToRun() {
+        return Collections.singletonList(SRV_RUN);
     }
 
     /** {@inheritDoc} */
-    @Override protected Collection<UUID> nodesToCheck() {
-        return Collections.singletonList(nodeId(SRV_CHECK));
+    @Override protected Collection<String> nodesToCheck() {
+        return Collections.singletonList(SRV_CHECK);
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
index 4b903a4..8023cec 100755
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
@@ -1230,8 +1230,16 @@
 
             info(">>> Stopping grid [name=" + ignite.name() + ", id=" + id + ']');
 
-            if (!isRemoteJvm(igniteInstanceName))
-                G.stop(igniteInstanceName, cancel);
+            if (!isRemoteJvm(igniteInstanceName)) {
+                IgniteUtils.setCurrentIgniteName(igniteInstanceName);
+
+                try {
+                    G.stop(igniteInstanceName, cancel);
+                }
+                finally {
+                    IgniteUtils.setCurrentIgniteName(null);
+                }
+            }
             else
                 IgniteProcessProxy.stop(igniteInstanceName, cancel);