use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testMessageLimitExceededForACommandResponseMessage.
/**
* Verifies that a command response message is rejected due to the limit exceeded.
*/
@Test
public void testMessageLimitExceededForACommandResponseMessage() {
// GIVEN an adapter with a downstream application attached
givenAnAdapter(properties);
givenACommandResponseSenderForAnyTenant();
// WHEN the message limit exceeds
when(resourceLimitChecks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
// WHEN a device publishes a command response
final Buffer payload = Buffer.buffer("some payload");
final HttpServerResponse response = mock(HttpServerResponse.class);
final HttpContext ctx = newHttpContext(payload, "application/text", mock(HttpServerRequest.class), response);
when(ctx.getRoutingContext().addBodyEndHandler(VertxMockSupport.anyHandler())).thenAnswer(invocation -> {
final Handler<Void> handler = invocation.getArgument(0);
handler.handle(null);
return 0;
});
adapter.uploadCommandResponseMessage(ctx, "tenant", "device", CMD_REQ_ID, 200);
// THEN the device gets a 429
assertContextFailedWithClientError(ctx, HttpUtils.HTTP_TOO_MANY_REQUESTS);
// AND has reported the message as unprocessable
verify(metrics).reportCommand(eq(Direction.RESPONSE), eq("tenant"), any(), eq(ProcessingOutcome.UNPROCESSABLE), eq(payload.length()), any());
}
use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadCommandResponseFailsForOtherDevice.
/**
* Verifies that an authenticated device that is not a gateway fails to
* upload a command response for another device.
*/
@Test
public void testUploadCommandResponseFailsForOtherDevice() {
givenAnAdapter(properties);
final Buffer payload = Buffer.buffer("some payload");
final HttpContext ctx = newHttpContext(payload, "application/text", mock(HttpServerRequest.class), mock(HttpServerResponse.class));
final TenantObject to = TenantObject.from("tenant", true);
// Given an adapter that is enabled for a device's tenant
to.addAdapter(new Adapter(ADAPTER_TYPE).setEnabled(Boolean.TRUE));
when(tenantClient.get(eq("tenant"), (SpanContext) any())).thenReturn(Future.succeededFuture(to));
// which is connected to a Credentials service that has credentials on record for device 9999
when(ctx.getAuthenticatedDevice()).thenReturn(new DeviceUser("tenant", "9999"));
// but for which no registration information is available
when(registrationClient.assertRegistration(anyString(), anyString(), any(), any())).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "cannot publish data for device of other tenant")));
adapter.uploadCommandResponseMessage(ctx, "tenant", "device", CMD_REQ_ID, 200);
// Then the device gets a 404
assertContextFailedWithClientError(ctx, HttpURLConnection.HTTP_NOT_FOUND);
// and the response has not been reported as forwarded
verify(metrics).reportCommand(eq(Direction.RESPONSE), eq("tenant"), eq(to), eq(ProcessingOutcome.UNPROCESSABLE), eq(payload.length()), any());
}
use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventFailsForRejectedOutcome.
/**
* Verifies that the adapter fails the upload of an event with a 400
* result if it is rejected by the downstream peer.
*/
@Test
public void testUploadEventFailsForRejectedOutcome() {
// 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 that is not accepted by the peer
final Buffer payload = Buffer.buffer("some payload");
final HttpContext ctx = newHttpContext(payload, newEventRequest(), mock(HttpServerResponse.class));
adapter.uploadEventMessage(ctx, "tenant", "device", payload, "application/text");
assertEventHasBeenSentDownstream("tenant", "device", "application/text");
outcome.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "malformed message"));
// THEN the device gets a 400
assertContextFailedWithClientError(ctx, HttpURLConnection.HTTP_BAD_REQUEST);
// 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());
}
use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventWaitsForAcceptedOutcome.
/**
* Verifies that the adapter waits for an event being settled and accepted
* by a downstream peer before responding with a 202 status to the device.
*/
@Test
public void testUploadEventWaitsForAcceptedOutcome() {
// 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
final Buffer payload = Buffer.buffer("some payload");
final HttpServerResponse response = mock(HttpServerResponse.class);
final HttpContext ctx = newHttpContext(payload, "application/text", newEventRequest(), 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");
assertEventHasBeenSentDownstream("tenant", "device", "application/text");
// THEN the device does not get a response
verify(response, never()).end();
// and the message is not reported as being 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());
// until the event has been accepted
outcome.complete();
verify(response).setStatusCode(202);
verify(response).end();
verify(metrics).reportTelemetry(eq(MetricsTags.EndpointType.EVENT), eq("tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), eq(MetricsTags.TtdStatus.NONE), any());
}
use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadTelemetryFailsForUnknownDevice.
/**
* Verifies that the adapter fails the upload of a message containing
* a TTD value with a 404 result if the device is not registered.
* Also verifies that the adapter does not open a command consumer for
* the device in this case.
*/
@Test
public void testUploadTelemetryFailsForUnknownDevice() {
// GIVEN an adapter
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
// with an enabled tenant
final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
when(tenantClient.get(eq("my-tenant"), any())).thenReturn(Future.succeededFuture(myTenantConfig));
// WHEN an unknown device that supposedly belongs to that tenant publishes a telemetry message
// with a TTD value set
when(registrationClient.assertRegistration(eq("my-tenant"), eq("unknown-device"), any(), any(SpanContext.class))).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
final Buffer payload = Buffer.buffer("some payload");
final HttpServerResponse response = mock(HttpServerResponse.class);
final HttpServerRequest request = mock(HttpServerRequest.class);
when(request.getHeader(eq(Constants.HEADER_TIME_TILL_DISCONNECT))).thenReturn("5");
final HttpContext ctx = newHttpContext(payload, "application/text", request, response);
adapter.uploadTelemetryMessage(ctx, "my-tenant", "unknown-device", payload, "application/text");
// THEN the device gets a 404
assertContextFailedWithClientError(ctx, HttpURLConnection.HTTP_NOT_FOUND);
verify(registrationClient).assertRegistration(eq("my-tenant"), eq("unknown-device"), any(), any(SpanContext.class));
// and the message has not been forwarded downstream
assertNoTelemetryMessageHasBeenSentDownstream();
// and no Command consumer has been created for the device
verify(commandConsumerFactory, never()).createCommandConsumer(anyString(), anyString(), VertxMockSupport.anyHandler(), any(), any());
// 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