use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class EventResourceTest method testMessageLimitExceededForAnEventMessage.
/**
* Verifies that an event message is rejected due to the limit exceeded.
*
* @param ctx The vert.x test context.
*/
@Test
public void testMessageLimitExceededForAnEventMessage(final VertxTestContext ctx) {
// GIVEN an adapter with a downstream event consumer attached
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
final var resource = givenAResource(adapter);
// WHEN the message limit exceeds
when(adapter.checkMessageLimit(any(TenantObject.class), anyLong(), any())).thenReturn(Future.failedFuture(new ClientErrorException(429)));
// WHEN a device publishes an event message
final Buffer payload = Buffer.buffer("some payload");
final CoapExchange coapExchange = newCoapExchange(payload, Type.CON, MediaTypeRegistry.TEXT_PLAIN);
final Device authenticatedDevice = new Device("tenant", "device");
final CoapContext context = CoapContext.fromRequest(coapExchange, authenticatedDevice, authenticatedDevice, "device", span);
resource.handlePostRequest(context).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the message is not being forwarded downstream
assertNoEventHasBeenSentDownstream();
// and the device gets a 4.29
assertThat(t).isInstanceOf(ClientErrorException.class);
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(429);
verify(metrics).reportTelemetry(eq(MetricsTags.EndpointType.EVENT), eq("tenant"), any(), eq(MetricsTags.ProcessingOutcome.UNPROCESSABLE), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), eq(TtdStatus.NONE), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class EventResourceTest method testUploadEventWaitsForAcceptedOutcome.
/**
* Verifies that the adapter waits for an event being send with wait for outcome before responding with a 2.04
* 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);
final var resource = givenAResource(adapter);
// WHEN a device publishes an event
final Buffer payload = Buffer.buffer("some payload");
final CoapExchange coapExchange = newCoapExchange(payload, Type.CON, MediaTypeRegistry.TEXT_PLAIN);
final Device authenticatedDevice = new Device("tenant", "device");
final CoapContext context = CoapContext.fromRequest(coapExchange, authenticatedDevice, authenticatedDevice, "device", span);
resource.handlePostRequest(context);
// THEN the message is being forwarded downstream
assertEventHasBeenSentDownstream("tenant", "device", "text/plain");
// but the device does not get a response
verify(coapExchange, never()).respond(any(Response.class));
// until the event has been accepted
outcome.complete();
verify(coapExchange).respond(argThat((Response res) -> ResponseCode.CHANGED.equals(res.getCode())));
verify(metrics).reportTelemetry(eq(MetricsTags.EndpointType.EVENT), eq("tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), eq(TtdStatus.NONE), any());
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class TelemetryResourceTest method testUploadTelemetryFailsForMissingContentFormat.
/**
* Verifies that the adapter fails the upload of an event with a 4.00 result
* if the request body is not empty but doesn't contain a content-format option.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryFailsForMissingContentFormat(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
final var resource = givenAResource(adapter);
// WHEN a device publishes a non-empty message that lacks a content-format option
final Buffer payload = Buffer.buffer("some payload");
final CoapExchange coapExchange = newCoapExchange(payload, Type.NON, (Integer) null);
final Device authenticatedDevice = new Device("my-tenant", "the-device");
final CoapContext context = CoapContext.fromRequest(coapExchange, authenticatedDevice, authenticatedDevice, "the-device", span);
resource.handlePostRequest(context).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the device gets a response with code 4.00
assertThat(t).isInstanceOf(ClientErrorException.class);
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_BAD_REQUEST);
// and the message has not been forwarded downstream
assertNoTelemetryMessageHasBeenSentDownstream();
verify(metrics, never()).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), any(MetricsTags.ProcessingOutcome.class), any(MetricsTags.QoS.class), anyInt(), any(TtdStatus.class), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class TelemetryResourceTest method testUploadEmptyNotificationSucceeds.
/**
* Verifies that the adapter forwards an empty notification downstream.
*/
@Test
public void testUploadEmptyNotificationSucceeds() {
// GIVEN an adapter
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
final var resource = givenAResource(adapter);
// WHEN a device publishes an empty message that is marked as an empty notification
final OptionSet options = new OptionSet();
options.addUriQuery(CoapContext.PARAM_EMPTY_CONTENT);
final CoapExchange coapExchange = newCoapExchange(null, Type.NON, options);
final Device authenticatedDevice = new Device("my-tenant", "the-device");
final CoapContext context = CoapContext.fromRequest(coapExchange, authenticatedDevice, authenticatedDevice, "the-device", span);
resource.handlePostRequest(context);
// THEN the device gets a response indicating success
verify(coapExchange).respond(argThat((Response res) -> ResponseCode.CHANGED.equals(res.getCode())));
// and the message has been forwarded downstream
assertTelemetryMessageHasBeenSentDownstream(QoS.AT_MOST_ONCE, "my-tenant", "the-device", EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION);
verify(metrics).reportTelemetry(eq(MetricsTags.EndpointType.TELEMETRY), eq("my-tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_MOST_ONCE), eq(0), eq(TtdStatus.NONE), any());
}
use of org.eclipse.hono.auth.Device in project hono by eclipse.
the class TelemetryResourceTest method testUploadTelemetryFailsForDisabledTenant.
/**
* Verifies that the adapter fails the upload of an event with a 4.03 result if the device belongs to a tenant for
* which the adapter is disabled.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryFailsForDisabledTenant(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
final var resource = givenAResource(adapter);
// which is disabled for tenant "my-tenant"
when(adapter.isAdapterEnabled(any(TenantObject.class))).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN)));
// WHEN a device that belongs to "my-tenant" publishes a telemetry message
final Buffer payload = Buffer.buffer("some payload");
final CoapExchange coapExchange = newCoapExchange(payload, Type.NON, MediaTypeRegistry.TEXT_PLAIN);
final Device authenticatedDevice = new Device("my-tenant", "the-device");
final CoapContext context = CoapContext.fromRequest(coapExchange, authenticatedDevice, authenticatedDevice, "the-device", span);
resource.handlePostRequest(context).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the device gets a response with code 4.03
assertThat(t).isInstanceOf(ClientErrorException.class);
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_FORBIDDEN);
// and the message has not been forwarded downstream
assertNoTelemetryMessageHasBeenSentDownstream();
verify(metrics).reportTelemetry(eq(MetricsTags.EndpointType.TELEMETRY), eq("my-tenant"), any(), eq(MetricsTags.ProcessingOutcome.UNPROCESSABLE), eq(MetricsTags.QoS.AT_MOST_ONCE), eq(payload.length()), eq(TtdStatus.NONE), any());
});
ctx.completeNow();
}));
}
Aggregations