use of com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField 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;
}
Aggregations