Search in sources :

Example 21 with HttpContext

use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.

the class LoraProtocolAdapterTest method customizeDownstreamMessageAddsProviderNameToMessage.

/**
 * Verifies that the provider name is added to the message when using customized downstream message.
 */
@Test
public void customizeDownstreamMessageAddsProviderNameToMessage() {
    final HttpContext httpContext = newHttpContext();
    final Map<String, Object> props = new HashMap<>();
    adapter.customizeDownstreamMessageProperties(props, httpContext);
    assertThat(props).containsEntry(LoraConstants.APP_PROPERTY_ORIG_LORA_PROVIDER, TEST_PROVIDER);
}
Also used : HashMap(java.util.HashMap) HttpContext(org.eclipse.hono.service.http.HttpContext) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 22 with HttpContext

use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.

the class LoraProtocolAdapterTest method handleProviderRouteDiscardsOtherMessages.

/**
 * Verifies that the provider route discards other messages.
 */
@Test
public void handleProviderRouteDiscardsOtherMessages() {
    givenATelemetrySenderForAnyTenant();
    final LoraMessage message = mock(LoraMessage.class);
    when(message.getType()).thenReturn(LoraMessageType.UNKNOWN);
    final LoraProvider providerMock = getLoraProviderMock(message);
    final HttpContext httpContext = newHttpContext();
    adapter.handleProviderRoute(httpContext, providerMock);
    verify(httpContext.getRoutingContext()).put(LoraConstants.APP_PROPERTY_ORIG_LORA_PROVIDER, TEST_PROVIDER);
    assertNoTelemetryMessageHasBeenSentDownstream();
    verify(httpContext.response()).setStatusCode(HttpResponseStatus.ACCEPTED.code());
    verify(processMessageSpan).finish();
}
Also used : LoraProvider(org.eclipse.hono.adapter.lora.providers.LoraProvider) HttpContext(org.eclipse.hono.service.http.HttpContext) Test(org.junit.jupiter.api.Test)

Example 23 with HttpContext

use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventWithTimeToLive.

/**
 * Verifies that the adapter uses the time to live value set in the down stream event message.
 */
