Search in sources :

Example 6 with JSONObjectReader

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

the class LDIFSearchTestCase method testJSONOutputFormat.

/**
 * Tests the behavior when using the JSON output format.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testJSONOutputFormat() throws Exception {
    final File ldifFile = createTempFile("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User");
    final File outputFile = createTempFile();
    assertTrue(outputFile.exists());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    assertEquals(LDIFSearch.main(out, out, "--ldifFile", ldifFile.getAbsolutePath(), "--outputFile", outputFile.getAbsolutePath(), "--baseDN", "ou=People,dc=example,dc=com", "--scope", "sub", "--outputFormat", "json", "(objectClass=person)", "objectClass", "uid", "cn"), ResultCode.SUCCESS);
    try (FileInputStream inputStream = new FileInputStream(outputFile);
        JSONObjectReader jsonReader = new JSONObjectReader(inputStream)) {
        JSONObject jsonObject = jsonReader.readObject();
        assertNotNull(jsonObject);
        assertEquals(jsonObject, new JSONObject(new JSONField("result-type", "entry"), new JSONField("dn", "uid=test.user,ou=People,dc=example,dc=com"), new JSONField("attributes", new JSONArray(new JSONObject(new JSONField("name", "objectClass"), new JSONField("values", new JSONArray(new JSONString("top"), new JSONString("person"), new JSONString("organizationalPerson"), new JSONString("inetOrgPerson")))), new JSONObject(new JSONField("name", "uid"), new JSONField("values", new JSONArray(new JSONString("test.user")))), new JSONObject(new JSONField("name", "cn"), new JSONField("values", new JSONArray(new JSONString("Test User"))))))));
        jsonObject = jsonReader.readObject();
        assertNull(jsonObject);
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) JSONField(com.unboundid.util.json.JSONField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 7 with JSONObjectReader

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

the class LDAPResultCodeTestCase method testJSON.

/**
 * Tests the behavior when using the JSON output format.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testJSON() throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    assertEquals(LDAPResultCode.main(out, err, "--list", "--output-format", "json"), ResultCode.SUCCESS);
    final byte[] outputBytes = out.toByteArray();
    assertTrue(outputBytes.length > 0);
    assertTrue(out.toByteArray().length > 0);
    try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputBytes);
        JSONObjectReader jsonObjectReader = new JSONObjectReader(byteArrayInputStream)) {
        int numResultCodes = 0;
        while (true) {
            final JSONObject o = jsonObjectReader.readObject();
            if (o == null) {
                break;
            }
            numResultCodes++;
            final String name = o.getFieldAsString("name");
            assertNotNull(name);
            final int intValue = o.getFieldAsInteger("int-value");
            final ResultCode rc = ResultCode.valueOf(intValue);
            assertNotNull(rc);
            assertTrue(name.equalsIgnoreCase(rc.getName()));
        }
        assertEquals(numResultCodes, ResultCode.values().length);
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResultCode(com.unboundid.ldap.sdk.ResultCode) Test(org.testng.annotations.Test)

Example 8 with JSONObjectReader

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

the class AuditLogMessage method readJSONObject.

/**
 * Reads a JSON object from the provided input stream.
 *
 * @param  logMessageLines  The lines that comprise the log message.  It must
 *                          not be {@code null} or empty.
 * @param  propertyName     The name of the property whose value is expected
 *                          to be a JSON object.  It must not be {@code null}.
 * @param  inputStream      The input stream from which to read the JSON
 *                          object.  It must not be {@code null}.
 *
 * @return  The JSON object that was read.
 *
 * @throws  AuditLogException  If a problem is encountered while trying to
 *                             read the JSON object.
 */
@NotNull()
private static JSONObject readJSONObject(@NotNull final List<String> logMessageLines, @NotNull final String propertyName, @NotNull final ByteArrayInputStream inputStream) throws AuditLogException {
    final JSONObject jsonObject;
    try {
        final JSONObjectReader reader = new JSONObjectReader(inputStream, false);
        jsonObject = reader.readObject();
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new AuditLogException(logMessageLines, ERR_AUDIT_LOG_MESSAGE_ERROR_READING_JSON_OBJECT.get(propertyName, StaticUtils.getExceptionMessage(e)), e);
    }
    readSpacesAndSemicolon(logMessageLines, propertyName, inputStream);
    return jsonObject;
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONObjectReader(com.unboundid.util.json.JSONObjectReader) ParseException(java.text.ParseException) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONObject (com.unboundid.util.json.JSONObject)8 JSONObjectReader (com.unboundid.util.json.JSONObjectReader)8 FileInputStream (java.io.FileInputStream)4 Test (org.testng.annotations.Test)4 File (java.io.File)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 DN (com.unboundid.ldap.sdk.DN)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 NotNull (com.unboundid.util.NotNull)1 JSONArray (com.unboundid.util.json.JSONArray)1 JSONField (com.unboundid.util.json.JSONField)1 JSONString (com.unboundid.util.json.JSONString)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1