@Override public final void assertRegistration( final String tenantId, final String deviceId, final Handler<AsyncResult<RegistrationResult>> resultHandler) { assertRegistration(tenantId, deviceId, NoopSpan.INSTANCE, resultHandler); }
@Override public final void assertRegistration( final String tenantId, final String deviceId, final String gatewayId, final Handler<AsyncResult<RegistrationResult>> resultHandler) { assertRegistration(tenantId, deviceId, gatewayId, NoopSpan.INSTANCE, resultHandler); }
private Future<EventBusMessage> processAssertRequest(final EventBusMessage request) { final String tenantId = request.getTenant(); final String deviceId = request.getDeviceId(); final String gatewayId = request.getGatewayId(); final SpanContext spanContext = request.getSpanContext(); final Span span = newChildSpan(SPAN_NAME_ASSERT_DEVICE_REGISTRATION, spanContext, tenantId, deviceId, gatewayId); final Future<EventBusMessage> resultFuture; if (tenantId == null || deviceId == null) { TracingHelper.logError(span, "missing tenant and/or device"); resultFuture = Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); } else { final Future<RegistrationResult> result = Future.future(); if (gatewayId == null) { log.debug("asserting registration of device [{}] with tenant [{}]", deviceId, tenantId); assertRegistration(tenantId, deviceId, span, result.completer()); } else { log.debug("asserting registration of device [{}] with tenant [{}] for gateway [{}]", deviceId, tenantId, gatewayId); assertRegistration(tenantId, deviceId, gatewayId, span, result.completer()); } resultFuture = result.map(res -> { return request.getResponse(res.getStatus()) .setDeviceId(deviceId) .setJsonPayload(res.getPayload()) .setCacheDirective(res.getCacheDirective()); }); } return finishSpanOnFutureCompletion(span, resultFuture); }
private Future<EventBusMessage> processAssertRequest(final EventBusMessage request) { final String tenantId = request.getTenant(); final String deviceId = request.getDeviceId(); final String gatewayId = request.getGatewayId(); if (tenantId == null || deviceId == null) { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); } else if (gatewayId == null) { log.debug("asserting registration of device [{}] with tenant [{}]", deviceId, tenantId); final Future<RegistrationResult> result = Future.future(); assertRegistration(tenantId, deviceId, result.completer()); return result.map(res -> { return request.getResponse(res.getStatus()) .setDeviceId(deviceId) .setJsonPayload(res.getPayload()) .setCacheDirective(res.getCacheDirective()); }); } else { log.debug("asserting registration of device [{}] with tenant [{}] for gateway [{}]", deviceId, tenantId, gatewayId); final Future<RegistrationResult> result = Future.future(); assertRegistration(tenantId, deviceId, gatewayId, result.completer()); return result.map(res -> { return request.getResponse(res.getStatus()) .setDeviceId(deviceId) .setJsonPayload(res.getPayload()) .setCacheDirective(res.getCacheDirective()); }); } }
/** * Verifies that a device's status can be asserted by an existing gateway. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationSucceedsForExistingGateway(final TestContext ctx) { // GIVEN a registry that contains an enabled device that is configured to // be connected to an enabled gateway final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert the device's registration status for a gateway registrationService.assertRegistration(Constants.DEFAULT_TENANT, "4711", "gw-1", ctx.asyncAssertSuccess(result -> { // THEN the response contains a 200 status ctx.assertEquals(HttpURLConnection.HTTP_OK, result.getStatus()); final JsonObject payload = result.getPayload(); ctx.assertNotNull(payload); // and contains a JWT token ctx.assertNotNull(payload.getString(RegistrationConstants.FIELD_ASSERTION)); })); }
/** * Verifies that an enabled device's status can be asserted successfully. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationReturnsToken(final TestContext ctx) { // GIVEN a registry that contains an enabled device with a default content type set final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert the device's registration status registrationService.assertRegistration(Constants.DEFAULT_TENANT, "4711", ctx.asyncAssertSuccess(result -> { ctx.assertEquals(result.getStatus(), HttpURLConnection.HTTP_OK); final JsonObject payload = result.getPayload(); ctx.assertNotNull(payload); // THEN the response contains a JWT token asserting the device's registration status final String compactJws = payload.getString(RegistrationConstants.FIELD_ASSERTION); ctx.assertNotNull(compactJws); // and contains the registered default content type final JsonObject defaults = payload.getJsonObject(RegistrationConstants.FIELD_DEFAULTS); ctx.assertNotNull(defaults); ctx.assertEquals("application/default", defaults.getString(MessageHelper.SYS_PROPERTY_CONTENT_TYPE)); })); }
/** * Verifies that a disabled device's status cannot be asserted. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationFailsForDisabledDevice(final TestContext ctx) { // GIVEN a registry that contains a disabled device final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert the device's registration status registrationService.assertRegistration(Constants.DEFAULT_TENANT, "4712", ctx.asyncAssertSuccess(result -> { // THEN the response does not contain a JWT token ctx.assertEquals(result.getStatus(), HttpURLConnection.HTTP_NOT_FOUND); ctx.assertNull(result.getPayload()); })); }
/** * Verifies that a non existing device's status cannot be asserted. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationFailsForNonExistingDevice(final TestContext ctx) { // GIVEN a registry that does not contain any devices final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert a device's registration status registrationService.assertRegistration(Constants.DEFAULT_TENANT, "non-existent", ctx.asyncAssertSuccess(result -> { // THEN the response does not contain a JWT token ctx.assertEquals(result.getStatus(), HttpURLConnection.HTTP_NOT_FOUND); ctx.assertNull(result.getPayload()); })); }
/** * Verifies that a device's status cannot be asserted by a non-existing gateway. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationFailsForNonExistingGateway(final TestContext ctx) { // GIVEN a registry that contains an enabled device but no gateway final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert the device's registration status for a gateway registrationService.assertRegistration(Constants.DEFAULT_TENANT, "4711", "non-existent-gw", ctx.asyncAssertSuccess(result -> { // THEN the response contains a 403 status ctx.assertEquals(result.getStatus(), HttpURLConnection.HTTP_FORBIDDEN); // and does not contain a JWT token ctx.assertNull(result.getPayload()); })); }
/** * Verifies that a device's status cannot be asserted by a gateway that does not * match the device's configured gateway. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationFailsForWrongGateway(final TestContext ctx) { // GIVEN a registry that contains an enabled device and two gateways: // 1. the gateway that the device is configured for. // 2. another gateway final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert the device's registration status for the wrong gateway registrationService.assertRegistration(Constants.DEFAULT_TENANT, "4711", "gw-2", ctx.asyncAssertSuccess(result -> { // THEN the response contains a 403 status ctx.assertEquals(result.getStatus(), HttpURLConnection.HTTP_FORBIDDEN); // and does not contain a JWT token ctx.assertNull(result.getPayload()); })); }
/** * Verifies that a device's status cannot be asserted by a disabled gateway. * * @param ctx The vertx unit test context. */ @Test public void testAssertDeviceRegistrationFailsForDisabledGateway(final TestContext ctx) { // GIVEN a registry that contains an enabled device // and a gateway that the device is configured for but // which is disabled final BaseRegistrationService<ServiceConfigProperties> registrationService = newRegistrationService(); registrationService.setRegistrationAssertionFactory(RegistrationAssertionHelperImpl.forSigning(vertx, props)); // WHEN trying to assert the device's registration status for a gateway registrationService.assertRegistration(Constants.DEFAULT_TENANT, "4713", "gw-3", ctx.asyncAssertSuccess(result -> { // THEN the response contains a 403 status ctx.assertEquals(result.getStatus(), HttpURLConnection.HTTP_FORBIDDEN); // and does not contain a JWT token ctx.assertNull(result.getPayload()); })); }