Search in sources :

Example 11 with JSONArray

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);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 12 with JSONArray

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);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 13 with JSONArray

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);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) Test(org.testng.annotations.Test)

Example 14 with JSONArray

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))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 15 with JSONArray

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")))))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

JSONArray (com.unboundid.util.json.JSONArray)98 JSONObject (com.unboundid.util.json.JSONObject)89 JSONString (com.unboundid.util.json.JSONString)77 Test (org.testng.annotations.Test)72 JSONField (com.unboundid.util.json.JSONField)68 JSONValue (com.unboundid.util.json.JSONValue)27 JSONNumber (com.unboundid.util.json.JSONNumber)22 NotNull (com.unboundid.util.NotNull)20 ArrayList (java.util.ArrayList)19 LinkedHashMap (java.util.LinkedHashMap)18 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)7 Entry (com.unboundid.ldap.sdk.Entry)6 Map (java.util.Map)6 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)5 JSONBoolean (com.unboundid.util.json.JSONBoolean)5 JSONException (com.unboundid.util.json.JSONException)5 Date (java.util.Date)4 List (java.util.List)4 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3