use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class IntegrationTestSupport method testJsonObjectToBeContained.
/**
* A simple implementation of subtree containment: all entries of the JsonObject that is tested to be contained
* must be contained in the other JsonObject as well. Nested JsonObjects are treated the same by recursively calling
* this method to test the containment.
* JsonArrays are tested for containment as well: all elements in a JsonArray belonging to the contained JsonObject
* must be present in the corresponding JsonArray of the other JsonObject as well. The sequence of the array elements
* is not important (suitable for the current tests).
* @param jsonObject The JsonObject that must fully contain the other JsonObject (but may contain more entries as well).
* @param jsonObjectToBeContained The JsonObject that needs to be fully contained inside the other JsonObject.
* @return The result of the containment test.
*/
public static boolean testJsonObjectToBeContained(final JsonObject jsonObject, final JsonObject jsonObjectToBeContained) {
if (jsonObjectToBeContained == null) {
return true;
}
if (jsonObject == null) {
return false;
}
AtomicBoolean containResult = new AtomicBoolean(true);
jsonObjectToBeContained.forEach(entry -> {
if (!jsonObject.containsKey(entry.getKey())) {
containResult.set(false);
} else {
if (entry.getValue() == null) {
if (jsonObject.getValue(entry.getKey()) != null) {
containResult.set(false);
}
} else if (entry.getValue() instanceof JsonObject) {
if (!(jsonObject.getValue(entry.getKey()) instanceof JsonObject)) {
containResult.set(false);
} else {
if (!testJsonObjectToBeContained((JsonObject) entry.getValue(), (JsonObject) jsonObject.getValue(entry.getKey()))) {
containResult.set(false);
}
}
} else if (entry.getValue() instanceof JsonArray) {
if (!(jsonObject.getValue(entry.getKey()) instanceof JsonArray)) {
containResult.set(false);
} else {
// compare two JsonArrays
JsonArray biggerArray = (JsonArray) jsonObject.getValue(entry.getKey());
JsonArray smallerArray = (JsonArray) entry.getValue();
if (!testJsonArrayToBeContained(biggerArray, smallerArray)) {
containResult.set(false);
}
}
} else {
if (!(entry.getValue().equals(jsonObject.getValue(entry.getKey())))) {
containResult.set(false);
}
}
}
});
return containResult.get();
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class CredentialsAmqpIT method pickFirstSecretFromPayload.
private JsonObject pickFirstSecretFromPayload(final CredentialsObject payload) {
// secrets: first entry is expected to be valid,
// second entry may have time stamps not yet active (not checked),
// more entries may be avail
final JsonArray secrets = payload.getSecrets();
assertNotNull(secrets);
assertTrue(secrets.size() > 0);
final JsonObject firstSecret = secrets.getJsonObject(0);
assertNotNull(firstSecret);
return firstSecret;
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class CredentialsAmqpIT method checkPayloadGetCredentialsReturnsMultipleSecrets.
private void checkPayloadGetCredentialsReturnsMultipleSecrets(final CredentialsObject payload) {
assertNotNull(payload);
// secrets: first entry is expected to be valid,
// second entry may have time stamps not yet active (not checked),
// more entries may be avail
final JsonArray secrets = payload.getSecrets();
assertNotNull(secrets);
// at least 2 entries to test multiple entries
assertTrue(secrets.size() > 1);
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class CredentialsHttpIT method testGetCredentialsForDeviceRegardlessOfType.
/**
* Verifies that the service returns all credentials registered for a given device regardless of type.
* <p>
* The returned JsonObject must consist of the total number of entries and contain all previously added credentials
* in the provided JsonArray that is found under the key of the endpoint {@link CredentialsConstants#CREDENTIALS_ENDPOINT}.
*
* @param context The vert.x test context.
* @throws InterruptedException if registration of credentials is interrupted.
*/
@Test
public void testGetCredentialsForDeviceRegardlessOfType(final TestContext context) throws InterruptedException {
final String pskAuthId = getRandomAuthId(TEST_AUTH_ID);
final List<JsonObject> credentialsToAdd = new ArrayList<>();
for (int i = 0; i < 5; i++) {
final JsonObject requestBody = newPskCredentials(deviceId, pskAuthId);
requestBody.put(CredentialsConstants.FIELD_TYPE, "type" + i);
credentialsToAdd.add(requestBody);
}
addMultipleCredentials(credentialsToAdd).compose(ar -> registry.getCredentials(TENANT, deviceId)).setHandler(context.asyncAssertSuccess(b -> {
assertResponseBodyContainsAllCredentials(context, b.toJsonObject(), credentialsToAdd);
}));
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class CredentialsHttpIT method assertResponseBodyContainsAllCredentials.
private static void assertResponseBodyContainsAllCredentials(final TestContext context, final JsonObject responseBody, final List<JsonObject> credentialsList) {
// the response must contain all of the payload of the add request, so test that now
context.assertTrue(responseBody.containsKey(CredentialsConstants.FIELD_CREDENTIALS_TOTAL));
Integer totalCredentialsFound = responseBody.getInteger(CredentialsConstants.FIELD_CREDENTIALS_TOTAL);
context.assertEquals(totalCredentialsFound, credentialsList.size());
context.assertTrue(responseBody.containsKey(CredentialsConstants.CREDENTIALS_ENDPOINT));
final JsonArray credentials = responseBody.getJsonArray(CredentialsConstants.CREDENTIALS_ENDPOINT);
context.assertNotNull(credentials);
context.assertEquals(credentials.size(), totalCredentialsFound);
// TODO: add full test if the lists are 'identical' (contain the same JsonObjects by using the
// contained helper method)
}
Aggregations