Search in sources :

Example 86 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class LDAPCompareJSONOutputHandlerTestCase method testErrorResult.

/**
 * Tests the behavior of the output handler for a compare operation in which
 * the assertion yielded an error result.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testErrorResult() throws Exception {
    final CompareRequest compareRequest = new CompareRequest("ou=missing,dc=example,dc=com", "testAttr", "irrelevant");
    final String[] referralURLs = { "ldap://ds1.example.com/", "ldap://ds2.example.com/" };
    final LDAPResult compareResult = new LDAPResult(-1, ResultCode.NO_SUCH_OBJECT, "Entry 'ou=missing,dc=example,dc=com' does not exist", "dc=example,dc=com", referralURLs, null);
    final LDAPCompareJSONOutputHandler outputHandler = new LDAPCompareJSONOutputHandler();
    assertNotNull(outputHandler.getHeaderLines());
    assertTrue(outputHandler.getHeaderLines().length == 0);
    final String formattedOutput = outputHandler.formatResult(compareRequest, compareResult);
    assertNotNull(formattedOutput);
    assertEquals(formattedOutput, new JSONObject(new JSONField("entry-dn", "ou=missing,dc=example,dc=com"), new JSONField("attribute-name", "testAttr"), new JSONField("assertion-value", "irrelevant"), new JSONField("result-code-value", 32), new JSONField("result-code-name", "no such object"), new JSONField("diagnostic-message", "Entry 'ou=missing,dc=example,dc=com' does not exist"), new JSONField("matched-dn", "dc=example,dc=com"), new JSONField("referral-urls", new JSONArray(new JSONString("ldap://ds1.example.com/"), new JSONString("ldap://ds2.example.com/")))).toSingleLineString());
}
Also used : CompareRequest(com.unboundid.ldap.sdk.CompareRequest) JSONObject(com.unboundid.util.json.JSONObject) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) 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)

Example 87 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class SubstringJSONObjectFilterTestCase method testSingleContains.

/**
 * Tests the behavior of a filter that only uses a single contains component.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSingleContains() throws Exception {
    SubstringJSONObjectFilter f = new SubstringJSONObjectFilter("test-field", null, "defghi", null);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("contains", "defghi")));
    f = (SubstringJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNull(f.getStartsWith());
    assertNotNull(f.getContains());
    assertEquals(f.getContains(), Collections.singletonList("defghi"));
    assertNull(f.getEndsWith());
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "substring");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("startsWith", "contains", "endsWith", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defghi"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "DeFgHi"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "DEFGHI"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defabcghijkl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defghijkl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHI"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abcdefghijkl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHIJKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "defghi"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("defghi"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ahcDEFghiJKL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abcdef"), new JSONString("ghijkl"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abcjkl"), new JSONString("defghi"))))));
    f.setCaseSensitive(true);
    assertTrue(f.caseSensitive());
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("contains", "defghi"), new JSONField("caseSensitive", true)));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defghi"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "DeFgHi"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "DEFGHI"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defghijkl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHI"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abcdefghijkl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHIJKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("defghi"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ahcDEFghiJKL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abcdef"), new JSONString("ghijkl"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abcjkl"), new JSONString("defghi"))))));
}
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)

Example 88 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class ScrambleAttributeTransformation method scrambleJSONValue.

/**
 * Scrambles the provided JSON value.
 *
 * @param  v                  The JSON value to be scrambled.
 * @param  scrambleAllFields  Indicates whether all fields of any JSON object
 *                            should be scrambled.
 *
 * @return  The scrambled JSON value.
 */
