use of com.unboundid.util.json.JSONValue 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.util.json.JSONValue 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.
}
}
use of com.unboundid.util.json.JSONValue 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.util.json.JSONValue in project ldapsdk by pingidentity.
the class JSONLogsTestCase method createMinimalFieldMap.
/**
* Creates a map with a minimal set of fields for a JSON-formatted log
* message.
*
* @param messageType The message type for the log message. This must not
* be {@code null}.
* @param operationType The operation type for the log message. This may be
* {@code null} if it is not an operation log message.
*
* @return The map that was created. It will be updatable.
*/
@NotNull()
private static Map<String, JSONValue> createMinimalFieldMap(@NotNull final AccessLogMessageType messageType, @Nullable final AccessLogOperationType operationType) {
final Map<String, JSONValue> fieldMap = new LinkedHashMap<>();
fieldMap.put(TIMESTAMP.getFieldName(), new JSONString(DEFAULT_TIMESTAMP_STRING));
fieldMap.put(LOG_TYPE.getFieldName(), new JSONString(ACCESS_LOG_TYPE));
fieldMap.put(MESSAGE_TYPE.getFieldName(), new JSONString(messageType.getLogIdentifier()));
if (operationType != null) {
fieldMap.put(OPERATION_TYPE.getFieldName(), new JSONString(operationType.getLogIdentifier()));
}
return fieldMap;
}
use of com.unboundid.util.json.JSONValue in project ldapsdk by pingidentity.
the class JSONAccessLogReaderTestCase method testReadObjectWithoutTimestamp.
/**
* Tests the behavior when trying to read a file containing a JSON object
* that doesn't include a timestamp.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testReadObjectWithoutTimestamp() throws Exception {
final JSONObject minimalMessageObject = createMinimalMessageObject(CONNECT, null);
final Map<String, JSONValue> fieldsWithoutTimestamp = new LinkedHashMap<>(minimalMessageObject.getFields());
assertNotNull(fieldsWithoutTimestamp.remove(TIMESTAMP.getFieldName()));
final JSONObject objectWithoutTimestamp = new JSONObject(fieldsWithoutTimestamp);
final File logFile = createTempFile(objectWithoutTimestamp.toSingleLineString());
try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
reader.readMessage();
fail("Expected an exception for a file that contains a JSON object " + "without a timestamp");
} catch (final LogException e) {
// This was expected.
}
}
Aggregations