Search in sources :

Example 41 with JSONField

use of com.unboundid.util.json.JSONField 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 JSONField

use of com.unboundid.util.json.JSONField in project ssam by pingidentity.

the class SSAMInstaller method writeLDAPConnectionDetails.

/**
 * Writes the LDAP connection details JSON file that governs how SSAM
 * is configured to communicate with the server.
 */
private void writeLDAPConnectionDetails(final File details) throws IOException {
    // Generate the server-details section.
    JSONObject serverDetails = new JSONObject(new JSONField("single-server", new JSONObject(new JSONField("address", localHostName), new JSONField("port", ldapPortArg.getValue()))));
    // Generate the communication-security section.
    List<JSONField> fields = new ArrayList<>();
    if (useSSLArg.isPresent() || useStartTLSArg.isPresent()) {
        if (useSSLArg.isPresent()) {
            fields.add(new JSONField("security-type", "SSL"));
        } else {
            fields.add(new JSONField("security-type", "StartTLS"));
        }
        if (trustStorePathArg.isPresent()) {
            fields.add(new JSONField("trust-store-file", trustStorePathArg.getValue().getCanonicalPath()));
            fields.add(new JSONField("trust-store-type", "JKS"));
            fields.add(new JSONField("verify-address-in-certificate", true));
        } else {
            fields.add(new JSONField("trust-all-certificates", true));
        }
    } else {
        fields.add(new JSONField("security-type", "none"));
    }
    JSONObject communicationSecurity = new JSONObject(fields.toArray(new JSONField[fields.size()]));
    // Generate the authentication-details section.
    JSONObject authenticationDetails = new JSONObject(new JSONField("authentication-type", "simple"), new JSONField("dn", ssamUserDN.toString()), new JSONField("password-file", ssamUserPasswordConfigFile.getCanonicalPath()));
    // ldap-connection-details.json
    JSONObject ldapConnectionDetails = new JSONObject(new JSONField("server-details", serverDetails), new JSONField("communication-security", communicationSecurity), new JSONField("authentication-details", authenticationDetails));
    writeToFile(details, ldapConnectionDetails.toString());
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONField(com.unboundid.util.json.JSONField)

Example 43 with JSONField

use of com.unboundid.util.json.JSONField 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 44 with JSONField

use of com.unboundid.util.json.JSONField 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 45 with JSONField

use of com.unboundid.util.json.JSONField 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)

Aggregations

JSONField (com.unboundid.util.json.JSONField)97 JSONObject (com.unboundid.util.json.JSONObject)97 Test (org.testng.annotations.Test)91 JSONArray (com.unboundid.util.json.JSONArray)68 JSONString (com.unboundid.util.json.JSONString)66 JSONNumber (com.unboundid.util.json.JSONNumber)20 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)8 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)7 Date (java.util.Date)7 Entry (com.unboundid.ldap.sdk.Entry)5 JSONException (com.unboundid.util.json.JSONException)5 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 JSONBoolean (com.unboundid.util.json.JSONBoolean)4 JSONValue (com.unboundid.util.json.JSONValue)4 CompareRequest (com.unboundid.ldap.sdk.CompareRequest)3 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)3 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)3 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3 ArrayList (java.util.ArrayList)3