@Test
public void testInterAppConstraintsByAppID()
    throws InvalidAllocationTagsQueryException {

  AllocationTagsManager tm = new AllocationTagsManager(rmContext);
  PlacementConstraintManagerService pcm =
      new MemoryPlacementConstraintManager();
  rmContext.setAllocationTagsManager(tm);
  rmContext.setPlacementConstraintManager(pcm);
  long ts = System.currentTimeMillis();
  ApplicationId application1 = BuilderUtils.newApplicationId(ts, 123);
  ApplicationId application2 = BuilderUtils.newApplicationId(ts, 124);
  ApplicationId application3 = BuilderUtils.newApplicationId(ts, 125);
  // Register App1 with anti-affinity constraint map.
  RMNode n0r1 = rmNodes.get(0);
  RMNode n1r1 = rmNodes.get(1);
  RMNode n2r2 = rmNodes.get(2);
  RMNode n3r2 = rmNodes.get(3);
  /**
   * Place container:
   *  n0: app1/hbase-m(1)
   *  n1: ""
   *  n2: app1/hbase-m(1)
   *  n3: ""
   */
  tm.addContainer(n0r1.getNodeID(),
      newContainerId(application1, 0), ImmutableSet.of("hbase-m"));
  tm.addContainer(n2r2.getNodeID(),
      newContainerId(application1, 1), ImmutableSet.of("hbase-m"));
  Assert.assertEquals(1L, tm.getAllocationTagsWithCount(n0r1.getNodeID())
      .get("hbase-m").longValue());
  Assert.assertEquals(1L, tm.getAllocationTagsWithCount(n2r2.getNodeID())
      .get("hbase-m").longValue());
  SchedulerNode schedulerNode0 =newSchedulerNode(n0r1.getHostName(),
      n0r1.getRackName(), n0r1.getNodeID());
  SchedulerNode schedulerNode1 =newSchedulerNode(n1r1.getHostName(),
      n1r1.getRackName(), n1r1.getNodeID());
  SchedulerNode schedulerNode2 =newSchedulerNode(n2r2.getHostName(),
      n2r2.getRackName(), n2r2.getNodeID());
  SchedulerNode schedulerNode3 =newSchedulerNode(n3r2.getHostName(),
      n3r2.getRackName(), n3r2.getNodeID());
  TargetApplicationsNamespace namespace =
      new TargetApplicationsNamespace.AppID(application1);
  Map<Set<String>, PlacementConstraint> constraintMap = new HashMap<>();
  PlacementConstraint constraint2 = PlacementConstraints
      .targetNotIn(NODE, allocationTagWithNamespace(namespace.toString(),
          "hbase-m"))
      .build();
  Set<String> srcTags2 = new HashSet<>();
  srcTags2.add("app2");
  constraintMap.put(srcTags2, constraint2);
  pcm.registerApplication(application2, constraintMap);
  // Anti-affinity with app1/hbase-m so it should not be able to be placed
  // onto n0 and n2 as they already have hbase-m allocated.
  Assert.assertFalse(PlacementConstraintsUtil.canSatisfyConstraints(
      application2, createSchedulingRequest(srcTags2),
      schedulerNode0, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil.canSatisfyConstraints(
      application2, createSchedulingRequest(srcTags2),
      schedulerNode1, pcm, tm));
  Assert.assertFalse(PlacementConstraintsUtil.canSatisfyConstraints(
      application2, createSchedulingRequest(srcTags2),
      schedulerNode2, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil.canSatisfyConstraints(
      application2, createSchedulingRequest(srcTags2),
      schedulerNode3, pcm, tm));
  // Intra-app constraint
  // Test with default and empty namespace
  TargetApplicationsNamespace self = new TargetApplicationsNamespace.Self();
  PlacementConstraint constraint3 = PlacementConstraints
      .targetNotIn(NODE, allocationTagWithNamespace(self.toString(),
          "hbase-m"))
      .build();
  Set<String> srcTags3 = new HashSet<>();
  srcTags3.add("app3");
  constraintMap.put(srcTags3, constraint3);
  pcm.registerApplication(application3, constraintMap);
  /**
   * Place container:
   *  n0: app1/hbase-m(1), app3/hbase-m
   *  n1: ""
   *  n2: app1/hbase-m(1)
   *  n3: ""
   */
  tm.addContainer(n0r1.getNodeID(),
      newContainerId(application3, 0), ImmutableSet.of("hbase-m"));
  // Anti-affinity to self/hbase-m
  Assert.assertFalse(PlacementConstraintsUtil
      .canSatisfyConstraints(application3, createSchedulingRequest(srcTags3),
          schedulerNode0, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil
      .canSatisfyConstraints(application3, createSchedulingRequest(srcTags3),
          schedulerNode1, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil
      .canSatisfyConstraints(application3, createSchedulingRequest(srcTags3),
          schedulerNode2, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil
      .canSatisfyConstraints(application3, createSchedulingRequest(srcTags3),
          schedulerNode3, pcm, tm));
  pcm.unregisterApplication(application3);
}