public static Status getServiceStatus() {
    return getServiceStatus(SERVICE_STATUS_CALLBACK);
  }
@Test
  public void testIdealStateMatch() {
    TestIdealStateAndExternalViewMatchServiceStatusCallback callback;

    // No ideal state = GOOD
    callback = buildTestISEVCallback();
    callback.setExternalView(new ExternalView(TABLE_NAME));
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // No external view, and ideal state shows this instance is assigned a segment of the resource = STARTING
    callback = buildTestISEVCallback();
    ZNRecord znRecord = new ZNRecord(TABLE_NAME);
    znRecord.setSimpleField("REBALANCE_MODE", "CUSTOMIZED");
    znRecord.setMapField("segment1", Map.of(INSTANCE_NAME, "ONLINE"));
    callback.setIdealState(new IdealState(znRecord));
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.STARTING);

    // No external view, and ideal state shows this instance is not assigned a segment of the resource = GOOD
    callback = buildTestISEVCallback();
    znRecord = new ZNRecord(TABLE_NAME);
    znRecord.setSimpleField("REBALANCE_MODE", "CUSTOMIZED");
    znRecord.setMapField("segment1", Map.of("otherServerInstance", "ONLINE"));
    callback.setIdealState(new IdealState(znRecord));
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // Online ideal state, and external view shows second segment is still offline = STARTING
    callback = buildTestISEVCallback();
    znRecord = new ZNRecord(TABLE_NAME);
    znRecord.setSimpleField("REBALANCE_MODE", "CUSTOMIZED");
    znRecord.setMapField("segment1", Map.of(INSTANCE_NAME, "ONLINE"));
    znRecord.setMapField("segment2", Map.of(INSTANCE_NAME, "ONLINE"));
    callback.setIdealState(new IdealState(znRecord));
    ExternalView externalView = new ExternalView(TABLE_NAME);
    externalView.setState("segment1", INSTANCE_NAME, "ONLINE");
    externalView.setState("segment2", INSTANCE_NAME, "OFFLINE");
    callback.setExternalView(externalView);
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.STARTING);

    // Empty ideal state + empty external view = GOOD
    callback = buildTestISEVCallback();
    callback.setIdealState(new IdealState(TABLE_NAME));
    callback.setExternalView(new ExternalView(TABLE_NAME));
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // Once the status is GOOD, it should keep on reporting GOOD no matter what
    callback.setIdealState(null);
    callback.setExternalView(null);
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // Non empty ideal state + empty external view = STARTING
    callback = buildTestISEVCallback();
    IdealState idealState = new IdealState(TABLE_NAME);
    idealState.setRebalanceMode(IdealState.RebalanceMode.CUSTOMIZED);
    idealState.setPartitionState("mySegment", INSTANCE_NAME, "ONLINE");
    callback.setIdealState(idealState);
    callback.setExternalView(new ExternalView(TABLE_NAME));
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.STARTING);

    // Should be good if the only ideal state is disabled
    callback.getResourceIdealState(TABLE_NAME).enable(false);
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // Should ignore offline segments in ideal state
    callback = buildTestISEVCallback();
    idealState = new IdealState(TABLE_NAME);
    idealState.setRebalanceMode(IdealState.RebalanceMode.CUSTOMIZED);
    idealState.setPartitionState("mySegment_1", INSTANCE_NAME, "ONLINE");
    idealState.setPartitionState("mySegment_2", INSTANCE_NAME, "OFFLINE");
    callback.setIdealState(idealState);
    externalView = new ExternalView(TABLE_NAME);
    externalView.setState("mySegment_1", INSTANCE_NAME, "ONLINE");
    callback.setExternalView(externalView);
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // Should ignore segments in error state in external view
    callback = buildTestISEVCallback();
    idealState = new IdealState(TABLE_NAME);
    idealState.setRebalanceMode(IdealState.RebalanceMode.CUSTOMIZED);
    idealState.setPartitionState("mySegment_1", INSTANCE_NAME, "ONLINE");
    idealState.setPartitionState("mySegment_2", INSTANCE_NAME, "OFFLINE");
    callback.setIdealState(idealState);
    externalView = new ExternalView(TABLE_NAME);
    externalView.setState("mySegment_1", INSTANCE_NAME, "ERROR");
    callback.setExternalView(externalView);
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);

    // Should ignore other instances
    callback = buildTestISEVCallback();
    idealState = new IdealState(TABLE_NAME);
    idealState.setRebalanceMode(IdealState.RebalanceMode.CUSTOMIZED);
    idealState.setPartitionState("mySegment_1", INSTANCE_NAME, "ONLINE");
    idealState.setPartitionState("mySegment_2", INSTANCE_NAME + "2", "ONLINE");
    callback.setIdealState(idealState);
    externalView = new ExternalView(TABLE_NAME);
    externalView.setState("mySegment_1", INSTANCE_NAME, "ONLINE");
    externalView.setState("mySegment_2", INSTANCE_NAME + "2", "OFFLINE");
    callback.setExternalView(externalView);
    assertEquals(callback.getServiceStatus(), ServiceStatus.Status.GOOD);
  }