Search in sources :

Example 56 with JSONArray

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

the class SubstringJSONObjectFilterTestCase method testMultipleContains.

/**
 * Tests the behavior of a filter that only uses multiple contains components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testMultipleContains() throws Exception {
    SubstringJSONObjectFilter f = new SubstringJSONObjectFilter(Collections.singletonList("test-field"), null, Arrays.asList("aba", "bab"), null);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("contains", new JSONArray(new JSONString("aba"), new JSONString("bab")))));
    f = (SubstringJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNull(f.getStartsWith());
    assertNotNull(f.getContains());
    assertEquals(f.getContains(), Arrays.asList("aba", "bab"));
    assertNull(f.getEndsWith());
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "substring");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("startsWith", "contains", "endsWith", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ababab"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABABAB"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abaxbab"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABAxBAB"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "xabaxbabx"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "xABAxBABx"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abab"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bababa"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("aBABab"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("aba"), new JSONString("bab"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ABAab"), new JSONString("BABab"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("AbAbAbAbA"), new JSONString("BaBaBaBaB"))))));
    f.setCaseSensitive(true);
    assertTrue(f.caseSensitive());
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("contains", new JSONArray(new JSONString("aba"), new JSONString("bab"))), new JSONField("caseSensitive", true)));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ababab"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABABAB"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abaxbab"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABAxBAB"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "xabaxbabx"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "xABAxBABx"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abab"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bababa"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ababab"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("aba"), new JSONString("bab"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ABAab"), new JSONString("BABab"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("AbAbAbAbA"), new JSONString("BaBaBaBaB"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ababababa"), new JSONString("BaBaBaBaB"))))));
}
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 57 with JSONArray

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

the class SubstringJSONObjectFilterTestCase method testGetAndSetFields.

/**
 * Tests the behavior of the methods used to get and set the field name.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetFields() throws Exception {
    final SubstringJSONObjectFilter f = new SubstringJSONObjectFilter("a", "b", "c", "d");
    f.setCaseSensitive(true);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("a"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "a"), new JSONField("startsWith", "b"), new JSONField("contains", "c"), new JSONField("endsWith", "d"), new JSONField("caseSensitive", true)));
    f.setField("different");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("different"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "different"), new JSONField("startsWith", "b"), new JSONField("contains", "c"), new JSONField("endsWith", "d"), new JSONField("caseSensitive", true)));
    f.setField("first", "second");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Arrays.asList("first", "second"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second"))), new JSONField("startsWith", "b"), new JSONField("contains", "c"), new JSONField("endsWith", "d"), new JSONField("caseSensitive", true)));
    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 58 with JSONArray

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

the class SubstringJSONObjectFilterTestCase method testStartsWith.

/**
 * Tests the behavior of a filter that only uses the startsWith component.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testStartsWith() throws Exception {
    SubstringJSONObjectFilter f = new SubstringJSONObjectFilter("test-field", "abc", null, null);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("startsWith", "abc")));
    f = (SubstringJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNotNull(f.getStartsWith());
    assertEquals(f.getStartsWith(), "abc");
    assertNotNull(f.getContains());
    assertTrue(f.getContains().isEmpty());
    assertNull(f.getEndsWith());
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "substring");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("startsWith", "contains", "endsWith", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abc"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "Abc"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABC"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ab"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abcdefghijkl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "AbCdEfGhIjKl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHIJKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defabcghi"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abcdef"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abc"), new JSONString("DEF"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("def"), new JSONString("ABC"))))));
    f.setCaseSensitive(true);
    assertTrue(f.caseSensitive());
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("startsWith", "abc"), new JSONField("caseSensitive", true)));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "Abc"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABC"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abcdefghijkl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "AbCdEfGhIjKl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHIJKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abcdef"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("abc"), new JSONString("DEF"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONBoolean.TRUE, new JSONString("abc"), JSONNull.NULL, new JSONString("DEF"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("def"), new JSONString("ABC"))))));
}
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 59 with JSONArray

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

the class EqualsJSONObjectFilterTestCase method testTopLevelFieldNull.

/**
 * Provides test coverage for the case in which a filter references a
 * top-level field and a {@code JSONNull} value.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testTopLevelFieldNull() throws Exception {
    EqualsJSONObjectFilter f = new EqualsJSONObjectFilter("top-level-field", JSONNull.NULL);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equals"), new JSONField("field", "top-level-field"), new JSONField("value", JSONNull.NULL)));
    f = (EqualsJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("top-level-field"));
    assertNotNull(f.getValue());
    assertEquals(f.getValue(), JSONNull.NULL);
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "equals");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "value")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONNull()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(JSONNull.NULL, JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("top-level-field", JSONNull.NULL))))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONNull(com.unboundid.util.json.JSONNull) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 60 with JSONArray

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

the class EqualsJSONObjectFilterTestCase method testTopLevelFieldString.

/**
 * Provides test coverage for the case in which a filter references a
 * top-level field and a string value.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testTopLevelFieldString() throws Exception {
    EqualsJSONObjectFilter f = new EqualsJSONObjectFilter("top-level-field", new JSONString("foo"));
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equals"), new JSONField("field", "top-level-field"), new JSONField("value", "foo")));
    f = (EqualsJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("top-level-field"));
    assertNotNull(f.getValue());
    assertEquals(f.getValue(), new JSONString("foo"));
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "equals");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "value")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "Foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Top-Level-Field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("foo"), new JSONString("bar"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("Bar"), new JSONString("Foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5678))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5678), new JSONString("foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONArray.EMPTY_ARRAY))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONObject.EMPTY_OBJECT))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("top-level-field", "foo"))))));
    f.setCaseSensitive(true);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equals"), new JSONField("field", "top-level-field"), new JSONField("value", "foo"), new JSONField("caseSensitive", true)));
    f = (EqualsJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("top-level-field"));
    assertNotNull(f.getValue());
    assertEquals(f.getValue(), new JSONString("foo"));
    assertTrue(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "equals");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "value")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "Foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Top-Level-Field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("foo"), new JSONString("bar"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("Bar"), new JSONString("Foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5678))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5678), new JSONString("foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONArray.EMPTY_ARRAY))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", JSONObject.EMPTY_OBJECT))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("top-level-field", "foo"))))));
}
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)

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