use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method givenAConfiguredTenant.
private TenantObject givenAConfiguredTenant(final String tenantId, final boolean enabled) {
final TenantObject tenantConfig = TenantObject.from(tenantId, Boolean.TRUE);
tenantConfig.addAdapter(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_AMQP).setEnabled(enabled));
when(tenantClient.get(eq(tenantId), (SpanContext) any())).thenReturn(Future.succeededFuture(tenantConfig));
return tenantConfig;
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testUploadTelemetryWithAtMostOnceDeliverySemantics.
/**
* Verifies that a request to upload a pre-settled telemetry message results
* in the downstream sender not waiting for the consumer's acknowledgment.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryWithAtMostOnceDeliverySemantics(final VertxTestContext ctx) {
// GIVEN an AMQP adapter with a configured server
givenAnAdapter(properties);
// sending of downstream telemetry message succeeds
givenATelemetrySenderForAnyTenant();
// which is enabled for a tenant
final TenantObject tenantObject = givenAConfiguredTenant(TEST_TENANT_ID, true);
// IF a device sends a 'fire and forget' telemetry message
final ProtonDelivery delivery = mock(ProtonDelivery.class);
when(delivery.remotelySettled()).thenReturn(true);
final Buffer payload = Buffer.buffer("payload");
final String to = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, TEST_DEVICE).toString();
adapter.onMessageReceived(AmqpContext.fromMessage(delivery, getFakeMessage(to, payload), span, null)).onComplete(ctx.succeeding(d -> {
ctx.verify(() -> {
// THEN the adapter has forwarded the message downstream
assertTelemetryMessageHasBeenSentDownstream(QoS.AT_MOST_ONCE, TEST_TENANT_ID, TEST_DEVICE, "text/plain");
// and acknowledged the message to the device
verify(delivery).disposition(any(Accepted.class), eq(true));
// and has reported the telemetry message
verify(metrics).reportTelemetry(eq(EndpointType.TELEMETRY), eq(TEST_TENANT_ID), eq(tenantObject), eq(ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_MOST_ONCE), eq(payload.length()), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testUploadEventRejectsPresettledMessage.
/**
* Verifies that the adapter rejects presettled messages with an event address.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadEventRejectsPresettledMessage(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
// with an enabled tenant
givenAConfiguredTenant(TEST_TENANT_ID, true);
// WHEN a device uploads an event using a presettled message
final Device gateway = new Device(TEST_TENANT_ID, "device");
final ProtonDelivery delivery = mock(ProtonDelivery.class);
// AT MOST ONCE
when(delivery.remotelySettled()).thenReturn(true);
final String to = ResourceIdentifier.fromString(EventConstants.EVENT_ENDPOINT).toString();
final Buffer payload = Buffer.buffer("some payload");
adapter.onMessageReceived(AmqpContext.fromMessage(delivery, getFakeMessage(to, payload), span, gateway)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the adapter does not forward the event
assertNoEventHasBeenSentDownstream();
// AND notifies the device by sending back a REJECTED disposition
verify(delivery).disposition(any(Rejected.class), eq(true));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testUploadEventFailsForGatewayOfDifferentTenant.
/**
* Verifies that a request from a gateway to upload an event on behalf of a device that belongs
* to another tenant than the gateway fails.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadEventFailsForGatewayOfDifferentTenant(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
// with an enabled tenant
givenAConfiguredTenant(TEST_TENANT_ID, true);
// WHEN a gateway uploads an event on behalf of a device of another tenant
final Device gateway = new Device(TEST_TENANT_ID, "gw");
final ProtonDelivery delivery = mock(ProtonDelivery.class);
// AT LEAST ONCE
when(delivery.remotelySettled()).thenReturn(false);
final String to = ResourceIdentifier.from(EventConstants.EVENT_ENDPOINT, "other-tenant", TEST_DEVICE).toString();
final Buffer payload = Buffer.buffer("some payload");
adapter.onMessageReceived(AmqpContext.fromMessage(delivery, getFakeMessage(to, payload), span, gateway)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the adapter does not send the event
assertNoEventHasBeenSentDownstream();
// AND notifies the device by sending back a REJECTED disposition
verify(delivery).disposition(any(Rejected.class), eq(true));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testUploadCommandResponseWithoutPayloadSucceeds.
/**
* Verify that the AMQP adapter forwards command responses that do not contain a payload downstream.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadCommandResponseWithoutPayloadSucceeds(final VertxTestContext ctx) {
// GIVEN an AMQP adapter
givenAnAdapter(properties);
final CommandResponseSender responseSender = givenACommandResponseSenderForAnyTenant();
when(responseSender.sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any())).thenReturn(Future.succeededFuture());
// which is enabled for the test tenant
final TenantObject tenantObject = givenAConfiguredTenant(TEST_TENANT_ID, true);
// WHEN an unauthenticated device publishes a command response
final String replyToAddress = String.format("%s/%s/%s", getCommandResponseEndpoint(), TEST_TENANT_ID, Commands.getDeviceFacingReplyToId("test-reply-id", TEST_DEVICE, MessagingType.amqp));
final Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put(MessageHelper.APP_PROPERTY_STATUS, 200);
final ApplicationProperties props = new ApplicationProperties(propertyMap);
final Message message = getFakeMessage(replyToAddress, null);
message.setCorrelationId("correlation-id");
message.setApplicationProperties(props);
final ProtonDelivery delivery = mock(ProtonDelivery.class);
adapter.onMessageReceived(AmqpContext.fromMessage(delivery, message, span, null)).onComplete(ctx.succeeding(ok -> {
ctx.verify(() -> {
// THEN the adapter forwards the command response message downstream
verify(responseSender).sendCommandResponse(eq(tenantObject), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any());
// and reports the forwarded message
verify(metrics).reportCommand(eq(Direction.RESPONSE), eq(TEST_TENANT_ID), eq(tenantObject), eq(ProcessingOutcome.FORWARDED), eq(0), any());
});
ctx.completeNow();
}));
}
Aggregations