use of com.unboundid.util.json.JSONNumber in project ldapsdk by pingidentity.
the class GreaterThanJSONObjectFilterTestCase method testGetAndSetValue.
/**
* Provides test coverage for the methods that can be used to get and set the
* value.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetAndSetValue() throws Exception {
final GreaterThanJSONObjectFilter f = new GreaterThanJSONObjectFilter("a", 1234);
assertEquals(f.getValue(), new JSONNumber(1234));
f.setValue(5678);
assertEquals(f.getValue(), new JSONNumber(5678));
f.setValue(1234.5);
assertEquals(f.getValue(), new JSONNumber(1234.5));
f.setValue("foo");
assertEquals(f.getValue(), new JSONString("foo"));
f.setValue(new JSONNumber("1.234e3"));
assertEquals(f.getValue(), new JSONNumber(1234));
f.setValue(new JSONString("1.234e3"));
assertEquals(f.getValue(), new JSONString("1.234e3"));
try {
f.setValue((String) null);
fail("Expected an exception from setValue String null");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
try {
f.setValue((JSONValue) null);
fail("Expected an exception from setValue JSONValue null");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
try {
f.setValue(JSONBoolean.TRUE);
fail("Expected an exception from setValue true");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
}
use of com.unboundid.util.json.JSONNumber in project ldapsdk by pingidentity.
the class ANDJSONObjectFilterTestCase method testMultipleComponents.
/**
* Tests the behavior of an AND filter with multiple components.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testMultipleComponents() throws Exception {
final EqualsJSONObjectFilter f1 = new EqualsJSONObjectFilter("a", new JSONString("b"));
final EqualsJSONObjectFilter f2 = new EqualsJSONObjectFilter("c", new JSONString("d"));
final EqualsJSONObjectFilter f3 = new EqualsJSONObjectFilter("e", new JSONString("f"));
ANDJSONObjectFilter f = new ANDJSONObjectFilter(f1, f2, f3);
assertNotNull(f.toJSONObject());
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", new JSONArray(f1.toJSONObject(), f2.toJSONObject(), f3.toJSONObject()))));
f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
assertNotNull(f);
assertNotNull(f.getANDFilters());
assertEquals(f.getANDFilters(), Arrays.asList(f1, f2, f3));
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));
assertFalse(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"))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("a"), JSONNull.NULL)))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"), new JSONField("g", "h"), new JSONField("i", "j"))));
}
use of com.unboundid.util.json.JSONNumber in project ldapsdk by pingidentity.
the class PasswordPolicyStateJSONTestCase method createState.
/**
* Creates a password policy state JSON object with the provided fields.
*
* @param fields The fields to include in the JSON object.
*
* @return The password policy state JSON object that was created.
*
* @throws Exception If an unexpected problem occurs.
*/
private PasswordPolicyStateJSON createState(final Map<PasswordPolicyStateJSONField, ?> fields) throws Exception {
final Map<String, JSONValue> jsonFields = new LinkedHashMap<>();
for (final PasswordPolicyStateJSONField field : fields.keySet()) {
final String name = field.getFieldName();
final Object value = fields.get(field);
if (value instanceof Boolean) {
final Boolean b = (Boolean) value;
jsonFields.put(name, new JSONBoolean(b));
} else if (value instanceof Integer) {
final Integer i = (Integer) value;
jsonFields.put(name, new JSONNumber(i));
} else if (value instanceof String) {
final String s = (String) value;
jsonFields.put(name, new JSONString(s));
} else if (value instanceof Date) {
final Date d = (Date) value;
jsonFields.put(name, new JSONString(StaticUtils.encodeRFC3339Time(d)));
} else if (value instanceof List) {
final List<?> l = (List<?>) value;
final List<JSONValue> arrayValues = new ArrayList<>();
for (final Object o : l) {
if (o instanceof Date) {
final Date d = (Date) o;
arrayValues.add(new JSONString(StaticUtils.encodeRFC3339Time(d)));
} else if (o instanceof String) {
final String s = (String) o;
arrayValues.add(new JSONString(s));
} else if (o instanceof PasswordPolicyStateAccountUsabilityError) {
final PasswordPolicyStateAccountUsabilityError e = (PasswordPolicyStateAccountUsabilityError) o;
if (e.getMessage() == null) {
arrayValues.add(new JSONObject(new JSONField("type-name", e.getName()), new JSONField("type-id", e.getIntValue())));
} else {
arrayValues.add(new JSONObject(new JSONField("type-name", e.getName()), new JSONField("type-id", e.getIntValue()), new JSONField("message", e.getMessage())));
}
} else if (o instanceof PasswordPolicyStateAccountUsabilityWarning) {
final PasswordPolicyStateAccountUsabilityWarning w = (PasswordPolicyStateAccountUsabilityWarning) o;
if (w.getMessage() == null) {
arrayValues.add(new JSONObject(new JSONField("type-name", w.getName()), new JSONField("type-id", w.getIntValue())));
} else {
arrayValues.add(new JSONObject(new JSONField("type-name", w.getName()), new JSONField("type-id", w.getIntValue()), new JSONField("message", w.getMessage())));
}
} else if (o instanceof PasswordPolicyStateAccountUsabilityNotice) {
final PasswordPolicyStateAccountUsabilityNotice n = (PasswordPolicyStateAccountUsabilityNotice) o;
if (n.getMessage() == null) {
arrayValues.add(new JSONObject(new JSONField("type-name", n.getName()), new JSONField("type-id", n.getIntValue())));
} else {
arrayValues.add(new JSONObject(new JSONField("type-name", n.getName()), new JSONField("type-id", n.getIntValue()), new JSONField("message", n.getMessage())));
}
} else {
fail("Unexpected list element " + o + " of type " + o.getClass().getName());
}
}
jsonFields.put(name, new JSONArray(arrayValues));
} else if (value instanceof JSONValue) {
jsonFields.put(name, (JSONValue) value);
} else {
fail("Unexpected field value " + value + " of type " + value.getClass().getName());
}
}
final JSONObject o = new JSONObject(jsonFields);
final Entry entry = new Entry("dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User");
entry.addAttribute("ds-pwp-state-json", o.toSingleLineString());
final PasswordPolicyStateJSON state = PasswordPolicyStateJSON.get(entry);
assertNotNull(state);
assertNotNull(state.getPasswordPolicyStateJSONObject());
assertFalse(state.getPasswordPolicyStateJSONObject().getFields().isEmpty());
assertEquals(state.getPasswordPolicyStateJSONObject().getFields().size(), jsonFields.size());
assertNotNull(state.toString());
assertFalse(state.toString().isEmpty());
return state;
}
use of com.unboundid.util.json.JSONNumber 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.JSONNumber in project ldapsdk by pingidentity.
the class LessThanJSONObjectFilter method matches.
/**
* Indicates whether the provided value matches the criteria of this filter.
*
* @param v The value for which to make the determination.
*
* @return {@code true} if the provided value matches the criteria of this
* filter, or {@code false} if not.
*/
private boolean matches(@NotNull final JSONValue v) {
if ((v instanceof JSONNumber) && (value instanceof JSONNumber)) {
final BigDecimal targetValue = ((JSONNumber) value).getValue();
final BigDecimal objectValue = ((JSONNumber) v).getValue();
if (allowEquals) {
return (objectValue.compareTo(targetValue) <= 0);
} else {
return (objectValue.compareTo(targetValue) < 0);
}
} else if ((v instanceof JSONString) && (value instanceof JSONString)) {
final String targetValue = ((JSONString) value).stringValue();
final String objectValue = ((JSONString) v).stringValue();
if (allowEquals) {
if (caseSensitive) {
return (objectValue.compareTo(targetValue) <= 0);
} else {
return (objectValue.compareToIgnoreCase(targetValue) <= 0);
}
} else {
if (caseSensitive) {
return (objectValue.compareTo(targetValue) < 0);
} else {
return (objectValue.compareToIgnoreCase(targetValue) < 0);
}
}
} else {
return false;
}
}
Aggregations