Search in sources :

Example 31 with Command

use of org.eclipse.hono.client.command.Command in project hono by eclipse.

the class AbstractMappingAndDelegatingCommandHandler method reportInvalidCommand.

/**
 * Reports an invalid command.
 *
 * @param commandContext The context of the command to report.
 * @param timer The timer indicating the amount of time used for processing the command message.
 */
protected void reportInvalidCommand(final CommandContext commandContext, final Timer.Sample timer) {
    final Command command = commandContext.getCommand();
    final Future<TenantObject> tenantObjectFuture = tenantClient.get(command.getTenant(), commandContext.getTracingContext());
    tenantObjectFuture.recover(// ignore error here
    thr -> Future.succeededFuture(null)).onSuccess(tenantObjectOrNull -> {
        metrics.reportCommand(command.isOneWay() ? MetricsTags.Direction.ONE_WAY : MetricsTags.Direction.REQUEST, command.getTenant(), tenantObjectOrNull, MetricsTags.ProcessingOutcome.UNPROCESSABLE, command.getPayloadSize(), timer);
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InternalCommandSender(org.eclipse.hono.client.command.InternalCommandSender) Command(org.eclipse.hono.client.command.Command) TenantConstants(org.eclipse.hono.util.TenantConstants) Lifecycle(org.eclipse.hono.util.Lifecycle) LoggerFactory(org.slf4j.LoggerFactory) ClientErrorException(org.eclipse.hono.client.ClientErrorException) TenantDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.TenantDisabledOrNotRegisteredException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Tags(io.opentracing.tag.Tags) NoConsumerException(org.eclipse.hono.client.NoConsumerException) DeviceDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.DeviceDisabledOrNotRegisteredException) MessagingType(org.eclipse.hono.util.MessagingType) Timer(io.micrometer.core.instrument.Timer) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) DeviceConnectionConstants(org.eclipse.hono.util.DeviceConnectionConstants) CommandContext(org.eclipse.hono.client.command.CommandContext) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) ServerErrorException(org.eclipse.hono.client.ServerErrorException) CommandTargetMapper(org.eclipse.hono.commandrouter.CommandTargetMapper) TenantClient(org.eclipse.hono.client.registry.TenantClient) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) CommandRouterMetrics(org.eclipse.hono.commandrouter.CommandRouterMetrics) TenantTraceSamplingHelper(org.eclipse.hono.tracing.TenantTraceSamplingHelper) Optional(java.util.Optional) Span(io.opentracing.Span) TenantObject(org.eclipse.hono.util.TenantObject) Command(org.eclipse.hono.client.command.Command)

Example 32 with Command

use of org.eclipse.hono.client.command.Command in project hono by eclipse.

the class HttpBasedMessageMappingTest method testMapCommandSucceedsIfNoMapperIsSet.

