Search in sources :

Example 26 with JSONField

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

the class SubstringJSONObjectFilterTestCase method testEndsWith.

/**
 * Tests the behavior of a filter that only uses the endsWith component.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testEndsWith() throws Exception {
    SubstringJSONObjectFilter f = new SubstringJSONObjectFilter("test-field", null, null, "jkl");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("endsWith", "jkl")));
    f = (SubstringJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field"));
    assertNull(f.getStartsWith());
    assertNotNull(f.getContains());
    assertTrue(f.getContains().isEmpty());
    assertNotNull(f.getEndsWith());
    assertEquals(f.getEndsWith(), "jkl");
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "substring");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("startsWith", "contains", "endsWith", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "jkl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "jkL"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "JKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "kl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abcdefghijkl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "AbCdEfGhIjKl"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHIJKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "defjklghi"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("Test-Field", "jkl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ghijkl"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(JSONNull.NULL, new JSONString("ghi"), new JSONNumber(1234), new JSONString("JKL"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("jkl"), new JSONString("GHI"))))));
    f.setCaseSensitive(true);
    assertTrue(f.caseSensitive());
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", "test-field"), new JSONField("endsWith", "jkl"), new JSONField("caseSensitive", true)));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "jkl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "jkL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "JKL"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "abcdefghijkl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "AbCdEfGhIjKl"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", "ABCDEFGHIJKL"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", JSONArray.EMPTY_ARRAY))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ghijkl"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("ghi"), new JSONString("JKL"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("test-field", new JSONArray(new JSONString("jkl"), new JSONString("GHI"))))));
}
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 27 with JSONField

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

the class SubstringJSONObjectFilterTestCase method testAllComponents.

/**
 * Tests the behavior of a filter that includes all substring components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testAllComponents() throws Exception {
    SubstringJSONObjectFilter f = new SubstringJSONObjectFilter(Arrays.asList("first", "second"), "abc", Arrays.asList("def", "ghi"), "jkl");
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "substring"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second"))), new JSONField("startsWith", "abc"), new JSONField("contains", new JSONArray(new JSONString("def"), new JSONString("ghi"))), new JSONField("endsWith", "jkl")));
    f = (SubstringJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Arrays.asList("first", "second"));
    assertNotNull(f.getStartsWith());
    assertEquals(f.getStartsWith(), "abc");
    assertNotNull(f.getContains());
    assertEquals(f.getContains(), Arrays.asList("def", "ghi"));
    assertNotNull(f.getEndsWith());
    assertEquals(f.getEndsWith(), "jkl");
    assertFalse(f.caseSensitive());
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "substring");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("field")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), new HashSet<String>(Arrays.asList("startsWith", "contains", "endsWith", "caseSensitive")));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", "abcdefghijkl"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", "AbcDefGhiJkl"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", "abcghidefjkl"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", "abcXXXdefXXXghiXXXjkl"))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", "ABCxxxDEFxxxGHIxxxJKL"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", "xxxABCxxxDEFxxxGHIxxxJKLxxx"))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", JSONArray.EMPTY_ARRAY))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", new JSONArray(new JSONString("abcdefghijkl"))))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", new JSONArray(new JSONString("abcdefghijkl"), new JSONString("mnopqrstuvwx"))))))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", new JSONArray(new JSONString("mnopqrstuvwx"), new JSONString("abcdefghijkl"))))))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("first", new JSONObject(new JSONField("second", new JSONArray(new JSONString("abc"), new JSONString("def"), new JSONString("ghi"), new JSONString("jkl"))))))));
}
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 28 with JSONField

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

the class JSONObjectExactMatchingRuleTestCase method testBasics.

/**
 * Provides basic test coverage for the matching rule class.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    final JSONObjectExactMatchingRule mr = JSONObjectExactMatchingRule.getInstance();
    assertNotNull(mr.getEqualityMatchingRuleName());
    assertEquals(mr.getEqualityMatchingRuleName(), "jsonObjectExactMatch");
    assertNotNull(mr.getEqualityMatchingRuleOID());
    assertEquals(mr.getEqualityMatchingRuleOID(), "1.3.6.1.4.1.30221.2.4.12");
    assertNull(mr.getOrderingMatchingRuleName());
    assertNull(mr.getOrderingMatchingRuleOID());
    assertNull(mr.getSubstringMatchingRuleName());
    assertNull(mr.getSubstringMatchingRuleOID());
    final JSONObject o1 = new JSONObject(new JSONField("one", 1), new JSONField("two", "2"));
    final JSONObject o2 = new JSONObject(new JSONField("two", "2"), new JSONField("one", 1));
    final JSONObject o3 = new JSONObject(new JSONField("one", "1"), new JSONField("two", 2));
    assertTrue(mr.valuesMatch(new ASN1OctetString(o1.toString()), new ASN1OctetString(o1.toString())));
    assertTrue(mr.valuesMatch(new ASN1OctetString(o1.toString()), new ASN1OctetString(o2.toString())));
    assertFalse(mr.valuesMatch(new ASN1OctetString(o1.toString()), new ASN1OctetString(o3.toString())));
    try {
        mr.valuesMatch(new ASN1OctetString("not a valid JSON object"), new ASN1OctetString(o1.toString()));
        fail("Expected a valuesMatch exception with an invalid first value");
    } catch (final LDAPException le) {
    // This was expected.
    }
    try {
        mr.valuesMatch(new ASN1OctetString(o1.toString()), new ASN1OctetString("not a valid JSON object"));
        fail("Expected a valuesMatch exception with an invalid second value");
    } catch (final LDAPException le) {
    // This was expected.
    }
    try {
        mr.matchesSubstring(new ASN1OctetString(o1.toString()), new ASN1OctetString("{"), new ASN1OctetString[0], null);
        fail("Expected a matchesSubstring exception");
    } catch (final LDAPException le) {
    // This was expected.
    }
    try {
        mr.compareValues(new ASN1OctetString(o1.toString()), new ASN1OctetString(o2.toString()));
        fail("Expected a compareValues exception");
    } catch (final LDAPException le) {
    // This was expected.
    }
    assertNotNull(mr.normalize(new ASN1OctetString(o1.toString())));
    assertEquals(mr.normalize(new ASN1OctetString(o1.toString())), new ASN1OctetString(o1.toNormalizedString()));
    try {
        mr.normalize(new ASN1OctetString("not a valid JSON object"));
        fail("Expected a normalize exception with an invalid object");
    } catch (final LDAPException le) {
    // This was expected.
    }
    try {
        mr.normalizeSubstring(new ASN1OctetString("{"), (byte) 0x80);
        fail("Expected a normalizeSubstring exception");
    } catch (final LDAPException le) {
    // This was expected.
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) JSONObject(com.unboundid.util.json.JSONObject) LDAPException(com.unboundid.ldap.sdk.LDAPException) JSONField(com.unboundid.util.json.JSONField) Test(org.testng.annotations.Test)

Example 29 with JSONField

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

the class JSONObjectFilterTestCase method testGetBoolean.

/**
 * Provides test coverage for the {@code getBoolean} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetBoolean() throws Exception {
    // A filter that will be used just to invoke the protected methods.
    final ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("name");
    // Test the case in which an object has a Boolean value of true.
    JSONObject o = new JSONObject(new JSONField("a", true));
    assertTrue(f.getBoolean(o, "a", null));
    // Test the case in which an object has a Boolean value of false.
    o = new JSONObject(new JSONField("a", false));
    assertFalse(f.getBoolean(o, "a", false));
    // Test the case in which an object is missing the target field and there
    // is a default value of true.
    assertTrue(f.getBoolean(JSONObject.EMPTY_OBJECT, "a", true));
    // Test the case in which an object is missing the target field and there
    // is a default value of false.
    assertFalse(f.getBoolean(JSONObject.EMPTY_OBJECT, "a", false));
    // is no default value.
    try {
        f.getBoolean(JSONObject.EMPTY_OBJECT, "a", null);
        fail("Expected an exception for a missing field");
    } catch (final JSONException e) {
    // This is expected.
    }
    // Test the cse in which an object has a non-Boolean value for the target
    // field.
    o = new JSONObject(new JSONField("a", new JSONString("true")));
    try {
        f.getBoolean(o, "a", null);
        fail("Expected an exception for a non-Boolean field");
    } catch (final JSONException e) {
    // This is expected.
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 30 with JSONField

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

the class LDAPCompareJSONOutputHandlerTestCase method testCompareFalseResult.

/**
 * Tests the behavior of the output handler for a compare operation in which
 * the assertion did not match.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testCompareFalseResult() throws Exception {
    final CompareRequest compareRequest = new CompareRequest("", "testAttr", "missingValue");
    final LDAPResult compareResult = new LDAPResult(-1, ResultCode.COMPARE_FALSE);
    final LDAPCompareJSONOutputHandler outputHandler = new LDAPCompareJSONOutputHandler();
    assertNotNull(outputHandler.getHeaderLines());
    assertTrue(outputHandler.getHeaderLines().length == 0);
    final String formattedOutput = outputHandler.formatResult(compareRequest, compareResult);
    assertNotNull(formattedOutput);
    assertEquals(formattedOutput, new JSONObject(new JSONField("entry-dn", ""), new JSONField("attribute-name", "testAttr"), new JSONField("assertion-value", "missingValue"), new JSONField("result-code-value", 5), new JSONField("result-code-name", "compare false")).toSingleLineString());
}
Also used : CompareRequest(com.unboundid.ldap.sdk.CompareRequest) JSONObject(com.unboundid.util.json.JSONObject) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) JSONField(com.unboundid.util.json.JSONField) 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