use of com.unboundid.util.json.JSONNumber in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testGetStringSet.
/**
* Tests the methods for getting a string list from a JSON object.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetStringSet() throws Exception {
// Test with a field whose value is an array of strings.
JSONObject o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4", "5.6.7.8"));
JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringSet(REQUEST_CONTROL_OIDS), StaticUtils.setOf("1.2.3.4", "5.6.7.8"));
// Test with a field whose value is a single string.
o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4"));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringSet(REQUEST_CONTROL_OIDS), Collections.singleton("1.2.3.4"));
// Test with a field whose value is a single number.
o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, 1L));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringSet(OPERATION_ID), Collections.emptySet());
// Test with a field whose value is an array of non-strings.
final LogField testField = new LogField("testField", new StringLogFieldSyntax(100));
o = createMinimalMessageObject(CONNECT, null, new JSONField(testField.getFieldName(), new JSONArray(JSONBoolean.TRUE, new JSONNumber("1234"))));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringSet(testField), Collections.emptySet());
// Test with a nonexistent field.
o = createMinimalMessageObject(CONNECT, null);
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringSet(REQUEST_CONTROL_OIDS), Collections.emptySet());
}
use of com.unboundid.util.json.JSONNumber in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testGetStringList.
/**
* Tests the methods for getting a string list from a JSON object.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetStringList() throws Exception {
// Test with a field whose value is an array of strings.
JSONObject o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4", "5.6.7.8"));
JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringList(REQUEST_CONTROL_OIDS), Arrays.asList("1.2.3.4", "5.6.7.8"));
// Test with a field whose value is a single string.
o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4"));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringList(REQUEST_CONTROL_OIDS), Collections.singletonList("1.2.3.4"));
// Test with a field whose value is a single number.
o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, 1L));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringList(OPERATION_ID), Collections.emptyList());
// Test with a field whose value is an array of non-strings.
final LogField testField = new LogField("testField", new StringLogFieldSyntax(100));
o = createMinimalMessageObject(CONNECT, null, new JSONField(testField.getFieldName(), new JSONArray(JSONBoolean.TRUE, new JSONNumber("1234"))));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringList(testField), Collections.emptyList());
// Test with a nonexistent field.
o = createMinimalMessageObject(CONNECT, null);
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringList(REQUEST_CONTROL_OIDS), Collections.emptyList());
}
use of com.unboundid.util.json.JSONNumber 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("}"));
}
use of com.unboundid.util.json.JSONNumber in project ldapsdk by pingidentity.
the class LessThanJSONObjectFilterTestCase method testGetAndSetValue.
/**
* Provides test coverage for the methods that can be used to get and set the
* value.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetAndSetValue() throws Exception {
final LessThanJSONObjectFilter f = new LessThanJSONObjectFilter("a", 1234);
assertEquals(f.getValue(), new JSONNumber(1234));
f.setValue(5678);
assertEquals(f.getValue(), new JSONNumber(5678));
f.setValue(1234.5);
assertEquals(f.getValue(), new JSONNumber(1234.5));
f.setValue("foo");
assertEquals(f.getValue(), new JSONString("foo"));
f.setValue(new JSONNumber("1.234e3"));
assertEquals(f.getValue(), new JSONNumber(1234));
f.setValue(new JSONString("1.234e3"));
assertEquals(f.getValue(), new JSONString("1.234e3"));
try {
f.setValue((String) null);
fail("Expected an exception from setValue String null");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
try {
f.setValue((JSONValue) null);
fail("Expected an exception from setValue JSONValue null");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
try {
f.setValue(JSONBoolean.TRUE);
fail("Expected an exception from setValue true");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
}
use of com.unboundid.util.json.JSONNumber 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)))));
}
Aggregations