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);
}
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);
}
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();
}
}
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);
}
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);
}
Aggregations