/** * Checks whether a given device is registered and enabled. * * @param device The device to check. * @param context The currently active OpenTracing span that is used to * trace the retrieval of the assertion or {@code null} * if no span is currently active. * @return A future indicating the outcome. * The future will be succeeded if the device is registered and enabled. * Otherwise, the future will be failed with a {@link ServiceInvocationException}. */ protected final Future<Void> checkDeviceRegistration(final Device device, final SpanContext context) { Objects.requireNonNull(device); return getRegistrationAssertion( device.getTenantId(), device.getDeviceId(), null, context).map(assertion -> null); }
/** * Checks whether a given device is registered and enabled. * * @param device The device to check. * @param context The currently active OpenTracing span that is used to * trace the retrieval of the assertion or {@code null} * if no span is currently active. * @return A future indicating the outcome. * The future will be succeeded if the device is registered and enabled. * Otherwise, the future will be failed with a {@link ServiceInvocationException}. */ protected final Future<Void> checkDeviceRegistration(final Device device, final SpanContext context) { Objects.requireNonNull(device); return getRegistrationAssertion( device.getTenantId(), device.getDeviceId(), null, context).map(assertion -> null); }
/** * Verifies that the adapter successfully retrieves a registration assertion * for an existing device. * * @param ctx The vert.x test context. */ @Test public void testGetRegistrationAssertionSucceedsForExistingDevice(final TestContext ctx) { // GIVEN an adapter connected to a registration service final JsonObject assertionResult = newRegistrationAssertionResult("token"); when(registrationClient.assertRegistration(eq("device"), any())).thenReturn(Future.succeededFuture(assertionResult)); when(registrationClient.assertRegistration(eq("device"), any(), any())).thenReturn(Future.succeededFuture(assertionResult)); // WHEN an assertion for the device is retrieved adapter.getRegistrationAssertion("tenant", "device", null).setHandler(ctx.asyncAssertSuccess(result -> { // THEN the result contains the registration assertion ctx.assertEquals(assertionResult, result); })); adapter.getRegistrationAssertion("tenant", "device", null, mock(SpanContext.class)).setHandler(ctx.asyncAssertSuccess(result -> { // THEN the result contains the registration assertion ctx.assertEquals(assertionResult, result); })); }
/** * Verifies that the adapter fails a request to get a registration assertion for * a non-existing device. * * @param ctx The vert.x test context. */ @Test public void testGetRegistrationAssertionFailsWith404ForNonExistingDevice(final TestContext ctx) { // GIVEN an adapter connected to a registration service when(registrationClient.assertRegistration(eq("non-existent"), any())).thenReturn( Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND))); when(registrationClient.assertRegistration(eq("non-existent"), any(), any())).thenReturn( Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND))); // WHEN an assertion for a non-existing device is retrieved adapter.getRegistrationAssertion("tenant", "non-existent", null).setHandler(ctx.asyncAssertFailure(t -> { // THEN the request fails with a 404 ctx.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ((ServiceInvocationException) t).getErrorCode()); })); adapter.getRegistrationAssertion("tenant", "non-existent", null, mock(SpanContext.class)).setHandler(ctx.asyncAssertFailure(t -> { // THEN the request fails with a 404 ctx.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ((ServiceInvocationException) t).getErrorCode()); })); }
Objects.requireNonNull(ttd); final Future<JsonObject> tokenTracker = getRegistrationAssertion(tenant, deviceId, authenticatedDevice, context); final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant, context); final Future<MessageSender> senderTracker = getEventSender(tenant);
Objects.requireNonNull(ttd); final Future<JsonObject> tokenTracker = getRegistrationAssertion(tenant, deviceId, authenticatedDevice, context); final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant, context); final Future<MessageSender> senderTracker = getEventSender(tenant);
/** * Verifies that the adapter fails a request to retrieve a token for a gateway that does not * belong to the same tenant as the device it wants to act on behalf of. * * @param ctx The vert.x test context. */ @Test public void testGetRegistrationAssertionFailsWith403ForNonMatchingTenant(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN a gateway tries to get an assertion for a device from another tenant adapter.getRegistrationAssertion( "tenant A", "device", new Device("tenant B", "gateway"), mock(SpanContext.class)).setHandler(ctx.asyncAssertFailure(t -> { // THEN the request fails with a 403 Forbidden error ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ClientErrorException) t).getErrorCode()); })); }