Search in sources :

Example 76 with JSONField

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

the class ContainsFieldJSONObjectFilterTestCase method testDecodeFilterInvalidType.

/**
 * Provides test coverage for the {@code decodeFilter} method for an object
 * that includes an unsupported expected type value.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { JSONException.class })
public void testDecodeFilterInvalidType() throws Exception {
    final JSONObject o = new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "test-field"), new JSONField("expectedType", "unrecognized"));
    JSONObjectFilter.decode(o);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) Test(org.testng.annotations.Test)

Example 77 with JSONField

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

the class ContainsFieldJSONObjectFilterTestCase method testThirdLevelField.

/**
 * Tests the behavior of this filter for a third-level field.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testThirdLevelField() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field", "second-level-field", "third-level-field");
    f.setExpectedType(ExpectedValueType.BOOLEAN);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", new JSONArray(new JSONString("top-level-field"), new JSONString("second-level-field"), new JSONString("third-level-field"))), new JSONField("expectedType", "boolean")));
    assertNotNull(JSONObjectFilter.decode(f.toJSONObject()));
    assertTrue(JSONObjectFilter.decode(f.toJSONObject()) instanceof ContainsFieldJSONObjectFilter);
    f = (ContainsFieldJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f.getField());
    assertFalse(f.getField().isEmpty());
    assertEquals(f.getField(), Arrays.asList("top-level-field", "second-level-field", "third-level-field"));
    assertNotNull(f.getExpectedType());
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.BOOLEAN));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "containsField");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("expectedType")));
    assertNotNull(f.toString());
    final StringBuilder toStringBuffer = new StringBuilder();
    f.toString(toStringBuffer);
    assertTrue(toStringBuffer.length() > 0);
    assertEquals(toStringBuffer.toString(), f.toString());
    final JSONObject toJSONObject = f.toJSONObject();
    final JSONObject toStringObject = new JSONObject(f.toString());
    assertEquals(toStringObject, toJSONObject);
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("third-level-field", true))))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("third-level-field", "not boolean"))))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("second-level-field", new JSONArray(new JSONObject(new JSONField("third-level-field", true))))))))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 78 with JSONField

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

the class ContainsFieldJSONObjectFilterTestCase method testSetField.

/**
 * Provides test coverage for the methods used to get and set the target
 * field.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSetField() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("field-name");
    assertEquals(f.getField(), Collections.singletonList("field-name"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name")));
    f.setField("different-name");
    assertEquals(f.getField(), Collections.singletonList("different-name"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "different-name")));
    f.setField("first", "second");
    assertEquals(f.getField(), Arrays.asList("first", "second"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second")))));
    try {
        f.setField();
        fail("Expected an exception from setField()");
    } catch (final LDAPSDKUsageException e) {
    // This was expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 79 with JSONField

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

the class LDIFSearchTestCase method testJSONOutputFormat.

/**
 * Tests the behavior when using the JSON output format.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testJSONOutputFormat() throws Exception {
    final File ldifFile = createTempFile("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "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");
    final File outputFile = createTempFile();
    assertTrue(outputFile.exists());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    assertEquals(LDIFSearch.main(out, out, "--ldifFile", ldifFile.getAbsolutePath(), "--outputFile", outputFile.getAbsolutePath(), "--baseDN", "ou=People,dc=example,dc=com", "--scope", "sub", "--outputFormat", "json", "(objectClass=person)", "objectClass", "uid", "cn"), ResultCode.SUCCESS);
    try (FileInputStream inputStream = new FileInputStream(outputFile);
        JSONObjectReader jsonReader = new JSONObjectReader(inputStream)) {
        JSONObject jsonObject = jsonReader.readObject();
        assertNotNull(jsonObject);
        assertEquals(jsonObject, new JSONObject(new JSONField("result-type", "entry"), new JSONField("dn", "uid=test.user,ou=People,dc=example,dc=com"), new JSONField("attributes", new JSONArray(new JSONObject(new JSONField("name", "objectClass"), new JSONField("values", new JSONArray(new JSONString("top"), new JSONString("person"), new JSONString("organizationalPerson"), new JSONString("inetOrgPerson")))), new JSONObject(new JSONField("name", "uid"), new JSONField("values", new JSONArray(new JSONString("test.user")))), new JSONObject(new JSONField("name", "cn"), new JSONField("values", new JSONArray(new JSONString("Test User"))))))));
        jsonObject = jsonReader.readObject();
        assertNull(jsonObject);
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) JSONField(com.unboundid.util.json.JSONField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 80 with JSONField

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

the class RecentLoginHistoryTestCase method testDecodeMalformedSuccessNotObject.

/**
 * Tests the behavior when trying to decode a JSON object with a malformed
 * set of successful attempts when the malformed attempt is not an object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeMalformedSuccessNotObject() throws Exception {
    final JSONObject o = new JSONObject(new JSONField("successful-attempts", new JSONArray(new JSONString("malformed"))));
    new RecentLoginHistory(o);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

JSONField (com.unboundid.util.json.JSONField)97 JSONObject (com.unboundid.util.json.JSONObject)97 Test (org.testng.annotations.Test)91 JSONArray (com.unboundid.util.json.JSONArray)68 JSONString (com.unboundid.util.json.JSONString)66 JSONNumber (com.unboundid.util.json.JSONNumber)20 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)8 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)7 Date (java.util.Date)7 Entry (com.unboundid.ldap.sdk.Entry)5 JSONException (com.unboundid.util.json.JSONException)5 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 JSONBoolean (com.unboundid.util.json.JSONBoolean)4 JSONValue (com.unboundid.util.json.JSONValue)4 CompareRequest (com.unboundid.ldap.sdk.CompareRequest)3 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)3 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)3 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3 ArrayList (java.util.ArrayList)3