/**
 * Verifies that the result returned by the mapping service contains the
 * original payload and target address if no upstream mapper has been defined for
 * the gateway.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testMapCommandSucceedsIfNoMapperIsSet(final VertxTestContext ctx) {
    config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
    final Command command = mock(Command.class);
    final Buffer payload = Buffer.buffer("payload");
    when(command.getPayload()).thenReturn(payload);
    messageMapping.mapUpstreamMessage(new RegistrationAssertion("gateway"), command).onComplete(ctx.succeeding(mappedBuffer -> {
        ctx.verify(() -> {
            assertThat(mappedBuffer).isEqualTo(payload);
            verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
        });
        ctx.completeNow();
    }));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) HttpResponse(io.vertx.ext.web.client.HttpResponse) WebClient(io.vertx.ext.web.client.WebClient) Command(org.eclipse.hono.client.command.Command) MultiMap(io.vertx.core.MultiMap) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Constants(org.eclipse.hono.util.Constants) RETURNS_SELF(org.mockito.Mockito.RETURNS_SELF) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MapperEndpoint(org.eclipse.hono.config.MapperEndpoint) ServerErrorException(org.eclipse.hono.client.ServerErrorException) HttpHeaders(io.vertx.core.http.HttpHeaders) Mockito.times(org.mockito.Mockito.times) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Device(org.eclipse.hono.auth.Device) Mockito.verify(org.mockito.Mockito.verify) HttpRequest(io.vertx.ext.web.client.HttpRequest) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) Mockito.withSettings(org.mockito.Mockito.withSettings) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Command(org.eclipse.hono.client.command.Command) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test)

Example 33 with Command

use of org.eclipse.hono.client.command.Command in project hono by eclipse.

the class HttpBasedMessageMappingTest method testMapCommandSucceedsIfNoMapperEndpointIsConfigured.

/**
 * Verifies that the result returned by the mapping service contains the
 * original payload and target address if no upstream mapper endpoint has been configured
 * for the adapter.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@Test
public void testMapCommandSucceedsIfNoMapperEndpointIsConfigured(final VertxTestContext ctx) {
    final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setUpstreamMessageMapper("mapper");
    final Command command = mock(Command.class);
    final Buffer payload = Buffer.buffer("payload");
    when(command.getPayload()).thenReturn(payload);
    messageMapping.mapUpstreamMessage(assertion, command).onComplete(ctx.succeeding(mappedBuffer -> {
        ctx.verify(() -> {
            assertThat(mappedBuffer).isEqualTo(payload);
            verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
        });
        ctx.completeNow();
    }));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) HttpResponse(io.vertx.ext.web.client.HttpResponse) WebClient(io.vertx.ext.web.client.WebClient) Command(org.eclipse.hono.client.command.Command) MultiMap(io.vertx.core.MultiMap) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Constants(org.eclipse.hono.util.Constants) RETURNS_SELF(org.mockito.Mockito.RETURNS_SELF) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MapperEndpoint(org.eclipse.hono.config.MapperEndpoint) ServerErrorException(org.eclipse.hono.client.ServerErrorException) HttpHeaders(io.vertx.core.http.HttpHeaders) Mockito.times(org.mockito.Mockito.times) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Device(org.eclipse.hono.auth.Device) Mockito.verify(org.mockito.Mockito.verify) HttpRequest(io.vertx.ext.web.client.HttpRequest) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) Mockito.withSettings(org.mockito.Mockito.withSettings) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Command(org.eclipse.hono.client.command.Command) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test)

Example 34 with Command

use of org.eclipse.hono.client.command.Command in project hono by eclipse.

the class SigfoxProtocolAdapter method setNonEmptyResponsePayload.

@Override
protected void setNonEmptyResponsePayload(final HttpServerResponse response, final CommandContext commandContext, final Span currentSpan) {
    currentSpan.log("responding with: payload");
    final Command command = commandContext.getCommand();
    response.setStatusCode(HttpURLConnection.HTTP_OK);
    final Buffer payload = convertToResponsePayload(command);
    LOG.debug("Setting response for ACK: {}", payload);
    HttpUtils.setResponseBody(response, payload, HttpUtils.CONTENT_TYPE_JSON);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Command(org.eclipse.hono.client.command.Command)

Aggregations

Command (org.eclipse.hono.client.command.Command)34 Span (io.opentracing.Span)23 Future (io.vertx.core.Future)23 HttpURLConnection (java.net.HttpURLConnection)22 ServerErrorException (org.eclipse.hono.client.ServerErrorException)22 CommandContext (org.eclipse.hono.client.command.CommandContext)22 Map (java.util.Map)19 Objects (java.util.Objects)19 Optional (java.util.Optional)19 Device (org.eclipse.hono.auth.Device)19 Constants (org.eclipse.hono.util.Constants)19 Buffer (io.vertx.core.buffer.Buffer)18 TracingHelper (org.eclipse.hono.tracing.TracingHelper)18 SpanContext (io.opentracing.SpanContext)17 Tags (io.opentracing.tag.Tags)17 Handler (io.vertx.core.Handler)17 ClientErrorException (org.eclipse.hono.client.ClientErrorException)17 MessageHelper (org.eclipse.hono.util.MessageHelper)17 TenantObject (org.eclipse.hono.util.TenantObject)17 List (java.util.List)16