Search in sources :

Example 41 with JSONArray

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()));
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) Test(org.testng.annotations.Test)

Example 42 with JSONArray

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()));
}
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) Test(org.testng.annotations.Test)

Example 43 with JSONArray

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));
}
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) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 44 with JSONArray

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());
}
Also used : StringLogFieldSyntax(com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.StringLogFieldSyntax) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) Test(org.testng.annotations.Test)

Example 45 with JSONArray

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());
}
Also used : StringLogFieldSyntax(com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.StringLogFieldSyntax) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) 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