@Test
  public void helixRemoveOldIdealStateChangeTest() throws Exception {
    assumeTrue(!useComposite && !overrideEnabled);
    HelixClusterManager helixClusterManager = (HelixClusterManager) clusterManager;
    verifyInitialClusterChanges(helixClusterManager, helixCluster, helixDcs);

    String resourceName = helixCluster.getResources(localDc).get(0);
    IdealState idealState = helixCluster.getResourceIdealState(resourceName, localDc);
    Set<String> partitionNames = idealState.getPartitionSet();

    // Add a new resource to override the old resource
    String newResourceName = String.valueOf(Integer.valueOf(resourceName) + 1000);
    helixCluster.addNewResource(newResourceName, cloneIdealState(idealState, newResourceName), localDc);

    // After change, all partitions in the partitionNames should have updated resourceName
    Map<String, Set<String>> partitionToResourceAfterUpdate =
        (helixClusterManager).getPartitionToResourceMapByDC().get(localDc);
    for (Map.Entry<String, Set<String>> partitionAndResource : partitionToResourceAfterUpdate.entrySet()) {
      String pName = partitionAndResource.getKey();
      Set<String> rNames = partitionAndResource.getValue();
      if (partitionNames.contains(pName)) {
        assertTrue(rNames.contains(newResourceName));
        assertFalse(rNames.contains(resourceName));
      }
    }

    // Now replace it with yet another resource, however, this time, resource name would be smaller
    String newResourceName2 = String.valueOf(Integer.valueOf(resourceName) + 100);
    helixCluster.addNewResource(newResourceName2, cloneIdealState(idealState, newResourceName2), localDc);
    partitionToResourceAfterUpdate = (helixClusterManager).getPartitionToResourceMapByDC().get(localDc);
    for (Map.Entry<String, Set<String>> partitionAndResource : partitionToResourceAfterUpdate.entrySet()) {
      String pName = partitionAndResource.getKey();
      Set<String> rNames = partitionAndResource.getValue();
      if (partitionNames.contains(pName)) {
        assertTrue(rNames.contains(newResourceName));
        assertFalse(rNames.contains(newResourceName2));
        assertFalse(rNames.contains(resourceName));
      }
    }

    // Now remove resourceName and newResourceName2, it shouldn't change the partition to resource mapping
    helixCluster.removeResourceIdealState(resourceName, localDc);
    helixCluster.removeResourceIdealState(newResourceName2, localDc);
    partitionToResourceAfterUpdate = (helixClusterManager).getPartitionToResourceMapByDC().get(localDc);
    for (Map.Entry<String, Set<String>> partitionAndResource : partitionToResourceAfterUpdate.entrySet()) {
      String pName = partitionAndResource.getKey();
      Set<String> rNames = partitionAndResource.getValue();
      if (partitionNames.contains(pName)) {
        assertTrue(rNames.contains(newResourceName));
        assertFalse(rNames.contains(newResourceName2));
        assertFalse(rNames.contains(resourceName));
      }
    }
  }
Test add and remove ideal state with duplicate partition ids
@throws Exception
ambry-clustermap/src/test/java/com/github/ambry/clustermap/HelixClusterManagerTest.java