public static void enforcePartitionExclusivity(ResourceRequest resReq,
      Set<String> enforcedPartitions, String appLabel) {
    if (enforcedPartitions == null || enforcedPartitions.isEmpty()) {
      return;
    }
    if (!enforcedPartitions.contains(appLabel)
        && enforcedPartitions.contains(resReq.getNodeLabelExpression())) {
      resReq.setNodeLabelExpression(appLabel);
    }
    if (enforcedPartitions.contains(appLabel)) {
      resReq.setNodeLabelExpression(appLabel);
    }
  }
@Test
  public void testEnforcePartitionExclusivity() {
    String enforcedExclusiveLabel = "x";
    Set<String> enforcedExclusiveLabelSet =
        Collections.singleton(enforcedExclusiveLabel);
    String dummyLabel = "y";
    String appLabel = "appLabel";
    ResourceRequest rr = BuilderUtils.newResourceRequest(
        mock(Priority.class), ResourceRequest.ANY, mock(Resource.class), 1);

    // RR label unset and app label does not match. Nothing should happen.
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        null);
    Assert.assertNull(rr.getNodeLabelExpression());
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        appLabel);
    Assert.assertNull(rr.getNodeLabelExpression());

    // RR label and app label do not match. Nothing should happen.
    rr.setNodeLabelExpression(dummyLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        null);
    Assert.assertEquals(dummyLabel, rr.getNodeLabelExpression());
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        appLabel);
    Assert.assertEquals(dummyLabel, rr.getNodeLabelExpression());

    // RR label matches but app label does not. RR label should be set
    // to app label
    rr.setNodeLabelExpression(enforcedExclusiveLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        null);
    Assert.assertNull(rr.getNodeLabelExpression());
    rr.setNodeLabelExpression(enforcedExclusiveLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        appLabel);
    Assert.assertEquals(appLabel, rr.getNodeLabelExpression());

    // RR label unset and app label matches. RR label should be set
    // to app label
    rr.setNodeLabelExpression(null);
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        enforcedExclusiveLabel);
    Assert.assertEquals(enforcedExclusiveLabel, rr.getNodeLabelExpression());

    // RR label does not match and app label matches. RR label should be set
    // to app label
    rr.setNodeLabelExpression(dummyLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        enforcedExclusiveLabel);
    Assert.assertEquals(enforcedExclusiveLabel, rr.getNodeLabelExpression());

    // RR label and app label matches. Nothing should happen.
    rr.setNodeLabelExpression(enforcedExclusiveLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, enforcedExclusiveLabelSet,
        enforcedExclusiveLabel);
    Assert.assertEquals(enforcedExclusiveLabel, rr.getNodeLabelExpression());

    // Unconfigured label: nothing should happen.
    rr.setNodeLabelExpression(null);
    SchedulerUtils.enforcePartitionExclusivity(rr, null,
        appLabel);
    Assert.assertNull(rr.getNodeLabelExpression());
    rr.setNodeLabelExpression(dummyLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, null,
        appLabel);
    Assert.assertEquals(dummyLabel, rr.getNodeLabelExpression());
    rr.setNodeLabelExpression(enforcedExclusiveLabel);
    SchedulerUtils.enforcePartitionExclusivity(rr, null,
        appLabel);
    Assert.assertEquals(enforcedExclusiveLabel, rr.getNodeLabelExpression());
  }