Search in sources :

Example 21 with LogException

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

the class JSONLogMessageTestCase method testGetDouble.

/**
 * Tests the methods for getting a Double value from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetDouble() throws Exception {
    // Test with a field whose value is a floating-point number
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(PROCESSING_TIME_MILLIS, 1.5d));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getDouble(PROCESSING_TIME_MILLIS).doubleValue(), 1.5d);
    assertEquals(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS).doubleValue(), 1.5d);
    // Test with a field whose value is an integer.
    o = createMinimalMessageObject(CONNECT, null, createField(PROCESSING_TIME_MILLIS, 1L));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getDouble(PROCESSING_TIME_MILLIS).doubleValue(), 1.0d);
    assertEquals(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS).doubleValue(), 1.0d);
    // Test with a field whose value is a string representation of a
    // floating-point number.
    o = createMinimalMessageObject(CONNECT, null, createField(PROCESSING_TIME_MILLIS, "1.5"));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getDouble(PROCESSING_TIME_MILLIS).doubleValue(), 1.5d);
    assertEquals(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS).doubleValue(), 1.5d);
    // Test with a field whose value is a string representation of an integer.
    o = createMinimalMessageObject(CONNECT, null, createField(PROCESSING_TIME_MILLIS, "1"));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getDouble(PROCESSING_TIME_MILLIS).doubleValue(), 1.0d);
    assertEquals(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS).doubleValue(), 1.0d);
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getDouble(PROCESSING_TIME_MILLIS));
    assertNull(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(PROCESSING_TIME_MILLIS, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getDouble(PROCESSING_TIME_MILLIS);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS));
    // Test with a field that has a non-numeric/non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(PROCESSING_TIME_MILLIS, true));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getDouble(PROCESSING_TIME_MILLIS);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getDoubleNoThrow(PROCESSING_TIME_MILLIS));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 22 with LogException

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

the class JSONLogMessageTestCase method testGetRFC3339Timestamp.

/**
 * Tests the methods for getting an RFC 3339 timestamp value from a JSON
 * object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetRFC3339Timestamp() throws Exception {
    // Test with a field whose value is a valid generalized time string.
    final LogField testField = new LogField("test-gt", RFC3339TimestampLogFieldSyntax.getInstance());
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(testField, StaticUtils.encodeRFC3339Time(DEFAULT_TIMESTAMP_DATE)));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getRFC3339Timestamp(testField), DEFAULT_TIMESTAMP_DATE);
    assertEquals(m.getRFC3339TimestampNoThrow(testField), DEFAULT_TIMESTAMP_DATE);
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getRFC3339Timestamp(testField));
    assertNull(m.getRFC3339TimestampNoThrow(testField));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(testField, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getRFC3339Timestamp(testField);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getRFC3339TimestampNoThrow(testField));
    // Test with a field that has a non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(testField, 1234L));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getRFC3339Timestamp(testField);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getRFC3339TimestampNoThrow(UNCACHED_DATA_ACCESSED));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 23 with LogException

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

the class JSONLogMessageTestCase method testGetGeneralizedTime.

/**
 * Tests the methods for getting a generalized time value from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetGeneralizedTime() throws Exception {
    // Test with a field whose value is a valid generalized time string.
    final LogField testField = new LogField("test-gt", GeneralizedTimeLogFieldSyntax.getInstance());
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(testField, StaticUtils.encodeGeneralizedTime(DEFAULT_TIMESTAMP_DATE)));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getGeneralizedTime(testField), DEFAULT_TIMESTAMP_DATE);
    assertEquals(m.getGeneralizedTimeNoThrow(testField), DEFAULT_TIMESTAMP_DATE);
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getGeneralizedTime(testField));
    assertNull(m.getGeneralizedTimeNoThrow(testField));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(testField, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getGeneralizedTime(testField);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getGeneralizedTimeNoThrow(testField));
    // Test with a field that has a non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(testField, 1234L));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getGeneralizedTime(testField);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getGeneralizedTimeNoThrow(UNCACHED_DATA_ACCESSED));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 24 with LogException

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

the class JSONLogMessageTestCase method testLogMessageWithTimestampNotString.

/**
 * Tests to ensure that it's not possible to create a log message with a
 * timestamp field whose value is not a string.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testLogMessageWithTimestampNotString() 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> fieldsWithNonStringTimestamp = new LinkedHashMap<>(validMessageObject.getFields());
    assertNotNull(fieldsWithNonStringTimestamp.put(TIMESTAMP.getFieldName(), JSONBoolean.TRUE));
    final JSONObject messageObjectWithNonStringTimestamp = new JSONObject(fieldsWithNonStringTimestamp);
    try {
        new JSONConnectAccessLogMessage(messageObjectWithNonStringTimestamp);
        fail("Expected an exception when trying to create a log message " + "with a non-string 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) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 25 with LogException

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

the class TextFormattedAccessLogReaderTestCase method testReadMessageOperationTypeWithoutMessageType.

/**
 * Tests the behavior when trying to read a file that contains a log message
 * that has one unnamed field that is an operation type rather than a message
 * type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadMessageOperationTypeWithoutMessageType() throws Exception {
    final StringBuilder messageBuffer = createLogMessage(true, null, null, false);
    messageBuffer.append("ADD");
    appendField(messageBuffer, DIAGNOSTIC_MESSAGE, "value1");
    appendField(messageBuffer, ADDITIONAL_INFO, "value2");
    final File logFile = createTempFile(messageBuffer.toString());
    try (TextFormattedAccessLogReader reader = new TextFormattedAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a message with " + "an unsupported second unnamed field");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : File(java.io.File) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) 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