use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class EqualsJSONObjectFilter method toJSONObject.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public JSONObject toJSONObject() {
final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(4));
fields.put(FIELD_FILTER_TYPE, new JSONString(FILTER_TYPE));
if (field.size() == 1) {
fields.put(FIELD_FIELD_PATH, new JSONString(field.get(0)));
} else {
final ArrayList<JSONValue> fieldNameValues = new ArrayList<>(field.size());
for (final String s : field) {
fieldNameValues.add(new JSONString(s));
}
fields.put(FIELD_FIELD_PATH, new JSONArray(fieldNameValues));
}
fields.put(FIELD_VALUE, value);
if (caseSensitive) {
fields.put(FIELD_CASE_SENSITIVE, JSONBoolean.TRUE);
}
return new JSONObject(fields);
}
use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class RegularExpressionJSONObjectFilter method toJSONObject.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public JSONObject toJSONObject() {
final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(4));
fields.put(FIELD_FILTER_TYPE, new JSONString(FILTER_TYPE));
if (field.size() == 1) {
fields.put(FIELD_FIELD_PATH, new JSONString(field.get(0)));
} else {
final ArrayList<JSONValue> fieldNameValues = new ArrayList<>(field.size());
for (final String s : field) {
fieldNameValues.add(new JSONString(s));
}
fields.put(FIELD_FIELD_PATH, new JSONArray(fieldNameValues));
}
fields.put(FIELD_REGULAR_EXPRESSION, new JSONString(regularExpression.toString()));
if (matchAllElements) {
fields.put(FIELD_MATCH_ALL_ELEMENTS, JSONBoolean.TRUE);
}
return new JSONObject(fields);
}
use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class RecentLoginHistoryTestCase method testDecodeMalformedFailureObject.
/**
* Tests the behavior when trying to decode a JSON object with a malformed
* set of failed attempts when the malformed attempt is an object.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeMalformedFailureObject() throws Exception {
final JSONObject o = new JSONObject(new JSONField("failed-attempts", new JSONArray(new JSONObject(new JSONField("malformed", true)))));
new RecentLoginHistory(o);
}
use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class ANDJSONObjectFilterTestCase method testOneComponent.
/**
* Tests the behavior of an AND filter with one component.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testOneComponent() throws Exception {
final EqualsJSONObjectFilter equalsFilter = new EqualsJSONObjectFilter("a", new JSONString("b"));
ANDJSONObjectFilter f = new ANDJSONObjectFilter(equalsFilter);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", new JSONArray(equalsFilter.toJSONObject()))));
f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertNotNull(f.getANDFilters());
assertEquals(f.getANDFilters(), Collections.singletonList(equalsFilter));
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));
assertTrue(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"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("b"), JSONNull.NULL)))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
}
use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class ContainsFieldJSONObjectFilterTestCase method testSecondLevelField.
/**
* Tests the behavior of this filter for a second-level field.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSecondLevelField() throws Exception {
ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field", "second-level-field");
f.setExpectedType(ExpectedValueType.STRING);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", new JSONArray(new JSONString("top-level-field"), new JSONString("second-level-field"))), new JSONField("expectedType", "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(), Arrays.asList("top-level-field", "second-level-field"));
assertNotNull(f.getExpectedType());
assertEquals(f.getExpectedType(), EnumSet.of(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", "foo"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("second-level-field", "foo"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("second-level-field"))))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", "foo"))))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", 1234))))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("second-level-field", "foo"))))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("second-level-field", "foo")), new JSONObject(new JSONField("second-level-field", "bar")))))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("some-other-field", "foo")), new JSONObject(new JSONField("second-level-field", "bar")))))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("some-other-field", "foo")), new JSONObject(new JSONField("another-field", "bar")))))));
}
Aggregations