@After
 public void tearDown() throws Exception {
   if (container.isStarted(SERVER1)) {
     container.stop(SERVER1);
   }
   restoreSnapshot(snapshotForServer1);
   if (container.isStarted(SERVER2)) {
     container.stop(SERVER2);
   }
   restoreSnapshot(snapshotForServer2);
 }
  @After
  public void stop() throws Exception {
    inputCache.getRemoteCacheManager().stop();
    outputCache.getRemoteCacheManager().stop();

    // If the last test is executed stop servers.
    if (testCounter == numberOfTestMethods) {
      if (controller.isStarted("container1")) {
        controller.stop("container1");
      }

      if (controller.isStarted("container2")) {
        controller.stop("container2");
      }
    }
  }
 public void startAppServer(@Observes(precedence = -1) BeforeClass event)
     throws MalformedURLException, InterruptedException, IOException {
   if (testContext.isAdapterTest()) {
     ContainerController controller = containerConrollerInstance.get();
     if (!controller.isStarted(testContext.getAppServerInfo().getQualifier())) {
       controller.start(testContext.getAppServerInfo().getQualifier());
     }
   }
 }
  @Before
  public void setUp() throws Exception {

    // start server1 and reload it in admin-only
    container.start(SERVER1);
    ModelControllerClient client1 = createClient1();
    snapshotForServer1 = takeSnapshot(client1);
    executeReloadAndWaitForCompletionOfServer1(client1, true);
    client1 = createClient1();

    // start server2 and reload it in admin-only
    container.start(SERVER2);
    ModelControllerClient client2 = createClient2();
    snapshotForServer2 = takeSnapshot(client2);
    executeReloadAndWaitForCompletionOfServer2(client2, true);
    client2 = createClient2();

    // setup both servers
    try {
      setUpServer1(client1);
      setUpServer2(client2);
    } catch (Exception e) {
      tearDown();
      throw e;
    }

    // reload server1 in normal mode
    executeReloadAndWaitForCompletionOfServer1(client1, false);
    client1 = createClient1();

    // reload server2  in normal mode
    executeReloadAndWaitForCompletionOfServer2(client2, false);
    client2 = createClient2();

    // both servers are started and configured
    assertTrue(container.isStarted(SERVER1));
    client1.close();
    assertTrue(container.isStarted(SERVER2));
    client2.close();
  }
  @Before
  public void prepare() throws Exception {
    // If already started don't start.
    if (!controller.isStarted("container1")) {
      controller.start("container1");
    }

    if (!controller.isStarted("container2")) {
      controller.start("container2");
    }
    testCounter++;

    String host1Address = server1.getHotrodEndpoint().getInetAddress().getHostAddress();
    int port1 = server1.getHotrodEndpoint().getPort();

    String host2Address = server2.getHotrodEndpoint().getInetAddress().getHostAddress();
    int port2 = server2.getHotrodEndpoint().getPort();

    inputCache =
        new RemoteCacheManager(
                new ConfigurationBuilder().addServer().host(host1Address).port(port1).build())
            .getCache(INPUT_CACHE_NAME);

    outputCache =
        new RemoteCacheManager(
                new ConfigurationBuilder().addServer().host(host2Address).port(port2).build())
            .getCache(OUTPUT_CACHE_NAME);

    inputCache.clear();
    outputCache.clear();

    List<WebPage> webPages = createData();

    saveToInputCache(webPages);
    saveToHDFS(miniHadoopCluster, webPages);
  }
  @Before
  public void setUp() throws Exception {

    container.start(CONTAINER_0);
    container.start(CONTAINER_1);

    // both servers are started and configured
    assertTrue(container.isStarted(CONTAINER_0));
    assertTrue(container.isStarted(CONTAINER_1));

    try (ModelControllerClient client = createClient1()) {
      JMSOperations jmsOperations = JMSOperationsProvider.getInstance(client);
      jmsOperations.createJmsQueue(jmsQueueName, "java:jboss/exported/" + jmsQueueLookup);
      jmsOperations.createJmsTopic(jmsTopicName, "java:jboss/exported/" + jmsTopicLookup);
    }
    try (ModelControllerClient client = createClient2()) {
      JMSOperations jmsOperations = JMSOperationsProvider.getInstance(client);
      jmsOperations.createJmsQueue(jmsQueueName, "java:jboss/exported/" + jmsQueueLookup);
      jmsOperations.createJmsTopic(jmsTopicName, "java:jboss/exported/" + jmsTopicLookup);
    }
  }
  @Test
  public void testHotRodRollingUpgradesDiffVersions() throws Exception {
    // Target node
    final int managementPortServer1 = 9990;
    MBeanServerConnectionProvider provider1;
    // Source node
    int managementPortServer2 = 10099; // jboss-as mgmt port
    MBeanServerConnectionProvider provider2;

    try {

      if (!Boolean.parseBoolean(System.getProperty("start.jboss.as.manually"))) {
        // start it by Arquillian
        controller.start("hotrod-rolling-upgrade-2-old");
        managementPortServer2 = 10090;
      }

      ConfigurationBuilder builder = new ConfigurationBuilder();
      builder
          .addServer()
          .host("127.0.0.1")
          .port(11322)
          .protocolVersion(ConfigurationProperties.PROTOCOL_VERSION_12);

      RemoteCacheManager rcm = new RemoteCacheManager(builder.build());
      final RemoteCache<String, String> c2 = rcm.getCache("default");

      c2.put("key1", "value1");
      assertEquals("value1", c2.get("key1"));

      for (int i = 0; i < 50; i++) {
        c2.put("keyLoad" + i, "valueLoad" + i);
      }

      controller.start("hotrod-rolling-upgrade-1");

      RemoteInfinispanMBeans s1 =
          createRemotes("hotrod-rolling-upgrade-1", "local", DEFAULT_CACHE_NAME);
      // hotrod.protocol.version, if explictily defined, is set in createRemotes() method
      final RemoteCache<Object, Object> c1 = createCache(s1);

      assertEquals(
          "Can't access etries stored in source node (target's RemoteCacheStore).",
          "value1",
          c1.get("key1"));

      provider2 = new MBeanServerConnectionProvider("127.0.0.1", managementPortServer2);

      final ObjectName rollMan =
          new ObjectName(
              "jboss."
                  + InfinispanSubsystem.SUBSYSTEM_NAME
                  + ":type=Cache,"
                  + "name=\"default(local)\","
                  + "manager=\"local\","
                  + "component=RollingUpgradeManager");

      invokeOperation(
          provider2,
          rollMan.toString(),
          "recordKnownGlobalKeyset",
          new Object[] {},
          new String[] {});

      provider1 =
          new MBeanServerConnectionProvider(
              s1.server.getHotrodEndpoint().getInetAddress().getHostName(), managementPortServer1);

      invokeOperation(
          provider1,
          rollMan.toString(),
          "synchronizeData",
          new Object[] {"hotrod"},
          new String[] {"java.lang.String"});

      invokeOperation(
          provider1,
          rollMan.toString(),
          "disconnectSource",
          new Object[] {"hotrod"},
          new String[] {"java.lang.String"});

      // is source (RemoteCacheStore) really disconnected?
      c2.put("disconnected", "source");
      assertEquals(
          "Can't obtain value from cache2 (source node).", "source", c2.get("disconnected"));
      assertNull(
          "Source node entries should NOT be accessible from target node (after RCS disconnection)",
          c1.get("disconnected"));

      // all entries migrated?
      assertEquals("Entry was not successfully migrated.", "value1", c1.get("key1"));
      for (int i = 0; i < 50; i++) {
        assertEquals(
            "Entry was not successfully migrated.", "valueLoad" + i, c1.get("keyLoad" + i));
      }
    } finally {
      if (controller.isStarted("hotrod-rolling-upgrade-1")) {
        controller.stop("hotrod-rolling-upgrade-1");
      }
      if (controller.isStarted("hotrod-rolling-upgrade-2-old")) {
        controller.stop("hotrod-rolling-upgrade-2-old");
      }
    }
  }
  @Test
  public void testHotRodRollingUpgradesDiffVersions() throws Exception {
    // Target node
    final int managementPortServer1 = 9999;
    MBeanServerConnectionProvider provider1;
    // Source node
    final int managementPortServer2 = 10099;
    MBeanServerConnectionProvider provider2;

    controller.start("hotrod-rolling-upgrade-2-old");
    try {
      // we use PROTOCOL_VERSION_12 here because older servers does not support higher versions
      RemoteInfinispanMBeans s2 =
          createRemotes("hotrod-rolling-upgrade-2-old", "local", DEFAULT_CACHE_NAME);
      final RemoteCache<Object, Object> c2 =
          createCache(s2, ConfigurationProperties.PROTOCOL_VERSION_12);

      c2.put("key1", "value1");
      assertEquals("value1", c2.get("key1"));

      for (int i = 0; i < 50; i++) {
        c2.put("keyLoad" + i, "valueLoad" + i);
      }

      controller.start("hotrod-rolling-upgrade-1");

      RemoteInfinispanMBeans s1 =
          createRemotes("hotrod-rolling-upgrade-1", "local", DEFAULT_CACHE_NAME);
      final RemoteCache<Object, Object> c1 = createCache(s1);

      assertEquals(
          "Can't access etries stored in source node (target's RemoteCacheStore).",
          "value1",
          c1.get("key1"));

      provider1 =
          new MBeanServerConnectionProvider(
              s1.server.getHotrodEndpoint().getInetAddress().getHostName(), managementPortServer1);
      provider2 =
          new MBeanServerConnectionProvider(
              s2.server.getHotrodEndpoint().getInetAddress().getHostName(), managementPortServer2);

      final ObjectName rollMan =
          new ObjectName(
              "jboss.infinispan:type=Cache,"
                  + "name=\"default(local)\","
                  + "manager=\"local\","
                  + "component=RollingUpgradeManager");

      invokeOperation(
          provider2,
          rollMan.toString(),
          "recordKnownGlobalKeyset",
          new Object[] {},
          new String[] {});

      invokeOperation(
          provider1,
          rollMan.toString(),
          "synchronizeData",
          new Object[] {"hotrod"},
          new String[] {"java.lang.String"});

      invokeOperation(
          provider1,
          rollMan.toString(),
          "disconnectSource",
          new Object[] {"hotrod"},
          new String[] {"java.lang.String"});

      // is source (RemoteCacheStore) really disconnected?
      c2.put("disconnected", "source");
      assertEquals(
          "Can't obtain value from cache2 (source node).", "source", c2.get("disconnected"));
      assertNull(
          "Source node entries should NOT be accessible from target node (after RCS disconnection)",
          c1.get("disconnected"));

      // all entries migrated?
      assertEquals("Entry was not successfully migrated.", "value1", c1.get("key1"));
      for (int i = 0; i < 50; i++) {
        assertEquals(
            "Entry was not successfully migrated.", "valueLoad" + i, c1.get("keyLoad" + i));
      }
    } finally {
      if (controller.isStarted("hotrod-rolling-upgrade-1")) {
        controller.stop("hotrod-rolling-upgrade-1");
      }
      if (controller.isStarted("hotrod-rolling-upgrade-2-old")) {
        controller.stop("hotrod-rolling-upgrade-2-old");
      }
    }
  }