use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ORJSONObjectFilterTestCase method testZeroComponents.
/**
* Tests the behavior of an OR filter with zero components.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testZeroComponents() throws Exception {
ORJSONObjectFilter f = new ORJSONObjectFilter();
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", JSONArray.EMPTY_ARRAY)));
f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertNotNull(f.getORFilters());
assertEquals(f.getORFilters(), Collections.emptyList());
assertFalse(f.exclusive());
assertNotNull(f.getFilterType());
assertEquals(f.getFilterType(), "or");
assertNotNull(f.getRequiredFieldNames());
assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("orFilters")));
assertNotNull(f.getOptionalFieldNames());
assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("exclusive")));
assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
assertFalse(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"))))));
f.setExclusive(true);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", JSONArray.EMPTY_ARRAY), new JSONField("exclusive", true)));
f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertTrue(f.exclusive());
assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
assertFalse(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 RegularExpressionJSONObjectFilterTestCase method testDecodeMalformedRegex.
/**
* Tests the behavior of the decode method when provided with a malformed
* regular expression.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { JSONException.class })
public void testDecodeMalformedRegex() throws Exception {
final JSONObject o = new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[malformed"));
JSONObjectFilter.decode(o);
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class RegularExpressionJSONObjectFilterTestCase method testGetAndSetRegularExpression.
/**
* Provides test coverage for the methods that can be used to get and set the
* regular expression for a filter.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetAndSetRegularExpression() throws Exception {
RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", "[a-zA-Z][a-zA-Z0-9]*");
assertNotNull(f.getRegularExpression());
assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z][a-zA-Z0-9]*");
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
f.setRegularExpression("[a-zA-Z0-9]+");
assertNotNull(f.getRegularExpression());
assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z0-9]+");
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z0-9]+")));
f.setRegularExpression(Pattern.compile("[a-zA-Z0-9]*"));
assertNotNull(f.getRegularExpression());
assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z0-9]*");
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z0-9]*")));
try {
f.setRegularExpression((String) null);
fail("Expected an exception with setRegularExpression of string null");
} catch (final LDAPSDKUsageException e) {
// This was expected
}
try {
f.setRegularExpression("[invalid");
fail("Expected an exception with setRegularExpression of string invalid");
} catch (final JSONException e) {
// This was expected
}
try {
f.setRegularExpression((Pattern) null);
fail("Expected an exception with setRegularExpression of pattern null");
} catch (final LDAPSDKUsageException e) {
// This was expected
}
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class RegularExpressionJSONObjectFilterTestCase method testPatternRegex.
/**
* Tests the regular expression filter with a pattern regex.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testPatternRegex() throws Exception {
RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", Pattern.compile("[a-zA-Z][a-zA-Z0-9]*"));
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
f = (RegularExpressionJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertNotNull(f.getField());
assertEquals(f.getField(), Collections.singletonList("a"));
assertNotNull(f.getRegularExpression());
assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z][a-zA-Z0-9]*");
assertFalse(f.matchAllElements());
assertNotNull(f.getFilterType());
assertEquals(f.getFilterType(), "regularExpression");
assertNotNull(f.getRequiredFieldNames());
assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "regularExpression")));
assertNotNull(f.getOptionalFieldNames());
assertEquals(f.getOptionalFieldNames(), Collections.singletonList("matchAllElements"));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "abc"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc123"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "a"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "ABC123"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "A"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "123abc"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", ""))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc!"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"))))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"), JSONNull.NULL)))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("456def"))))));
f.setMatchAllElements(true);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*"), new JSONField("matchAllElements", true)));
f = (RegularExpressionJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertTrue(f.matchAllElements());
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "abc"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc123"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "a"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "ABC123"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "A"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "123abc"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", ""))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc!"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"))))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"), JSONNull.NULL)))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("456def"))))));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class RegularExpressionJSONObjectFilterTestCase method testGetAndSetField.
/**
* Provides test coverage for the methods that can be used to get and set the
* target field for a filter.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetAndSetField() throws Exception {
RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", "[a-zA-Z][a-zA-Z0-9]*");
assertNotNull(f.getField());
assertEquals(f.getField(), Collections.singletonList("a"));
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
f.setField("b");
assertNotNull(f.getField());
assertEquals(f.getField(), Collections.singletonList("b"));
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "b"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
f.setField("first", "second", "third");
assertNotNull(f.getField());
assertEquals(f.getField(), Arrays.asList("first", "second", "third"));
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second"), new JSONString("third"))), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
try {
f.setField();
fail("Expected an exception with setField of empty");
} catch (final LDAPSDKUsageException e) {
// This was expected
}
}
Aggregations