use of com.unboundid.util.json.JSONField 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.JSONField in project ldapsdk by pingidentity.
the class JSONObjectFilterTestCase method testGetValues.
/**
* Provides test coverage for the {@code getValues} method.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetValues() throws Exception {
// Test the case in which a target top-level field does not exist.
assertNotNull(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Collections.singletonList("a")));
assertEquals(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Collections.singletonList("a")), Collections.emptyList());
// Test the case in which an object has a single value for a top-level
// field.
JSONObject o = new JSONObject(new JSONField("a", "b"));
assertNotNull(JSONObjectFilter.getValues(o, Collections.singletonList("a")));
assertEquals(JSONObjectFilter.getValues(o, Collections.singletonList("a")), Collections.singletonList(new JSONString("b")));
// Test the case in which an object has an array value for a top-level
// field.
o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("b"), new JSONString("c"), new JSONString("d"))));
assertNotNull(JSONObjectFilter.getValues(o, Collections.singletonList("a")));
assertEquals(JSONObjectFilter.getValues(o, Collections.singletonList("a")), Collections.singletonList(new JSONArray(new JSONString("b"), new JSONString("c"), new JSONString("d"))));
// Test the case in which the first level of a two-level field does not
// exist.
assertNotNull(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Arrays.asList("a", "b")), Collections.emptyList());
// Test the case in which the first level of a two-level field exists but
// has a simple value that isn't an object or an array.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", JSONBoolean.TRUE)), Arrays.asList("a", "b")));
// Test the case in which the first level of a two-level field exists but
// is an object that doesn't have the second-level field.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("x", "x")))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("x", "y")))), Arrays.asList("a", "b")), Collections.emptyList());
// Test the case in which the first level of a two-level field exists and
// is an object that has the second-level field.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "x")))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "x")))), Arrays.asList("a", "b")), Collections.singletonList(new JSONString("x")));
// Test the case in which the first level of a two-level field exists and
// is an empty array.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY)), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY)), Arrays.asList("a", "b")), Collections.emptyList());
// Test the case in which the first level of a two-level field exists and
// is an array that doesn't contain any objects.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL))), Arrays.asList("a", "b")), Collections.emptyList());
// Test the case in which the first level of a two-level field exists and
// is an array that contains an object without the target field.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT))), Arrays.asList("a", "b")), Collections.emptyList());
// Test the case in which the first level of a two-level field exists and
// is an array that contains multiple objects with the target field.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT, new JSONObject(new JSONField("b", "x")), new JSONObject(new JSONField("b", "y")), new JSONObject(new JSONField("b", "z"))))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT, new JSONObject(new JSONField("b", "x")), new JSONObject(new JSONField("b", "y")), new JSONObject(new JSONField("b", "z"))))), Arrays.asList("a", "b")), Arrays.asList(new JSONString("x"), new JSONString("y"), new JSONString("z")));
// Test the case in which the first level of a two-level field exists and
// is an object containing the second-level field whose value is an array.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONArray(new JSONString("x"), new JSONString("y"), new JSONString("z")))))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONArray(new JSONString("x"), new JSONString("y"), new JSONString("z")))))), Arrays.asList("a", "b")), Collections.singletonList(new JSONArray(new JSONString("x"), new JSONString("y"), new JSONString("z"))));
// Test the case in which the first level of a two-level field exists and
// is an array of objects, and each of those objects contains the
// second-level field with array values.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONString("1a"), new JSONString("1b"), new JSONString("1c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("2a"), new JSONString("2b"), new JSONString("2c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("3a"), new JSONString("3b"), new JSONString("3c"))))))), Arrays.asList("a", "b")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONString("1a"), new JSONString("1b"), new JSONString("1c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("2a"), new JSONString("2b"), new JSONString("2c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("3a"), new JSONString("3b"), new JSONString("3c"))))))), Arrays.asList("a", "b")), Arrays.asList(new JSONArray(new JSONString("1a"), new JSONString("1b"), new JSONString("1c")), new JSONArray(new JSONString("2a"), new JSONString("2b"), new JSONString("2c")), new JSONArray(new JSONString("3a"), new JSONString("3b"), new JSONString("3c"))));
// Test the case in which a three-level field references exactly one
// element.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONObject(new JSONField("c", "foo")))))), Arrays.asList("a", "b", "c")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONObject(new JSONField("c", "foo")))))), Arrays.asList("a", "b", "c")), Collections.singletonList(new JSONString("foo")));
// Test the case in which the first and second elements of a three-level
// field reference arrays.
assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "first")), new JSONObject(new JSONField("c", "second"))))), new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "third")), new JSONObject(new JSONField("c", "fourth")))))))), Arrays.asList("a", "b", "c")));
assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "first")), new JSONObject(new JSONField("c", "second"))))), new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "third")), new JSONArray(new JSONObject(new JSONField("c", "fourth"))))))))), Arrays.asList("a", "b", "c")), Arrays.asList(new JSONString("first"), new JSONString("second"), new JSONString("third"), new JSONString("fourth")));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ANDJSONObjectFilterTestCase method testZeroComponents.
/**
* Tests the behavior of an AND filter with zero components.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testZeroComponents() throws Exception {
ANDJSONObjectFilter f = new ANDJSONObjectFilter();
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", JSONArray.EMPTY_ARRAY)));
f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertNotNull(f.getANDFilters());
assertEquals(f.getANDFilters(), Collections.emptyList());
assertNotNull(f.getFilterType());
assertEquals(f.getFilterType(), "and");
assertNotNull(f.getRequiredFieldNames());
assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("andFilters")));
assertNotNull(f.getOptionalFieldNames());
assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
assertTrue(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "foo"), new JSONField("b", 1234), new JSONField("c", true), new JSONField("d", false), new JSONField("e", JSONNull.NULL), new JSONField("f", JSONArray.EMPTY_ARRAY), new JSONField("g", new JSONArray(new JSONString("foo"), new JSONString("bar"))), new JSONField("h", JSONObject.EMPTY_OBJECT), new JSONField("i", new JSONObject(new JSONField("j", "k"))))));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ANDJSONObjectFilterTestCase method testMultipleComponents.
/**
* Tests the behavior of an AND filter with multiple components.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testMultipleComponents() throws Exception {
final EqualsJSONObjectFilter f1 = new EqualsJSONObjectFilter("a", new JSONString("b"));
final EqualsJSONObjectFilter f2 = new EqualsJSONObjectFilter("c", new JSONString("d"));
final EqualsJSONObjectFilter f3 = new EqualsJSONObjectFilter("e", new JSONString("f"));
ANDJSONObjectFilter f = new ANDJSONObjectFilter(f1, f2, f3);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", new JSONArray(f1.toJSONObject(), f2.toJSONObject(), f3.toJSONObject()))));
f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertNotNull(f.getANDFilters());
assertEquals(f.getANDFilters(), Arrays.asList(f1, f2, f3));
assertNotNull(f.getFilterType());
assertEquals(f.getFilterType(), "and");
assertNotNull(f.getRequiredFieldNames());
assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("andFilters")));
assertNotNull(f.getOptionalFieldNames());
assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "x"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("x", "b"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("a"), JSONNull.NULL)))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"), new JSONField("g", "h"), new JSONField("i", "j"))));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ContainsFieldJSONObjectFilterTestCase method testTopLevelFieldWithExpectedType.
/**
* Tests the behavior of this filter for a top-level field with an expected
* type.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testTopLevelFieldWithExpectedType() throws Exception {
ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field");
f.setExpectedType(ExpectedValueType.NON_EMPTY_ARRAY, ExpectedValueType.STRING);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "top-level-field"), new JSONField("expectedType", new JSONArray(new JSONString("non-empty-array"), new JSONString("string")))));
assertNotNull(JSONObjectFilter.decode(f.toJSONObject()));
assertTrue(JSONObjectFilter.decode(f.toJSONObject()) instanceof ContainsFieldJSONObjectFilter);
f = (ContainsFieldJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f.getField());
assertFalse(f.getField().isEmpty());
assertEquals(f.getField(), Collections.singletonList("top-level-field"));
assertNotNull(f.getExpectedType());
assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.NON_EMPTY_ARRAY, ExpectedValueType.STRING));
assertNotNull(f.getFilterType());
assertEquals(f.getFilterType(), "containsField");
assertNotNull(f.getRequiredFieldNames());
assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
assertNotNull(f.getOptionalFieldNames());
assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("expectedType")));
assertNotNull(f.toString());
final StringBuilder toStringBuffer = new StringBuilder();
f.toString(toStringBuffer);
assertTrue(toStringBuffer.length() > 0);
assertEquals(toStringBuffer.toString(), f.toString());
final JSONObject toJSONObject = f.toJSONObject();
final JSONObject toStringObject = new JSONObject(f.toString());
assertEquals(toStringObject, toJSONObject);
assertFalse(f.matchesJSONObject(new JSONObject()));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", 1234))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "foo"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("foo"), new JSONString("bar"))))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "foo"), new JSONField("another-top-level-field", 5678))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("embeddedObject", new JSONObject(new JSONField("top-level-field", false))))));
}
Aggregations