Search in sources :

Example 26 with JSONValue

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.
    }
}
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 27 with JSONValue

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

Example 28 with JSONValue

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.
    }
}
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 29 with JSONValue

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;
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 30 with JSONValue

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

Aggregations

JSONValue (com.unboundid.util.json.JSONValue)50 JSONString (com.unboundid.util.json.JSONString)45 JSONObject (com.unboundid.util.json.JSONObject)40 NotNull (com.unboundid.util.NotNull)33 LinkedHashMap (java.util.LinkedHashMap)31 JSONArray (com.unboundid.util.json.JSONArray)27 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)9 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)8 JSONBoolean (com.unboundid.util.json.JSONBoolean)6 Map (java.util.Map)6 JSONNumber (com.unboundid.util.json.JSONNumber)5 File (java.io.File)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONException (com.unboundid.util.json.JSONException)4 JSONField (com.unboundid.util.json.JSONField)4 Date (java.util.Date)4 List (java.util.List)4 Entry (com.unboundid.ldap.sdk.Entry)3 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)2