Search in sources :

Example 1 with MemoryBasedLogHandler

use of com.unboundid.util.MemoryBasedLogHandler in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerTestCase method testLDIFImportAndExport.

/**
 * Tests operations involving LDIF import and export.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testLDIFImportAndExport() throws Exception {
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.setAccessLogHandler(new MemoryBasedLogHandler());
    cfg.setLDAPDebugLogHandler(new MemoryBasedLogHandler());
    cfg.setCodeLogDetails(createTempFile().getAbsolutePath(), true);
    cfg.setSchema(Schema.getDefaultStandardSchema());
    final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
    assertNotNull(ds);
    assertEquals(ds.countEntries(), 0);
    assertNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getSchema());
    assertNotNull(ds.getBaseDNs());
    assertFalse(ds.getBaseDNs().isEmpty());
    assertEquals(ds.getBaseDNs().size(), 1);
    assertTrue(ds.getBaseDNs().contains(new DN("dc=example,dc=com")));
    final File ldifFile1 = createTempFile("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "dn: uid=user.1,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.1", "givenName: User", "sn: 1", "cn: User 1", "userPassword: password");
    final File ldifFile2 = createTempFile("dn: uid=user.2,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.2", "givenName: User", "sn: 2", "cn: User 2", "userPassword: password", "", "dn: uid=user.3,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.3", "givenName: User", "sn: 3", "cn: User 3", "userPassword: password", "", "dn: uid=user.4,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.4", "givenName: User", "sn: 4", "cn: User 4", "userPassword: password", "", "dn: uid=user.5,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: user.5", "givenName: User", "sn: 5", "cn: User 5", "userPassword: password");
    final File ldifFile3 = createTempFile("This is not a valid LDIF file.", "Attempting to import it shouldn't have any effect on existing data.");
    assertEquals(ds.importFromLDIF(true, ldifFile1.getAbsolutePath()), 3);
    assertEquals(ds.countEntries(), 3);
    assertNotNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getEntry("ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("uid=user.1,ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("uid=user.2,ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("uid=user.3,ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("uid=user.4,ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("uid=user.5,ou=People,dc=example,dc=com"));
    assertEquals(ds.importFromLDIF(false, ldifFile2.getAbsolutePath()), 4);
    assertEquals(ds.countEntries(), 7);
    assertNotNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getEntry("ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("uid=user.1,ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("uid=user.2,ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("uid=user.3,ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("uid=user.4,ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("uid=user.5,ou=People,dc=example,dc=com"));
    // Test an export and verify that all entries are written out.
    final File exportFile = createTempFile();
    assertEquals(ds.exportToLDIF(exportFile.getAbsolutePath(), false, true), 7);
    // Test an export with an invalid path.
    try {
        final String badPath = exportFile.getAbsolutePath() + File.separator + "bogus";
        ds.exportToLDIF(badPath, false, true);
        fail("Expected an exception when trying to export to a bad path.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.LOCAL_ERROR);
    }
    // Re-initialize the data set with the exported LDIF file.
    assertEquals(ds.importFromLDIF(true, exportFile.getAbsolutePath()), 7);
    assertEquals(ds.countEntries(), 7);
    // Try to add entries that already exist.
    try {
        ds.importFromLDIF(false, ldifFile1.getAbsolutePath());
        fail("Expected an exception for an import with entries that already " + "exist");
    } catch (final LDAPException le) {
    // This was expected.
    } finally {
        assertEquals(ds.countEntries(), 7);
    }
    // Try to clear and add entries not starting with the base DN.
    try {
        ds.importFromLDIF(true, ldifFile2.getAbsolutePath());
        fail("Expected an exception for an import without top entries.");
    } catch (final LDAPException le) {
    // This was expected.
    } finally {
        assertEquals(ds.countEntries(), 7);
    }
    // Try to clear and import from a file that doesn't exist.
    try {
        ds.importFromLDIF(true, ldifFile2.getAbsolutePath() + ".missing");
        fail("Expected an exception for an import from a missing file.");
    } catch (final LDAPException le) {
    // This was expected.
    } finally {
        assertEquals(ds.countEntries(), 7);
    }
    // Try to clear and import from a malformed file.
    try {
        ds.importFromLDIF(true, ldifFile3.getAbsolutePath());
        fail("Expected an exception for an import from a malformed file.");
    } catch (final LDAPException le) {
    // This was expected.
    } finally {
        assertEquals(ds.countEntries(), 7);
    }
    // Try to clear and add entries starting with the base DN.
    assertEquals(ds.importFromLDIF(true, ldifFile1.getAbsolutePath()), 3);
    assertEquals(ds.countEntries(), 3);
}
Also used : MemoryBasedLogHandler(com.unboundid.util.MemoryBasedLogHandler) LDAPException(com.unboundid.ldap.sdk.LDAPException) DN(com.unboundid.ldap.sdk.DN) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with MemoryBasedLogHandler

use of com.unboundid.util.MemoryBasedLogHandler in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerConfigTestCase method testLDAPDebugLogHandler.

/**
 * Tests the behavior of the methods for the LDAP debug log handler.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testLDAPDebugLogHandler() throws Exception {
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    assertNull(cfg.getLDAPDebugLogHandler());
    assertNotNull(cfg.toString());
    cfg.setLDAPDebugLogHandler(new MemoryBasedLogHandler());
    assertNotNull(cfg.getLDAPDebugLogHandler());
    assertNotNull(cfg.toString());
    cfg.setLDAPDebugLogHandler(null);
    assertNull(cfg.getLDAPDebugLogHandler());
    assertNotNull(cfg.toString());
}
Also used : MemoryBasedLogHandler(com.unboundid.util.MemoryBasedLogHandler) Test(org.testng.annotations.Test)

Example 3 with MemoryBasedLogHandler

use of com.unboundid.util.MemoryBasedLogHandler in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerConfigTestCase method testAccessLogHandler.

/**
 * Tests the behavior of the methods for the access log handler.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAccessLogHandler() throws Exception {
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    assertNull(cfg.getAccessLogHandler());
    assertNotNull(cfg.toString());
    cfg.setAccessLogHandler(new MemoryBasedLogHandler());
    assertNotNull(cfg.getAccessLogHandler());
    assertNotNull(cfg.toString());
    cfg.setAccessLogHandler(null);
    assertNull(cfg.getAccessLogHandler());
    assertNotNull(cfg.toString());
}
Also used : MemoryBasedLogHandler(com.unboundid.util.MemoryBasedLogHandler) Test(org.testng.annotations.Test)

Example 4 with MemoryBasedLogHandler

use of com.unboundid.util.MemoryBasedLogHandler in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerTestCase method testApplyChangesFromLDIF.

/**
 * Tests operations involving applying changes from LDIF.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testApplyChangesFromLDIF() throws Exception {
    // Create the in-memory directory server.  Enable a changelog.
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.setAccessLogHandler(new MemoryBasedLogHandler());
    cfg.setLDAPDebugLogHandler(new MemoryBasedLogHandler());
    cfg.setCodeLogDetails(createTempFile().getAbsolutePath(), true);
    cfg.setSchema(Schema.getDefaultStandardSchema());
    cfg.setMaxChangeLogEntries(1000);
    final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
    // Make sure that the server has the expected state.
    assertNotNull(ds);
    assertEquals(ds.countEntries(), 0);
    assertNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getEntry("cn=changelog"));
    assertNotNull(ds.getEntry("", "changeLog").getAttribute("changeLog"));
    assertNotNull(ds.getEntry("", "firstChangeNumber").getAttribute("firstChangeNumber"));
    assertEquals(ds.getEntry("", "firstChangeNumber").getAttributeValue("firstChangeNumber"), "0");
    assertNotNull(ds.getEntry("", "lastChangeNumber").getAttribute("lastChangeNumber"));
    assertEquals(ds.getEntry("", "lastChangeNumber").getAttributeValue("lastChangeNumber"), "0");
    assertNotNull(ds.getSchema());
    assertNotNull(ds.getBaseDNs());
    assertFalse(ds.getBaseDNs().isEmpty());
    assertEquals(ds.getBaseDNs().size(), 2);
    assertTrue(ds.getBaseDNs().contains(new DN("dc=example,dc=com")));
    assertTrue(ds.getBaseDNs().contains(new DN("cn=changelog")));
    // Create an LDIF file with all types of changes.  Include a change that
    // will fail as the last item.
    final File ldifFile1 = createTempFile("dn: dc=example,dc=com", "changetype: add", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "changetype: add", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User", "userPassword: password", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "changetype: modify", "replace: description", "description: foo", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "changetype: moddn", "newrdn: cn=Test User", "deleteoldrdn: 0", "", "dn: cn=Test User,ou=People,dc=example,dc=com", "changetype: delete", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "changetype: delete");
    // Try to apply the changes.  Make sure we get an LDAPException.
    try {
        ds.applyChangesFromLDIF(ldifFile1.getAbsolutePath());
        fail("Expected an exception when trying to apply changes from LDIF " + "when the last change should fail.");
    } catch (final LDAPException e) {
    // This was expected.
    }
    // Make sure that the server is still empty, and that there are no
    // changelog records.
    assertNotNull(ds);
    assertEquals(ds.countEntries(), 0);
    assertNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getEntry("cn=changelog"));
    assertNotNull(ds.getEntry("", "changeLog").getAttribute("changeLog"));
    assertNotNull(ds.getEntry("", "firstChangeNumber").getAttribute("firstChangeNumber"));
    assertEquals(ds.getEntry("", "firstChangeNumber").getAttributeValue("firstChangeNumber"), "0");
    assertNotNull(ds.getEntry("", "lastChangeNumber").getAttribute("lastChangeNumber"));
    assertEquals(ds.getEntry("", "lastChangeNumber").getAttributeValue("lastChangeNumber"), "0");
    // Create another LDIF file with the same set of changes, minus the last one
    // that failed.  We should be able to apply this successfully.
    final File ldifFile2 = createTempFile("dn: dc=example,dc=com", "changetype: add", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "changetype: add", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User", "userPassword: password", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "changetype: modify", "replace: description", "description: foo", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "changetype: moddn", "newrdn: cn=Test User", "deleteoldrdn: 0", "", "dn: cn=Test User,ou=People,dc=example,dc=com", "changetype: delete");
    // Try to apply the changes.  Make sure that it succeeds and that the
    // return value is what we expected.
    final int changesApplied = ds.applyChangesFromLDIF(ldifFile2.getAbsolutePath());
    assertEquals(changesApplied, 6);
    // Make sure that the server is now not empty.  It should have two
    // entries and six changelog records.
    assertNotNull(ds);
    assertEquals(ds.countEntries(), 2);
    assertNotNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getEntry("ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("uid=test.user,ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("cn=Test User,ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("cn=changelog"));
    assertNotNull(ds.getEntry("", "changeLog").getAttribute("changeLog"));
    assertNotNull(ds.getEntry("", "firstChangeNumber").getAttribute("firstChangeNumber"));
    assertEquals(ds.getEntry("", "firstChangeNumber").getAttributeValue("firstChangeNumber"), "1");
    assertNotNull(ds.getEntry("", "lastChangeNumber").getAttribute("lastChangeNumber"));
    assertEquals(ds.getEntry("", "lastChangeNumber").getAttributeValue("lastChangeNumber"), "6");
    // Create a third LDIF file with a malformed LDIF record.
    final File ldifFile3 = createTempFile("dn: ou=People,dc=example,dc=com", "changetype: delete", "", "dn: dc=example,dc=com", "changetype: delete", "", "dn: malformedrecord", "changetype: malformed");
    // Try to apply the changes.  Make sure that we get an exception.
    try {
        ds.applyChangesFromLDIF(ldifFile3.getAbsolutePath());
        fail("Expected an LDAPException from trying to apply changes from an " + "LDIF file with a malformed record.");
    } catch (final LDAPException e) {
    // This was expected.
    }
    // Make sure that the server still has the same content it had before the
    // change attempt.
    assertNotNull(ds);
    assertEquals(ds.countEntries(), 2);
    assertNotNull(ds.getEntry("dc=example,dc=com"));
    assertNotNull(ds.getEntry("ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("uid=test.user,ou=People,dc=example,dc=com"));
    assertNull(ds.getEntry("cn=Test User,ou=People,dc=example,dc=com"));
    assertNotNull(ds.getEntry("cn=changelog"));
    assertNotNull(ds.getEntry("", "changeLog").getAttribute("changeLog"));
    assertNotNull(ds.getEntry("", "firstChangeNumber").getAttribute("firstChangeNumber"));
    assertEquals(ds.getEntry("", "firstChangeNumber").getAttributeValue("firstChangeNumber"), "1");
    assertNotNull(ds.getEntry("", "lastChangeNumber").getAttribute("lastChangeNumber"));
    assertEquals(ds.getEntry("", "lastChangeNumber").getAttributeValue("lastChangeNumber"), "6");
}
Also used : MemoryBasedLogHandler(com.unboundid.util.MemoryBasedLogHandler) LDAPException(com.unboundid.ldap.sdk.LDAPException) DN(com.unboundid.ldap.sdk.DN) File(java.io.File) Test(org.testng.annotations.Test)

Example 5 with MemoryBasedLogHandler

use of com.unboundid.util.MemoryBasedLogHandler in project ldapsdk by pingidentity.

the class JSONAccessLogRequestHandlerTestCase method createListener.

/**
 * Creates a new listener that will write log messages to an in-memory buffer.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@BeforeClass()
public void createListener() throws Exception {
    logHandler = new MemoryBasedLogHandler();
    logHandler.setFormatter(new MinimalLogFormatter(null, false, false, false));
    logHandler.setLevel(Level.INFO);
    final CannedResponseRequestHandler successHandler = new CannedResponseRequestHandler();
    final LDAPListenerConfig successConfig = new LDAPListenerConfig(0, new JSONAccessLogRequestHandler(logHandler, successHandler));
    successListener = new LDAPListener(successConfig);
    successListener.startListening();
    successPort = successListener.getListenPort();
    assertTrue(successPort > 0);
    final CannedResponseRequestHandler failureHandler = new CannedResponseRequestHandler(ResultCode.NO_SUCH_OBJECT, "dc=example,dc=com", "The target entry was not found", Arrays.asList("ldap://server1.example.com/dc=example,dc=com", "ldap://server2.example.com/dc=example,dc=com"));
    final LDAPListenerConfig failureConfig = new LDAPListenerConfig(0, new JSONAccessLogRequestHandler(logHandler, failureHandler));
    failureListener = new LDAPListener(failureConfig);
    failureListener.startListening();
    failurePort = failureListener.getListenPort();
    assertTrue(failurePort > 0);
}
Also used : MemoryBasedLogHandler(com.unboundid.util.MemoryBasedLogHandler) MinimalLogFormatter(com.unboundid.util.MinimalLogFormatter) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

MemoryBasedLogHandler (com.unboundid.util.MemoryBasedLogHandler)6 Test (org.testng.annotations.Test)4 DN (com.unboundid.ldap.sdk.DN)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 MinimalLogFormatter (com.unboundid.util.MinimalLogFormatter)2 File (java.io.File)2 BeforeClass (org.testng.annotations.BeforeClass)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1