@Test
public void testPackageRequests() {

  MockAMSimulator app = new MockAMSimulator();
  List<ContainerSimulator> containerSimulators = new ArrayList<>();
  Resource resource = Resources.createResource(1024);
  int priority = 1;
  ExecutionType execType = ExecutionType.GUARANTEED;
  String type = "map";
  ContainerSimulator s1 = new ContainerSimulator(resource, 100,
      "/default-rack/h1", priority, type, execType);
  ContainerSimulator s2 = new ContainerSimulator(resource, 100,
      "/default-rack/h1", priority, type, execType);
  ContainerSimulator s3 = new ContainerSimulator(resource, 100,
      "/default-rack/h2", priority, type, execType);
  containerSimulators.add(s1);
  containerSimulators.add(s2);
  containerSimulators.add(s3);
  List<ResourceRequest> res = app.packageRequests(containerSimulators,
      priority);
  // total 4 resource requests: any -> 1, rack -> 1, node -> 2
  // All resource requests for any would be packaged into 1.
  // All resource requests for racks would be packaged into 1 as all of them
  // are for same rack.
  // All resource requests for nodes would be packaged into 2 as there are
  // two different nodes.
  Assert.assertEquals(4, res.size());
  int anyRequestCount = 0;
  int rackRequestCount = 0;
  int nodeRequestCount = 0;
  for (ResourceRequest request : res) {
    String resourceName = request.getResourceName();
    if (resourceName.equals("*")) {
      anyRequestCount++;
    } else if (resourceName.equals("/default-rack")) {
      rackRequestCount++;
    } else {
      nodeRequestCount++;
    }
  }
  Assert.assertEquals(1, anyRequestCount);
  Assert.assertEquals(1, rackRequestCount);
  Assert.assertEquals(2, nodeRequestCount);
  containerSimulators.clear();
  s1 = new ContainerSimulator(resource, 100,
      "/default-rack/h1", priority, type, execType, 1, 0);
  s2 = new ContainerSimulator(resource, 100,
      "/default-rack/h1", priority, type, execType, 2, 0);
  s3 = new ContainerSimulator(resource, 100,
      "/default-rack/h2", priority, type, execType, 1, 0);
  containerSimulators.add(s1);
  containerSimulators.add(s2);
  containerSimulators.add(s3);
  res = app.packageRequests(containerSimulators, priority);
  // total 7 resource requests: any -> 2, rack -> 2, node -> 3
  // All resource requests for any would be packaged into 2 as there are
  // two different allocation id.
  // All resource requests for racks would be packaged into 2 as all of them
  // are for same rack but for two different allocation id.
  // All resource requests for nodes would be packaged into 3 as either node
  // or allocation id is different for each request.
  Assert.assertEquals(7, res.size());
  anyRequestCount = 0;
  rackRequestCount = 0;
  nodeRequestCount = 0;
  for (ResourceRequest request : res) {
    String resourceName = request.getResourceName();
    long allocationId = request.getAllocationRequestId();
    // allocation id should be either 1 or 2
    Assert.assertTrue(allocationId == 1 || allocationId == 2);
    if (resourceName.equals("*")) {
      anyRequestCount++;
    } else if (resourceName.equals("/default-rack")) {
      rackRequestCount++;
    } else {
      nodeRequestCount++;
    }
  }
  Assert.assertEquals(2, anyRequestCount);
  Assert.assertEquals(2, rackRequestCount);
  Assert.assertEquals(3, nodeRequestCount);
}