Search in sources :

Example 6 with JSONException

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

the class RegularExpressionJSONObjectFilterTestCase method testGetAndSetRegularExpression.

/**
 * Provides test coverage for the methods that can be used to get and set the
 * regular expression for a filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetRegularExpression() throws Exception {
    RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", "[a-zA-Z][a-zA-Z0-9]*");
    assertNotNull(f.getRegularExpression());
    assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z][a-zA-Z0-9]*");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
    f.setRegularExpression("[a-zA-Z0-9]+");
    assertNotNull(f.getRegularExpression());
    assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z0-9]+");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z0-9]+")));
    f.setRegularExpression(Pattern.compile("[a-zA-Z0-9]*"));
    assertNotNull(f.getRegularExpression());
    assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z0-9]*");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[a-zA-Z0-9]*")));
    try {
        f.setRegularExpression((String) null);
        fail("Expected an exception with setRegularExpression of string null");
    } catch (final LDAPSDKUsageException e) {
    // This was expected
    }
    try {
        f.setRegularExpression("[invalid");
        fail("Expected an exception with setRegularExpression of string invalid");
    } catch (final JSONException e) {
    // This was expected
    }
    try {
        f.setRegularExpression((Pattern) null);
        fail("Expected an exception with setRegularExpression of pattern null");
    } catch (final LDAPSDKUsageException e) {
    // This was expected
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) JSONException(com.unboundid.util.json.JSONException) Test(org.testng.annotations.Test)

Example 7 with JSONException

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

the class JSONObjectFilterTestCase method testGetBoolean.

/**
 * Provides test coverage for the {@code getBoolean} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetBoolean() throws Exception {
    // A filter that will be used just to invoke the protected methods.
    final ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("name");
    // Test the case in which an object has a Boolean value of true.
    JSONObject o = new JSONObject(new JSONField("a", true));
    assertTrue(f.getBoolean(o, "a", null));
    // Test the case in which an object has a Boolean value of false.
    o = new JSONObject(new JSONField("a", false));
    assertFalse(f.getBoolean(o, "a", false));
    // Test the case in which an object is missing the target field and there
    // is a default value of true.
    assertTrue(f.getBoolean(JSONObject.EMPTY_OBJECT, "a", true));
    // Test the case in which an object is missing the target field and there
    // is a default value of false.
    assertFalse(f.getBoolean(JSONObject.EMPTY_OBJECT, "a", false));
    // is no default value.
    try {
        f.getBoolean(JSONObject.EMPTY_OBJECT, "a", null);
        fail("Expected an exception for a missing field");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the cse in which an object has a non-Boolean value for the target
    // field.
    o = new JSONObject(new JSONField("a", new JSONString("true")));
    try {
        f.getBoolean(o, "a", null);
        fail("Expected an exception for a non-Boolean field");
    } catch (final JSONException e) {
    // This is expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 8 with JSONException

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

the class JSONLDAPResultWriter method toJSON.

/**
 * Encodes the provided LDAP result as a JSON object.
 *
 * @param  result  The LDAP result to be encoded as a JSON object.  It must
 *                 not be {@code null}.
 *
 * @return  The JSON object containing the encoded representation of the
 *          LDAP result.
 */
@NotNull()
public static JSONObject toJSON(@NotNull final LDAPResult result) {
    try {
        final JSONBuffer jsonBuffer = new JSONBuffer();
        toJSON(result, jsonBuffer);
        return jsonBuffer.toJSONObject();
    } catch (final JSONException e) {
        // This should never happen.
        Debug.debugException(e);
        throw new LDAPRuntimeException(new LDAPException(ResultCode.ENCODING_ERROR, e.getMessage(), e));
    }
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONBuffer(com.unboundid.util.json.JSONBuffer) JSONException(com.unboundid.util.json.JSONException) LDAPRuntimeException(com.unboundid.ldap.sdk.LDAPRuntimeException) NotNull(com.unboundid.util.NotNull)

Example 9 with JSONException

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

the class JSONLDAPResultWriter method toJSON.

/**
 * Encodes the provided entry as a JSON object.
 *
 * @param  entry  The entry to be encoded as a JSON object.  It must not be
 *                {@code null}.
 *
 * @return  The JSON object containing the encoded representation of the
 *          entry.
 */
@NotNull()
public static JSONObject toJSON(@NotNull final Entry entry) {
    try {
        final JSONBuffer jsonBuffer = new JSONBuffer();
        toJSON(entry, jsonBuffer);
        return jsonBuffer.toJSONObject();
    } catch (final JSONException e) {
        // This should never happen.
        Debug.debugException(e);
        throw new LDAPRuntimeException(new LDAPException(ResultCode.ENCODING_ERROR, e.getMessage(), e));
    }
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONBuffer(com.unboundid.util.json.JSONBuffer) JSONException(com.unboundid.util.json.JSONException) LDAPRuntimeException(com.unboundid.ldap.sdk.LDAPRuntimeException) NotNull(com.unboundid.util.NotNull)

Example 10 with JSONException

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

the class JSONObjectFilterTestCase method testGetFilters.

/**
 * Provides test coverage for the {@code getFilters} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetFilters() throws Exception {
    // A filter that will be used just to invoke the protected methods.
    final ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("name");
    // Test the case in which an object has a single filter for the target
    // field.
    JSONObject o = new JSONObject(new JSONField("a", new JSONArray(f.toJSONObject())));
    assertNotNull(f.getFilters(o, "a"));
    assertEquals(f.getFilters(o, "a"), Collections.singletonList(f));
    // Test the case in which an object has a non-empty array of filters for the
    // target field.
    o = new JSONObject(new JSONField("a", new JSONArray(f.toJSONObject(), new EqualsJSONObjectFilter("b", new JSONString("c")).toJSONObject())));
    assertNotNull(f.getFilters(o, "a"));
    assertEquals(f.getFilters(o, "a"), Arrays.asList(f, new EqualsJSONObjectFilter("b", new JSONString("c"))));
    // Test the case in which an object has an empty array of filters for the
    // target field.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    assertNotNull(f.getFilters(o, "a"));
    assertEquals(f.getFilters(o, "a"), Collections.emptyList());
    // Test the case in which an object is missing the target field.
    try {
        f.getFilters(JSONObject.EMPTY_OBJECT, "a");
        fail("Expected an exception for a missing field");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a non-array value for the target
    // field.
    o = new JSONObject(new JSONField("a", JSONBoolean.TRUE));
    try {
        f.getFilters(o, "a");
        fail("Expected an exception for a non-array value");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has an array containing an element that
    // is not an object.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("foo"))));
    try {
        f.getFilters(o, "a");
        fail("Expected an exception for an array with a non-object element");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has an array containing an object that
    // is not a valid filter.
    o = new JSONObject(new JSONField("a", JSONObject.EMPTY_OBJECT));
    try {
        f.getFilters(o, "a");
        fail("Expected an exception for an array with a non-filter object");
    } catch (final JSONException e) {
    // This is expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

JSONException (com.unboundid.util.json.JSONException)17 NotNull (com.unboundid.util.NotNull)10 JSONString (com.unboundid.util.json.JSONString)10 JSONObject (com.unboundid.util.json.JSONObject)9 JSONArray (com.unboundid.util.json.JSONArray)5 JSONField (com.unboundid.util.json.JSONField)5 Test (org.testng.annotations.Test)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONValue (com.unboundid.util.json.JSONValue)4 LDAPRuntimeException (com.unboundid.ldap.sdk.LDAPRuntimeException)3 JSONBuffer (com.unboundid.util.json.JSONBuffer)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1