@Test
  public void getNewReplicaTest() throws Exception {
    assumeTrue(!useComposite);
    clusterManager.close();
    metricRegistry = new MetricRegistry();
    // 1. test the case where ZNRecord is NULL in Helix PropertyStore
    HelixClusterManager helixClusterManager = new HelixClusterManager(clusterMapConfig, selfInstanceName,
        new MockHelixManagerFactory(helixCluster, null, null, useAggregatedView), metricRegistry);
    PartitionId partitionOfNewReplica = helixClusterManager.getAllPartitionIds(null).get(0);
    DataNode dataNodeOfNewReplica = currentNode;
    assertNull("New replica should be null because no replica infos ZNRecord in Helix",
        helixClusterManager.getBootstrapReplica(partitionOfNewReplica.toPathString(), dataNodeOfNewReplica));
    helixClusterManager.close();

    // Prepare new replica info map in Helix property store. We use the first partition in PartitionLayout and current
    // node from constructor to build the replica info map of an existing partition. Also, we place a new partition on
    // last data node in HardwareLayout. The format should be as follows.
    // {
    //   "0":{
    //       "partitionClass" : "defaultPartitionClass"
    //       "replicaCapacityInBytes" : "107374182400"
    //       "localhost_18088" : "/mnt0"
    //   }
    //   "100":{
    //       "partitionClass" : "defaultPartitionClass"
    //       "replicaCapacityInBytes" : "107374182400"
    //       "localhost_18099" : "/mnt5"
    //       "localhost_18088" : "/mnt10"
    //       "newhost_001"     : "/mnt0"
    //   }
    // }
    Map<String, Map<String, String>> partitionToReplicaInfosMap = new HashMap<>();
    // new replica of existing partition
    Map<String, String> newReplicaInfos1 = new HashMap<>();
    newReplicaInfos1.put(PARTITION_CLASS_STR, DEFAULT_PARTITION_CLASS);
    newReplicaInfos1.put(REPLICAS_CAPACITY_STR, String.valueOf(TestPartitionLayout.defaultReplicaCapacityInBytes));
    newReplicaInfos1.put(dataNodeOfNewReplica.getHostname() + "_" + dataNodeOfNewReplica.getPort(),
        dataNodeOfNewReplica.getDisks().get(0).getMountPath());
    partitionToReplicaInfosMap.put(partitionOfNewReplica.toPathString(), newReplicaInfos1);
    // new replica of a new partition
    Map<String, String> newReplicaInfos2 = new HashMap<>();
    newReplicaInfos2.put(PARTITION_CLASS_STR, DEFAULT_PARTITION_CLASS);
    newReplicaInfos2.put(REPLICAS_CAPACITY_STR, String.valueOf(TestPartitionLayout.defaultReplicaCapacityInBytes));
    // find a node that is different from currentNode
    DataNode dataNodeOfNewPartition = testHardwareLayout.getAllExistingDataNodes()
        .stream()
        .filter(node -> node.getPort() != currentNode.getPort())
        .findFirst()
        .get();
    int diskNum = dataNodeOfNewPartition.getDisks().size();
    newReplicaInfos2.put(dataNodeOfNewPartition.getHostname() + "_" + dataNodeOfNewPartition.getPort(),
        dataNodeOfNewPartition.getDisks().get(diskNum - 1).getMountPath());
    // add two fake entries (fake disk and fake host)
    newReplicaInfos2.put(getInstanceName(dataNodeOfNewReplica.getHostname(), dataNodeOfNewReplica.getPort()), "/mnt10");
    newReplicaInfos2.put("newhost_100", "/mnt0");
    partitionToReplicaInfosMap.put(NEW_PARTITION_ID_STR, newReplicaInfos2);
    // fake node that doesn't exist in current clustermap
    DataNodeId fakeNode = Mockito.mock(DataNode.class);
    Mockito.when(fakeNode.getHostname()).thenReturn("new_host");
    Mockito.when(fakeNode.getPort()).thenReturn(100);
    // set ZNRecord
    ZNRecord replicaInfosZNRecord = new ZNRecord(REPLICA_ADDITION_STR);
    replicaInfosZNRecord.setMapFields(partitionToReplicaInfosMap);
    // populate znRecordMap
    Map<String, ZNRecord> znRecordMap = new HashMap<>();
    znRecordMap.put(REPLICA_ADDITION_ZNODE_PATH, replicaInfosZNRecord);
    // create a new cluster manager
    metricRegistry = new MetricRegistry();
    helixClusterManager = new HelixClusterManager(clusterMapConfig, selfInstanceName,
        new MockHelixManagerFactory(helixCluster, znRecordMap, null, useAggregatedView), metricRegistry);

    // 2. test that cases: 1) partition is not found  2) host is not found in helix property store that associates with new replica
    // select a partition that doesn't equal to partitionOfNewReplica
    PartitionId partitionForTest;
    Random random = new Random();
    List<PartitionId> partitionsInClusterManager = helixClusterManager.getAllPartitionIds(null);
    do {
      partitionForTest = partitionsInClusterManager.get(random.nextInt(partitionsInClusterManager.size()));
    } while (partitionForTest.toString().equals(partitionOfNewReplica.toString()));
    assertNull("New replica should be null because given partition id is not in Helix property store",
        helixClusterManager.getBootstrapReplica(partitionForTest.toPathString(), dataNodeOfNewReplica));
    // select partitionOfNewReplica but a random node that doesn't equal to
    DataNode dataNodeForTest;
    List<DataNode> dataNodesInHardwareLayout = testHardwareLayout.getAllExistingDataNodes();
    do {
      dataNodeForTest = dataNodesInHardwareLayout.get(random.nextInt(dataNodesInHardwareLayout.size()));
    } while (dataNodeForTest == dataNodeOfNewReplica);
    assertNull("New replica should be null because hostname is not found in replica info map",
        helixClusterManager.getBootstrapReplica(partitionOfNewReplica.toPathString(), dataNodeForTest));

    // 3. test that new replica is from a new partition which doesn't exist in current cluster yet. (Mock adding new partition case)
    //    3.1 test new partition on host that is not present in current clustermap
    assertNull("New replica should be null because host is not present in clustermap",
        helixClusterManager.getBootstrapReplica(NEW_PARTITION_ID_STR, fakeNode));
    //    3.2 test new partition on disk that doesn't exist
    assertNull("New replica should be null because disk doesn't exist",
        helixClusterManager.getBootstrapReplica(NEW_PARTITION_ID_STR, dataNodeOfNewReplica));
    //    3.3 test replica is created for new partition
    assertNotNull("New replica should be created successfully",
        helixClusterManager.getBootstrapReplica(NEW_PARTITION_ID_STR, dataNodeOfNewPartition));
    // verify that boostrap replica map is empty because recently added replica is not on current node
    assertTrue("Bootstrap replica map should be empty", helixClusterManager.getBootstrapReplicaMap().isEmpty());
    // 4. test that new replica of existing partition is successfully created based on infos from Helix property store.
    assertNotNull("New replica should be created successfully",
        helixClusterManager.getBootstrapReplica(partitionOfNewReplica.toPathString(), dataNodeOfNewReplica));
    assertEquals("There should be exactly one entry in bootstrap replica map", 1,
        helixClusterManager.getBootstrapReplicaMap().size());
    helixClusterManager.close();
  }
Test HelixClusterManager can get a new replica with given partitionId, hostname and port number.
@throws Exception
ambry-clustermap/src/test/java/com/github/ambry/clustermap/HelixClusterManagerTest.java