use of org.eclipse.hono.messaging.UpstreamReceiver in project hono by eclipse.
the class ForwardingTelemetryDownstreamAdapterTest method testProcessMessageForwardsDownstreamDisposition.
/**
* Verifies that an unsettled telemetry message received from an upstream client is
* forwarded to the downstream container and the downstream container's disposition
* is forwarded to the upstream client.
*
* @param ctx The test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessMessageForwardsDownstreamDisposition(final TestContext ctx) {
final UpstreamReceiver client = TestSupport.newClient();
// GIVEN an adapter with a connection to a downstream container
final ProtonSender sender = TestSupport.newMockSender(false);
final ForwardingTelemetryDownstreamAdapter adapter = new ForwardingTelemetryDownstreamAdapter(vertx, TestSupport.newMockSenderFactory(sender));
adapter.setMetrics(mock(MessagingMetrics.class));
adapter.setDownstreamConnectionFactory(connectionFactory);
adapter.start(Future.future());
adapter.addSender(client, sender);
// WHEN processing an unsettled telemetry message
final Message msg = ProtonHelper.message(TELEMETRY_MSG_CONTENT);
MessageHelper.addDeviceId(msg, DEVICE_ID);
final ProtonDelivery upstreamDelivery = mock(ProtonDelivery.class);
when(upstreamDelivery.remotelySettled()).thenReturn(Boolean.FALSE);
adapter.processMessage(client, upstreamDelivery, msg);
// THEN the message is being delivered to the downstream container
final ArgumentCaptor<Handler> deliveryHandler = ArgumentCaptor.forClass(Handler.class);
verify(sender).send(eq(msg), deliveryHandler.capture());
// and when the downstream container rejects the message
final ProtonDelivery downstreamDelivery = mock(ProtonDelivery.class);
when(downstreamDelivery.remotelySettled()).thenReturn(Boolean.TRUE);
when(downstreamDelivery.getRemoteState()).thenReturn(new Rejected());
deliveryHandler.getValue().handle(downstreamDelivery);
// then the upstream delivery is settled with the rejected outcome
verify(upstreamDelivery).disposition(any(Rejected.class), eq(Boolean.TRUE));
}
use of org.eclipse.hono.messaging.UpstreamReceiver in project hono by eclipse.
the class TestSupport method newClient.
/**
* Creates a new mock upstream client for a link and connection ID.
*
* @param linkId The client's link ID.
* @param connectionId The client's conenction ID.
* @return The new client.
*/
public static UpstreamReceiver newClient(final String linkId, final String connectionId) {
UpstreamReceiver client = mock(UpstreamReceiver.class);
when(client.getLinkId()).thenReturn(linkId);
when(client.getConnectionId()).thenReturn(connectionId);
when(client.getTargetAddress()).thenReturn(DEFAULT_ADDRESS);
return client;
}
use of org.eclipse.hono.messaging.UpstreamReceiver in project hono by eclipse.
the class ForwardingEventDownstreamAdapterTest method testProcessMessageForwardsMessageToDownstreamSender.
/**
* Verifies that an event uploaded by an upstream client is forwarded to the
* downstream container.
*
* @param ctx The test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessMessageForwardsMessageToDownstreamSender(final TestContext ctx) {
final UpstreamReceiver client = newClient();
final ProtonDelivery delivery = mock(ProtonDelivery.class);
final ProtonDelivery downstreamDelivery = mock(ProtonDelivery.class);
when(downstreamDelivery.getRemoteState()).thenReturn(ACCEPTED);
when(downstreamDelivery.remotelySettled()).thenReturn(true);
// GIVEN an adapter with a connection to a downstream container
final Async msgSent = ctx.async();
ProtonSender sender = newMockSender(false);
when(sender.send(any(Message.class), any(Handler.class))).then(invocation -> {
msgSent.complete();
final Handler handler = invocation.getArgument(1);
handler.handle(downstreamDelivery);
return null;
});
ForwardingEventDownstreamAdapter adapter = new ForwardingEventDownstreamAdapter(vertx, newMockSenderFactory(sender));
adapter.setMetrics(mock(MessagingMetrics.class));
adapter.setDownstreamConnectionFactory(newMockConnectionFactory(false));
adapter.start(Future.future());
adapter.addSender(client, sender);
// WHEN processing an event
Message msg = ProtonHelper.message(EVENT_MSG_CONTENT);
MessageHelper.addDeviceId(msg, DEVICE_ID);
adapter.processMessage(client, delivery, msg);
// THEN the message has been delivered to the downstream container
msgSent.await(1000);
// and disposition was returned
verify(delivery).disposition(any(Accepted.class), eq(Boolean.TRUE));
}
use of org.eclipse.hono.messaging.UpstreamReceiver in project hono by eclipse.
the class ForwardingEventDownstreamAdapterTest method testProcessMessageReleasesMessageIfNoCreditIsAvailable.
/**
* Verifies that an event is released if no downstream credit is available.
*
* @param ctx The test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testProcessMessageReleasesMessageIfNoCreditIsAvailable(final TestContext ctx) {
final UpstreamReceiver client = newClient();
final ProtonDelivery delivery = mock(ProtonDelivery.class);
when(delivery.remotelySettled()).thenReturn(Boolean.FALSE);
// GIVEN an adapter with a connection to a downstream container
ProtonSender sender = newMockSender(false);
when(sender.sendQueueFull()).thenReturn(true);
ForwardingEventDownstreamAdapter adapter = new ForwardingEventDownstreamAdapter(vertx, newMockSenderFactory(sender));
adapter.setMetrics(mock(MessagingMetrics.class));
adapter.setDownstreamConnectionFactory(newMockConnectionFactory(false));
adapter.start(Future.future());
adapter.addSender(client, sender);
// WHEN processing an event
Message msg = ProtonHelper.message(EVENT_MSG_CONTENT);
MessageHelper.addDeviceId(msg, DEVICE_ID);
adapter.processMessage(client, delivery, msg);
// THEN the message is released
verify(delivery).disposition(any(Released.class), eq(Boolean.TRUE));
// and not delivered to the downstream container
verify(sender, never()).send(any(Message.class), any(Handler.class));
}
use of org.eclipse.hono.messaging.UpstreamReceiver in project hono by eclipse.
the class ForwardingTelemetryDownstreamAdapterTest method testProcessMessageForwardsMessageToDownstreamSender.
/**
* Verifies that pre-settled telemetry data uploaded by an upstream client is
* forwarded to the downstream container and is accepted and settled immediately.
*
* @param ctx The test context.
*/
@Test
public void testProcessMessageForwardsMessageToDownstreamSender(final TestContext ctx) {
final UpstreamReceiver client = TestSupport.newClient();
// GIVEN an adapter with a connection to a downstream container
final ProtonSender sender = TestSupport.newMockSender(false);
final ForwardingTelemetryDownstreamAdapter adapter = new ForwardingTelemetryDownstreamAdapter(vertx, TestSupport.newMockSenderFactory(sender));
adapter.setMetrics(mock(MessagingMetrics.class));
adapter.setDownstreamConnectionFactory(connectionFactory);
adapter.start(Future.future());
adapter.addSender(client, sender);
// WHEN processing a pre-settled telemetry message
final Message msg = ProtonHelper.message(TELEMETRY_MSG_CONTENT);
MessageHelper.addDeviceId(msg, DEVICE_ID);
final ProtonDelivery upstreamDelivery = mock(ProtonDelivery.class);
when(upstreamDelivery.remotelySettled()).thenReturn(Boolean.TRUE);
adapter.processMessage(client, upstreamDelivery, msg);
// THEN the message is being delivered to the downstream container
verify(sender).send(eq(msg));
// and the upstream delivery is settled with the accepted outcome
verify(upstreamDelivery).disposition(any(Accepted.class), eq(Boolean.TRUE));
}
Aggregations