@Test
  public void testReserveMetadataBlobId() throws Exception {
    String partitionClass = MockClusterMap.SPECIAL_PARTITION_CLASS;
    String invalidPartitionClass = "test";
    List<PartitionId> partitionsToExclude = null;
    ReservedMetadataIdMetrics reservedMetadataIdMetrics =
        ReservedMetadataIdMetrics.getReservedMetadataIdMetrics(new MetricRegistry());
    ClusterMap clusterMap = new MockClusterMap();
    short accountId = 1;
    short containerId = 1;
    boolean isEncrypted = false;
    Properties properties = new Properties();
    properties.setProperty(RouterConfig.ROUTER_HOSTNAME, "localhost");
    properties.setProperty(RouterConfig.ROUTER_DATACENTER_NAME, "DEV");
    RouterConfig routerConfig = new RouterConfig(new VerifiableProperties(properties));
    // test a simple success case.
    BlobId blobId =
        ClusterMapUtils.reserveMetadataBlobId(partitionClass, partitionsToExclude, reservedMetadataIdMetrics,
            clusterMap, accountId, containerId, isEncrypted, routerConfig);
    assertNotNull(blobId);
    assertEquals(partitionClass, blobId.getPartition().getPartitionClass());
    assertEquals(0, reservedMetadataIdMetrics.numFailedPartitionReserveAttempts.getCount());
    assertEquals(0, reservedMetadataIdMetrics.numUnexpectedReservedPartitionClassCount.getCount());

    // test for non-existent partition class.
    blobId =
        ClusterMapUtils.reserveMetadataBlobId(invalidPartitionClass, partitionsToExclude, reservedMetadataIdMetrics,
            clusterMap, accountId, containerId, isEncrypted, routerConfig);
    assertNotNull(blobId);
    assertNotEquals(partitionClass, blobId.getPartition().getPartitionClass());
    assertEquals(MockClusterMap.DEFAULT_PARTITION_CLASS, blobId.getPartition().getPartitionClass());
    assertEquals(0, reservedMetadataIdMetrics.numFailedPartitionReserveAttempts.getCount());
    assertEquals(1, reservedMetadataIdMetrics.numUnexpectedReservedPartitionClassCount.getCount());

    // test when no partitions were selected from cluster map
    List<PartitionId> partitionIdList = clusterMap.getAllPartitionIds(null).stream().map(p -> ((PartitionId) p)).collect(
        Collectors.toList());
    assertNull(ClusterMapUtils.reserveMetadataBlobId(partitionClass, partitionIdList, reservedMetadataIdMetrics,
        clusterMap, accountId, containerId, isEncrypted, routerConfig));
    assertEquals(1, reservedMetadataIdMetrics.numFailedPartitionReserveAttempts.getCount());
    assertEquals(1, reservedMetadataIdMetrics.numUnexpectedReservedPartitionClassCount.getCount());
  }
Tests for {@link ClusterMapUtils#reserveMetadataBlobId}.
@throws Exception in case of any errors.
ambry-clustermap/src/test/java/com/github/ambry/clustermap/ClusterMapUtilsTest.java