use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class GetRecentLoginHistoryResponseControlTestCase method testDecodeControlValueNotValidLoginHistory.
/**
* Tests the behavior when trying to decode a control whose value is a JSON
* object that can't be parsed as a valid recent login history.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeControlValueNotValidLoginHistory() throws Exception {
final JSONObject o = new JSONObject(new JSONField("successful-attempts", new JSONArray(new JSONObject(new JSONField("malformed", true)))));
new GetRecentLoginHistoryResponseControl("1.3.6.1.4.1.30221.2.5.62", false, new ASN1OctetString(o.toSingleLineString()));
}
use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testValueToStrings.
/**
* Tests the behavior for the {@code valueToStrings} method.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testValueToStrings() throws Exception {
// Simple values.
assertEquals(JSONLogMessage.valueToStrings(JSONBoolean.TRUE), Collections.singletonList("true"));
assertEquals(JSONLogMessage.valueToStrings(JSONBoolean.FALSE), Collections.singletonList("false"));
assertEquals(JSONLogMessage.valueToStrings(JSONNull.NULL), Collections.singletonList("null"));
assertEquals(JSONLogMessage.valueToStrings(new JSONNumber(1234L)), Collections.singletonList("1234"));
assertEquals(JSONLogMessage.valueToStrings(new JSONNumber(1.5d)), Collections.singletonList("1.5"));
assertEquals(JSONLogMessage.valueToStrings(new JSONString("foo")), Collections.singletonList("foo"));
// A JSON object value.
final JSONObject o = new JSONObject(new JSONField("foo", "a"), new JSONField("bar", "b"));
assertEquals(JSONLogMessage.valueToStrings(o), Collections.singletonList(o.toSingleLineString()));
// An array of strings.
final JSONArray stringArray = new JSONArray(new JSONString("one"), new JSONString("two"), new JSONString("three"));
assertEquals(JSONLogMessage.valueToStrings(stringArray), Arrays.asList("one", "two", "three"));
// An array that mixes values of different types.
final JSONArray mixedTypeArray = new JSONArray(JSONBoolean.TRUE, new JSONNumber(0L), new JSONString("foo"), JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT);
assertEquals(JSONLogMessage.valueToStrings(mixedTypeArray), Arrays.asList("true", "0", "foo", JSONArray.EMPTY_ARRAY.toSingleLineString(), JSONObject.EMPTY_OBJECT.toSingleLineString()));
}
use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testGetFirstValue.
/**
* Tests the methods for retrieving the first value for a given field.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetFirstValue() throws Exception {
// Test with a field whose value is a single string.
JSONObject o = createMinimalMessageObject(CONNECT, null, createField(CONNECT_FROM_ADDRESS, "1.2.3.4"));
JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(CONNECT_FROM_ADDRESS), new JSONString("1.2.3.4"));
// Test with a field whose value is a single Boolean.
o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, true));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(UNCACHED_DATA_ACCESSED), JSONBoolean.TRUE);
// Test with a field whose value is a non-empty array of strings.
o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4", "5.6.7.8"));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(REQUEST_CONTROL_OIDS), new JSONString("1.2.3.4"));
// Test with a field whose value is a non-empty array of non-strings.
final LogField testField = new LogField("testField", IntegerLogFieldSyntax.getInstance());
o = createMinimalMessageObject(CONNECT, null, new JSONField("testField", new JSONArray(new JSONNumber(1L), new JSONNumber(2L), new JSONNumber(3L), new JSONNumber(4L))));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(testField), new JSONNumber(1L));
// Test with a field whose value is an empty array.
o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS));
m = new JSONConnectAccessLogMessage(o);
assertNull(m.getFirstValue(REQUEST_CONTROL_OIDS));
}
use of com.unboundid.util.json.JSONArray 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.JSONArray 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());
}
Aggregations