Search in sources :

Example 26 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class EventSenderImplTest method testSendMessageWaitsForAcceptedOutcome.

/**
 * Verifies that the sender waits for the peer to settle and
 * accept a message before succeeding the returned future.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked" })
@Test
public void testSendMessageWaitsForAcceptedOutcome(final TestContext ctx) {
    // GIVEN a sender that has credit
    when(sender.sendQueueFull()).thenReturn(Boolean.FALSE);
    MessageSender messageSender = new EventSenderImpl(config, sender, "tenant", "telemetry/tenant", context);
    final AtomicReference<Handler<ProtonDelivery>> handlerRef = new AtomicReference<>();
    doAnswer(invocation -> {
        handlerRef.set(invocation.getArgument(1));
        return mock(ProtonDelivery.class);
    }).when(sender).send(any(Message.class), any(Handler.class));
    // WHEN trying to send a message
    final Future<ProtonDelivery> result = messageSender.send("device", "some payload", "application/text", "token");
    // THEN the message has been sent
    // and the result is not completed yet
    verify(sender).send(any(Message.class), eq(handlerRef.get()));
    assertFalse(result.isComplete());
    // until it gets accepted by the peer
    ProtonDelivery accepted = mock(ProtonDelivery.class);
    when(accepted.remotelySettled()).thenReturn(Boolean.TRUE);
    when(accepted.getRemoteState()).thenReturn(new Accepted());
    handlerRef.get().handle(accepted);
    assertTrue(result.succeeded());
}
Also used : Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) MessageSender(org.eclipse.hono.client.MessageSender) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Test(org.junit.Test)

Example 27 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class EventSenderImplTest method testSendMessageFailsForRejectedOutcome.

/**
 * Verifies that the sender fails if the peer does not accept a message.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked" })
@Test
public void testSendMessageFailsForRejectedOutcome(final TestContext ctx) {
    // GIVEN a sender that has credit
    when(sender.sendQueueFull()).thenReturn(Boolean.FALSE);
    MessageSender messageSender = new EventSenderImpl(config, sender, "tenant", "telemetry/tenant", context);
    final AtomicReference<Handler<ProtonDelivery>> handlerRef = new AtomicReference<>();
    doAnswer(invocation -> {
        handlerRef.set(invocation.getArgument(1));
        return mock(ProtonDelivery.class);
    }).when(sender).send(any(Message.class), any(Handler.class));
    // WHEN trying to send a message
    final Future<ProtonDelivery> result = messageSender.send("device", "some payload", "application/text", "token");
    // THEN the message has been sent
    // and the result is not completed yet
    verify(sender).send(any(Message.class), eq(handlerRef.get()));
    assertFalse(result.isComplete());
    // and the result fails once the peer rejects the message
    ProtonDelivery rejected = mock(ProtonDelivery.class);
    when(rejected.remotelySettled()).thenReturn(Boolean.TRUE);
    when(rejected.getRemoteState()).thenReturn(new Rejected());
    handlerRef.get().handle(rejected);
    assertFalse(result.succeeded());
}
Also used : Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) MessageSender(org.eclipse.hono.client.MessageSender) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Test(org.junit.Test)

Example 28 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class MessageForwardingEndpointTest method testForwardMessageAcceptsMissingRegistrationAssertion.

/**
 * Verifies that a message that does not contain a registration assertion is
 * forwarded to the downstream adapter if the adapter is configured to not
 * require registration assertion validation.
 */
@Test
public void testForwardMessageAcceptsMissingRegistrationAssertion() {
    // GIVEN an adapter that does not validate registration assertions
    final String validToken = getToken(SECRET, "tenant", "4711");
    final UpstreamReceiver client = mock(UpstreamReceiver.class);
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    final DownstreamAdapter adapter = mock(DownstreamAdapter.class);
    when(tokenValidator.isValid(validToken, "tenant", "4711")).thenReturn(Boolean.TRUE);
    final MessageForwardingEndpoint<HonoMessagingConfigProperties> endpoint = getEndpoint();
    endpoint.setRegistrationAssertionValidator(tokenValidator);
    endpoint.setDownstreamAdapter(adapter);
    config.setAssertionValidationRequired(false);
    // WHEN processing a message lacking a valid registration assertion
    final Message msg = ProtonHelper.message();
    MessageHelper.addAnnotation(msg, MessageHelper.APP_PROPERTY_RESOURCE, "telemetry/tenant/4711");
    endpoint.forwardMessage(client, delivery, msg);
    // THEN the message is sent downstream
    verify(adapter).processMessage(client, delivery, msg);
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Test(org.junit.Test)

Example 29 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class MessageForwardingEndpointTest method testProcessMessageRejectsRegistrationAssertionForWrongTenant.

/**
 * Verifies that a message containing a registration assertion for a tenant
 * other than the one from the message's target address is rejected.
 */
@Test
public void testProcessMessageRejectsRegistrationAssertionForWrongTenant() {
    final String invalidToken = getToken(SECRET, "wrong-tenant", "4711");
    final UpstreamReceiver client = mock(UpstreamReceiver.class);
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    when(tokenValidator.isValid(invalidToken, "tenant", "4711")).thenReturn(Boolean.FALSE);
    final MessageForwardingEndpoint<HonoMessagingConfigProperties> endpoint = getEndpoint();
    endpoint.setRegistrationAssertionValidator(tokenValidator);
    final Message msg = ProtonHelper.message();
    MessageHelper.addRegistrationAssertion(msg, invalidToken);
    MessageHelper.addAnnotation(msg, MessageHelper.APP_PROPERTY_RESOURCE, "telemetry/tenant/4711");
    endpoint.forwardMessage(client, delivery, msg);
    verify(delivery).disposition(any(Rejected.class), anyBoolean());
    verify(client, never()).close(any(ErrorCondition.class));
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Test(org.junit.Test)

Example 30 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery 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

ProtonDelivery (io.vertx.proton.ProtonDelivery)38 Test (org.junit.Test)31 Message (org.apache.qpid.proton.message.Message)29 Handler (io.vertx.core.Handler)21 ProtonSender (io.vertx.proton.ProtonSender)15 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)15 ProtonReceiver (io.vertx.proton.ProtonReceiver)13 Async (io.vertx.ext.unit.Async)12 Vertx (io.vertx.core.Vertx)11 ProtonHelper (io.vertx.proton.ProtonHelper)11 JsonObject (io.vertx.core.json.JsonObject)10 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 Before (org.junit.Before)10 Rule (org.junit.Rule)10 RunWith (org.junit.runner.RunWith)10 ArgumentCaptor (org.mockito.ArgumentCaptor)10 Mockito (org.mockito.Mockito)10 Context (io.vertx.core.Context)9 HttpURLConnection (java.net.HttpURLConnection)9