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());
}));
}
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());
}));
}
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());
}));
}
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());
}));
}
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;
});
}
Aggregations