use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class GetRecentLoginHistoryResponseControlTestCase method testDecodeControlValueNotValidLoginHistory.
/**
* Tests the behavior when trying to decode a control whose value is a JSON
* object that can't be parsed as a valid recent login history.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeControlValueNotValidLoginHistory() throws Exception {
final JSONObject o = new JSONObject(new JSONField("successful-attempts", new JSONArray(new JSONObject(new JSONField("malformed", true)))));
new GetRecentLoginHistoryResponseControl("1.3.6.1.4.1.30221.2.5.62", false, new ASN1OctetString(o.toSingleLineString()));
}
use of com.unboundid.util.json.JSONField in project ssam by pingidentity.
the class SSAMInstaller method writeLDAPConnectionDetails.
/**
* Writes the LDAP connection details JSON file that governs how SSAM
* is configured to communicate with the server.
*/
private void writeLDAPConnectionDetails(final File details) throws IOException {
// Generate the server-details section.
JSONObject serverDetails = new JSONObject(new JSONField("single-server", new JSONObject(new JSONField("address", localHostName), new JSONField("port", ldapPortArg.getValue()))));
// Generate the communication-security section.
List<JSONField> fields = new ArrayList<>();
if (useSSLArg.isPresent() || useStartTLSArg.isPresent()) {
if (useSSLArg.isPresent()) {
fields.add(new JSONField("security-type", "SSL"));
} else {
fields.add(new JSONField("security-type", "StartTLS"));
}
if (trustStorePathArg.isPresent()) {
fields.add(new JSONField("trust-store-file", trustStorePathArg.getValue().getCanonicalPath()));
fields.add(new JSONField("trust-store-type", "JKS"));
fields.add(new JSONField("verify-address-in-certificate", true));
} else {
fields.add(new JSONField("trust-all-certificates", true));
}
} else {
fields.add(new JSONField("security-type", "none"));
}
JSONObject communicationSecurity = new JSONObject(fields.toArray(new JSONField[fields.size()]));
// Generate the authentication-details section.
JSONObject authenticationDetails = new JSONObject(new JSONField("authentication-type", "simple"), new JSONField("dn", ssamUserDN.toString()), new JSONField("password-file", ssamUserPasswordConfigFile.getCanonicalPath()));
// ldap-connection-details.json
JSONObject ldapConnectionDetails = new JSONObject(new JSONField("server-details", serverDetails), new JSONField("communication-security", communicationSecurity), new JSONField("authentication-details", authenticationDetails));
writeToFile(details, ldapConnectionDetails.toString());
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testValueToStrings.
/**
* Tests the behavior for the {@code valueToStrings} method.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testValueToStrings() throws Exception {
// Simple values.
assertEquals(JSONLogMessage.valueToStrings(JSONBoolean.TRUE), Collections.singletonList("true"));
assertEquals(JSONLogMessage.valueToStrings(JSONBoolean.FALSE), Collections.singletonList("false"));
assertEquals(JSONLogMessage.valueToStrings(JSONNull.NULL), Collections.singletonList("null"));
assertEquals(JSONLogMessage.valueToStrings(new JSONNumber(1234L)), Collections.singletonList("1234"));
assertEquals(JSONLogMessage.valueToStrings(new JSONNumber(1.5d)), Collections.singletonList("1.5"));
assertEquals(JSONLogMessage.valueToStrings(new JSONString("foo")), Collections.singletonList("foo"));
// A JSON object value.
final JSONObject o = new JSONObject(new JSONField("foo", "a"), new JSONField("bar", "b"));
assertEquals(JSONLogMessage.valueToStrings(o), Collections.singletonList(o.toSingleLineString()));
// An array of strings.
final JSONArray stringArray = new JSONArray(new JSONString("one"), new JSONString("two"), new JSONString("three"));
assertEquals(JSONLogMessage.valueToStrings(stringArray), Arrays.asList("one", "two", "three"));
// An array that mixes values of different types.
final JSONArray mixedTypeArray = new JSONArray(JSONBoolean.TRUE, new JSONNumber(0L), new JSONString("foo"), JSONArray.EMPTY_ARRAY, JSONObject.EMPTY_OBJECT);
assertEquals(JSONLogMessage.valueToStrings(mixedTypeArray), Arrays.asList("true", "0", "foo", JSONArray.EMPTY_ARRAY.toSingleLineString(), JSONObject.EMPTY_OBJECT.toSingleLineString()));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testGetFirstValue.
/**
* Tests the methods for retrieving the first value for a given field.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetFirstValue() throws Exception {
// Test with a field whose value is a single string.
JSONObject o = createMinimalMessageObject(CONNECT, null, createField(CONNECT_FROM_ADDRESS, "1.2.3.4"));
JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(CONNECT_FROM_ADDRESS), new JSONString("1.2.3.4"));
// Test with a field whose value is a single Boolean.
o = createMinimalMessageObject(CONNECT, null, createField(UNCACHED_DATA_ACCESSED, true));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(UNCACHED_DATA_ACCESSED), JSONBoolean.TRUE);
// Test with a field whose value is a non-empty array of strings.
o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4", "5.6.7.8"));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(REQUEST_CONTROL_OIDS), new JSONString("1.2.3.4"));
// Test with a field whose value is a non-empty array of non-strings.
final LogField testField = new LogField("testField", IntegerLogFieldSyntax.getInstance());
o = createMinimalMessageObject(CONNECT, null, new JSONField("testField", new JSONArray(new JSONNumber(1L), new JSONNumber(2L), new JSONNumber(3L), new JSONNumber(4L))));
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getFirstValue(testField), new JSONNumber(1L));
// Test with a field whose value is an empty array.
o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS));
m = new JSONConnectAccessLogMessage(o);
assertNull(m.getFirstValue(REQUEST_CONTROL_OIDS));
}
use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.
the class JSONLogMessageTestCase method testGetStringSet.
/**
* Tests the methods for getting a string list from a JSON object.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testGetStringSet() 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.getStringSet(REQUEST_CONTROL_OIDS), StaticUtils.setOf("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.getStringSet(REQUEST_CONTROL_OIDS), Collections.singleton("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.getStringSet(OPERATION_ID), Collections.emptySet());
// 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.getStringSet(testField), Collections.emptySet());
// Test with a nonexistent field.
o = createMinimalMessageObject(CONNECT, null);
m = new JSONConnectAccessLogMessage(o);
assertEquals(m.getStringSet(REQUEST_CONTROL_OIDS), Collections.emptySet());
}
Aggregations