Search in sources :

Example 21 with JSONField

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

the class ORJSONObjectFilterTestCase method testZeroComponents.

/**
 * Tests the behavior of an OR filter with zero components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testZeroComponents() throws Exception {
    ORJSONObjectFilter f = new ORJSONObjectFilter();
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", JSONArray.EMPTY_ARRAY)));
    f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getORFilters());
    assertEquals(f.getORFilters(), Collections.emptyList());
    assertFalse(f.exclusive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "or");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("orFilters")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("exclusive")));
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "foo"), new JSONField("b", 1234), new JSONField("c", true), new JSONField("d", false), new JSONField("e", JSONNull.NULL), new JSONField("f", JSONArray.EMPTY_ARRAY), new JSONField("g", new JSONArray(new JSONString("foo"), new JSONString("bar"))), new JSONField("h", JSONObject.EMPTY_OBJECT), new JSONField("i", new JSONObject(new JSONField("j", "k"))))));
    f.setExclusive(true);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", JSONArray.EMPTY_ARRAY), new JSONField("exclusive", true)));
    f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.exclusive());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "foo"), new JSONField("b", 1234), new JSONField("c", true), new JSONField("d", false), new JSONField("e", JSONNull.NULL), new JSONField("f", JSONArray.EMPTY_ARRAY), new JSONField("g", new JSONArray(new JSONString("foo"), new JSONString("bar"))), new JSONField("h", JSONObject.EMPTY_OBJECT), new JSONField("i", new JSONObject(new JSONField("j", "k"))))));
}
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 22 with JSONField

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

the class RegularExpressionJSONObjectFilterTestCase method testDecodeMalformedRegex.

/**
 * Tests the behavior of the decode method when provided with a malformed
 * regular expression.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { JSONException.class })
public void testDecodeMalformedRegex() throws Exception {
    final JSONObject o = new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "a"), new JSONField("regularExpression", "[malformed"));
    JSONObjectFilter.decode(o);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) Test(org.testng.annotations.Test)

Example 23 with JSONField

use of com.unboundid.util.json.JSONField 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 24 with JSONField

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

the class RegularExpressionJSONObjectFilterTestCase method testPatternRegex.

/**
 * Tests the regular expression filter with a pattern regex.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testPatternRegex() throws Exception {
    RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", Pattern.compile("[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 = (RegularExpressionJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("a"));
    assertNotNull(f.getRegularExpression());
    assertEquals(f.getRegularExpression().pattern(), "[a-zA-Z][a-zA-Z0-9]*");
    assertFalse(f.matchAllElements());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "regularExpression");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "regularExpression")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.singletonList("matchAllElements"));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "abc"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc123"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "a"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "ABC123"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "A"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "123abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", ""))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc!"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"), JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("456def"))))));
    f.setMatchAllElements(true);
    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]*"), new JSONField("matchAllElements", true)));
    f = (RegularExpressionJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.matchAllElements());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "abc"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc123"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "a"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "ABC123"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "A"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "123abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", ""))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "abc!"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("def456"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONString("abc"), new JSONString("456def"))))));
}
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 25 with JSONField

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

the class RegularExpressionJSONObjectFilterTestCase method testGetAndSetField.

/**
 * Provides test coverage for the methods that can be used to get and set the
 * target field for a filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetField() throws Exception {
    RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", "[a-zA-Z][a-zA-Z0-9]*");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("a"));
    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.setField("b");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("b"));
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", "b"), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
    f.setField("first", "second", "third");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Arrays.asList("first", "second", "third"));
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "regularExpression"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second"), new JSONString("third"))), new JSONField("regularExpression", "[a-zA-Z][a-zA-Z0-9]*")));
    try {
        f.setField();
        fail("Expected an exception with setField of empty");
    } 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)

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