AbstractProtocolAdapterBase extends AbstractServiceBase<T> { protected final Future<JsonObject> getRegistrationAssertion( final String tenantId, final String deviceId, final Device authenticatedDevice, final SpanContext context) { Objects.requireNonNull(tenantId); Objects.requireNonNull(deviceId); final Future<String> gatewayId = getGatewayId(tenantId, deviceId, authenticatedDevice); return gatewayId .compose(gwId -> getRegistrationClient(tenantId)) .compose(client -> client.assertRegistration(deviceId, gatewayId.result(), context)) .compose(registrationAssertion -> { updateLastGateway(registrationAssertion, tenantId, deviceId, authenticatedDevice, context) .otherwise(t -> { log.warn("failed to update last gateway [tenantId: {}, deviceId: {}]", tenantId, deviceId, t); return null; }); return Future.succeededFuture(registrationAssertion); }); }  @Autowired @Override void setConfig(final T configuration); @Qualifier(TenantConstants.TENANT_ENDPOINT) @Autowired final void setTenantClientFactory(final TenantClientFactory factory); final TenantClientFactory getTenantClientFactory(); @Qualifier(DeviceConnectionConstants.DEVICE_CONNECTION_ENDPOINT) @Autowired final void setDeviceConnectionClientFactory(final BasicDeviceConnectionClientFactory factory); @Deprecated(forRemoval = true) final DeviceConnectionClientFactory getDeviceConnectionClientFactory(); @Qualifier(Constants.QUALIFIER_MESSAGING) @Autowired final void setDownstreamSenderFactory(final DownstreamSenderFactory factory); final DownstreamSenderFactory getDownstreamSenderFactory(); @Qualifier(RegistrationConstants.REGISTRATION_ENDPOINT) @Autowired final void setRegistrationClientFactory(final RegistrationClientFactory factory); final RegistrationClientFactory getRegistrationClientFactory(); @Qualifier(CredentialsConstants.CREDENTIALS_ENDPOINT) @Autowired final void setCredentialsClientFactory(final CredentialsClientFactory factory); final CredentialsClientFactory getCredentialsClientFactory(); @Autowired(required = false) void setConnectionEventProducer(final ConnectionEventProducer connectionEventProducer); ConnectionEventProducer getConnectionEventProducer(); @Autowired final void setCommandConsumerFactory(final ProtocolAdapterCommandConsumerFactory factory); final ProtocolAdapterCommandConsumerFactory getCommandConsumerFactory(); @Autowired final void setCommandTargetMapper(final CommandTargetMapper commandTargetMapper); @Autowired(required = false) final void setResourceLimitChecks(final ResourceLimitChecks resourceLimitChecks); final void setConnectionLimitManager(final ConnectionLimitManager connectionLimitManager); @Override void registerReadinessChecks(final HealthCheckHandler handler); @Override void registerLivenessChecks(final HealthCheckHandler handler); static ErrorCondition getErrorCondition(final Throwable t); static ConnectionAttemptOutcome getOutcome(final Throwable e);  }
@Test public void testGetRegistrationAssertionSucceedsForExistingDevice(final VertxTestContext ctx) { final JsonObject assertionResult = newRegistrationAssertionResult(); when(registrationClient.assertRegistration(eq("device"), any(), any())).thenReturn(Future.succeededFuture(assertionResult)); adapter.getRegistrationAssertion("tenant", "device", null, mock(SpanContext.class)) .onComplete(ctx.succeeding(result -> { ctx.verify(() -> { assertEquals(assertionResult, result); }); ctx.completeNow(); })); }
@Test public void testGetRegistrationAssertionFailsWith404ForNonExistingDevice(final VertxTestContext ctx) { when(registrationClient.assertRegistration(eq("non-existent"), any(), any())).thenReturn( Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND))); adapter.getRegistrationAssertion("tenant", "non-existent", null, mock(SpanContext.class)) .onComplete(ctx.failing(t -> { ctx.verify(() -> { assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ((ServiceInvocationException) t).getErrorCode()); }); ctx.completeNow(); })); }
@Test public void testGetRegistrationAssertionFailsWith403ForNonMatchingTenant(final VertxTestContext ctx) { adapter = newProtocolAdapter(properties, null); adapter.getRegistrationAssertion( "tenant A", "device", new Device("tenant B", "gateway"), mock(SpanContext.class)).onComplete(ctx.failing(t -> ctx.verify(() -> { assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ClientErrorException) t).getErrorCode()); ctx.completeNow(); }))); }