@Test
public void testNotSelfAppConstraints()
    throws InvalidAllocationTagsQueryException {

  long ts = System.currentTimeMillis();
  ApplicationId application1 = BuilderUtils.newApplicationId(ts, 100);
  ApplicationId application2 = BuilderUtils.newApplicationId(ts, 101);
  ApplicationId application3 = BuilderUtils.newApplicationId(ts, 102);
  ConcurrentMap<ApplicationId, RMApp> allApps = new ConcurrentHashMap<>();
  allApps.put(application1, new MockRMApp(123, 1000,
      RMAppState.NEW, "userA", ImmutableSet.of("")));
  allApps.put(application2, new MockRMApp(124, 1001,
      RMAppState.NEW, "userA", ImmutableSet.of("")));
  allApps.put(application3, new MockRMApp(125, 1002,
      RMAppState.NEW, "userA", ImmutableSet.of("")));
  RMContext mockedContext = Mockito.spy(rmContext);
  when(mockedContext.getRMApps()).thenReturn(allApps);
  AllocationTagsManager tm = new AllocationTagsManager(mockedContext);
  PlacementConstraintManagerService pcm =
      new MemoryPlacementConstraintManager();
  mockedContext.setAllocationTagsManager(tm);
  mockedContext.setPlacementConstraintManager(pcm);
  // 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/A(1), app2/A(1)
   *  n1: app3/A(3)
   *  n2: app1/A(2)
   *  n3: ""
   */
  tm.addContainer(n0r1.getNodeID(),
      newContainerId(application1), ImmutableSet.of("A"));
  tm.addContainer(n0r1.getNodeID(),
      newContainerId(application2), ImmutableSet.of("A"));
  tm.addContainer(n1r1.getNodeID(),
      newContainerId(application3), ImmutableSet.of("A"));
  tm.addContainer(n1r1.getNodeID(),
      newContainerId(application3), ImmutableSet.of("A"));
  tm.addContainer(n1r1.getNodeID(),
      newContainerId(application3), ImmutableSet.of("A"));
  tm.addContainer(n2r2.getNodeID(),
      newContainerId(application1), ImmutableSet.of("A"));
  tm.addContainer(n2r2.getNodeID(),
      newContainerId(application1), ImmutableSet.of("A"));
  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 notSelf =
      new TargetApplicationsNamespace.NotSelf();
  //***************************
  // 1) not-self, app1
  //***************************
  // Anti-affinity with "A" from app2 and app3,
  // n0 and n1 both have tag "A" from either app2 or app3, so they are
  // not qualified for the placement.
  PlacementConstraint constraint1 = PlacementConstraints.targetNotIn(
      NODE, allocationTagWithNamespace(notSelf.toString(), "A"))
      .build();
  Map<Set<String>, PlacementConstraint> constraintMap = new HashMap<>();
  Set<String> srcTags1 = ImmutableSet.of("A");
  constraintMap.put(srcTags1, constraint1);
  pcm.registerApplication(application1, constraintMap);
  Assert.assertFalse(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags1),
      schedulerNode0, pcm, tm));
  Assert.assertFalse(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags1),
      schedulerNode1, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags1),
      schedulerNode2, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags1),
      schedulerNode3, pcm, tm));
  pcm.unregisterApplication(application1);
  //***************************
  // 2) not-self, app1
  //***************************
  // Affinity with "A" from app2 and app3,
  // N0 and n1 are qualified for the placement.
  PlacementConstraint constraint2 = PlacementConstraints.targetIn(
      NODE, allocationTagWithNamespace(notSelf.toString(), "A"))
      .build();
  Map<Set<String>, PlacementConstraint> cm2 = new HashMap<>();
  Set<String> srcTags2 = ImmutableSet.of("A");
  cm2.put(srcTags2, constraint2);
  pcm.registerApplication(application1, cm2);
  Assert.assertTrue(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags2),
      schedulerNode0, pcm, tm));
  Assert.assertTrue(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags2),
      schedulerNode1, pcm, tm));
  Assert.assertFalse(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags2),
      schedulerNode2, pcm, tm));
  Assert.assertFalse(PlacementConstraintsUtil.canSatisfyConstraints(
      application1, createSchedulingRequest(srcTags2),
      schedulerNode3, pcm, tm));
  pcm.unregisterApplication(application1);
}