use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ContainsFieldJSONObjectFilterTestCase method testDecodeFilterInvalidType.
/**
* Provides test coverage for the {@code decodeFilter} method for an object
* that includes an unsupported expected type value.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { JSONException.class })
public void testDecodeFilterInvalidType() throws Exception {
final JSONObject o = new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "test-field"), new JSONField("expectedType", "unrecognized"));
JSONObjectFilter.decode(o);
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ContainsFieldJSONObjectFilterTestCase method testThirdLevelField.
/**
* Tests the behavior of this filter for a third-level field.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testThirdLevelField() throws Exception {
ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("top-level-field", "second-level-field", "third-level-field");
f.setExpectedType(ExpectedValueType.BOOLEAN);
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 JSONString("third-level-field"))), new JSONField("expectedType", "boolean")));
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", "third-level-field"));
assertNotNull(f.getExpectedType());
assertEquals(f.getExpectedType(), EnumSet.of(ExpectedValueType.BOOLEAN));
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);
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("third-level-field", true))))))));
assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONObject(new JSONField("second-level-field", new JSONObject(new JSONField("third-level-field", "not boolean"))))))));
assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("top-level-field", new JSONArray(new JSONObject(new JSONField("second-level-field", new JSONArray(new JSONObject(new JSONField("third-level-field", true))))))))));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class ContainsFieldJSONObjectFilterTestCase method testSetField.
/**
* Provides test coverage for the methods used to get and set the target
* field.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSetField() throws Exception {
ContainsFieldJSONObjectFilter f = new ContainsFieldJSONObjectFilter("field-name");
assertEquals(f.getField(), Collections.singletonList("field-name"));
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "field-name")));
f.setField("different-name");
assertEquals(f.getField(), Collections.singletonList("different-name"));
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", "different-name")));
f.setField("first", "second");
assertEquals(f.getField(), Arrays.asList("first", "second"));
assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "containsField"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second")))));
try {
f.setField();
fail("Expected an exception from setField()");
} catch (final LDAPSDKUsageException e) {
// This was expected.
}
}
use of com.unboundid.util.json.JSONField 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);
}
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class RecentLoginHistoryTestCase method testDecodeMalformedSuccessNotObject.
/**
* Tests the behavior when trying to decode a JSON object with a malformed
* set of successful attempts when the malformed attempt is not an object.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeMalformedSuccessNotObject() throws Exception {
final JSONObject o = new JSONObject(new JSONField("successful-attempts", new JSONArray(new JSONString("malformed"))));
new RecentLoginHistory(o);
}
Aggregations