@Test
  public void testGetRandomWritablePartition() {
    Set<PartitionState> writablePartitionStates = new HashSet<>();
    writablePartitionStates.add(PartitionState.READ_WRITE);
    writablePartitionStates.add(PartitionState.PARTIAL_READ_WRITE);
    List<PartitionId> partitionIdsToExclude = new ArrayList<>();
    List<? extends PartitionId> allPartitionIds = clusterManager.getAllPartitionIds(null);
    int totalWritablePartitions = (int) allPartitionIds.stream()
        .filter(partitionId -> partitionId.getPartitionState() != PartitionState.READ_ONLY)
        .count();

    // Call getRandomWritablePartition as many times as there are writable partitions. It should always return writable partition.
    for (int i = 0; i < totalWritablePartitions; i++) {
      PartitionId partitionId = clusterManager.getRandomWritablePartition(null, partitionIdsToExclude);
      if (partitionId == null) {
        System.out.print("null");
      }
      assertTrue(
          "getRandomWritablePartition should always return partitions in READ_WRITE or PARTIAL_READ_WRITE states only.",
          writablePartitionStates.contains(partitionId.getPartitionState()));
      partitionIdsToExclude.add(partitionId);
    }
    // Now that all the writable partitions are in the excluded list, getRandomWritablePartition should not return any partition.
    assertNull(clusterManager.getRandomWritablePartition(null, partitionIdsToExclude));
  }
Test for {@link HelixClusterManager#getRandomWritablePartition(String, List)}.
ambry-clustermap/src/test/java/com/github/ambry/clustermap/HelixClusterManagerTest.java