Search in sources :

Example 46 with JSONArray

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

the class JSONLogFieldSyntaxTestCase method testBasicSyntax.

/**
 * Tests the basic functionality of the syntax.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasicSyntax() throws Exception {
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, null, null);
    assertEquals(syntax.getMaxStringLengthCharacters(), 10);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertTrue(syntax.getIncludedSensitiveFields().isEmpty());
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertTrue(syntax.getExcludedSensitiveFields().isEmpty());
    assertNotNull(syntax.getSyntaxName());
    assertEquals(syntax.getSyntaxName(), "json");
    assertNotNull(syntax.valueToSanitizedString(JSONObject.EMPTY_OBJECT));
    assertEquals(syntax.valueToSanitizedString(JSONObject.EMPTY_OBJECT), "{ }");
    final JSONObject singleFieldObject = new JSONObject(new JSONField("foo", "bar"));
    assertNotNull(syntax.valueToSanitizedString(singleFieldObject));
    assertEquals(syntax.valueToSanitizedString(singleFieldObject), "{ \"foo\":\"bar\" }");
    final JSONObject multiFieldObject = new JSONObject(new JSONField("a", "b"), new JSONField("c", "ThisIsALongerValue"), new JSONField("d", 123), new JSONField("e", 4.5), new JSONField("f", true), new JSONField("g", JSONNull.NULL), new JSONField("h", new JSONArray(new JSONString("i"), JSONBoolean.FALSE, new JSONNumber(678), JSONNull.NULL, new JSONObject(new JSONField("j", "k")))), new JSONField("l", new JSONObject(new JSONField("m", "n"))));
    assertNotNull(syntax.valueToSanitizedString(multiFieldObject));
    assertEquals(syntax.valueToSanitizedString(multiFieldObject), "{ \"a\":\"b\", " + "\"c\":\"ThisIsALon{8 more characters}\", " + "\"d\":123, " + "\"e\":4.5, " + "\"f\":true, " + "\"g\":null, " + "\"h\":[ " + "\"i\", " + "false, " + "678, " + "null, " + "{ \"j\":\"k\" } ], " + "\"l\":{ \"m\":\"n\" } }");
    assertNotNull(syntax.parseValue(singleFieldObject.toSingleLineString()));
    assertEquals(syntax.parseValue(singleFieldObject.toSingleLineString()), singleFieldObject);
    try {
        syntax.parseValue("{REDACTED}");
        fail("Expected an exception when trying to parse a redacted value.");
    } catch (final RedactedValueException e) {
    // This was expected.
    }
    try {
        syntax.parseValue("{TOKENIZED:1234567890ABCDEF}");
        fail("Expected an exception when trying to parse a tokenized value.");
    } catch (final TokenizedValueException e) {
    // This was expected
    }
    try {
        syntax.parseValue("malformed");
        fail("Expected an exception when trying to parse a malformed value.");
    } catch (final LogSyntaxException e) {
        // This was expected.
        assertFalse((e instanceof RedactedValueException) || (e instanceof TokenizedValueException));
    }
    assertFalse(syntax.valueStringIsCompletelyRedacted(singleFieldObject.toSingleLineString()));
    assertTrue(syntax.valueStringIsCompletelyRedacted("{ \"redacted\":\"{REDACTED}\" }"));
    assertTrue(syntax.valueStringIsCompletelyRedacted("{REDACTED}"));
    assertTrue(syntax.completelyRedactedValueConformsToSyntax());
    assertNotNull(syntax.redactEntireValue());
    assertEquals(syntax.redactEntireValue(), "{ \"redacted\":\"{REDACTED}\" }");
    JSONObject tokenizedObject = new JSONObject(syntax.redactEntireValue());
    assertTrue(syntax.supportsRedactedComponents());
    assertTrue(syntax.valueWithRedactedComponentsConformsToSyntax());
    assertNotNull(syntax.redactComponents(singleFieldObject));
    assertEquals(syntax.redactComponents(singleFieldObject), "{ \"foo\":\"{REDACTED}\" }");
    tokenizedObject = new JSONObject(syntax.redactComponents(singleFieldObject));
    assertNotNull(syntax.redactComponents(multiFieldObject));
    assertEquals(syntax.redactComponents(multiFieldObject), "{ \"a\":\"{REDACTED}\", " + "\"c\":\"{REDACTED}\", " + "\"d\":\"{REDACTED}\", " + "\"e\":\"{REDACTED}\", " + "\"f\":\"{REDACTED}\", " + "\"g\":\"{REDACTED}\", " + "\"h\":\"{REDACTED}\", " + "\"l\":\"{REDACTED}\" }");
    tokenizedObject = new JSONObject(syntax.redactComponents(multiFieldObject));
    assertFalse(syntax.valueStringIsCompletelyTokenized(singleFieldObject.toSingleLineString()));
    assertTrue(syntax.valueStringIsCompletelyTokenized("{ \"tokenized\":\"{TOKENIZED:abcdef}\" }"));
    assertTrue(syntax.valueStringIsCompletelyTokenized("{TOKENIZED:abcdef}"));
    assertTrue(syntax.completelyTokenizedValueConformsToSyntax());
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    String tokenizedString = syntax.tokenizeEntireValue(singleFieldObject, pepper);
    assertNotNull(tokenizedString);
    assertTrue(tokenizedString.startsWith("{ \"tokenized\":\"{TOKENIZED:"));
    assertTrue(tokenizedString.endsWith("}\" }"));
    tokenizedObject = new JSONObject(tokenizedString);
    assertTrue(syntax.supportsTokenizedComponents());
    assertTrue(syntax.valueWithTokenizedComponentsConformsToSyntax());
    tokenizedString = syntax.tokenizeComponents(singleFieldObject, pepper);
    tokenizedObject = new JSONObject(tokenizedString);
    assertEquals(tokenizedObject.getFields().size(), 1);
    assertNotNull(tokenizedObject.getFieldAsString("foo"));
    assertTrue(tokenizedObject.getFieldAsString("foo").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("foo").endsWith("}"));
    tokenizedString = syntax.tokenizeComponents(multiFieldObject, pepper);
    tokenizedObject = new JSONObject(tokenizedString);
    assertEquals(tokenizedObject.getFields().size(), 8);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertTrue(tokenizedObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("a").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("c"));
    assertTrue(tokenizedObject.getFieldAsString("c").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("c").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertTrue(tokenizedObject.getFieldAsString("d").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("d").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("e"));
    assertTrue(tokenizedObject.getFieldAsString("e").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("e").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("f"));
    assertTrue(tokenizedObject.getFieldAsString("f").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("f").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("g"));
    assertTrue(tokenizedObject.getFieldAsString("g").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("g").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("h"));
    assertTrue(tokenizedObject.getFieldAsString("h").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("h").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("l"));
    assertTrue(tokenizedObject.getFieldAsString("l").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("l").endsWith("}"));
}
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 47 with JSONArray

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

the class JSONLogFieldSyntaxTestCase method testExcludeFields.

/**
 * Tests the behavior for the syntax when it is configured to exclude a
 * specified set of fields as sensitive, and therefore only those fields will
 * not be redacted or tokenized when operating on components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testExcludeFields() throws Exception {
    final Set<String> excludeFields = StaticUtils.setOf("a", "b", "c");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, null, excludeFields);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertTrue(syntax.getIncludedSensitiveFields().isEmpty());
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertEquals(syntax.getExcludedSensitiveFields(), excludeFields);
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", 5), new JSONField("c", true), new JSONField("d", "e"), new JSONField("f", new JSONObject(new JSONField("a", "g"), new JSONField("h", "i"))), new JSONField("j", new JSONArray(new JSONString("k"), new JSONObject(new JSONField("a", "l"), new JSONField("m", "n")))));
    assertNotNull(syntax.valueToSanitizedString(o));
    assertEquals(syntax.valueToSanitizedString(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"g\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"l\", \"m\":\"n\" } ] }");
    assertNotNull(syntax.redactComponents(o));
    assertEquals(syntax.redactComponents(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"{REDACTED}\", " + "\"f\":\"{REDACTED}\", " + "\"j\":\"{REDACTED}\" }");
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    assertNotNull(syntax.tokenizeComponents(o, pepper));
    final JSONObject tokenizedObject = new JSONObject(syntax.tokenizeComponents(o, pepper));
    assertEquals(tokenizedObject.getFields().size(), 6);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertEquals(tokenizedObject.getFieldAsString("a"), "foo");
    assertNotNull(tokenizedObject.getFieldAsInteger("b"));
    assertEquals(tokenizedObject.getFieldAsInteger("b").intValue(), 5);
    assertNotNull(tokenizedObject.getFieldAsBoolean("c"));
    assertTrue(tokenizedObject.getFieldAsBoolean("c").booleanValue());
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertTrue(tokenizedObject.getFieldAsString("d").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("f"));
    assertTrue(tokenizedObject.getFieldAsString("f").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("j"));
    assertTrue(tokenizedObject.getFieldAsString("j").startsWith("{TOKENIZED:"));
}
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 48 with JSONArray

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

the class LessThanJSONObjectFilterTestCase 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 {
    LessThanJSONObjectFilter f = new LessThanJSONObjectFilter("test-field", "foo");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", "foo")));
    f = (LessThanJSONObjectFilter) 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(), "lessThan");
    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", "boo"))));
    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", "boo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "flo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fold"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fr"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FLO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOLD"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FR"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "BO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "B"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("fob"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("fool"))))));
    assertFalse(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("too"), new JSONString("boo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("FOOL"))))));
    assertFalse(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("TOO"), new JSONString("BOO"))))));
    f.setMatchAllElements(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true)));
    f = (LessThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.matchAllElements());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    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", "boo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "flo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fold"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fr"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FLO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOLD"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FR"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "BO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "B"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("fob"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), 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("too"), new JSONString("boo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), 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("TOO"), new JSONString("BOO"))))));
    f.setAllowEquals(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true), new JSONField("allowEquals", true)));
    f = (LessThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.allowEquals());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    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", "boo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "flo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fold"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fr"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FLO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOLD"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FR"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "BO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "B"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("fob"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), 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("too"), new JSONString("boo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), 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("TOO"), new JSONString("BOO"))))));
    f.setCaseSensitive(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true), new JSONField("allowEquals", true), new JSONField("caseSensitive", true)));
    f = (LessThanJSONObjectFilter) 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", "boo"))));
    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", "flo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fold"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "BOO"))));
    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"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOLD"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "BO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "B"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("fold"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("foo"), new JSONString("fold"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("too"), new JSONString("goo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), new JSONString("FOLD"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("FOO"), new JSONString("FOLD"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("too"), new JSONString("BOO"))))));
    f.setAllowEquals(false);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", "foo"), new JSONField("matchAllElements", true), new JSONField("caseSensitive", true)));
    f = (LessThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertFalse(f.allowEquals());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    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", "boo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "flo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fool"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fold"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "fr"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "f"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "bo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "boo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FLO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOOL"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FOLD"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "FR"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "F"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "BO"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "B"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), new JSONString("fob"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("boo"), 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("too"), new JSONString("boo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("BOO"), 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("TOO"), new JSONString("BOO"))))));
}
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 49 with JSONArray

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

the class LessThanJSONObjectFilterTestCase method testLessThanNumber.

/**
 * Tests the behavior of the less-than filter with a numeric value.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testLessThanNumber() throws Exception {
    LessThanJSONObjectFilter f = new LessThanJSONObjectFilter("test-field", 5678);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", 5678)));
    f = (LessThanJSONObjectFilter) 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(), "lessThan");
    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", 5677))));
    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", 5677))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 9999))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 10000))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 100))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677.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", "5677"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5678), new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677), new JSONNumber(1000), new JSONString("foo"), JSONNull.NULL)))));
    f.setMatchAllElements(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", 5678), new JSONField("matchAllElements", true)));
    f = (LessThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.matchAllElements());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677))));
    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", 5677))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 9999))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 10000))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 100))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677.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", "5677"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5678), new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677), new JSONNumber(1000))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677), new JSONNumber(1000), new JSONString("foo"), JSONNull.NULL)))));
    f.setAllowEquals(true);
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "lessThan"), new JSONField("field", "test-field"), new JSONField("value", 5678), new JSONField("matchAllElements", true), new JSONField("allowEquals", true)));
    f = (LessThanJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.allowEquals());
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677))));
    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", 5677))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 9999))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 10000))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 999))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 100))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", 5677.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", "5677"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5678), new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(1234), new JSONNumber(5677), new JSONNumber(1000))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677), new JSONNumber(1000))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONNumber(5677), new JSONNumber(1000), 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 50 with JSONArray

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

the class ORJSONObjectFilterTestCase method testOneComponent.

/**
 * Tests the behavior of an OR filter with one component.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testOneComponent() throws Exception {
    final EqualsJSONObjectFilter equalsFilter = new EqualsJSONObjectFilter("a", new JSONString("b"));
    ORJSONObjectFilter f = new ORJSONObjectFilter(equalsFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", new JSONArray(equalsFilter.toJSONObject()))));
    f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getORFilters());
    assertEquals(f.getORFilters(), Collections.singletonList(equalsFilter));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "or");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("orFilters")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("exclusive")));
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(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"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("b"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    f.setExclusive(true);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "or"), new JSONField("orFilters", new JSONArray(equalsFilter.toJSONObject())), new JSONField("exclusive", true)));
    f = (ORJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.exclusive());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(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"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("b"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

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