/**
   * Test start kaa node server application without Zk started.
   *
   * @throws Exception the exception
   */
  @Ignore("KAA-1281 Kaa node should block startup process if zookeeper is unavailable.")
  @Test
  public void testStartKaaNodeServerApplicationWithoutZkStarted() throws Exception {
    TTransport transport = null;
    Thread kaaNodeServerLauncherThread = null;
    KaaNodeThriftService.Client client = null;
    try {
      kaaNodeServerLauncherThread =
          new Thread(
              new Runnable() {
                @Override
                public void run() {
                  LOG.info("Starting Kaa Node Server ...");
                  KaaNodeApplication.main(
                      new String[] {"common-zk-test-context.xml", "kaa-node-zk-test.properties"});
                  LOG.info("Kaa Node Server Stopped");
                }
              });

      kaaNodeServerLauncherThread.start();

      Thread.sleep(30000);

      transport = new TSocket(HOST, PORT);
      TProtocol protocol = new TBinaryProtocol(transport);
      TMultiplexedProtocol mp =
          new TMultiplexedProtocol(protocol, KaaThriftService.KAA_NODE_SERVICE.getServiceName());
      client = new KaaNodeThriftService.Client(mp);
      transport.open();
      client.shutdown();
    } finally {
      if (transport != null && transport.isOpen()) {
        try {
          transport.close();
        } catch (Exception e) {
        }
      }
      if (kaaNodeServerLauncherThread != null) {
        kaaNodeServerLauncherThread.join(30000);
        if (kaaNodeServerLauncherThread.isAlive()) {
          throw new TimeoutException(
              "Timeout (30 sec) occured while waiting kaa node server shutdown thread!");
        }
      }
    }
  }
  /**
   * Test start kaa node server application.
   *
   * @throws Exception the exception
   */
  @Test
  public void testStartKaaNodeServerApplication() throws Exception {
    TestingCluster zkCluster = null;
    TTransport transport = null;
    Thread kaaNodeServerLauncherThread = null;
    KaaNodeThriftService.Client client = null;
    CuratorFramework zkClient = null;
    CountDownLatch latch = new CountDownLatch(1);
    boolean kaaNodeStarted = false;
    TestKaaNodeLauncher launcher = new TestKaaNodeLauncher();
    try {
      zkCluster = new TestingCluster(new InstanceSpec(null, 2185, -1, -1, true, -1, -1, -1));
      zkCluster.start();
      zkClient =
          CuratorFrameworkFactory.newClient(zkCluster.getConnectString(), new RetryOneTime(100));
      zkClient.start();

      kaaNodeServerLauncherThread = new Thread(launcher);
      kaaNodeServerLauncherThread.start();

      OperationsNodeStartupListener operationsNodeStartupListener =
          new OperationsNodeStartupListener();
      zkClient.getCuratorListenable().addListener(operationsNodeStartupListener);
      zkClient.getChildren().inBackground(latch).forPath(OPERATIONS_SERVER_NODE_PATH);
      // Wait for operations service to start
      kaaNodeStarted = latch.await(KAA_NODE_START_TIMEOUT_SEC, TimeUnit.SECONDS);
      zkClient.getCuratorListenable().removeListener(operationsNodeStartupListener);

      transport = new TSocket(HOST, PORT);
      TProtocol protocol = new TBinaryProtocol(transport);
      TMultiplexedProtocol mp =
          new TMultiplexedProtocol(protocol, KaaThriftService.KAA_NODE_SERVICE.getServiceName());
      client = new KaaNodeThriftService.Client(mp);
      transport.open();
      client.shutdown();

    } finally {
      boolean shutdownFailed = false;
      Closeables.close(zkClient, true);
      if (transport != null && transport.isOpen()) {
        Closeables.close(transport, true);
      }
      if (kaaNodeServerLauncherThread != null) {
        kaaNodeServerLauncherThread.join(30000);
        shutdownFailed = kaaNodeServerLauncherThread.isAlive();
      }
      Closeables.close(zkCluster, true);
      if (launcher != null) {
        ConfigurableApplicationContext appContext = launcher.getApplicationContext();
        if (appContext.isActive()) {
          Closeables.close(appContext, true);
        }
      }
      if (!kaaNodeStarted) {
        throw new TimeoutException(
            "Timeout ("
                + KAA_NODE_START_TIMEOUT_SEC
                + " sec) occured while waiting kaa node server to start!");
      } else if (shutdownFailed) {
        throw new TimeoutException(
            "Timeout ("
                + KAA_NODE_STOP_TIMEOUT_SEC
                + " sec) occured while waiting kaa node server shutdown thread!");
      }
    }
  }