Search in sources :

Example 6 with LogField

use of com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField in project ldapsdk by pingidentity.

the class JSONLogMessageTestCase method testGetGeneralizedTime.

/**
 * Tests the methods for getting a generalized time value from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetGeneralizedTime() throws Exception {
    // Test with a field whose value is a valid generalized time string.
    final LogField testField = new LogField("test-gt", GeneralizedTimeLogFieldSyntax.getInstance());
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(testField, StaticUtils.encodeGeneralizedTime(DEFAULT_TIMESTAMP_DATE)));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getGeneralizedTime(testField), DEFAULT_TIMESTAMP_DATE);
    assertEquals(m.getGeneralizedTimeNoThrow(testField), DEFAULT_TIMESTAMP_DATE);
    // Test with a field that is missing.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertNull(m.getGeneralizedTime(testField));
    assertNull(m.getGeneralizedTimeNoThrow(testField));
    // Test with a field that has an invalid string value.
    o = createMinimalMessageObject(CONNECT, null, createField(testField, "invalid"));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getGeneralizedTime(testField);
        fail("Expected an exception for an invalid string value.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getGeneralizedTimeNoThrow(testField));
    // Test with a field that has a non-string value.
    o = createMinimalMessageObject(CONNECT, null, createField(testField, 1234L));
    m = new JSONConnectAccessLogMessage(o);
    try {
        m.getGeneralizedTime(testField);
        fail("Expected an exception for an invalid value type.");
    } catch (final LogException e) {
    // This was expected.
    }
    assertNull(m.getGeneralizedTimeNoThrow(UNCACHED_DATA_ACCESSED));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) Test(org.testng.annotations.Test)

Example 7 with LogField

use of com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField 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)

Example 8 with LogField

use of com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField in project ldapsdk by pingidentity.

the class JSONFormattedAccessLogFields method createField.

/**
 * Creates a new log field with the provided name and syntax and registers it
 * in the {@link #DEFINED_FIELDS} map.
 *
 * @param  constantName  The name for the constant in which the field is
 *                       defined.  It must not be {@code null} or empty.
 * @param  fieldName     The name for the field as it appears in log messages.
 *                       It must not be {@code null} or empty.
 * @param  fieldSyntax   The expected syntax for the field.  It must not be
 *                       {@code null} or empty.
 *
 * @return  The log field that was created.
 */
@NotNull()
private static LogField createField(@NotNull final String constantName, @NotNull final String fieldName, @NotNull final LogFieldSyntax<?> fieldSyntax) {
    final LogField field = new LogField(fieldName, constantName, fieldSyntax);
    DEFINED_FIELDS.put(constantName, field);
    return field;
}
Also used : LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) NotNull(com.unboundid.util.NotNull)

Example 9 with LogField

use of com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField in project ldapsdk by pingidentity.

the class TextFormattedAccessLogFields method createField.

/**
 * Creates a new log field with the provided name and syntax and registers it
 * in the {@link #DEFINED_FIELDS} map.
 *
 * @param  constantName  The name for the constant in which the field is
 *                       defined.  It must not be {@code null} or empty.
 * @param  fieldName     The name for the field as it appears in log messages.
 *                       It must not be {@code null} or empty.
 * @param  fieldSyntax   The expected syntax for the field.  It must not be
 *                       {@code null} or empty.
 *
 * @return  The log field that was created.
 */
@NotNull()
private static LogField createField(@NotNull final String constantName, @NotNull final String fieldName, @NotNull final LogFieldSyntax<?> fieldSyntax) {
    final LogField field = new LogField(fieldName, constantName, fieldSyntax);
    DEFINED_FIELDS.put(constantName, field);
    return field;
}
Also used : LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) NotNull(com.unboundid.util.NotNull)

Aggregations

LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)9 Test (org.testng.annotations.Test)7 JSONObject (com.unboundid.util.json.JSONObject)5 JSONArray (com.unboundid.util.json.JSONArray)3 JSONField (com.unboundid.util.json.JSONField)3 JSONNumber (com.unboundid.util.json.JSONNumber)3 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)2 StringLogFieldSyntax (com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.StringLogFieldSyntax)2 NotNull (com.unboundid.util.NotNull)2 Field (java.lang.reflect.Field)2 JSONString (com.unboundid.util.json.JSONString)1