/**
   * 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);
  }
  /**
   * 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 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 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");
  }
  /**
   * 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 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 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 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);
  }
  /**
   * 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();
  }
  /**
   * 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:/");
  }
示例#13
0
  /**
   * Updates the config file during the upgrade process.
   *
   * @param configPath The original path to the file.
   * @param filter The filter to select entries. Only useful for modify change type.
   * @param changeType The change type which must be applied to ldif lines.
   * @param ldifLines The change record ldif lines. For ADD change type, the first line must be the
   *     dn. For DELETE change type, the first and only line must be the dn.
   * @throws IOException If an Exception occurs during the input output methods.
   * @return The changes number that have occurred.
   */
  static int updateConfigFile(
      final String configPath,
      final Filter filter,
      final ChangeOperationType changeType,
      final String... ldifLines)
      throws IOException {
    final File original = new File(configPath);
    final File copyConfig = File.createTempFile("copyConfig", ".tmp", original.getParentFile());

    int changeCount = 0;
    LDIFEntryReader entryReader = null;
    LDIFEntryWriter writer = null;
    try {
      final Schema schema = getUpgradeSchema();
      entryReader = new LDIFEntryReader(new FileInputStream(configPath)).setSchema(schema);

      writer = new LDIFEntryWriter(new FileOutputStream(copyConfig));
      writer.setWrapColumn(80);

      // Writes the header on the new file.
      writer.writeComment(INFO_CONFIG_FILE_HEADER.get());
      writer.setWrapColumn(0);

      boolean entryAlreadyExist = false;
      DN ldifDN = null;
      if (filter == null && (changeType == ADD || changeType == DELETE)) {
        // The first line should start with dn:
        ldifDN = DN.valueOf(ldifLines[0].replaceFirst("dn: ", ""));
      }
      final Filter f = filter != null ? filter : Filter.alwaysFalse();
      final Matcher matcher = f.matcher(schema);
      while (entryReader.hasNext()) {
        Entry entry = entryReader.readEntry();
        final DN entryDN = entry.getName();
        // Searching for the related entries
        if (changeType == MODIFY && matcher.matches(entry) == ConditionResult.TRUE) {
          try {
            final ModifyRequest mr =
                Requests.newModifyRequest(readLDIFLines(entryDN, changeType, ldifLines));
            entry = Entries.modifyEntryPermissive(entry, mr.getModifications());
            changeCount++;
            logger.debug(
                LocalizableMessage.raw("The following entry has been modified : %s", entryDN));
          } catch (Exception ex) {
            logger.error(LocalizableMessage.raw(ex.getMessage()));
          }
        }

        if (entryDN.equals(ldifDN)) {
          logger.debug(LocalizableMessage.raw("Entry %s found", entryDN));
          entryAlreadyExist = true;

          if (changeType == DELETE) {
            entry = null;
            changeCount++;
            logger.debug(
                LocalizableMessage.raw("The following entry has been deleted : %s", entryDN));
          }
        }

        if (entry != null) {
          writer.writeEntry(entry);
        }
      }

      if (changeType == ADD && !entryAlreadyExist) {
        final AddRequest ar = Requests.newAddRequest(ldifLines);
        writer.writeEntry(ar);
        logger.debug(
            LocalizableMessage.raw(
                "Entry successfully added %s in %s", ldifDN, original.getAbsolutePath()));
        changeCount++;
      }
    } catch (Exception ex) {
      throw new IOException(ex.getMessage());
    } finally {
      // The reader and writer must be close before renaming files.
      // Otherwise it causes exceptions under windows OS.
      StaticUtils.close(entryReader, writer);
    }

    try {
      // Renaming the file, overwriting previous one.
      rename(copyConfig, new File(configPath));
    } catch (IOException e) {
      logger.error(LocalizableMessage.raw(e.getMessage()));
      deleteRecursively(original);
      throw e;
    }

    return changeCount;
  }
  /**
   * Write a full example containing multiple change records.
   *
   * @throws Exception
   */
  @Test
  public void testWriteMultipleChangeRecords() throws Exception {
    final List<String> actual = new ArrayList<>();
    final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual);
    final ChangeRecord changeRequest = Requests.newAddRequest(getAddLDIFChangeRecord());

    // @formatter:off
    final ModifyDNRequest changeRequest2 =
        Requests.newModifyDNRequest("cn=scarter,dc=example,dc=com", "cn=Susan Jacobs")
            .setDeleteOldRDN(false);
    // @formatter:on

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

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

    final File file = File.createTempFile("sdk", null);
    final String url = file.toURI().toURL().toString();

    // @formatter:off
    final AddRequest changeRequest5 =
        Requests.newAddRequest(
            "version: 1",
            "# Add a new record",
            "dn: cn=Fiona Jensen, ou=Marketing, dc=airius, dc=com",
            "changetype: add",
            "objectclass: top",
            "objectclass: person",
            "objectclass: organizationalPerson",
            "cn: Fiona Jensen",
            "sn: Jensen",
            "uid: fiona",
            "telephonenumber: +1 408 555 1212",
            "jpegphoto:< " + url);
    // @formatter:on

    writer.writeChangeRecord(changeRequest);
    writer.writeChangeRecord(changeRequest2);
    writer.writeChangeRecord(changeRequest3);
    writer.writeChangeRecord(changeRequest4);
    writer.writeComment("A comment...");
    writer.writeChangeRecord(changeRequest5);
    writer.close();

    assertThat(actual.size()).isGreaterThan(10);
    assertThat(actual.get(0)).isEqualTo("dn: uid=scarter,ou=People,dc=example,dc=com");
    assertThat(actual.get(actual.size() - 1)).isEqualTo("");

    file.delete();
  }