Search in sources :

Example 66 with JSONArray

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

the class JSONObjectFilterTestCase method testGetValues.

/**
 * Provides test coverage for the {@code getValues} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetValues() throws Exception {
    // Test the case in which a target top-level field does not exist.
    assertNotNull(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Collections.singletonList("a")));
    assertEquals(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Collections.singletonList("a")), Collections.emptyList());
    // Test the case in which an object has a single value for a top-level
    // field.
    JSONObject o = new JSONObject(new JSONField("a", "b"));
    assertNotNull(JSONObjectFilter.getValues(o, Collections.singletonList("a")));
    assertEquals(JSONObjectFilter.getValues(o, Collections.singletonList("a")), Collections.singletonList(new JSONString("b")));
    // Test the case in which an object has an array value for a top-level
    // field.
    o = new JSONObject(new JSONField("a", new JSONArray(new JSONString("b"), new JSONString("c"), new JSONString("d"))));
    assertNotNull(JSONObjectFilter.getValues(o, Collections.singletonList("a")));
    assertEquals(JSONObjectFilter.getValues(o, Collections.singletonList("a")), Collections.singletonList(new JSONArray(new JSONString("b"), new JSONString("c"), new JSONString("d"))));
    // Test the case in which the first level of a two-level field does not
    // exist.
    assertNotNull(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(JSONObject.EMPTY_OBJECT, Arrays.asList("a", "b")), Collections.emptyList());
    // Test the case in which the first level of a two-level field exists but
    // has a simple value that isn't an object or an array.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", JSONBoolean.TRUE)), Arrays.asList("a", "b")));
    // Test the case in which the first level of a two-level field exists but
    // is an object that doesn't have the second-level field.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("x", "x")))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("x", "y")))), Arrays.asList("a", "b")), Collections.emptyList());
    // Test the case in which the first level of a two-level field exists and
    // is an object that has the second-level field.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "x")))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", "x")))), Arrays.asList("a", "b")), Collections.singletonList(new JSONString("x")));
    // Test the case in which the first level of a two-level field exists and
    // is an empty array.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY)), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", JSONArray.EMPTY_ARRAY)), Arrays.asList("a", "b")), Collections.emptyList());
    // Test the case in which the first level of a two-level field exists and
    // is an array that doesn't contain any objects.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL))), Arrays.asList("a", "b")), Collections.emptyList());
    // Test the case in which the first level of a two-level field exists and
    // is an array that contains an object without the target field.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT))), Arrays.asList("a", "b")), Collections.emptyList());
    // Test the case in which the first level of a two-level field exists and
    // is an array that contains multiple objects with the target field.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT, new JSONObject(new JSONField("b", "x")), new JSONObject(new JSONField("b", "y")), new JSONObject(new JSONField("b", "z"))))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(JSONBoolean.TRUE, JSONNull.NULL, JSONObject.EMPTY_OBJECT, new JSONObject(new JSONField("b", "x")), new JSONObject(new JSONField("b", "y")), new JSONObject(new JSONField("b", "z"))))), Arrays.asList("a", "b")), Arrays.asList(new JSONString("x"), new JSONString("y"), new JSONString("z")));
    // Test the case in which the first level of a two-level field exists and
    // is an object containing the second-level field whose value is an array.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONArray(new JSONString("x"), new JSONString("y"), new JSONString("z")))))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONArray(new JSONString("x"), new JSONString("y"), new JSONString("z")))))), Arrays.asList("a", "b")), Collections.singletonList(new JSONArray(new JSONString("x"), new JSONString("y"), new JSONString("z"))));
    // Test the case in which the first level of a two-level field exists and
    // is an array of objects, and each of those objects contains the
    // second-level field with array values.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONString("1a"), new JSONString("1b"), new JSONString("1c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("2a"), new JSONString("2b"), new JSONString("2c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("3a"), new JSONString("3b"), new JSONString("3c"))))))), Arrays.asList("a", "b")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONString("1a"), new JSONString("1b"), new JSONString("1c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("2a"), new JSONString("2b"), new JSONString("2c")))), new JSONObject(new JSONField("b", new JSONArray(new JSONString("3a"), new JSONString("3b"), new JSONString("3c"))))))), Arrays.asList("a", "b")), Arrays.asList(new JSONArray(new JSONString("1a"), new JSONString("1b"), new JSONString("1c")), new JSONArray(new JSONString("2a"), new JSONString("2b"), new JSONString("2c")), new JSONArray(new JSONString("3a"), new JSONString("3b"), new JSONString("3c"))));
    // Test the case in which a three-level field references exactly one
    // element.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONObject(new JSONField("c", "foo")))))), Arrays.asList("a", "b", "c")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONObject(new JSONField("b", new JSONObject(new JSONField("c", "foo")))))), Arrays.asList("a", "b", "c")), Collections.singletonList(new JSONString("foo")));
    // Test the case in which the first and second elements of a three-level
    // field reference arrays.
    assertNotNull(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "first")), new JSONObject(new JSONField("c", "second"))))), new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "third")), new JSONObject(new JSONField("c", "fourth")))))))), Arrays.asList("a", "b", "c")));
    assertEquals(JSONObjectFilter.getValues(new JSONObject(new JSONField("a", new JSONArray(new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "first")), new JSONObject(new JSONField("c", "second"))))), new JSONObject(new JSONField("b", new JSONArray(new JSONObject(new JSONField("c", "third")), new JSONArray(new JSONObject(new JSONField("c", "fourth"))))))))), Arrays.asList("a", "b", "c")), Arrays.asList(new JSONString("first"), new JSONString("second"), new JSONString("third"), new JSONString("fourth")));
}
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 67 with JSONArray

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

the class ANDJSONObjectFilterTestCase method testZeroComponents.

/**
 * Tests the behavior of an AND filter with zero components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testZeroComponents() throws Exception {
    ANDJSONObjectFilter f = new ANDJSONObjectFilter();
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", JSONArray.EMPTY_ARRAY)));
    f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getANDFilters());
    assertEquals(f.getANDFilters(), Collections.emptyList());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "and");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("andFilters")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
    assertTrue(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "foo"), new JSONField("b", 1234), new JSONField("c", true), new JSONField("d", false), new JSONField("e", JSONNull.NULL), new JSONField("f", JSONArray.EMPTY_ARRAY), new JSONField("g", new JSONArray(new JSONString("foo"), new JSONString("bar"))), new JSONField("h", JSONObject.EMPTY_OBJECT), new JSONField("i", new JSONObject(new JSONField("j", "k"))))));
}
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 68 with JSONArray

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

the class ANDJSONObjectFilterTestCase method testMultipleComponents.

/**
 * Tests the behavior of an AND filter with multiple components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testMultipleComponents() throws Exception {
    final EqualsJSONObjectFilter f1 = new EqualsJSONObjectFilter("a", new JSONString("b"));
    final EqualsJSONObjectFilter f2 = new EqualsJSONObjectFilter("c", new JSONString("d"));
    final EqualsJSONObjectFilter f3 = new EqualsJSONObjectFilter("e", new JSONString("f"));
    ANDJSONObjectFilter f = new ANDJSONObjectFilter(f1, f2, f3);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", new JSONArray(f1.toJSONObject(), f2.toJSONObject(), f3.toJSONObject()))));
    f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getANDFilters());
    assertEquals(f.getANDFilters(), Arrays.asList(f1, f2, f3));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "and");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("andFilters")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "x"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("x", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("a"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"), new JSONField("c", "d"), new JSONField("e", "f"), new JSONField("g", "h"), new JSONField("i", "j"))));
}
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 69 with JSONArray

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

the class ContainsFieldJSONObjectFilterTestCase method testTopLevelFieldWithExpectedType.

/**
 * Tests the behavior of this filter for a top-level field with an expected
 * type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testTopLevelFieldWithExpectedType() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field");
    f.setExpectedType(ExpectedValueType.NON_EMPTY_ARRAY, ExpectedValueType.STRING);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "top-level-field"), new JSONField("expectedType", new JSONArray(new JSONString("non-empty-array"), new JSONString("string")))));
    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.of(ExpectedValueType.NON_EMPTY_ARRAY, ExpectedValueType.STRING));
    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()));
    assertFalse(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", "foo"), 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 70 with JSONArray

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

the class ContainsFieldJSONObjectFilterTestCase method testThirdLevelField.

/**
 * Tests the behavior of this filter for a third-level field.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testThirdLevelField() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field", "second-level-field", "third-level-field");
    f.setExpectedType(ExpectedValueType.BOOLEAN);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", new JSONArray(new JSONString("top-level-field"), new JSONString("second-level-field"), new JSONString("third-level-field"))), new JSONField("expectedType", "boolean")));
    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(), Arrays.asList("top-level-field", "second-level-field", "third-level-field"));
    assertNotNull(f.getExpectedType());
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.BOOLEAN));
    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);
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("third-level-field", true))))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("third-level-field", "not boolean"))))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("second-level-field", new JSONArray(new JSONObject(new JSONField("third-level-field", true))))))))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

JSONArray (com.unboundid.util.json.JSONArray)98 JSONObject (com.unboundid.util.json.JSONObject)89 JSONString (com.unboundid.util.json.JSONString)77 Test (org.testng.annotations.Test)72 JSONField (com.unboundid.util.json.JSONField)68 JSONValue (com.unboundid.util.json.JSONValue)27 JSONNumber (com.unboundid.util.json.JSONNumber)22 NotNull (com.unboundid.util.NotNull)20 ArrayList (java.util.ArrayList)19 LinkedHashMap (java.util.LinkedHashMap)18 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)7 Entry (com.unboundid.ldap.sdk.Entry)6 Map (java.util.Map)6 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)5 JSONBoolean (com.unboundid.util.json.JSONBoolean)5 JSONException (com.unboundid.util.json.JSONException)5 Date (java.util.Date)4 List (java.util.List)4 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3