Search in sources :

Example 11 with JSONException

use of com.unboundid.util.json.JSONException in project ldapsdk by pingidentity.

the class JSONObjectFilterTestCase method testGetString.

/**
 * Provides test coverage for the {@code getString} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetString() throws Exception {
    // A filter that will be used just to invoke the protected methods.
    final ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("name");
    // Test the case in which an object has a single string for the target
    // field.
    JSONObject o = new JSONObject(new JSONField("a", "b"));
    assertNotNull(f.getString(o, "a", null, true));
    assertEquals(f.getString(o, "a", null, true), "b");
    // Test the case in which an object does not contain the specified field
    // but that is OK and there is no default.
    assertNull(f.getString(JSONObject.EMPTY_OBJECT, "a", null, false));
    // Test the case in which an object does not contain the specified field
    // but that is OK and there is a default.
    assertNotNull(f.getString(JSONObject.EMPTY_OBJECT, "a", "default", false));
    assertEquals(f.getString(JSONObject.EMPTY_OBJECT, "a", "default", false), "default");
    // but that is not OK.
    try {
        f.getString(JSONObject.EMPTY_OBJECT, "a", null, true);
        fail("Expected an exception from getString with a nonexistent field");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has an empty array for the target
    // field.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    try {
        f.getString(o, "a", null, true);
        fail("Expected an exception from getString with an empty array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a single-string array for the target
    // field.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("b"))));
    try {
        f.getString(o, "a", null, true);
        fail("Expected an exception from getString with a single-string array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a non-string, non-array value for
    // the target field.
    o = new JSONObject(new JSONField("a", JSONBoolean.TRUE));
    try {
        f.getString(o, "a", null, true);
        fail("Expected an exception from getString with a boolean");
    } catch (final JSONException e) {
    // This is expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 12 with JSONException

use of com.unboundid.util.json.JSONException in project ldapsdk by pingidentity.

the class JSONObjectFilterTestCase method testGetStrings.

/**
 * Provides test coverage for the {@code getStrings} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetStrings() throws Exception {
    // A filter that will be used just to invoke the protected methods.
    final ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("name");
    // Test the case in which an object has a single string for the target
    // field.
    JSONObject o = new JSONObject(new JSONField("a", "b"));
    assertNotNull(f.getStrings(o, "a", true, null));
    assertFalse(f.getStrings(o, "a", true, null).isEmpty());
    assertEquals(f.getStrings(o, "a", true, null), Collections.singletonList("b"));
    // Test the case in which an object has an array of strings for the target
    // field.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("b"), new JSONString("c"), new JSONString("d"))));
    assertNotNull(f.getStrings(o, "a", true, null));
    assertFalse(f.getStrings(o, "a", true, null).isEmpty());
    assertEquals(f.getStrings(o, "a", true, null), Arrays.asList("b", "c", "d"));
    // Test the case in which an object has an empty array, when that is
    // allowed.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    assertNotNull(f.getStrings(o, "a", true, null));
    assertTrue(f.getStrings(o, "a", true, null).isEmpty());
    // Test the case in which an object has an empty array, when that is not
    // allowed.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    try {
        f.getStrings(o, "a", false, null);
        fail("Expected an exception from getStrings with empty array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a value that is neither a string
    // nor an array.
    o = new JSONObject(new JSONField("a", JSONBoolean.TRUE));
    try {
        f.getStrings(o, "a", false, null);
        fail("Expected an exception from getStrings with true");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a value that has an array
    // containing a non-string element.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("foo"), JSONNull.NULL, new JSONString("bar"))));
    try {
        f.getStrings(o, "a", false, null);
        fail("Expected an exception from getStrings with non-string array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object does not have the target field when that
    // is allowed.
    assertNotNull(f.getStrings(JSONObject.EMPTY_OBJECT, "a", false, Collections.<String>emptyList()));
    assertTrue(f.getStrings(JSONObject.EMPTY_OBJECT, "a", false, Collections.<String>emptyList()).isEmpty());
    // is not allowed.
    try {
        f.getStrings(JSONObject.EMPTY_OBJECT, "a", false, null);
        fail("Expected an exception from getStrings with nonexistent field");
    } catch (final JSONException e) {
    // This is expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 13 with JSONException

use of com.unboundid.util.json.JSONException in project graylog-plugin-integrations by Graylog2.

the class GreyNoiseQuickIPDataAdapter method parseResponse.

@VisibleForTesting
static LookupResult parseResponse(Response response) {
    if (response.isSuccessful()) {
        Map<Object, Object> map = Maps.newHashMap();
        try {
            JSONObject obj = new JSONObject(response.body().string());
            map.put("ip", Objects.requireNonNull(obj).getFieldAsString("ip"));
            map.put("noise", Objects.requireNonNull(obj).getFieldAsBoolean("noise"));
            map.put("code", Objects.requireNonNull(obj).getFieldAsString("code"));
            map.put("riot", Objects.requireNonNull(obj).getFieldAsBoolean("riot"));
        } catch (JSONException | IOException e) {
            LOG.error("An error occurred while parsing Lookup result [{}]", e.toString());
        }
        return LookupResult.withoutTTL().multiValue(map).build();
    } else {
        return LookupResult.empty();
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONException(com.unboundid.util.json.JSONException) JSONObject(com.unboundid.util.json.JSONObject) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 14 with JSONException

use of com.unboundid.util.json.JSONException in project ldapsdk by pingidentity.

the class JSONObjectFilter method decode.

/**
 * Decodes the provided JSON object as a JSON object filter.
 *
 * @param  o  The JSON object to be decoded as a JSON object filter.
 *
 * @return  The JSON object filter decoded from the provided JSON object.
 *
 * @throws  JSONException  If the provided JSON object cannot be decoded as a
 *                         JSON object filter.
 */
