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