Search in sources :

Example 51 with JSONArray

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

the class ORJSONObjectFilterTestCase method testMultipleComponents.

/**
 * Tests the behavior of an OR filter with multiple components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testMultipleComponents() throws Exception {
    final EqualsJSONObjectFilter f1 = new EqualsJSONObjectFilter("a", new JSONString("b"));
    final EqualsJSONObjectFilter f2 = new EqualsJSONObjectFilter("c", new JSONString("d"));
    final EqualsJSONObjectFilter f3 = new EqualsJSONObjectFilter("e", new JSONString("f"));
    ORJSONObjectFilter f = new ORJSONObjectFilter(f1, f2, f3);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", new JSONArray(f1.toJSONObject(), f2.toJSONObject(), f3.toJSONObject()))));
    f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getORFilters());
    assertEquals(f.getORFilters(), Arrays.asList(f1, f2, f3));
    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));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "x"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("x", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("a"), JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("b"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"), new JSONField("g", "h"), new JSONField("i", "j"))));
    f.setExclusive(true);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", JSONArray.EMPTY_ARRAY), new JSONField("orFilters", new JSONArray(f1.toJSONObject(), f2.toJSONObject(), f3.toJSONObject())), new JSONField("exclusive", true)));
    f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.exclusive());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "x"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("x", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("a"), JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("b"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"), new JSONField("g", "h"), new JSONField("i", "j"))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 52 with JSONArray

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

the class ObjectMatchesJSONObjectFilterTestCase method testObjectMatchesCompoundFilter.

/**
 * Tests the behavior of an object matches filter under a variety of
 * conditions with a compound filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testObjectMatchesCompoundFilter() throws Exception {
    final ANDJSONObjectFilter andFilter = new ANDJSONObjectFilter(new EqualsJSONObjectFilter("one", new JSONString("uno")), new EqualsJSONObjectFilter("two", new JSONString("dos")));
    ObjectMatchesJSONObjectFilter f = new ObjectMatchesJSONObjectFilter("a", andFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "objectMatches"), new JSONField("field", "a"), new JSONField("filter", andFilter.toJSONObject())));
    f = (ObjectMatchesJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("a"));
    assertNotNull(f.getFilter());
    assertEquals(f.getFilter(), andFilter);
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "objectMatches");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "filter")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONObject(new JSONField("one", "uno"), new JSONField("two", "dos"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("language", "Spanish"), new JSONField("one", "uno"), new JSONField("two", "dos")), new JSONObject(new JSONField("language", "French"), new JSONField("one", "une"), new JSONField("two", "deux")), new JSONObject(new JSONField("language", "Italian"), new JSONField("one", "una"), new JSONField("two", "due")))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("one", "uno"), new JSONField("two", "due")), new JSONObject(new JSONField("one", "une"), new JSONField("two", "dos")), new JSONObject(new JSONField("one", "una"), new JSONField("two", "deux")))))));
}
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 53 with JSONArray

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

the class ObjectMatchesJSONObjectFilterTestCase method testObjectMatchesSimpleFilter.

/**
 * Tests the behavior of an object matches filter under a variety of
 * conditions with a simple filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testObjectMatchesSimpleFilter() throws Exception {
    EqualsJSONObjectFilter equalsFilter = new EqualsJSONObjectFilter("b", new JSONString("c"));
    ObjectMatchesJSONObjectFilter f = new ObjectMatchesJSONObjectFilter("a", equalsFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "objectMatches"), new JSONField("field", "a"), new JSONField("filter", equalsFilter.toJSONObject())));
    f = (ObjectMatchesJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("a"));
    assertNotNull(f.getFilter());
    assertEquals(f.getFilter(), equalsFilter);
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "objectMatches");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "filter")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "c"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "c"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "c"))))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", "c")), new JSONObject(new JSONField("b", "d")))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONArray(new JSONString("c"), new JSONString("d"))))))));
    equalsFilter = new EqualsJSONObjectFilter(Arrays.asList("a", "b"), new JSONString("c"));
    f = new ObjectMatchesJSONObjectFilter("a", equalsFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "objectMatches"), new JSONField("field", "a"), new JSONField("filter", equalsFilter.toJSONObject())));
    f = (ObjectMatchesJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "c"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "c"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "c"))))))));
}
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 54 with JSONArray

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

the class ObjectMatchesJSONObjectFilterTestCase method testGetAndSetField.

/**
 * Provides test coverage for the methods that can be used to get and set
 * the field.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetField() throws Exception {
    final EqualsJSONObjectFilter equalsFilter = new EqualsJSONObjectFilter("b", new JSONString("c"));
    ObjectMatchesJSONObjectFilter f = new ObjectMatchesJSONObjectFilter("a", equalsFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "objectMatches"), new JSONField("field", "a"), new JSONField("filter", equalsFilter.toJSONObject())));
    f = (ObjectMatchesJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("a"));
    f.setField("x");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "objectMatches"), new JSONField("field", "x"), new JSONField("filter", equalsFilter.toJSONObject())));
    f = (ObjectMatchesJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("x"));
    f.setField("one", "two", "three");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "objectMatches"), new JSONField("field", new JSONArray(new JSONString("one"), new JSONString("two"), new JSONString("three"))), new JSONField("filter", equalsFilter.toJSONObject())));
    f = (ObjectMatchesJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Arrays.asList("one", "two", "three"));
    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)

Example 55 with JSONArray

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

the class RegularExpressionJSONObjectFilterTestCase method testStringRegex.

/**
 * Tests the regular expression filter with a valid string regex.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testStringRegex() throws Exception {
    RegularExpressionJSONObjectFilter f = new RegularExpressionJSONObjectFilter("a", "[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)

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