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