@Test
public void testUploadEventWithTimeToLive() {
    // GIVEN an adapter with a downstream event consumer attached
    givenAnAdapter(properties);
    final Promise<Void> outcome = Promise.promise();
    givenAnEventSenderForAnyTenant(outcome);
    // WHEN a device publishes an event with a time to live value as a header
    final Buffer payload = Buffer.buffer("some payload");
    final HttpServerResponse response = mock(HttpServerResponse.class);
    final HttpServerRequest request = mock(HttpServerRequest.class);
    when(request.uri()).thenReturn("/" + EventConstants.EVENT_ENDPOINT);
    when(request.getHeader(eq(Constants.HEADER_TIME_TO_LIVE))).thenReturn("10");
    final HttpContext ctx = newHttpContext(payload, "text/plain", request, response);
    when(ctx.getRoutingContext().addBodyEndHandler(VertxMockSupport.anyHandler())).thenAnswer(invocation -> {
        final Handler<Void> handler = invocation.getArgument(0);
        handler.handle(null);
        return 0;
    });
    adapter.uploadEventMessage(ctx, "tenant", "device");
    // verifies that the downstream message contains the time to live value
    assertEventHasBeenSentDownstream("tenant", "device", "text/plain", 10L);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpContext(org.eclipse.hono.service.http.HttpContext) Test(org.junit.jupiter.api.Test)

Example 24 with HttpContext

use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testMessageLimitExceededForAnEventMessage.

/**
 * Verifies that an event message is rejected due to the limit exceeded.
 */
@Test
public void testMessageLimitExceededForAnEventMessage() {
    // GIVEN an adapter with a downstream event consumer attached
    givenAnAdapter(properties);
    givenAnEventSenderForAnyTenant();
    final Buffer payload = Buffer.buffer("some payload");
    final HttpContext routingContext = newHttpContext(payload);
    // WHEN the message limit exceeds
    when(resourceLimitChecks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
    // WHEN a device that belongs to "my-tenant" publishes an event message
    adapter.uploadEventMessage(routingContext, "my-tenant", "the-device", payload, "application/text");
    // THEN the device gets a 429
    assertContextFailedWithClientError(routingContext, HttpUtils.HTTP_TOO_MANY_REQUESTS);
    assertNoEventHasBeenSentDownstream();
    // the message has been reported
    verify(metrics).reportTelemetry(eq(EndpointType.EVENT), eq("my-tenant"), any(), eq(ProcessingOutcome.UNPROCESSABLE), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), eq(TtdStatus.NONE), any());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) HttpContext(org.eclipse.hono.service.http.HttpContext) Test(org.junit.jupiter.api.Test)

Example 25 with HttpContext

use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadTelemetryFailsForDisabledTenant.

/**
 * Verifies that the adapter fails the upload of a message with a 403
 * result if the device belongs to a tenant for which the adapter is
 * disabled.
 */
@Test
public void testUploadTelemetryFailsForDisabledTenant() {
    // GIVEN an adapter
    givenAnAdapter(properties);
    givenATelemetrySenderForAnyTenant();
    // which is disabled for tenant "my-tenant"
    final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
    myTenantConfig.addAdapter(new Adapter(ADAPTER_TYPE).setEnabled(Boolean.FALSE));
    when(tenantClient.get(eq("my-tenant"), any())).thenReturn(Future.succeededFuture(myTenantConfig));
    // WHEN a device that belongs to "my-tenant" publishes a telemetry message
    final Buffer payload = Buffer.buffer("some payload");
    final HttpContext ctx = newHttpContext(payload);
    adapter.uploadTelemetryMessage(ctx, "my-tenant", "the-device", payload, "application/text");
    // THEN the device gets a 403
    assertContextFailedWithClientError(ctx, HttpURLConnection.HTTP_FORBIDDEN);
    // and no Command consumer has been created for the device
    verify(commandConsumerFactory, never()).createCommandConsumer(anyString(), anyString(), VertxMockSupport.anyHandler(), any(), any());
    // and the message has not been forwarded downstream
    assertNoTelemetryMessageHasBeenSentDownstream();
    // and has not been reported as processed
    verify(metrics, never()).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), any(MetricsTags.QoS.class), anyInt(), any(MetricsTags.TtdStatus.class), any());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TenantObject(org.eclipse.hono.util.TenantObject) QoS(org.eclipse.hono.util.QoS) HttpContext(org.eclipse.hono.service.http.HttpContext) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) TtdStatus(org.eclipse.hono.service.metric.MetricsTags.TtdStatus) Adapter(org.eclipse.hono.util.Adapter) Test(org.junit.jupiter.api.Test)

Aggregations

HttpContext (org.eclipse.hono.service.http.HttpContext)31 Test (org.junit.jupiter.api.Test)27 Buffer (io.vertx.core.buffer.Buffer)21 HttpServerResponse (io.vertx.core.http.HttpServerResponse)18 HttpServerRequest (io.vertx.core.http.HttpServerRequest)15 TenantObject (org.eclipse.hono.util.TenantObject)11 SpanContext (io.opentracing.SpanContext)9 ClientErrorException (org.eclipse.hono.client.ClientErrorException)9 Handler (io.vertx.core.Handler)8 RoutingContext (io.vertx.ext.web.RoutingContext)8 LoraProvider (org.eclipse.hono.adapter.lora.providers.LoraProvider)8 CommandConsumer (org.eclipse.hono.client.command.CommandConsumer)8 EndpointType (org.eclipse.hono.service.metric.MetricsTags.EndpointType)8 TtdStatus (org.eclipse.hono.service.metric.MetricsTags.TtdStatus)8 Future (io.vertx.core.Future)6 HashMap (java.util.HashMap)6 Span (io.opentracing.Span)5 AsyncResult (io.vertx.core.AsyncResult)5 HttpURLConnection (java.net.HttpURLConnection)5 ServerErrorException (org.eclipse.hono.client.ServerErrorException)5