Search in sources :

Example 11 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class BaseTenantServiceTest method testAddFailsForAdapterConfigWithoutType.

/**
 * Verifies that the base service fails for a payload that defines an adapter entry, but does not provide the
 * mandatory field {@link TenantConstants#FIELD_ADAPTERS_TYPE}.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAddFailsForAdapterConfigWithoutType(final TestContext ctx) {
    final JsonObject testPayload = createValidTenantPayload();
    final JsonArray adapterArray = new JsonArray();
    // no type specified (which is a violation of the API)
    adapterArray.add(new JsonObject());
    testPayload.put(TenantConstants.FIELD_ADAPTERS, adapterArray);
    final EventBusMessage msg = createRequest(TenantConstants.TenantAction.add, testPayload);
    tenantService.processRequest(msg).setHandler(ctx.asyncAssertFailure(t -> {
        ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) TenantResult(org.eclipse.hono.util.TenantResult) BeforeClass(org.junit.BeforeClass) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) JsonArray(io.vertx.core.json.JsonArray) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) EventBusMessage(org.eclipse.hono.util.EventBusMessage) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Example 12 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class BaseTenantServiceTest method testAddSucceedsForMinimalData.

/**
 * Verifies that the base service accepts a request for adding
 * a tenant that contains the minimum required properties.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAddSucceedsForMinimalData(final TestContext ctx) {
    final JsonObject testPayload = createValidTenantPayload();
    final EventBusMessage request = createRequest(TenantConstants.TenantAction.add, testPayload);
    tenantService.processRequest(request).setHandler(ctx.asyncAssertSuccess(response -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus());
        ctx.assertEquals(TEST_TENANT, response.getTenant());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) TenantResult(org.eclipse.hono.util.TenantResult) BeforeClass(org.junit.BeforeClass) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) JsonArray(io.vertx.core.json.JsonArray) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) EventBusMessage(org.eclipse.hono.util.EventBusMessage) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 13 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class FileBasedRegistrationServiceTest method testGetFailsForDeregisteredDevice.

/**
 * Verifies that the registry returns 404 when getting an unregistered device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetFailsForDeregisteredDevice(final TestContext ctx) {
    final EventBusMessage createRequest = newRequest(RegistrationConstants.ACTION_REGISTER, TENANT);
    final EventBusMessage deregisterRequest = newRequest(RegistrationConstants.ACTION_DEREGISTER, TENANT);
    final EventBusMessage getRequest = newRequest(RegistrationConstants.ACTION_GET, TENANT);
    registrationService.processRequest(createRequest).compose(response -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus());
        return registrationService.processRequest(deregisterRequest);
    }).compose(response -> {
        ctx.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, response.getStatus());
        return registrationService.processRequest(getRequest);
    }).setHandler(ctx.asyncAssertSuccess(response -> {
        ctx.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getStatus());
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) ArgumentMatchers(org.mockito.ArgumentMatchers) RunWith(org.junit.runner.RunWith) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) EventBus(io.vertx.core.eventbus.EventBus) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) Assert.assertFalse(org.junit.Assert.assertFalse) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Test(org.junit.Test)

Example 14 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class FileBasedRegistrationServiceTest method testGetSucceedsForRegisteredDevice.

/**
 * Verifies that the registry returns 200 when getting an existing device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetSucceedsForRegisteredDevice(final TestContext ctx) {
    final EventBusMessage createRequest = newRequest(RegistrationConstants.ACTION_REGISTER, TENANT);
    final EventBusMessage getRequest = newRequest(RegistrationConstants.ACTION_GET, TENANT);
    registrationService.processRequest(createRequest).compose(response -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus());
        return registrationService.processRequest(getRequest);
    }).setHandler(ctx.asyncAssertSuccess(response -> {
        ctx.assertEquals(HttpURLConnection.HTTP_OK, response.getStatus());
        ctx.assertNotNull(response.getJsonPayload());
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) ArgumentMatchers(org.mockito.ArgumentMatchers) RunWith(org.junit.runner.RunWith) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) EventBus(io.vertx.core.eventbus.EventBus) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) Assert.assertFalse(org.junit.Assert.assertFalse) FileSystem(io.vertx.core.file.FileSystem) Handler(io.vertx.core.Handler) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Test(org.junit.Test)

Example 15 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class EventBusService method processRequestMessage.

private void processRequestMessage(final Message<JsonObject> msg) {
    if (log.isTraceEnabled()) {
        log.trace("received request message: {}", msg.body().encodePrettily());
    }
    final EventBusMessage request = EventBusMessage.fromJson(msg.body());
    processRequest(request).recover(t -> {
        log.debug("cannot process request [operation: {}]: {}", request.getOperation(), t.getMessage());
        final int status = Optional.of(t).map(cause -> {
            if (cause instanceof ServiceInvocationException) {
                return ((ServiceInvocationException) cause).getErrorCode();
            } else {
                return null;
            }
        }).orElse(HttpURLConnection.HTTP_INTERNAL_ERROR);
        return Future.succeededFuture(request.getResponse(status));
    }).map(response -> {
        if (response.getReplyToAddress() == null) {
            log.debug("sending response as direct reply to request [operation: {}]", request.getOperation());
            msg.reply(response.toJson());
        } else if (response.hasResponseProperties()) {
            log.debug("sending response [operation: {}, reply-to: {}]", request.getOperation(), request.getReplyToAddress());
            vertx.eventBus().send(request.getReplyToAddress(), response.toJson());
        } else {
            log.warn("discarding response lacking correlation ID or operation");
        }
        return null;
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Message(io.vertx.core.eventbus.Message) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) ConfigurationSupportingVerticle(org.eclipse.hono.util.ConfigurationSupportingVerticle) Future(io.vertx.core.Future) Objects(java.util.Objects) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) EventBusMessage(org.eclipse.hono.util.EventBusMessage) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException)

Aggregations

EventBusMessage (org.eclipse.hono.util.EventBusMessage)29 Future (io.vertx.core.Future)26 JsonObject (io.vertx.core.json.JsonObject)26 HttpURLConnection (java.net.HttpURLConnection)26 Handler (io.vertx.core.Handler)24 AsyncResult (io.vertx.core.AsyncResult)21 ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)18 TestContext (io.vertx.ext.unit.TestContext)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)16 Test (org.junit.Test)16 Timeout (org.junit.rules.Timeout)16 RunWith (org.junit.runner.RunWith)16 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)14 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)13 CredentialsObject (org.eclipse.hono.util.CredentialsObject)13 CredentialsResult (org.eclipse.hono.util.CredentialsResult)13 BeforeClass (org.junit.BeforeClass)13 TenantConstants (org.eclipse.hono.util.TenantConstants)11 Objects (java.util.Objects)10 ClientErrorException (org.eclipse.hono.client.ClientErrorException)8