@Test
public void testCalculateDeviationForResource() {

  String resource = "resource";
  CustomModeISBuilder isBuilder = new CustomModeISBuilder(resource);
  isBuilder.assignInstanceAndState("0", "host1", "ONLINE");
  isBuilder.assignInstanceAndState("0", "host2", "ONLINE");
  isBuilder.assignInstanceAndState("1", "host2", "ONLINE");
  isBuilder.assignInstanceAndState("1", "host3", "ONLINE");
  isBuilder.setNumPartitions(2);
  isBuilder.setNumReplica(3);
  isBuilder.setRebalancerMode(IdealState.RebalanceMode.CUSTOMIZED);
  isBuilder.setStateModel("OnlineOffline");
  IdealState idealState = isBuilder.build();
  // Check with deviation.
  Map<String, List<InstanceConfig>> onlinePartitionMap1 = (Map)ImmutableMap.of(
      "0", ImmutableList.of(hostConfig1, hostConfig2),
      "1", ImmutableList.of(hostConfig3, hostConfig4));
  TestRoutingTableProvider routingTableProvider =
      new TestRoutingTableProvider(ImmutableMap.of(resource, onlinePartitionMap1));
  assertEquals(0.25, HdfsManager.calculateDeviationForResource(
      resource, idealState, routingTableProvider), 0.01);
  // Check perfect match.
  Map<String, List<InstanceConfig>> onlinePartitionMap2 = (Map)ImmutableMap.of(
      "0", ImmutableList.of(hostConfig1, hostConfig2),
      "1", ImmutableList.of(hostConfig2, hostConfig3));
  routingTableProvider.setOnlinePartitionMap(ImmutableMap.of(resource, onlinePartitionMap2));
  assertEquals(0.0, HdfsManager.calculateDeviationForResource(
      resource, idealState, routingTableProvider), 0.01);
}