Search in sources :

Example 1 with UpstreamReceiver

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));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Handler(io.vertx.core.Handler) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Test(org.junit.Test)

Example 2 with UpstreamReceiver

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;
}
Also used : UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver)

Example 3 with UpstreamReceiver

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));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Test(org.junit.Test)

Example 4 with UpstreamReceiver

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));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) Released(org.apache.qpid.proton.amqp.messaging.Released) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Handler(io.vertx.core.Handler) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Test(org.junit.Test)

Example 5 with UpstreamReceiver

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));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Test(org.junit.Test)

Aggregations

UpstreamReceiver (org.eclipse.hono.messaging.UpstreamReceiver)6 ProtonDelivery (io.vertx.proton.ProtonDelivery)5 ProtonSender (io.vertx.proton.ProtonSender)5 Message (org.apache.qpid.proton.message.Message)5 MessagingMetrics (org.eclipse.hono.messaging.MessagingMetrics)5 Test (org.junit.Test)5 Handler (io.vertx.core.Handler)4 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)2 Released (org.apache.qpid.proton.amqp.messaging.Released)2 Async (io.vertx.ext.unit.Async)1 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)1 ForwardingEventDownstreamAdapter (org.eclipse.hono.event.impl.ForwardingEventDownstreamAdapter)1