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);
}
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();
}
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);
}
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());
}
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());
}
Aggregations