@NotNull()
public static JSONObjectFilter decode(@NotNull final JSONObject o) throws JSONException {
    // Get the value of the filter type field for the object and use it to get
    // a filter instance we can use to decode filters of that type.
    final JSONValue filterTypeValue = o.getField(FIELD_FILTER_TYPE);
    if (filterTypeValue == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_FILTER_TYPE.get(String.valueOf(o), FIELD_FILTER_TYPE));
    }
    if (!(filterTypeValue instanceof JSONString)) {
        throw new JSONException(ERR_OBJECT_FILTER_INVALID_FILTER_TYPE.get(String.valueOf(o), FIELD_FILTER_TYPE));
    }
    final String filterType = StaticUtils.toLowerCase(((JSONString) filterTypeValue).stringValue());
    final JSONObjectFilter decoder = FILTER_TYPES.get(filterType);
    if (decoder == null) {
        throw new JSONException(ERR_OBJECT_FILTER_INVALID_FILTER_TYPE.get(String.valueOf(o), FIELD_FILTER_TYPE));
    }
    // Validate the set of fields contained in the provided object to ensure
    // that all required fields were provided and that no disallowed fields were
    // included.
    final HashSet<String> objectFields = new HashSet<>(o.getFields().keySet());
    objectFields.remove(FIELD_FILTER_TYPE);
    for (final String requiredField : decoder.getRequiredFieldNames()) {
        if (!objectFields.remove(requiredField)) {
            throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(o), decoder.getFilterType(), requiredField));
        }
    }
    for (final String remainingField : objectFields) {
        if (!decoder.getOptionalFieldNames().contains(remainingField)) {
            throw new JSONException(ERR_OBJECT_FILTER_UNRECOGNIZED_FIELD.get(String.valueOf(o), decoder.getFilterType(), remainingField));
        }
    }
    return decoder.decodeFilter(o);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) HashSet(java.util.HashSet) NotNull(com.unboundid.util.NotNull)

Example 15 with JSONException

use of com.unboundid.util.json.JSONException in project ldapsdk by pingidentity.

the class ObjectMatchesJSONObjectFilter method decodeFilter.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
protected ObjectMatchesJSONObjectFilter decodeFilter(@NotNull final JSONObject filterObject) throws JSONException {
    final List<String> fieldPath = getStrings(filterObject, FIELD_FIELD_PATH, false, null);
    final JSONValue v = filterObject.getField(FIELD_FILTER);
    if (v == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_FILTER));
    }
    if (!(v instanceof JSONObject)) {
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_OBJECT.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_FILTER));
    }
    try {
        return new ObjectMatchesJSONObjectFilter(fieldPath, JSONObjectFilter.decode((JSONObject) v));
    } catch (final JSONException e) {
        Debug.debugException(e);
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_FILTER.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_FILTER, e.getMessage()), e);
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONException (com.unboundid.util.json.JSONException)17 NotNull (com.unboundid.util.NotNull)10 JSONString (com.unboundid.util.json.JSONString)10 JSONObject (com.unboundid.util.json.JSONObject)9 JSONArray (com.unboundid.util.json.JSONArray)5 JSONField (com.unboundid.util.json.JSONField)5 Test (org.testng.annotations.Test)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONValue (com.unboundid.util.json.JSONValue)4 LDAPRuntimeException (com.unboundid.ldap.sdk.LDAPRuntimeException)3 JSONBuffer (com.unboundid.util.json.JSONBuffer)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1