Search in sources :

Example 16 with JSONObject

use of com.unboundid.util.json.JSONObject in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testAddResultWithAllComponents.

/**
 * Provides coverage for the case in which an add result message contains
 * all components.
 *
 * Tests the behavior when logging search operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAddResultWithAllComponents() throws Exception {
    // Get the in-memory directory server instance.
    final InMemoryDirectoryServer ds = getTestDS(false, false);
    // Create a logger to use for the test.
    final TestLogHandler logHandler = new TestLogHandler();
    final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
    final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
    final LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setConnectionLogger(logger);
    // Generate a log message.
    try (LDAPConnection connection = ds.getConnection()) {
        final LDAPResult addResult = new LDAPResult(1, ResultCode.REFERRAL, "Try the operation somewhere else", "dc=example,dc=com", new String[] { "ldap://ds1.example.com:389/dc=example,dc=com", "ldap://ds2.example.com:389/dc=example,dc=com" }, new Control[] { new Control("1.2.3.4", false, null), new Control("1.2.3.5", true, new ASN1OctetString("foo")) });
        logger.logAddResult(connection, 1, addResult);
    }
    // Make sure that we can decode the message.
    assertEquals(logHandler.getMessageCount(), 1, logHandler.getMessagesString());
    final List<JSONObject> logMessages = parseLogMessages(logHandler);
    assertMessageIs(logMessages.get(0), "result", OperationType.ADD);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 17 with JSONObject

use of com.unboundid.util.json.JSONObject in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testSimpleBindLogging.

/**
 * Tests the behavior when logging simple bind operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSimpleBindLogging() throws Exception {
    // Get the in-memory directory server instance.
    final InMemoryDirectoryServer ds = getTestDS();
    // Create a logger to use for the test.
    final TestLogHandler logHandler = new TestLogHandler();
    final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
    final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
    final LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setConnectionLogger(logger);
    // Establish a connection and send a simple bind request on it.
    try (LDAPConnection connection = new LDAPConnection(options, "localhost", ds.getListenPort())) {
        connection.bind("cn=Directory Manager", "password");
    }
    // Make sure that there were five log messages:
    // - Connect
    // - Bind request
    // - Bind result
    // - Unbind
    // - Disconnect
    assertEquals(logHandler.getMessageCount(), 5, logHandler.getMessagesString());
    final List<JSONObject> logMessages = parseLogMessages(logHandler);
    assertMessageIs(logMessages.get(0), "connect", null);
    assertMessageIs(logMessages.get(1), "request", OperationType.BIND);
    assertMessageIs(logMessages.get(2), "result", OperationType.BIND);
    assertMessageIs(logMessages.get(3), "request", OperationType.UNBIND);
    assertMessageIs(logMessages.get(4), "disconnect", null);
}
Also used : TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 18 with JSONObject

use of com.unboundid.util.json.JSONObject in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testFailedConnectionLogging.

/**
 * Tests the behavior when a connection attempt fails.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFailedConnectionLogging() throws Exception {
    // Get the in-memory directory server instance and shut it down.
    final InMemoryDirectoryServer ds = getTestDS();
    final int port = ds.getListenPort();
    ds.shutDown(true);
    try {
        // Create a logger to use for the test.
        final TestLogHandler logHandler = new TestLogHandler();
        final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
        final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
        final LDAPConnectionOptions options = new LDAPConnectionOptions();
        options.setConnectionLogger(logger);
        // Try and fail to establish a connection to the offline server.
        try (LDAPConnection connection = new LDAPConnection(options, "localhost", port)) {
            assertFalse(connection.isConnected());
            fail("Unexpectedly connected to an offline server");
        } catch (final LDAPException e) {
        // This was expected.
        }
        // Make sure that there was one log message, and that it was a
        // connect failure message.
        assertEquals(logHandler.getMessageCount(), 1);
        final List<JSONObject> logMessages = parseLogMessages(logHandler);
        assertMessageIs(logMessages.get(0), "connect-failure", null);
    } finally {
        ds.startListening();
    }
}
Also used : TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 19 with JSONObject

use of com.unboundid.util.json.JSONObject in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testBindResultWithServerSASLCredentials.

/**
 * Provides coverage for the case in which a bind result message includes
 * server SASL credentials.
 *
 * Tests the behavior when logging search operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBindResultWithServerSASLCredentials() throws Exception {
    // Get the in-memory directory server instance.
    final InMemoryDirectoryServer ds = getTestDS(false, false);
    // Create a logger to use for the test.
    final TestLogHandler logHandler = new TestLogHandler();
    final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
    final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
    final LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setConnectionLogger(logger);
    // Generate a log message.
    try (LDAPConnection connection = ds.getConnection()) {
        final BindResult bindResult = new BindResult(1, ResultCode.SUCCESS, null, null, null, null, new ASN1OctetString("creds"));
        logger.logBindResult(connection, 1, bindResult);
    }
    // Make sure that we can decode the message.
    assertEquals(logHandler.getMessageCount(), 1, logHandler.getMessagesString());
    final List<JSONObject> logMessages = parseLogMessages(logHandler);
    assertMessageIs(logMessages.get(0), "result", OperationType.BIND);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Example 20 with JSONObject

use of com.unboundid.util.json.JSONObject in project ldapsdk by pingidentity.

the class JSONLDAPConnectionLoggerTestCase method testAddWithControls.

/**
 * Provides coverage for the case in which an add request includes controls,
 * which will  be processed as an array.
 *
 * Tests the behavior when logging search operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAddWithControls() throws Exception {
    // Get the in-memory directory server instance.
    final InMemoryDirectoryServer ds = getTestDS(false, false);
    // Create a logger to use for the test.
    final TestLogHandler logHandler = new TestLogHandler();
    final JSONLDAPConnectionLoggerProperties properties = new JSONLDAPConnectionLoggerProperties();
    final JSONLDAPConnectionLogger logger = new JSONLDAPConnectionLogger(logHandler, properties);
    final LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setConnectionLogger(logger);
    // Generate a log message.
    try (LDAPConnection connection = ds.getConnection()) {
        final AddRequest addRequest = new AddRequest("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example");
        addRequest.addControl(new Control("1.2.3.4", false, null));
        addRequest.addControl(new Control("1.2.3.5", true, new ASN1OctetString("foo")));
        logger.logAddRequest(connection, 1, addRequest);
    }
    // Make sure that we can decode the message.
    assertEquals(logHandler.getMessageCount(), 1, logHandler.getMessagesString());
    final List<JSONObject> logMessages = parseLogMessages(logHandler);
    assertMessageIs(logMessages.get(0), "request", OperationType.ADD);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) TestLogHandler(com.unboundid.util.TestLogHandler) JSONObject(com.unboundid.util.json.JSONObject) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) Test(org.testng.annotations.Test)

Aggregations

JSONObject (com.unboundid.util.json.JSONObject)253 Test (org.testng.annotations.Test)205 JSONString (com.unboundid.util.json.JSONString)105 JSONField (com.unboundid.util.json.JSONField)97 JSONArray (com.unboundid.util.json.JSONArray)89 File (java.io.File)67 JSONValue (com.unboundid.util.json.JSONValue)40 LinkedHashMap (java.util.LinkedHashMap)32 NotNull (com.unboundid.util.NotNull)27 ArrayList (java.util.ArrayList)26 JSONNumber (com.unboundid.util.json.JSONNumber)23 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)22 TestLogHandler (com.unboundid.util.TestLogHandler)21 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)20 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)11 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)10 Entry (com.unboundid.ldap.sdk.Entry)9 JSONException (com.unboundid.util.json.JSONException)9 Date (java.util.Date)9