Search in sources :

Example 16 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONAccessLogReaderTestCase method testReadUnbindAssuranceCompleteMessage.

/**
 * Tests the behavior when trying to read a file containing a JSON object for
 * an assurance complete log message for an unbind operation (for which
 * assurance isn't available).
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadUnbindAssuranceCompleteMessage() throws Exception {
    final JSONObject messageObject = createMinimalMessageObject(ASSURANCE_COMPLETE, UNBIND);
    final File logFile = createTempFile(messageObject.toSingleLineString());
    try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a result message with an operation " + "type of unbind.");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) File(java.io.File) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 17 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONAccessLogReaderTestCase method testReadObjectWithoutMessageType.

/**
 * Tests the behavior when trying to read a file containing a JSON object
 * that doesn't include a message type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadObjectWithoutMessageType() throws Exception {
    final JSONObject minimalMessageObject = createMinimalMessageObject(CONNECT, null);
    final Map<String, JSONValue> fieldsWithoutMessageType = new LinkedHashMap<>(minimalMessageObject.getFields());
    assertNotNull(fieldsWithoutMessageType.remove(MESSAGE_TYPE.getFieldName()));
    final JSONObject objectWithoutMessageType = new JSONObject(fieldsWithoutMessageType);
    final File logFile = createTempFile(objectWithoutMessageType.toSingleLineString());
    try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a JSON object " + "without a message type");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString) File(java.io.File) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 18 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONLogMessageTestCase method testGetBoolean.

/**
 * Tests the methods for getting a Boolean value from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetBoolean() throws Exception {
    // Test with a field whose value is a Boolean true.
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, true));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertTrue(m.getBoolean(UNCACHED_DATA_ACCESSED));
    assertTrue(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
    // Test with a field whose value is a Boolean false.
    o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, false));
    m = new JSONConnectAccessLogMessage(o);
    assertFalse(m.getBoolean(UNCACHED_DATA_ACCESSED));
    assertFalse(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
    // Test with a field whose value is a string true.
    o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, "true"));
    m = new JSONConnectAccessLogMessage(o);
    assertTrue(m.getBoolean(UNCACHED_DATA_ACCESSED));
    assertTrue(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
    // Test with a field whose value is a string false.
    o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, "false"));
    m = new JSONConnectAccessLogMessage(o);
    assertFalse(m.getBoolean(UNCACHED_DATA_ACCESSED));
    assertFalse(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getBoolean(UNCACHED_DATA_ACCESSED));
    assertNull(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getBoolean(UNCACHED_DATA_ACCESSED);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
    // Test with a field that has a non-Boolean/non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, 1234L));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getBoolean(UNCACHED_DATA_ACCESSED);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getBooleanNoThrow(UNCACHED_DATA_ACCESSED));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 19 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONLogMessageTestCase method testGetInteger.

/**
 * Tests the methods for getting an Integer value from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetInteger() throws Exception {
    // Test with a field whose value is a valid positive integer.
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, 1234L));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getInteger(OPERATION_ID).intValue(), 1234);
    assertEquals(m.getIntegerNoThrow(OPERATION_ID).intValue(), 1234);
    // Test with a field whose value is a valid negative integer.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, -5678L));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getInteger(OPERATION_ID).intValue(), -5678);
    assertEquals(m.getIntegerNoThrow(OPERATION_ID).intValue(), -5678);
    // Test with a field whose value is a string representation of an
    // integer.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, "4321"));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getInteger(OPERATION_ID).intValue(), 4321);
    assertEquals(m.getIntegerNoThrow(OPERATION_ID).intValue(), 4321);
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getInteger(OPERATION_ID));
    assertNull(m.getIntegerNoThrow(OPERATION_ID));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getInteger(OPERATION_ID);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getIntegerNoThrow(OPERATION_ID));
    // Test with a field that has a non-numeric/non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, true));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getInteger(OPERATION_ID);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getIntegerNoThrow(OPERATION_ID));
    // Test with a field that has a numeric value that is out of the valid int
    // range.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, Long.MAX_VALUE));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getInteger(OPERATION_ID);
        fail("Expected an exception for a value that is out of range.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getIntegerNoThrow(OPERATION_ID));
    // Test with a field that has a string value that is out of the valid int
    // range.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, String.valueOf(Long.MIN_VALUE)));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getInteger(OPERATION_ID);
        fail("Expected an exception for a value that is out of range.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getIntegerNoThrow(OPERATION_ID));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 20 with LogException

use of com.unboundid.ldap.sdk.unboundidds.logs.LogException in project ldapsdk by pingidentity.

the class JSONLogMessageTestCase method testLogMessageWithMalformedTimestamp.

/**
 * Tests to ensure that it's not possible to create a log message with a
 * malformed timestamp.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testLogMessageWithMalformedTimestamp() throws Exception {
    // Create a valid minimal message object and make sure we can create a log
    // message from it.
    final JSONObject validMessageObject = createMinimalMessageObject(CONNECT, null);
    new JSONConnectAccessLogMessage(validMessageObject);
    // Create a JSON object with all of the fields from the valid object except
    // this time use a malformed timestamp.
    final Map<String, JSONValue> fieldsWithMalformedTimestamp = new LinkedHashMap<>(validMessageObject.getFields());
    assertNotNull(fieldsWithMalformedTimestamp.put(TIMESTAMP.getFieldName(), new JSONString("malformed")));
    final JSONObject messageObjectWithMalformedTimestamp = new JSONObject(fieldsWithMalformedTimestamp);
    try {
        new JSONConnectAccessLogMessage(messageObjectWithMalformedTimestamp);
        fail("Expected an exception when trying to create a log message " + "with a malformed timestamp.");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)34 Test (org.testng.annotations.Test)30 File (java.io.File)22 JSONObject (com.unboundid.util.json.JSONObject)20 JSONString (com.unboundid.util.json.JSONString)8 JSONValue (com.unboundid.util.json.JSONValue)8 LinkedHashMap (java.util.LinkedHashMap)8 NotNull (com.unboundid.util.NotNull)4 AccessLogMessageType (com.unboundid.ldap.sdk.unboundidds.logs.AccessLogMessageType)2 AccessLogOperationType (com.unboundid.ldap.sdk.unboundidds.logs.AccessLogOperationType)2 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)2 FileInputStream (java.io.FileInputStream)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 SearchScope (com.unboundid.ldap.sdk.SearchScope)1 AbandonRequestAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.AbandonRequestAccessLogMessage)1 AccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.AccessLogMessage)1 AccessLogReader (com.unboundid.ldap.sdk.unboundidds.logs.v2.AccessLogReader)1 AddResultAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.AddResultAccessLogMessage)1 BindResultAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.BindResultAccessLogMessage)1 CompareResultAccessLogMessage (com.unboundid.ldap.sdk.unboundidds.logs.v2.CompareResultAccessLogMessage)1