Search in sources :

Example 66 with JSONField

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

Example 67 with JSONField

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

the class GreaterThanJSONObjectFilterTestCase method testGreaterThanNumber.

/**
 * Tests the behavior of the greater-than filter with a numeric value.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGreaterThanNumber() throws Exception {
    GreaterThanJSONObjectFilter f = new GreaterThanJSONObjectFilter("test-field", 5678);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", 5678)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNotNull(f.getValue());
    assertEquals(f.getValue(), new JSONNumber(5678));
    assertFalse(f.allowEquals());
    assertFalse(f.matchAllElements());
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "greaterThan");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "value")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("allowEquals", "matchAllElements", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5679))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", 5679))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 9999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 10000))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678.5))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678.0))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "5679"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5678), new JSONNumber(5679), new JSONNumber(10000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5679), new JSONNumber(10000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679), new JSONNumber(10000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679), new JSONNumber(10000), new JSONString("foo"), JSONNull.NULL)))));
    f.setMatchAllElements(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", 5678), new JSONField("matchAllElements", true)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.matchAllElements());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5679))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", 5679))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 9999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 10000))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5679))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678.5))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678.0))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "5679"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5678), new JSONNumber(5679), new JSONNumber(10000))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5679), new JSONNumber(10000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679), new JSONNumber(10000))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679), new JSONNumber(10000), new JSONString("foo"), JSONNull.NULL)))));
    f.setAllowEquals(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", 5678), new JSONField("matchAllElements", true), new JSONField("allowEquals", true)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.allowEquals());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5679))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", 5679))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 9999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 10000))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5679))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678.5))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5678.0))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "5679"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5678), new JSONNumber(5679), new JSONNumber(10000))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5679), new JSONNumber(10000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679), new JSONNumber(10000))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5679), new JSONNumber(10000), new JSONString("foo"), JSONNull.NULL)))));
}
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 68 with JSONField

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

the class GreaterThanJSONObjectFilterTestCase method testGreaterThanString.

/**
 * Tests the behavior of the greater-than filter with a string value.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGreaterThanString() throws Exception {
    GreaterThanJSONObjectFilter f = new GreaterThanJSONObjectFilter("test-field", "foo");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", "foo")));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNotNull(f.getValue());
    assertEquals(f.getValue(), new JSONString("foo"));
    assertFalse(f.allowEquals());
    assertFalse(f.matchAllElements());
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "greaterThan");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "value")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("allowEquals", "matchAllElements", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fro"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "go"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "g"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GOO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FRO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "G"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("goo"), new JSONString("fool"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("fool"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("goo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("GOO"), new JSONString("FOOL"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"), new JSONString("FOOL"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("GOO"))))));
    f.setMatchAllElements(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.matchAllElements());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fro"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "go"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "g"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GOO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FRO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "G"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("goo"), new JSONString("fool"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("fool"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("goo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("GOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("GOO"))))));
    f.setAllowEquals(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true), new JSONField("allowEquals", true)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.allowEquals());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "goo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fro"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "go"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "g"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FRO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "G"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("goo"), new JSONString("fool"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("fool"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("goo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("GOO"), new JSONString("FOOL"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("GOO"))))));
    f.setCaseSensitive(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true), new JSONField("allowEquals", true), new JSONField("caseSensitive", true)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.caseSensitive());
    // NOTE:  All uppercase characters are considered "less than" all lowercase.
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "goo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fro"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "go"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "g"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GOO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FRO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "G"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("goo"), new JSONString("fool"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("fool"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("goo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("GOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("GOO"))))));
    f.setAllowEquals(false);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "greaterThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true), new JSONField("caseSensitive", true)));
    f = (GreaterThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertFalse(f.allowEquals());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", Boolean.TRUE))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "goo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fro"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "go"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "g"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GOO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FRO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "GO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "G"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("goo"), new JSONString("fool"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("fool"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("goo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("GOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"), new JSONString("FOOL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("GOO"))))));
}
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 69 with JSONField

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

Example 70 with JSONField

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

the class JSONObjectFilterTestCase method testGetString.

/**
 * Provides test coverage for the {@code getString} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetString() 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 string for the target
    // field.
    JSONObject o = new JSONObject(new JSONField("a", "b"));
    assertNotNull(f.getString(o, "a", null, true));
    assertEquals(f.getString(o, "a", null, true), "b");
    // Test the case in which an object does not contain the specified field
    // but that is OK and there is no default.
    assertNull(f.getString(JSONObject.EMPTY_OBJECT, "a", null, false));
    // Test the case in which an object does not contain the specified field
    // but that is OK and there is a default.
    assertNotNull(f.getString(JSONObject.EMPTY_OBJECT, "a", "default", false));
    assertEquals(f.getString(JSONObject.EMPTY_OBJECT, "a", "default", false), "default");
    // but that is not OK.
    try {
        f.getString(JSONObject.EMPTY_OBJECT, "a", null, true);
        fail("Expected an exception from getString with a nonexistent field");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has an empty array for the target
    // field.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    try {
        f.getString(o, "a", null, true);
        fail("Expected an exception from getString with an empty array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a single-string array for the target
    // field.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("b"))));
    try {
        f.getString(o, "a", null, true);
        fail("Expected an exception from getString with a single-string array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a non-string, non-array value for
    // the target field.
    o = new JSONObject(new JSONField("a", JSONBoolean.TRUE));
    try {
        f.getString(o, "a", null, true);
        fail("Expected an exception from getString with a boolean");
    } 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

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