AbstractProtocolAdapterBase extends AbstractServiceBase<T> { protected final Future<ResourceIdentifier> validateAddress(final ResourceIdentifier address, final Device authenticatedDevice) { Objects.requireNonNull(address); final Promise<ResourceIdentifier> result = Promise.promise(); if (authenticatedDevice == null) { if (Strings.isNullOrEmpty(address.getTenantId()) || Strings.isNullOrEmpty(address.getResourceId())) { result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "unauthenticated client must provide tenant and device ID in message address")); } else { result.complete(address); } } else { if (!Strings.isNullOrEmpty(address.getTenantId()) && Strings.isNullOrEmpty(address.getResourceId())) { result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "message address must not contain tenant ID only")); } else if (!Strings.isNullOrEmpty(address.getTenantId()) && !address.getTenantId().equals(authenticatedDevice.getTenantId())) { result.fail(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "can only publish for device of same tenant")); } else if (Strings.isNullOrEmpty(address.getTenantId()) && Strings.isNullOrEmpty(address.getResourceId())) { final ResourceIdentifier resource = ResourceIdentifier.from(address, authenticatedDevice.getTenantId(), authenticatedDevice.getDeviceId()); result.complete(resource); } else if (Strings.isNullOrEmpty(address.getTenantId())) { final ResourceIdentifier resource = ResourceIdentifier.from(address, authenticatedDevice.getTenantId(), address.getResourceId()); result.complete(resource); } else { result.complete(address); } } return result.future().recover(t -> { log.debug("validation failed for address [{}], device [{}]: {}", address, authenticatedDevice, t.getMessage()); return Future.failedFuture(t); }); }  @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 testValidateAddressUsesDeviceIdentityForAddressWithoutTenantAndDevice(final VertxTestContext ctx) { final Device authenticatedDevice = new Device("my-tenant", "4711"); final ResourceIdentifier address = ResourceIdentifier.fromString(TelemetryConstants.TELEMETRY_ENDPOINT); adapter.validateAddress(address, authenticatedDevice).onComplete(ctx.succeeding(r -> ctx.verify(() -> { assertEquals("my-tenant", r.getTenantId()); assertEquals("4711", r.getResourceId()); ctx.completeNow(); }))); }
@Test public void testValidateAddressUsesDeviceIdentityForAddressWithoutTenant(final VertxTestContext ctx) { final Device authenticatedDevice = new Device("my-tenant", "4711"); final ResourceIdentifier address = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, "", "4712"); adapter.validateAddress(address, authenticatedDevice).onComplete(ctx.succeeding(r -> ctx.verify(() -> { assertEquals("my-tenant", r.getTenantId()); assertEquals("4712", r.getResourceId()); ctx.completeNow(); }))); }