Search in sources :

Example 11 with RegistrationAssertion

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());
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) CommandResponse(org.eclipse.hono.client.command.CommandResponse) Test(org.junit.jupiter.api.Test)

Example 12 with RegistrationAssertion

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());
}
Also used : ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) EventSender(org.eclipse.hono.client.telemetry.EventSender) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) CommandResponse(org.eclipse.hono.client.command.CommandResponse) Test(org.junit.jupiter.api.Test)

Example 13 with RegistrationAssertion

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;
}
Also used : RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion)

Example 14 with RegistrationAssertion

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();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Commands(org.eclipse.hono.client.command.Commands) Timeout(io.vertx.junit5.Timeout) MessagingType(org.eclipse.hono.util.MessagingType) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Message(org.apache.qpid.proton.message.Message) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) HonoConnection(org.eclipse.hono.client.HonoConnection) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Tracer(io.opentracing.Tracer) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) ClientErrorException(org.eclipse.hono.client.ClientErrorException) CommandResponse(org.eclipse.hono.client.command.CommandResponse) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.jupiter.api.Test)

Example 15 with RegistrationAssertion

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();
}
Also used : AddressHelper(org.eclipse.hono.util.AddressHelper) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) ProtonHelper(io.vertx.proton.ProtonHelper) MessageHelper(org.eclipse.hono.util.MessageHelper) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Future(io.vertx.core.Future) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) AbstractServiceClient(org.eclipse.hono.client.amqp.AbstractServiceClient) DownstreamAmqpMessageFactory(org.eclipse.hono.client.amqp.DownstreamAmqpMessageFactory) DownstreamMessageProperties(org.eclipse.hono.client.util.DownstreamMessageProperties) StatusCodeMapper(org.eclipse.hono.client.StatusCodeMapper) Map(java.util.Map) Span(io.opentracing.Span) Message(org.apache.qpid.proton.message.Message) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) HonoConnection(org.eclipse.hono.client.HonoConnection) GenericSenderLink(org.eclipse.hono.client.amqp.GenericSenderLink) CommandConstants(org.eclipse.hono.util.CommandConstants) Message(org.apache.qpid.proton.message.Message) Span(io.opentracing.Span)

Aggregations

RegistrationAssertion (org.eclipse.hono.util.RegistrationAssertion)42 Future (io.vertx.core.Future)31 HttpURLConnection (java.net.HttpURLConnection)29 MessageHelper (org.eclipse.hono.util.MessageHelper)29 Map (java.util.Map)27 Span (io.opentracing.Span)26 TenantObject (org.eclipse.hono.util.TenantObject)26 Buffer (io.vertx.core.buffer.Buffer)24 Constants (org.eclipse.hono.util.Constants)22 ServerErrorException (org.eclipse.hono.client.ServerErrorException)21 Device (org.eclipse.hono.auth.Device)20 Test (org.junit.jupiter.api.Test)20 ClientErrorException (org.eclipse.hono.client.ClientErrorException)18 Handler (io.vertx.core.Handler)17 Objects (java.util.Objects)17 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)17 HashMap (java.util.HashMap)16 Command (org.eclipse.hono.client.command.Command)16 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 SpanContext (io.opentracing.SpanContext)15