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());
}
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"))))));
}
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;
}
}
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();
}
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);
}
Aggregations