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