Search in sources :

Example 81 with JSONArray

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

the class PasswordPolicyStateJSONTestCase method testGetAddPasswordQualityRequirements.

/**
 * Tests the behavior for the properties related to password quality
 * requirements for add operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAddPasswordQualityRequirements() throws Exception {
    final PasswordQualityRequirement allRequirement = new PasswordQualityRequirement("all-requirement-description", "all-requirement-type", Collections.<String, String>emptyMap());
    final PasswordQualityRequirement noneRequirement = new PasswordQualityRequirement("none-requirement-description", "none-requirement-type", Collections.<String, String>emptyMap());
    final PasswordQualityRequirement addRequirement = new PasswordQualityRequirement("add-requirement-description", "add-requirement-type", Collections.singletonMap("property-1", "value-1"));
    final PasswordQualityRequirement selfChangeRequirement = new PasswordQualityRequirement("self-change-requirement-description", "self-change-requirement-type", StaticUtils.mapOf("property-1", "value-1", "property-2", "value-2"));
    final PasswordQualityRequirement adminResetRequirement = new PasswordQualityRequirement("admin-reset-requirement-description", "admin-reset-requirement-type", StaticUtils.mapOf("property-1", "value-1", "property-2", "value-2", "property-3", "value-3"));
    final PasswordQualityRequirement bindRequirement = new PasswordQualityRequirement("bind-requirement-description", null, null);
    final JSONArray requirementsArray = new JSONArray(encodeRequirement(allRequirement, true, true, true, true), encodeRequirement(noneRequirement, false, false, false, false), encodeRequirement(addRequirement, true, false, false, false), encodeRequirement(selfChangeRequirement, false, true, false, false), encodeRequirement(adminResetRequirement, false, false, true, false), encodeRequirement(bindRequirement, false, false, false, true));
    final PasswordPolicyStateJSON state = createState(StaticUtils.mapOf(PASSWORD_QUALITY_REQUIREMENTS, requirementsArray));
    assertNotNull(state.getAddPasswordQualityRequirements());
    assertFalse(state.getAddPasswordQualityRequirements().isEmpty());
    assertEquals(state.getAddPasswordQualityRequirements().size(), 2);
    final PasswordQualityRequirement requirement0 = state.getAddPasswordQualityRequirements().get(0);
    assertEquals(requirement0.getDescription(), allRequirement.getDescription());
    assertEquals(requirement0.getClientSideValidationType(), allRequirement.getClientSideValidationType());
    assertEquals(requirement0.getClientSideValidationProperties(), allRequirement.getClientSideValidationProperties());
    final PasswordQualityRequirement requirement1 = state.getAddPasswordQualityRequirements().get(1);
    assertEquals(requirement1.getDescription(), addRequirement.getDescription());
    assertEquals(requirement1.getClientSideValidationType(), addRequirement.getClientSideValidationType());
    assertEquals(requirement1.getClientSideValidationProperties(), addRequirement.getClientSideValidationProperties());
}
Also used : PasswordQualityRequirement(com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement) JSONArray(com.unboundid.util.json.JSONArray) Test(org.testng.annotations.Test)

Example 82 with JSONArray

use of com.unboundid.util.json.JSONArray 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;
}
Also used : PasswordPolicyStateAccountUsabilityError(com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityError) ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) PasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField) JSONField(com.unboundid.util.json.JSONField) 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) PasswordPolicyStateAccountUsabilityNotice(com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityNotice) PasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) PasswordPolicyStateAccountUsabilityWarning(com.unboundid.ldap.sdk.unboundidds.extensions.PasswordPolicyStateAccountUsabilityWarning) JSONObject(com.unboundid.util.json.JSONObject) JSONNumber(com.unboundid.util.json.JSONNumber) ArrayList(java.util.ArrayList) List(java.util.List) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONString(com.unboundid.util.json.JSONString)

Example 83 with JSONArray

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

the class PasswordPolicyStateJSONTestCase method testGetPasswordQualityRequirementsPropertyNotObject.

/**
 * Tests the behavior when trying to retrieve password quality requirements
 * when the properties array has a non-object element.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetPasswordQualityRequirementsPropertyNotObject() throws Exception {
    final PasswordPolicyStateJSON state = createState(StaticUtils.mapOf(PASSWORD_QUALITY_REQUIREMENTS, new JSONArray(new JSONObject(new JSONField("description", "description"), new JSONField("client-side-validation-type", "type"), new JSONField("client-side-validation-properties", new JSONArray(new JSONString("foo"))), new JSONField("applies-to-add", true)))));
    assertNotNull(state.getAddPasswordQualityRequirements());
    assertFalse(state.getAddPasswordQualityRequirements().isEmpty());
    assertEquals(state.getAddPasswordQualityRequirements().size(), 1);
    final PasswordQualityRequirement r = state.getAddPasswordQualityRequirements().get(0);
    assertEquals(r.getDescription(), "description");
    assertEquals(r.getClientSideValidationType(), "type");
    assertNotNull(r.getClientSideValidationProperties());
    assertTrue(r.getClientSideValidationProperties().isEmpty());
}
Also used : PasswordQualityRequirement(com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) PasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 84 with JSONArray

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

the class PasswordPolicyStateJSONTestCase method encodeRequirement.

/**
 * Encodes the provided password quality requirement to a JSON object suitable
 * for inclusion in the password policy state properties object.
 *
 * @param  requirement          The requirement to be encoded.
 * @param  appliesToAdd         Indicates whether the requirement applies to
 *                              add operations.
 * @param  appliesToSelfChange  Indicates whether the requirement applies to
 *                              self password changes.
 * @param  appliesToAdminReset  Indicates whether the requirement applies to
 *                              administrative password resets.
 * @param  appliesToBind        Indicates whether the requirement applies to
 *                              bind operations.
 *
 * @return  The encoded JSON object.
 */
