/**
   * Write a modify request using an exclusion filter attribute.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModifyRequestFilterAttributesExcluded() 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

    writer.setExcludeAttribute(AttributeDescription.valueOf("work-phone"));
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(3);
    assertThat(actual.get(0)).isEqualTo("dn: cn=scarter,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: modify");
    assertThat(actual.get(2)).isEqualTo("");
  }
  /**
   * Write a modify request.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModifyRequest() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ModifyRequest changeRequest =
        Requests.newModifyRequest(
            "version: 1",
            "dn: cn=scarter,dc=example,dc=com",
            "changetype: modify",
            "add: work-phone",
            "work-phone: 650/506-7000");

    // @formatter:on
    writer.writeChangeRecord(changeRequest);
    writer.close();

    // version number is skipped.
    assertThat(actual.get(0)).isEqualTo("dn: cn=scarter,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: modify");
    assertThat(actual.get(2)).isEqualTo("add: work-phone");
    assertThat(actual.get(3)).isEqualTo("work-phone: 650/506-7000");
    assertThat(actual.get(4)).isEqualTo("-");
  }
  /**
   * A delete Record with a control.
   *
   * @throws Exception
   */
  @Test
  public void testWriteDeleteRequestContainingControl() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);
    final DN dn = DN.valueOf("uid=scarter,ou=People,dc=example,dc=com");

    // @formatter:off
    final DeleteRequest changeRequest =
        Requests.newDeleteRequest(dn)
            .addControl(
                PersistentSearchRequestControl.newControl(
                    true,
                    true,
                    true, // isCritical, changesOnly, returnECs
                    PersistentSearchChangeType.ADD,
                    PersistentSearchChangeType.DELETE,
                    PersistentSearchChangeType.MODIFY,
                    PersistentSearchChangeType.MODIFY_DN));
    // @formatter:on

    writer.writeComment("This record contains a control");
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("# This record contains a control");
    assertThat(actual.get(1)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.get(2)).contains("control: 2.16.840.1.113730.3.4.3 true");
    assertThat(actual.get(3)).isEqualTo("changetype: delete");
    assertThat(actual.get(4)).isEqualTo("");
  }
  /**
   * 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 SetIncludeBranch method of LDIFChangeRecordWriter Throws a NullPointerException if the
  * includeBranch is null.
  *
  * @throws Exception If the test failed unexpectedly.
  */
 @Test(expectedExceptions = NullPointerException.class)
 public void testSetIncludeBranchDoesntAllowNull() throws Exception {
   final List<String> actual = new ArrayList<>();
   final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);
   try {
     writer.setIncludeBranch(null);
   } finally {
     writer.close();
   }
 }
  /**
   * Test to write a simple comment with the LDFChangeRecordWriter.
   *
   * @throws Exception
   */
  @Test
  public void testWriteComment() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    writer.writeComment("TLDIFChangeRecordWriter, this is a comment.");
    writer.close();

    assertThat(actual.get(0)).isEqualTo("# TLDIFChangeRecordWriter, this is a comment.");
  }
  /**
   * Test to write a record adding the user friendly Comment.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(enabled = false)
  public void testSetAddUserFriendlyComments() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final CharSequence comment = "A simple comment";

    writer.setAddUserFriendlyComments(true);
    writer.writeComment0(comment);
    writer.close();
  }
  /**
   * Write an AddRequestChange LDIF.
   *
   * @throws Exception
   */
  @Test
  public void testWriteAddRequest() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

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

    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.size()).isEqualTo(14);
  }
 /**
  * Verify the LDIFWriteChangeRecord write and correctly flush and close.
  *
  * @throws Exception
  */
 @Test
 public void testWriteChangeRecordFlushClose() throws Exception {
   final OutputStream mockOutput = mock(OutputStream.class);
   final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(mockOutput);
   try {
     writer.writeComment("TLDIFChangeRecordWriter, this is a comment.");
     writer.flush();
     writer.flush();
     verify(mockOutput, times(2)).flush();
   } finally {
     writer.close();
     verify(mockOutput, times(1)).close();
   }
 }
  /**
   * Test SetIncludeBranch method of LDIFChangeRecordWriter DN included is "dc=example,dc=com",
   * which is not the one from the record. Record must not be written.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetIncludeBranchWithNoMatch() 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.setIncludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    // No result expected
    assertThat(actual.size()).isEqualTo(0);
  }
  /**
   * Test SetIncludeAttribute method of LDIFChangeRecordWriter. Inserting attribute cn (common name)
   * & sn (surname)
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetIncludeAttributeWithNoMatch() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final AddRequest changeRequest = Requests.newAddRequest(getAddLDIFChangeRecord());

    writer.setIncludeAttribute(AttributeDescription.valueOf("vip"));
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: add");
    assertThat(actual.get(2)).contains("");
  }
  /**
   * 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 SetIncludeBranch method of LDIFChangeRecordWriter verifying right data are present.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetIncludeBranchWithMatch() 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.setIncludeBranch(dn);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    // Must contains all the attributes
    assertThat(actual.get(0)).contains("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.size()).isEqualTo(14);
  }
  /**
   * Test SetExcludeBranch method of LDIFChangeRecordWriter.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetExcludeBranchWrongDN() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

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

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

    // Even if DN is wrong then record must be write.
    assertThat(actual.size()).isEqualTo(14);
    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
  }
  /**
   * Write an ModifyRequest LDIF.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModifyRequestNoModifications() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ModifyRequest changeRequest =
        Requests.newModifyRequest(
            "version: 1", "", "dn: cn=scarter,dc=example,dc=com", "changetype: modify");
    // @formatter:on
    writer.writeChangeRecord(changeRequest);
    writer.close();

    // No changes, nothing to do, the record is not written.
    assertThat(actual.size()).isEqualTo(0);
  }
  /**
   * Test to write a record excluding all operational attributes setExcludeAllOperationalAttributes
   * is forced to true.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetExcludeAllOperationalAttributesTrue() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final AddRequest changeRequest = Requests.newAddRequest(getAddLDIFChangeRecord());
    writer.setExcludeAllOperationalAttributes(true);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    for (String line : actual) {
      assertThat(line).doesNotContain("entryUUID");
      assertThat(line).doesNotContain("entryDN");
    }
    assertThat(actual.size()).isEqualTo(10);
  }
  /**
   * 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);
  }
  /**
   * Test to write an LDIFChangeRecordWriter with attribute exclusions. In this case, vip attribute
   * is not present in the example. All lines must be written.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetExcludeAttributeWithNoMatch() throws Exception {
    final AttributeDescription attribute = AttributeDescription.valueOf("vip");
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    final AddRequest changeRequest = Requests.newAddRequest(getAddLDIFChangeRecord());
    writer.setExcludeAttribute(attribute);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.size()).isEqualTo(14);
    for (String line : actual) {
      // we have excluded this attribute especially.
      assertThat(line).doesNotContain("vip");
    }
  }
  /**
   * 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);
  }
  /**
   * Test WriteComment method of LDIFChangeRecordWriter using the wrap function. set wrap to 0.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testWriteCommentUsingTheWrapFunctionShortComment() throws Exception {
    final CharSequence comment = "Lorem ipsum dolor";

    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    int wrapColumn = 30;
    writer.setWrapColumn(wrapColumn);
    writer.writeComment(comment);
    writer.close();

    for (String line : actual) {
      // Each line started with #
      assertThat(line.startsWith("#")).isTrue();
      assertThat(line.length()).isLessThanOrEqualTo(wrapColumn);
    }
  }
  /**
   * Write an ChangeRecord Moddn LDIF.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModdnRequest() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ModifyDNRequest changeRequest =
        Requests.newModifyDNRequest("uid=scarter,ou=People,dc=example,dc=com", "cn=carter");
    // @formatter:on

    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(5);
    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: modrdn");
    assertThat(actual.get(2)).isEqualTo("newrdn: cn=carter");
    assertThat(actual.get(3)).isEqualTo("deleteoldrdn: 0");
  }
  /**
   * Test WriteComment method of LDIFChangeRecordWriter using the wrap function. The comment doesn't
   * contain any empty spaces.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testWriteCommentUsingTheWrapFunctionNoEmptySpace() throws Exception {
    final CharSequence comment = "Lorem ipsumdolorsitamet,consecteturadipisicingelit";

    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    int wrapColumn = 15;
    writer.setWrapColumn(wrapColumn);
    writer.writeComment(comment);
    writer.close();

    for (String line : actual) {
      // The line length <= writer.wrapColumn
      assertThat(line.length()).isLessThanOrEqualTo(wrapColumn);
      // Each line started with #
      assertThat(line.startsWith("#")).isTrue();
    }
  }
  /**
   * Write an AddRequestChange LDIF. The dn/sn is base64 encoded, and contain ascii chars. If they
   * aren't containing ascii, they will not be translated.
   *
   * @throws Exception
   */
  @Test
  public void testWriteAddBinaryRequest() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final AddRequest changeRequest =
        Requests.newAddRequest(
            "dn:: dWlkPXJvZ2FzYXdhcmE=", "changetype: add", "sn::cm9nYXNhd2FyYQ==");
    // @formatter:on

    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: uid=rogasawara");
    assertThat(actual.get(2)).isEqualTo("sn: rogasawara");
    assertThat(actual.get(3)).isEqualTo("");
    assertThat(actual.size()).isEqualTo(4);
  }
  /**
   * Write a delete request.
   *
   * @throws Exception
   */
  @Test
  public void testWriteDeleteRequest() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final DeleteRequest changeRequest =
        (DeleteRequest)
            Requests.newChangeRecord(
                "# Delete an existing entry",
                "dn: cn=Robert Jensen, ou=Marketing, dc=airius, dc=com",
                "changetype: delete");
    // @formatter:on

    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: cn=Robert Jensen, ou=Marketing, dc=airius, dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: delete");
  }
  /**
   * Write an ChangeRecord add type LDIF.
   *
   * @throws Exception
   */
  @Test
  public void testWriteChangeRecord() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ChangeRecord changeRequest =
        Requests.newChangeRecord(
            "version: 1",
            "dn: uid=scarter,ou=People,dc=example,dc=com",
            "changetype: add",
            "sn: Carter");
    // @formatter:on

    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: add");
    assertThat(actual.get(2)).isEqualTo("sn: Carter");
    assertThat(actual.get(3)).isEqualTo("");
  }
  /**
   * Write a modify request containing a control.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModifyRequestUsingControl() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);

    // @formatter:off
    final ModifyRequest changeRequest =
        Requests.newModifyRequest("cn=scarter,dc=example,dc=com")
            .addControl(PreReadRequestControl.newControl(true, "mail"))
            .addModification(ModificationType.REPLACE, "mail", "*****@*****.**");
    // @formatter:on

    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.size()).isEqualTo(7);
    assertThat(actual.get(0)).isEqualTo("dn: cn=scarter,dc=example,dc=com");
    assertThat(actual.get(1)).contains("control: 1.3.6.1.1.13.1 true:");
    assertThat(actual.get(2)).isEqualTo("changetype: modify");
    assertThat(actual.get(3)).isEqualTo("replace: mail");
    assertThat(actual.get(4)).isEqualTo("mail: [email protected]");
    assertThat(actual.get(5)).isEqualTo("-");
  }
  /**
   * 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);
  }
  /**
   * Test the LDIFWriteChangeRecord using an output file verifying write is correctly invoked.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testWriteEntryOutputStreamUsingByteArrayOutputStream() throws Exception {
    final OutputStream out = new ByteArrayOutputStream();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(out);

    // @formatter:off
    final String[] lines = {"dn: cn=scarter,dc=example,dc=com", "changetype: add", "sn: Carter"};
    // @formatter:on

    final AddRequest changeRequest = Requests.newAddRequest(lines);
    writer.writeChangeRecord(changeRequest);
    writer.close();

    BufferedReader reader =
        new BufferedReader(
            new InputStreamReader(new ByteArrayInputStream(out.toString().getBytes())));

    assertThat(reader.readLine()).isEqualTo(lines[0]);
    assertThat(reader.readLine()).isEqualTo(lines[1]);
    assertThat(reader.readLine()).isEqualTo(lines[2]);
    assertThat(reader.readLine()).isEqualTo("");
    assertThat(reader.readLine()).isNull();
  }
  /**
   * Write an ChangeRecord Moddn LDIF.
   *
   * @throws Exception
   */
  @Test
  public void testWriteModdnRequestNewSuperior() 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: uid=scarter,ou=People,dc=example,dc=com",
                "changetype: moddn",
                "newrdn: cn=carter",
                "deleteoldrdn: true",
                "newsuperior:   ou=People,dc=example,dc=org");
    // @formatter:on
    writer.writeChangeRecord(changeRequest);
    writer.close();

    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.size()).isEqualTo(6);
    assertThat(actual.get(3)).isEqualTo("deleteoldrdn: 1");
    assertThat(actual.get(4)).isEqualTo("newsuperior: ou=People,dc=example,dc=org");
  }
  /**
   * Test to write a change record containing an URL.
   *
   * @throws Exception
   */
  @Test
  public void testWriteAddRequestJpegAttributeOk() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);
    final File file = File.createTempFile("sdk", ".jpeg");
    final String url = file.toURI().toURL().toString();

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

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

    writer.writeChangeRecord(changeRequest);
    file.delete();
    writer.close();

    assertThat(actual.size()).isEqualTo(5);
    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.get(1)).isEqualTo("changetype: add");
    assertThat(actual.get(2)).isEqualTo("sn: Carter");
    assertThat(actual.get(3)).contains("jpegphoto: file:/");
  }