@Test
  public void getPartitionsTest() throws IOException, JSONException {
    String specialPartitionClass = "specialPartitionClass";
    TestHardwareLayout hardwareLayout = new TestHardwareLayout("Alpha");
    String dc = hardwareLayout.getRandomDatacenter().getName();
    TestPartitionLayout testPartitionLayout = new TestPartitionLayout(hardwareLayout, dc);
    assertTrue("There should be more than 1 replica per partition in each DC for this test to work",
        testPartitionLayout.replicaCountPerDc > 1);
    PartitionRangeCheckParams defaultRw =
        new PartitionRangeCheckParams(0, testPartitionLayout.partitionCount, DEFAULT_PARTITION_CLASS,
            PartitionState.READ_WRITE);
    // add 15 RW partitions for the special class
    PartitionRangeCheckParams specialRw =
        new PartitionRangeCheckParams(defaultRw.rangeEnd + 1, 15, specialPartitionClass, PartitionState.READ_WRITE);
    testPartitionLayout.addNewPartitions(specialRw.count, specialPartitionClass, PartitionState.READ_WRITE, dc);
    // add 10 RO partitions for the default class
    PartitionRangeCheckParams defaultRo =
        new PartitionRangeCheckParams(specialRw.rangeEnd + 1, 10, DEFAULT_PARTITION_CLASS, PartitionState.READ_ONLY);
    testPartitionLayout.addNewPartitions(defaultRo.count, DEFAULT_PARTITION_CLASS, PartitionState.READ_ONLY, dc);
    // add 5 RO partitions for the special class
    PartitionRangeCheckParams specialRo =
        new PartitionRangeCheckParams(defaultRo.rangeEnd + 1, 5, specialPartitionClass, PartitionState.READ_ONLY);
    testPartitionLayout.addNewPartitions(specialRo.count, specialPartitionClass, PartitionState.READ_ONLY, dc);

    PartitionLayout partitionLayout = testPartitionLayout.getPartitionLayout();

    Properties props = new Properties();
    props.setProperty("clustermap.host.name", "localhost");
    props.setProperty("clustermap.cluster.name", "cluster");
    props.setProperty("clustermap.datacenter.name", dc);
    ClusterMapConfig clusterMapConfig = new ClusterMapConfig(new VerifiableProperties(props));
    File tempDir = Files.createTempDirectory("helixClusterManager-" + new Random().nextInt(1000)).toFile();
    String tempDirPath = tempDir.getAbsolutePath();
    String hardwareLayoutPath = tempDirPath + File.separator + "hardwareLayoutTest.json";
    String partitionLayoutPath = tempDirPath + File.separator + "partitionLayoutTest.json";
    Utils.writeJsonObjectToFile(hardwareLayout.getHardwareLayout().toJSONObject(), hardwareLayoutPath);
    Utils.writeJsonObjectToFile(partitionLayout.toJSONObject(), partitionLayoutPath);
    ClusterMap clusterMapManager =
        (new StaticClusterAgentsFactory(clusterMapConfig, hardwareLayoutPath, partitionLayoutPath)).getClusterMap();

    // "good" cases for getPartitions() and getWritablePartitions() only
    // getPartitions(), class null
    List<? extends PartitionId> returnedPartitions = clusterMapManager.getAllPartitionIds(null);
    checkReturnedPartitions(returnedPartitions, Arrays.asList(defaultRw, defaultRo, specialRw, specialRo));
    // getWritablePartitions(), class null
    returnedPartitions = clusterMapManager.getWritablePartitionIds(null);
    checkReturnedPartitions(returnedPartitions, Arrays.asList(defaultRw, specialRw));

    // getPartitions(), class default
    returnedPartitions = clusterMapManager.getAllPartitionIds(DEFAULT_PARTITION_CLASS);
    checkReturnedPartitions(returnedPartitions, Arrays.asList(defaultRw, defaultRo));
    // getWritablePartitions(), class default
    returnedPartitions = clusterMapManager.getWritablePartitionIds(DEFAULT_PARTITION_CLASS);
    checkReturnedPartitions(returnedPartitions, Collections.singletonList(defaultRw));

    // getPartitions(), class special
    returnedPartitions = clusterMapManager.getAllPartitionIds(specialPartitionClass);
    checkReturnedPartitions(returnedPartitions, Arrays.asList(specialRw, specialRo));
    // getWritablePartitions(), class special
    returnedPartitions = clusterMapManager.getWritablePartitionIds(specialPartitionClass);
    checkReturnedPartitions(returnedPartitions, Collections.singletonList(specialRw));

    // to test the dc affinity, we pick one datanode from "dc" and insert 1 replica for part1 (special class) in "dc"
    // and make sure that it is returned in getPartitions() but not in getWritablePartitions() (because all the other
    // partitions have more than 1 replica in "dc").
    DataNode dataNode = hardwareLayout.getRandomDataNodeFromDc(dc);
    Partition partition =
        partitionLayout.addNewPartition(dataNode.getDisks().subList(0, 1), testPartitionLayout.replicaCapacityInBytes,
            specialPartitionClass);
    Utils.writeJsonObjectToFile(partitionLayout.toJSONObject(), partitionLayoutPath);
    clusterMapManager =
        (new StaticClusterAgentsFactory(clusterMapConfig, hardwareLayoutPath, partitionLayoutPath)).getClusterMap();
    PartitionRangeCheckParams extraPartCheckParams =
        new PartitionRangeCheckParams(specialRo.rangeEnd + 1, 1, specialPartitionClass, PartitionState.READ_WRITE);
    // getPartitions(), class special
    returnedPartitions = clusterMapManager.getAllPartitionIds(specialPartitionClass);
    assertTrue("Added partition should exist in returned partitions", returnedPartitions.contains(partition));
    checkReturnedPartitions(returnedPartitions, Arrays.asList(specialRw, specialRo, extraPartCheckParams));
    // getWritablePartitions(), class special
    returnedPartitions = clusterMapManager.getWritablePartitionIds(specialPartitionClass);
    assertFalse("Added partition should not exist in returned partitions", returnedPartitions.contains(partition));
    checkReturnedPartitions(returnedPartitions, Collections.singletonList(specialRw));
  }
Tests for {@link PartitionLayout#getPartitions(String)} and {@link PartitionLayout#getWritablePartitions(String)}.
@throws IOException
@throws JSONException
ambry-clustermap/src/test/java/com/github/ambry/clustermap/StaticClusterManagerTest.java