/**
   * Assert application activation
   *
   * @param applicationName
   * @param tenantId
   */
  private void assertClusterWithScalingup(String applicationName, int tenantId) {
    Application application =
        ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId);
    assertNotNull(
        String.format("Application is not found: [application-id] %s", applicationName),
        application);
    boolean clusterScaleup = false;
    String clusterId = null;
    long startTime = System.currentTimeMillis();
    while (!clusterScaleup) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ignore) {
      }
      Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively();
      for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) {
        String serviceUuid = clusterDataHolder.getServiceUuid();
        clusterId = clusterDataHolder.getClusterId();
        Service service = TopologyManager.getTopology().getService(serviceUuid);
        assertNotNull(
            String.format(
                "Service is not found: [application-id] %s [service] %s",
                applicationName, serviceUuid),
            service);

        Cluster cluster = service.getCluster(clusterId);
        assertNotNull(
            String.format(
                "Cluster is not found: [application-id] %s [service] %s [cluster-id] %s",
                applicationName, serviceUuid, clusterId),
            cluster);
        for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) {
          int activeInstances = 0;
          for (Member member : cluster.getMembers()) {
            if (member.getClusterInstanceId().equals(instance.getInstanceId())) {
              if (member.getStatus().equals(MemberStatus.Active)) {
                activeInstances++;
              }
            }
          }
          clusterScaleup = activeInstances >= clusterDataHolder.getMinInstances();
          if (clusterScaleup) {
            activeInstancesAfterScaleup = activeInstances;
            break;
          }
        }
        application =
            ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId);
        if ((System.currentTimeMillis() - startTime) > CLUSTER_SCALE_UP_TIMEOUT) {
          break;
        }
      }
    }
    assertEquals(
        true,
        clusterScaleup,
        String.format("Cluster did not get scaled up: [cluster-id] %s", clusterId));
  }
  private boolean doProcess(GroupInstanceTerminatingEvent event, Applications applications) {

    // Validate event against the existing applications
    Application application = applications.getApplication(event.getAppId());
    if (application == null) {
      if (log.isWarnEnabled()) {
        log.warn(String.format("Application does not exist: [service] %s", event.getAppId()));
      }
      return false;
    }
    Group group = application.getGroupRecursively(event.getGroupId());

    if (group == null) {
      if (log.isWarnEnabled()) {
        log.warn(
            String.format(
                "Group not exists in service: [AppId] %s [groupId] %s",
                event.getAppId(), event.getGroupId()));
        return false;
      }
    } else {
      // Apply changes to the applications
      GroupInstance context = group.getInstanceContexts(event.getInstanceId());
      if (context == null) {
        if (log.isWarnEnabled()) {
          log.warn(
              String.format(
                  "Group Instance not exists in Group: [AppId] %s [groupId] %s "
                      + "[instanceId] %s",
                  event.getAppId(), event.getGroupId(), event.getInstanceId()));
          return false;
        }
      }
      // Apply changes to the topology
      GroupStatus status = GroupStatus.Terminating;
      if (!context.isStateTransitionValid(status)) {
        log.error("Invalid State Transition from " + context.getStatus() + " to " + status);
        return false;
      }
      context.setStatus(status);
    }

    // Notify event listeners
    notifyEventListeners(event);
    return true;
  }