Search in sources :

Example 41 with JsonArray

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();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JsonObject(io.vertx.core.json.JsonObject)

Example 42 with JsonArray

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;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Example 43 with JsonArray

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);
}
Also used : JsonArray(io.vertx.core.json.JsonArray)

Example 44 with JsonArray

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);
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) After(org.junit.After) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Rule(org.junit.Rule) DeviceRegistryHttpClient(org.eclipse.hono.tests.DeviceRegistryHttpClient) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 45 with JsonArray

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)
}
Also used : JsonArray(io.vertx.core.json.JsonArray)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)116 JsonObject (io.vertx.core.json.JsonObject)82 Test (org.junit.Test)72 ArrayList (java.util.ArrayList)19 Future (io.vertx.core.Future)15 Buffer (io.vertx.core.buffer.Buffer)15 Handler (io.vertx.core.Handler)14 HttpURLConnection (java.net.HttpURLConnection)13 HashMap (java.util.HashMap)13 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 RunWith (org.junit.runner.RunWith)10 StandardCharsets (java.nio.charset.StandardCharsets)9 Instant (java.time.Instant)9 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)9 Vertx (io.vertx.core.Vertx)8 Async (io.vertx.ext.unit.Async)8 Constants (org.eclipse.hono.util.Constants)8 CredentialsObject (org.eclipse.hono.util.CredentialsObject)8 Before (org.junit.Before)8