/**
   * Write a modify request using branch exclusion.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModifyRequestBranchExcludedNoMatch() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ModifyRequest changeRequest =
        (ModifyRequest)
            Requests.newChangeRecord(
                "version: 1",
                "",
                "dn: cn=scarter,dc=example,dc=com",
                "changetype: modify",
                "replace: work-phone",
                "work-phone: 555-555-1155");
    // @formatter:on

    final DN dn = DN.valueOf("dc=example,dc=org");
    writer.setExcludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(6); // all line plus a ""
    assertThat(actual.get(0)).isEqualTo("dn: cn=scarter,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: modify");
    assertThat(actual.get(5)).isEqualTo("");
  }
  /**
   * Test SetExcludeBranch method of LDIFChangeRecordWriter Throws a NullPointerException if the
   * excludeBranch is null.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = NullPointerException.class)
  public void testSetExcludeBranchDoesntAllowNull() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    try {
      writer.setExcludeBranch(null);
    } finally {
      writer.close();
    }
  }
  /**
   * Test SetExcludeBranch method of LDIFChangeRecordWriter.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetExcludeBranchWithNoMatch() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final DN dn = DN.valueOf("dc=example,dc=com");

    final AddRequest changeRequest = Requests.newAddRequest(getAddLDIFChangeRecord());
    writer.setExcludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    // No values expected - we have excluded the branch.
    Assert.assertFalse(actual.size() > 0);
  }
  /**
   * Test SetExcludeBranch method of LDIFChangeRecordWriter.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetExcludeBranchWithMatch() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final DN dn = DN.valueOf("dc=example,dc=org");

    final AddRequest changeRequest = Requests.newAddRequest(getAddLDIFChangeRecord());
    writer.setExcludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    // The record must be written
    assertThat(actual.size()).isEqualTo(14);
    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
  }
  /**
   * Write an DeleteRequest LDIF. Branch is excluded. The record musn't be written.
   *
   * @throws Exception
   */
  @Test
  public void testWriteDeleteRequestBranchExcluded() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);
    // @formatter:off
    final DeleteRequest changeRequest =
        (DeleteRequest)
            Requests.newChangeRecord(
                "version: 1", "dn: uid=scarter,ou=People,dc=example,dc=com", "changetype: delete");
    // @formatter:on
    final DN dn = DN.valueOf("dc=example,dc=com");
    writer.setExcludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(0);
  }
  /**
   * Write an ChangeRecord LDIF.
   *
   * @throws Exception
   */
  @Test
  public void testWriteAddRequestBranchExcluded() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final DN dnAdd = DN.valueOf("uid=scarter,ou=People,dc=example,dc=com");

    // @formatter:off
    final ChangeRecord changeRequest = Requests.newAddRequest(dnAdd).addAttribute("sn", "Carter");
    // @formatter:on

    final DN dn = DN.valueOf("dc=example,dc=com");
    writer.setExcludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(0);
  }
  /**
   * Write a modifyDN request with a branch exclusion.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModifyDNRequestBranchExcludedMatch() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ModifyDNRequest changeRequest =
        (ModifyDNRequest)
            Requests.newChangeRecord(
                "version: 1",
                "dn: cn=scarter,dc=example,dc=com",
                "changetype: modrdn",
                "newrdn: cn=Susan Jacobs",
                "deleteoldrdn: no");
    // @formatter:on

    final DN dn = DN.valueOf("dc=example,dc=com");
    writer.setExcludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(0);
  }