Search in sources :

Example 1 with ModifiablePasswordPolicyStateJSONField

use of com.unboundid.ldap.sdk.unboundidds.ModifiablePasswordPolicyStateJSONField in project ldapsdk by pingidentity.

the class ModifiablePasswordPolicyStateJSONTestCase 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 ModifiablePasswordPolicyStateJSON createState(final Map<ModifiablePasswordPolicyStateJSONField, ?> fields) throws Exception {
    final Map<String, JSONValue> jsonFields = new LinkedHashMap<>();
    for (final ModifiablePasswordPolicyStateJSONField 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 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 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-modifiable-state-json", o.toSingleLineString());
    final ModifiablePasswordPolicyStateJSON state = ModifiablePasswordPolicyStateJSON.get(entry);
    assertNotNull(state);
    assertNotNull(state.getModifiablePasswordPolicyStateJSONObject());
    assertFalse(state.getModifiablePasswordPolicyStateJSONObject().getFields().isEmpty());
    assertEquals(state.getModifiablePasswordPolicyStateJSONObject().getFields().size(), jsonFields.size());
    assertNotNull(state.toString());
    assertFalse(state.toString().isEmpty());
    return state;
}
Also used : ModifiablePasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.ModifiablePasswordPolicyStateJSONField) JSONString(com.unboundid.util.json.JSONString) Date(java.util.Date) 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) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONString(com.unboundid.util.json.JSONString)

Aggregations

Entry (com.unboundid.ldap.sdk.Entry)1 ModifiablePasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.ModifiablePasswordPolicyStateJSONField)1 JSONBoolean (com.unboundid.util.json.JSONBoolean)1 JSONObject (com.unboundid.util.json.JSONObject)1 JSONString (com.unboundid.util.json.JSONString)1 JSONValue (com.unboundid.util.json.JSONValue)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1