@NotNull()
private JSONValue scrambleJSONValue(@NotNull final JSONValue v, final boolean scrambleAllFields) {
    if (v instanceof JSONArray) {
        final JSONArray a = (JSONArray) v;
        final List<JSONValue> originalValues = a.getValues();
        final ArrayList<JSONValue> scrambledValues = new ArrayList<>(originalValues.size());
        for (final JSONValue arrayValue : originalValues) {
            scrambledValues.add(scrambleJSONValue(arrayValue, true));
        }
        return new JSONArray(scrambledValues);
    } else if (v instanceof JSONBoolean) {
        return new JSONBoolean(ThreadLocalRandom.get().nextBoolean());
    } else if (v instanceof JSONNumber) {
        try {
            return new JSONNumber(scrambleNumericValue(v.toString()));
        } catch (final Exception e) {
            // This should never happen.
            Debug.debugException(e);
            return v;
        }
    } else if (v instanceof JSONObject) {
        final JSONObject o = (JSONObject) v;
        final Map<String, JSONValue> originalFields = o.getFields();
        final LinkedHashMap<String, JSONValue> scrambledFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            final JSONValue scrambledValue;
            final String fieldName = e.getKey();
            final JSONValue originalValue = e.getValue();
            if (scrambleAllFields || jsonFields.contains(StaticUtils.toLowerCase(fieldName))) {
                scrambledValue = scrambleJSONValue(originalValue, scrambleAllFields);
            } else if (originalValue instanceof JSONArray) {
                scrambledValue = scrambleObjectsInArray((JSONArray) originalValue);
            } else if (originalValue instanceof JSONObject) {
                scrambledValue = scrambleJSONValue(originalValue, false);
            } else {
                scrambledValue = originalValue;
            }
            scrambledFields.put(fieldName, scrambledValue);
        }
        return new JSONObject(scrambledFields);
    } else if (v instanceof JSONString) {
        final JSONString s = (JSONString) v;
        return new JSONString(scrambleString(s.stringValue()));
    } else {
        // those.
        return v;
    }
}
Also used : JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) JSONValue(com.unboundid.util.json.JSONValue) Entry(com.unboundid.ldap.sdk.Entry) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONNumber(com.unboundid.util.json.JSONNumber) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Example 89 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class ScrambleAttributeTransformation method scrambleJSONObject.

/**
 * Scrambles the provided JSON object value.  If the provided value can be
 * parsed as a valid JSON object, then the resulting value will be a JSON
 * object with all field names preserved and some or all of the field values
 * scrambled.  If this {@code AttributeScrambler} was created with a set of
 * JSON fields, then only the values of those fields will be scrambled;
 * otherwise, all field values will be scrambled.
 *
 * @param  s  The time value to scramble.
 *
 * @return  The scrambled value.
 */
@Nullable()
public String scrambleJSONObject(@Nullable final String s) {
    if (s == null) {
        return null;
    }
    // Try to parse the value as a JSON object.  If this fails, then just
    // scramble it as a generic string.
    final JSONObject o;
    try {
        o = new JSONObject(s);
    } catch (final Exception e) {
        Debug.debugException(e);
        return scrambleString(s);
    }
    final boolean scrambleAllFields = jsonFields.isEmpty();
    final Map<String, JSONValue> originalFields = o.getFields();
    final LinkedHashMap<String, JSONValue> scrambledFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
    for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
        final JSONValue scrambledValue;
        final String fieldName = e.getKey();
        final JSONValue originalValue = e.getValue();
        if (scrambleAllFields || jsonFields.contains(StaticUtils.toLowerCase(fieldName))) {
            scrambledValue = scrambleJSONValue(originalValue, true);
        } else if (originalValue instanceof JSONArray) {
            scrambledValue = scrambleObjectsInArray((JSONArray) originalValue);
        } else if (originalValue instanceof JSONObject) {
            scrambledValue = scrambleJSONValue(originalValue, false);
        } else {
            scrambledValue = originalValue;
        }
        scrambledFields.put(fieldName, scrambledValue);
    }
    return new JSONObject(scrambledFields).toString();
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONString(com.unboundid.util.json.JSONString) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Nullable(com.unboundid.util.Nullable)

Example 90 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class GreaterThanJSONObjectFilter method toJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public JSONObject toJSONObject() {
    final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(6));
    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 (allowEquals) {
        fields.put(FIELD_ALLOW_EQUALS, JSONBoolean.TRUE);
    }
    if (matchAllElements) {
        fields.put(FIELD_MATCH_ALL_ELEMENTS, JSONBoolean.TRUE);
    }
    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)

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