private static JSONObject encodeRequirement(final PasswordQualityRequirement requirement, final boolean appliesToAdd, final boolean appliesToSelfChange, final boolean appliesToAdminReset, final boolean appliesToBind) {
    final Map<String, JSONValue> objectFields = new LinkedHashMap<>();
    objectFields.put("description", new JSONString(requirement.getDescription()));
    final String validationType = requirement.getClientSideValidationType();
    if (validationType != null) {
        objectFields.put("client-side-validation-type", new JSONString(validationType));
        final List<JSONValue> propertyObjects = new ArrayList<>();
        for (Map.Entry<String, String> e : requirement.getClientSideValidationProperties().entrySet()) {
            propertyObjects.add(new JSONObject(new JSONField("name", e.getKey()), new JSONField("value", e.getValue())));
        }
        objectFields.put("client-side-validation-properties", new JSONArray(propertyObjects));
    }
    objectFields.put("applies-to-add", new JSONBoolean(appliesToAdd));
    objectFields.put("applies-to-self-change", new JSONBoolean(appliesToSelfChange));
    objectFields.put("applies-to-administrative-reset", new JSONBoolean(appliesToAdminReset));
    objectFields.put("applies-to-bind", new JSONBoolean(appliesToBind));
    return new JSONObject(objectFields);
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) PasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString)

Example 85 with JSONArray

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

the class PasswordPolicyStateJSONTestCase method testGetBindPasswordQualityRequirements.

/**
 * Tests the behavior for the properties related to password quality
 * requirements for bind operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetBindPasswordQualityRequirements() throws Exception {
    final PasswordQualityRequirement allRequirement = new PasswordQualityRequirement("all-requirement-description", "all-requirement-type", Collections.<String, String>emptyMap());
    final PasswordQualityRequirement noneRequirement = new PasswordQualityRequirement("none-requirement-description", "none-requirement-type", Collections.<String, String>emptyMap());
    final PasswordQualityRequirement addRequirement = new PasswordQualityRequirement("add-requirement-description", "add-requirement-type", Collections.singletonMap("property-1", "value-1"));
    final PasswordQualityRequirement selfChangeRequirement = new PasswordQualityRequirement("self-change-requirement-description", "self-change-requirement-type", StaticUtils.mapOf("property-1", "value-1", "property-2", "value-2"));
    final PasswordQualityRequirement adminResetRequirement = new PasswordQualityRequirement("admin-reset-requirement-description", "admin-reset-requirement-type", StaticUtils.mapOf("property-1", "value-1", "property-2", "value-2", "property-3", "value-3"));
    final PasswordQualityRequirement bindRequirement = new PasswordQualityRequirement("bind-requirement-description", null, null);
    final JSONArray requirementsArray = new JSONArray(encodeRequirement(allRequirement, true, true, true, true), encodeRequirement(noneRequirement, false, false, false, false), encodeRequirement(addRequirement, true, false, false, false), encodeRequirement(selfChangeRequirement, false, true, false, false), encodeRequirement(adminResetRequirement, false, false, true, false), encodeRequirement(bindRequirement, false, false, false, true));
    final PasswordPolicyStateJSON state = createState(StaticUtils.mapOf(PASSWORD_QUALITY_REQUIREMENTS, requirementsArray));
    assertNotNull(state.getBindPasswordQualityRequirements());
    assertFalse(state.getBindPasswordQualityRequirements().isEmpty());
    assertEquals(state.getBindPasswordQualityRequirements().size(), 2);
    final PasswordQualityRequirement requirement0 = state.getBindPasswordQualityRequirements().get(0);
    assertEquals(requirement0.getDescription(), allRequirement.getDescription());
    assertEquals(requirement0.getClientSideValidationType(), allRequirement.getClientSideValidationType());
    assertEquals(requirement0.getClientSideValidationProperties(), allRequirement.getClientSideValidationProperties());
    final PasswordQualityRequirement requirement1 = state.getBindPasswordQualityRequirements().get(1);
    assertEquals(requirement1.getDescription(), bindRequirement.getDescription());
    assertNull(requirement1.getClientSideValidationType());
    assertNotNull(requirement1.getClientSideValidationProperties());
    assertTrue(requirement1.getClientSideValidationProperties().isEmpty());
}
Also used : PasswordQualityRequirement(com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement) JSONArray(com.unboundid.util.json.JSONArray) Test(org.testng.annotations.Test)

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