Search in sources :

Example 61 with JSONArray

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

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

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

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

Example 65 with JSONArray

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

the class JSONObjectFilterTestCase method testGetStrings.

/**
 * Provides test coverage for the {@code getStrings} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetStrings() 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.getStrings(o, "a", true, null));
    assertFalse(f.getStrings(o, "a", true, null).isEmpty());
    assertEquals(f.getStrings(o, "a", true, null), Collections.singletonList("b"));
    // Test the case in which an object has an array of strings for the target
    // field.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("b"), new JSONString("c"), new JSONString("d"))));
    assertNotNull(f.getStrings(o, "a", true, null));
    assertFalse(f.getStrings(o, "a", true, null).isEmpty());
    assertEquals(f.getStrings(o, "a", true, null), Arrays.asList("b", "c", "d"));
    // Test the case in which an object has an empty array, when that is
    // allowed.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    assertNotNull(f.getStrings(o, "a", true, null));
    assertTrue(f.getStrings(o, "a", true, null).isEmpty());
    // Test the case in which an object has an empty array, when that is not
    // allowed.
    o = new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY));
    try {
        f.getStrings(o, "a", false, null);
        fail("Expected an exception from getStrings with empty array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a value that is neither a string
    // nor an array.
    o = new JSONObject(new JSONField("a", JSONBoolean.TRUE));
    try {
        f.getStrings(o, "a", false, null);
        fail("Expected an exception from getStrings with true");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object has a value that has an array
    // containing a non-string element.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("foo"), JSONNull.NULL, new JSONString("bar"))));
    try {
        f.getStrings(o, "a", false, null);
        fail("Expected an exception from getStrings with non-string array");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the case in which an object does not have the target field when that
    // is allowed.
    assertNotNull(f.getStrings(JSONObject.EMPTY_OBJECT, "a", false, Collections.<String>emptyList()));
    assertTrue(f.getStrings(JSONObject.EMPTY_OBJECT, "a", false, Collections.<String>emptyList()).isEmpty());
    // is not allowed.
    try {
        f.getStrings(JSONObject.EMPTY_OBJECT, "a", false, null);
        fail("Expected an exception from getStrings with nonexistent field");
    } 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) 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