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
}
}
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.
}
}
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));
}
}
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));
}
}
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.
}
}
Aggregations