Search in sources :

Example 16 with JSONArray

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

the class ContainsFieldJSONObjectFilterTestCase method testTopLevelFieldWithoutExpectedType.

/**
 * Tests the behavior of this filter for a top-level field without an
 * expected type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testTopLevelFieldWithoutExpectedType() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "top-level-field")));
    assertNotNull(JSONObjectFilter.decode(f.toJSONObject()));
    assertTrue(JSONObjectFilter.decode(f.toJSONObject()) instanceof ContainsFieldJSONObjectFilter);
    f = (ContainsFieldJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f.getField());
    assertFalse(f.getField().isEmpty());
    assertEquals(f.getField(), Collections.singletonList("top-level-field"));
    assertNotNull(f.getExpectedType());
    assertEquals(f.getExpectedType(), EnumSet.allOf(ExpectedValueType.class));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "containsField");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("expectedType")));
    assertNotNull(f.toString());
    final StringBuilder toStringBuffer = new StringBuilder();
    f.toString(toStringBuffer);
    assertTrue(toStringBuffer.length() > 0);
    assertEquals(toStringBuffer.toString(), f.toString());
    final JSONObject toJSONObject = f.toJSONObject();
    final JSONObject toStringObject = new JSONObject(f.toString());
    assertEquals(toStringObject, toJSONObject);
    assertFalse(f.matchesJSONObject(new JSONObject()));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", 1234))));
    assertTrue(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", 1234), new JSONField("another-top-level-field", 5678))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("embeddedObject", new JSONObject(new JSONField("top-level-field", false))))));
}
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 17 with JSONArray

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

the class ContainsFieldJSONObjectFilterTestCase method testSetExpectedType.

/**
 * Provides test coverage for the methods used to get and set the target
 * expected data type(s).
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSetExpectedType() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("field-name");
    assertEquals(f.getExpectedType(), EnumSet.allOf(ExpectedValueType.class));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.STRING);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.STRING));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "string")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType();
    assertEquals(f.getExpectedType(), EnumSet.allOf(ExpectedValueType.class));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.BOOLEAN);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.BOOLEAN));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "boolean")));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.NUMBER);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.NUMBER));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "number")));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.NULL);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.NULL));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "null")));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.EMPTY_ARRAY);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.EMPTY_ARRAY));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "empty-array")));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.NON_EMPTY_ARRAY);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.NON_EMPTY_ARRAY));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "non-empty-array")));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.OBJECT);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.OBJECT));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", "object")));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType((ExpectedValueType[]) null);
    assertEquals(f.getExpectedType(), EnumSet.allOf(ExpectedValueType.class));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
    f.setExpectedType(ExpectedValueType.BOOLEAN, ExpectedValueType.NUMBER, ExpectedValueType.STRING);
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.BOOLEAN, ExpectedValueType.NUMBER, ExpectedValueType.STRING));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name"), new JSONField("expectedType", new JSONArray(new JSONString("boolean"), new JSONString("number"), new JSONString("string")))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", false))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("field-name", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", JSONNull.NULL))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONArray(JSONBoolean.TRUE)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject()))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("field-name", new JSONObject(new JSONField("foo", "bar"))))));
}
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) Test(org.testng.annotations.Test)

Example 18 with JSONArray

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

the class EqualsAnyJSONObjectFilterTestCase method testEqualsAnyList.

/**
 * Tests the behavior of the equals any filter with a number of cases and
 * values specified in a list.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testEqualsAnyList() throws Exception {
    EqualsAnyJSONObjectFilter f = new EqualsAnyJSONObjectFilter("test-field", Arrays.asList(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT));
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "test-field"), new JSONField("values", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT))));
    f = (EqualsAnyJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNotNull(f.getValues());
    assertEquals(f.getValues(), Arrays.asList(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT));
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "equalsAny");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "values")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("caseSensitive")));
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bar"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "baz"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONObject.EMPTY_OBJECT))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("bar"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("baz"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONArray.EMPTY_ARRAY)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONObject.EMPTY_OBJECT)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT)))));
    f.setCaseSensitive(true);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "test-field"), new JSONField("values", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT)), new JSONField("caseSensitive", true)));
    f = (EqualsAnyJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.caseSensitive());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bar"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "baz"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONObject.EMPTY_OBJECT))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("bar"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("baz"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONArray.EMPTY_ARRAY)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONObject.EMPTY_OBJECT)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT)))));
}
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 19 with JSONArray

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

the class EqualsAnyJSONObjectFilterTestCase method testEqualsAnyArray.

/**
 * Tests the behavior of the equals any filter with a number of cases and
 * values specified in an array.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testEqualsAnyArray() throws Exception {
    EqualsAnyJSONObjectFilter f = new EqualsAnyJSONObjectFilter("test-field", new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "test-field"), new JSONField("values", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT))));
    f = (EqualsAnyJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNotNull(f.getValues());
    assertEquals(f.getValues(), Arrays.asList(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT));
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "equalsAny");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Arrays.asList("field", "values")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("caseSensitive")));
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bar"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "baz"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONObject.EMPTY_OBJECT))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("bar"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("baz"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONArray.EMPTY_ARRAY)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONObject.EMPTY_OBJECT)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT)))));
    f.setCaseSensitive(true);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "test-field"), new JSONField("values", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT)), new JSONField("caseSensitive", true)));
    f = (EqualsAnyJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.caseSensitive());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bar"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "baz"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONNull.NULL))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONObject.EMPTY_OBJECT))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("bar"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("baz"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONBoolean.TRUE)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONNull.NULL)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONArray.EMPTY_ARRAY)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONObject.EMPTY_OBJECT)))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("bar"), new JSONNumber(1234), JSONBoolean.TRUE, JSONNull.NULL, JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT)))));
}
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 20 with JSONArray

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

the class EqualsAnyJSONObjectFilterTestCase method testGetAndSetField.

/**
 * Provides test coverage for the methods that can be used to get and set the
 * target field name for a filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetField() throws Exception {
    final EqualsAnyJSONObjectFilter f = new EqualsAnyJSONObjectFilter("test-field-name", JSONNull.NULL);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field-name"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "test-field-name"), new JSONField("values", new JSONArray(JSONNull.NULL))));
    f.setField("different-field-name");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("different-field-name"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "different-field-name"), new JSONField("values", new JSONArray(JSONNull.NULL))));
    f.setField("first", "second", "third");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Arrays.asList("first", "second", "third"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second"), new JSONString("third"))), new JSONField("values", new JSONArray(JSONNull.NULL))));
    try {
        f.setField();
        fail("Expected an exception with setFieldName of empty");
    } catch (final LDAPSDKUsageException e) {
    // This was expected
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

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