use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testGetCommandResponseSenderSetOnCommandResponse.
/**
* Verifies that the messaging type encoded in a command response message is getting used when sending the
* command response.
*/
@Test
public void testGetCommandResponseSenderSetOnCommandResponse() {
final CommandResponse kafkaResponse = CommandResponse.fromRequestId(Commands.encodeRequestIdParameters("", "replyTo", "4711", MessagingType.kafka), Constants.DEFAULT_TENANT, "4711", null, null, HttpURLConnection.HTTP_OK);
final TenantObject tenant = new TenantObject("tenant", true);
final var device = new RegistrationAssertion("4711");
tenant.setProperty(TenantConstants.FIELD_EXT, Map.of(TenantConstants.FIELD_EXT_MESSAGING_TYPE, MessagingType.amqp.name()));
adapter.sendCommandResponse(tenant, device, kafkaResponse, null);
verify(kafkaCommandResponseSender).sendCommandResponse(eq(tenant), eq(device), eq(kafkaResponse), any());
verify(amqpCommandResponseSender, never()).sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), any());
final CommandResponse amqpResponse = CommandResponse.fromRequestId(Commands.encodeRequestIdParameters("", "replyTo", "4711", MessagingType.amqp), Constants.DEFAULT_TENANT, "4711", null, null, HttpURLConnection.HTTP_OK);
tenant.setProperty(TenantConstants.FIELD_EXT, Map.of(TenantConstants.FIELD_EXT_MESSAGING_TYPE, MessagingType.kafka.name()));
adapter.sendCommandResponse(tenant, device, amqpResponse, null);
verify(amqpCommandResponseSender).sendCommandResponse(eq(tenant), eq(device), eq(amqpResponse), any());
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testGetCommandResponseSenderConfiguredOnTenant.
/**
* Verifies that when the messaging system to be used, as configured for the command response, is not available,
* then the messaging system type configuration from the tenant is used.
*/
@Test
public void testGetCommandResponseSenderConfiguredOnTenant() {
final var commandResponseSenderProvider = new MessagingClientProvider<CommandResponseSender>().setClient(amqpCommandResponseSender);
messagingClientProviders = new MessagingClientProviders(new MessagingClientProvider<TelemetrySender>().setClient(amqpTelemetrySender), new MessagingClientProvider<EventSender>().setClient(amqpEventSender), commandResponseSenderProvider);
properties = new ProtocolAdapterProperties();
adapter = newProtocolAdapter(properties, ADAPTER_NAME);
setCollaborators(adapter);
final CommandResponse response = CommandResponse.fromRequestId(Commands.encodeRequestIdParameters("", "replyTo", "4711", MessagingType.kafka), Constants.DEFAULT_TENANT, "4711", null, null, HttpURLConnection.HTTP_OK);
final TenantObject tenant = new TenantObject("tenant", true);
final var device = new RegistrationAssertion("4711");
tenant.setProperty(TenantConstants.FIELD_EXT, Map.of(TenantConstants.FIELD_EXT_MESSAGING_TYPE, MessagingType.amqp.name()));
adapter.sendCommandResponse(tenant, device, response, null);
verify(amqpCommandResponseSender).sendCommandResponse(eq(tenant), eq(device), eq(response), any());
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method newRegistrationAssertionResult.
private static RegistrationAssertion newRegistrationAssertionResult(final String deviceId, final String defaultContentType) {
final RegistrationAssertion result = new RegistrationAssertion(deviceId);
Optional.ofNullable(defaultContentType).ifPresent(ct -> result.setDefaults(Map.of(MessageHelper.SYS_PROPERTY_CONTENT_TYPE, ct)));
return result;
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class ProtonBasedCommandResponseSenderTest method testSenderClientCreationErrorIsMappedToServerError.
/**
* Verifies that a ClientErrorException when creating an AMQP sender is returned as a server error
* on the <em>sendCommandResponse</em> invocation.
*
* @param ctx The vert.x test context.
*/
@Test
public void testSenderClientCreationErrorIsMappedToServerError(final VertxTestContext ctx) {
// GIVEN a scenario where creating the AMQP sender always fails with a client error
when(connection.createSender(anyString(), any(), any())).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "cannot open sender")));
// WHEN sending a command response message
final CommandResponse commandResponse = CommandResponse.fromRequestId(Commands.encodeRequestIdParameters(CORRELATION_ID, REPLY_TO_ID, DEVICE_ID, MessagingType.amqp), TENANT_ID, DEVICE_ID, null, null, HttpURLConnection.HTTP_OK);
sender.sendCommandResponse(TenantObject.from(TENANT_ID), new RegistrationAssertion(DEVICE_ID), commandResponse, span.context()).onComplete(ctx.failing(thr -> {
ctx.verify(() -> {
// THEN the invocation is failed with a server error
assertThat(thr).isInstanceOf(ServiceInvocationException.class);
assertThat(((ServiceInvocationException) thr).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class ProtonBasedCommandResponseSender method sendCommandResponse.
@Override
public Future<Void> sendCommandResponse(final TenantObject tenant, final RegistrationAssertion device, final CommandResponse response, final SpanContext context) {
Objects.requireNonNull(tenant);
Objects.requireNonNull(device);
Objects.requireNonNull(response);
final var sender = createSender(response.getTenantId(), response.getReplyToId());
return sender.recover(thr -> Future.failedFuture(StatusCodeMapper.toServerError(thr))).compose(s -> {
final Message msg = createDownstreamMessage(response, tenant, device, response.getAdditionalProperties());
final Span span = newChildSpan(context, "forward Command response");
if (response.getMessagingType() != getMessagingType()) {
span.log(String.format("using messaging type %s instead of type %s used for the original command", getMessagingType(), response.getMessagingType()));
}
return s.sendAndWaitForOutcome(msg, span);
}).onSuccess(delivery -> sender.result().close()).mapEmpty();
}
Aggregations