Search in sources :

Example 6 with JSONField

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

the class JSONLogFieldSyntaxTestCase method testIncludeFields.

/**
 * Tests the behavior for the syntax when it is configured to only consider
 * a specified set of fields as sensitive, and therefore only those fields
 * will be redacted or tokenized when operating on components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testIncludeFields() throws Exception {
    final Set<String> includeFields = StaticUtils.setOf("a", "b", "c");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, includeFields, null);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertEquals(syntax.getIncludedSensitiveFields(), includeFields);
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertTrue(syntax.getExcludedSensitiveFields().isEmpty());
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", 5), new JSONField("c", true), new JSONField("d", "e"), new JSONField("f", new JSONObject(new JSONField("a", "g"), new JSONField("h", "i"))), new JSONField("j", new JSONArray(new JSONString("k"), new JSONObject(new JSONField("a", "l"), new JSONField("m", "n")))));
    assertNotNull(syntax.valueToSanitizedString(o));
    assertEquals(syntax.valueToSanitizedString(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"g\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"l\", \"m\":\"n\" } ] }");
    assertNotNull(syntax.redactComponents(o));
    assertEquals(syntax.redactComponents(o), "{ \"a\":\"{REDACTED}\", " + "\"b\":\"{REDACTED}\", " + "\"c\":\"{REDACTED}\", " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"{REDACTED}\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"{REDACTED}\", \"m\":\"n\" } ] }");
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    assertNotNull(syntax.tokenizeComponents(o, pepper));
    final JSONObject tokenizedObject = new JSONObject(syntax.tokenizeComponents(o, pepper));
    assertEquals(tokenizedObject.getFields().size(), 6);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertTrue(tokenizedObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("b"));
    assertTrue(tokenizedObject.getFieldAsString("b").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("c"));
    assertTrue(tokenizedObject.getFieldAsString("c").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertEquals(tokenizedObject.getFieldAsString("d"), "e");
    final JSONObject tokenizedFObject = tokenizedObject.getFieldAsObject("f");
    assertNotNull(tokenizedFObject);
    assertEquals(tokenizedFObject.getFields().size(), 2);
    assertNotNull(tokenizedFObject.getFieldAsString("a"));
    assertTrue(tokenizedFObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedFObject.getFieldAsString("h"));
    assertEquals(tokenizedFObject.getFieldAsString("h"), "i");
    final List<JSONValue> arrayElements = tokenizedObject.getFieldAsArray("j");
    assertNotNull(arrayElements);
    assertEquals(arrayElements.size(), 2);
    assertEquals(arrayElements.get(0), new JSONString("k"));
    final JSONObject arrayObject = (JSONObject) arrayElements.get(1);
    assertEquals(arrayObject.getFields().size(), 2);
    assertTrue(arrayObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertEquals(arrayObject.getFieldAsString("m"), "n");
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 7 with JSONField

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

the class RecentLoginHistoryTestCase method testDecodeMalformedFailureObject.

/**
 * Tests the behavior when trying to decode a JSON object with a malformed
 * set of failed attempts when the malformed attempt is an object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeMalformedFailureObject() throws Exception {
    final JSONObject o = new JSONObject(new JSONField("failed-attempts", new JSONArray(new JSONObject(new JSONField("malformed", true)))));
    new RecentLoginHistory(o);
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) Test(org.testng.annotations.Test)

Example 8 with JSONField

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

the class ANDJSONObjectFilterTestCase method testOneComponent.

/**
 * Tests the behavior of an AND filter with one component.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testOneComponent() throws Exception {
    final EqualsJSONObjectFilter equalsFilter = new EqualsJSONObjectFilter("a", new JSONString("b"));
    ANDJSONObjectFilter f = new ANDJSONObjectFilter(equalsFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "and"), new JSONField("andFilters", new JSONArray(equalsFilter.toJSONObject()))));
    f = (ANDJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getANDFilters());
    assertEquals(f.getANDFilters(), Collections.singletonList(equalsFilter));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "and");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("andFilters")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
    assertFalse(f.matchesJSONObject(JSONObject.EMPTY_OBJECT));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "x"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("x", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", new JSONArray(new JSONNumber(1234), new JSONString("b"), JSONNull.NULL)))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
}
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) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 9 with JSONField

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

the class ContainsFieldJSONObjectFilterTestCase method testSecondLevelField.

/**
 * Tests the behavior of this filter for a second-level field.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSecondLevelField() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field", "second-level-field");
    f.setExpectedType(ExpectedValueType.STRING);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", new JSONArray(new JSONString("top-level-field"), new JSONString("second-level-field"))), new JSONField("expectedType", "string")));
    assertNotNull(JSONObjectFilter.decode(f.toJSONObject()));
    assertTrue(JSONObjectFilter.decode(f.toJSONObject()) instanceof ContainsFieldJSONObjectFilter);
    f = (ContainsFieldJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f.getField());
    assertFalse(f.getField().isEmpty());
    assertEquals(f.getField(), Arrays.asList("top-level-field", "second-level-field"));
    assertNotNull(f.getExpectedType());
    assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.STRING));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "containsField");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("expectedType")));
    assertNotNull(f.toString());
    final StringBuilder toStringBuffer = new StringBuilder();
    f.toString(toStringBuffer);
    assertTrue(toStringBuffer.length() > 0);
    assertEquals(toStringBuffer.toString(), f.toString());
    final JSONObject toJSONObject = f.toJSONObject();
    final JSONObject toStringObject = new JSONObject(f.toString());
    assertEquals(toStringObject, toJSONObject);
    assertFalse(f.matchesJSONObject(new JSONObject()));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("second-level-field", "foo"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("second-level-field"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", "foo"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", 1234))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("second-level-field", "foo"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("second-level-field", "foo")), new JSONObject(new JSONField("second-level-field", "bar")))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("some-other-field", "foo")), new JSONObject(new JSONField("second-level-field", "bar")))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("some-other-field", "foo")), new JSONObject(new JSONField("another-field", "bar")))))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 10 with JSONField

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

the class ContainsFieldJSONObjectFilterTestCase method testTopLevelFieldWithoutExpectedType.

/**
 * Tests the behavior of this filter for a top-level field without an
 * expected type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testTopLevelFieldWithoutExpectedType() throws Exception {
    ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "top-level-field")));
    assertNotNull(JSONObjectFilter.decode(f.toJSONObject()));
    assertTrue(JSONObjectFilter.decode(f.toJSONObject()) instanceof ContainsFieldJSONObjectFilter);
    f = (ContainsFieldJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f.getField());
    assertFalse(f.getField().isEmpty());
    assertEquals(f.getField(), Collections.singletonList("top-level-field"));
    assertNotNull(f.getExpectedType());
    assertEquals(f.getExpectedType(), EnumSet.allOf(ExpectedValueType.class));
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "containsField");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Collections.singletonList("expectedType")));
    assertNotNull(f.toString());
    final StringBuilder toStringBuffer = new StringBuilder();
    f.toString(toStringBuffer);
    assertTrue(toStringBuffer.length() > 0);
    assertEquals(toStringBuffer.toString(), f.toString());
    final JSONObject toJSONObject = f.toJSONObject();
    final JSONObject toStringObject = new JSONObject(f.toString());
    assertEquals(toStringObject, toJSONObject);
    assertFalse(f.matchesJSONObject(new JSONObject()));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", "foo"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONString("foo"), new JSONString("bar"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", 1234), new JSONField("another-top-level-field", 5678))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("embeddedObject", new JSONObject(new JSONField("top-level-field", false))))));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) 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