Search in sources :

Example 1 with Rejected

use of org.apache.qpid.proton.amqp.messaging.Rejected in project azure-iot-sdk-java by Azure.

the class AmqpsMessageTest method acknowledgeSetsRejectedDispositionForReject.

// Tests_SRS_AMQPSMESSAGE_14_003: [If the ACK_TYPE is REJECT, the function shall set a Rejected disposition on the private Delivery object.]
@Test
public void acknowledgeSetsRejectedDispositionForReject(@Mocked final Rejected mockRejected) {
    new NonStrictExpectations() {

        {
            new Rejected();
            result = mockRejected;
        }
    };
    AmqpsMessage message = new AmqpsMessage();
    message.setDelivery(mockDelivery);
    message.acknowledge(AmqpsMessage.ACK_TYPE.REJECT);
    final Delivery expectedDelivery = mockDelivery;
    new Verifications() {

        {
            expectedDelivery.disposition(mockRejected);
        }
    };
}
Also used : Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Delivery(org.apache.qpid.proton.engine.Delivery) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 2 with Rejected

use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.

the class MessageForwardingEndpointTest method testMessageHandlerRejectsMalformedMessage.

/**
 * Verifies that the endpoint rejects messages that do not pass formal verification.
 */
@SuppressWarnings({ "unchecked" })
@Test
public void testMessageHandlerRejectsMalformedMessage() {
    // GIVEN an endpoint with an attached client
    final ResourceIdentifier targetAddress = ResourceIdentifier.fromString("telemetry/tenant");
    final ProtonConnection connection = mock(ProtonConnection.class);
    when(connection.getRemoteContainer()).thenReturn("test-client");
    final ProtonReceiver receiver = mock(ProtonReceiver.class);
    when(receiver.getRemoteQoS()).thenReturn(ProtonQoS.AT_MOST_ONCE);
    final DownstreamAdapter adapter = mock(DownstreamAdapter.class);
    doAnswer(invocation -> {
        final Handler<AsyncResult<Void>> resultHandler = invocation.getArgument(1);
        resultHandler.handle(Future.succeededFuture());
        return null;
    }).when(adapter).onClientAttach(any(UpstreamReceiver.class), any(Handler.class));
    final MessageForwardingEndpoint<HonoMessagingConfigProperties> endpoint = getEndpoint(false);
    endpoint.setDownstreamAdapter(adapter);
    endpoint.onLinkAttach(connection, receiver, targetAddress);
    final ArgumentCaptor<ProtonMessageHandler> messageHandler = ArgumentCaptor.forClass(ProtonMessageHandler.class);
    verify(receiver).handler(messageHandler.capture());
    // WHEN a client sends a malformed message
    final Message message = ProtonHelper.message("malformed");
    final ProtonDelivery upstreamDelivery = mock(ProtonDelivery.class);
    messageHandler.getValue().handle(upstreamDelivery, message);
    // THEN the endpoint rejects the message
    final ArgumentCaptor<Rejected> deliveryState = ArgumentCaptor.forClass(Rejected.class);
    verify(upstreamDelivery).disposition(deliveryState.capture(), eq(Boolean.TRUE));
    assertThat(deliveryState.getValue().getError().getCondition(), is(AmqpError.DECODE_ERROR));
    // but does not close the link
    verify(receiver, never()).close();
    // and the message is not forwarded to the downstream adapter
    verify(adapter, never()).processMessage(any(UpstreamReceiver.class), eq(upstreamDelivery), eq(message));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Handler(io.vertx.core.Handler) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.Test)

Example 3 with Rejected

use of org.apache.qpid.proton.amqp.messaging.Rejected 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 4 with Rejected

use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.

the class EventSenderImpl method sendMessage.

@Override
protected Future<ProtonDelivery> sendMessage(final Message message) {
    Objects.requireNonNull(message);
    final Future<ProtonDelivery> result = Future.future();
    final String messageId = String.format("%s-%d", getClass().getSimpleName(), MESSAGE_COUNTER.getAndIncrement());
    message.setMessageId(messageId);
    sender.send(message, deliveryUpdated -> {
        if (deliveryUpdated.remotelySettled()) {
            if (Accepted.class.isInstance(deliveryUpdated.getRemoteState())) {
                LOG.trace("event [message ID: {}] accepted by peer", messageId);
                result.complete(deliveryUpdated);
            } else if (Rejected.class.isInstance(deliveryUpdated.getRemoteState())) {
                Rejected rejected = (Rejected) deliveryUpdated.getRemoteState();
                if (rejected.getError() == null) {
                    LOG.debug("event [message ID: {}] rejected by peer", messageId);
                    result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
                } else {
                    LOG.debug("event [message ID: {}] rejected by peer: {}, {}", messageId, rejected.getError().getCondition(), rejected.getError().getDescription());
                    result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, rejected.getError().getDescription()));
                }
            } else {
                LOG.debug("event [message ID: {}] not accepted by peer: {}", messageId, deliveryUpdated.getRemoteState());
                result.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
            }
        } else {
            LOG.warn("peer did not settle event, failing delivery [new remote state: {}]", deliveryUpdated.getRemoteState());
            result.fail(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR));
        }
    });
    LOG.trace("sent event [ID: {}], remaining credit: {}, queued messages: {}", messageId, sender.getCredit(), sender.getQueued());
    return result;
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ServerErrorException(org.eclipse.hono.client.ServerErrorException)

Example 5 with Rejected

use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.

the class TelemetrySenderImpl method sendMessage.

@Override
protected Future<ProtonDelivery> sendMessage(final Message message) {
    Objects.requireNonNull(message);
    final String messageId = String.format("%s-%d", getClass().getSimpleName(), MESSAGE_COUNTER.getAndIncrement());
    message.setMessageId(messageId);
    final ProtonDelivery result = sender.send(message, deliveryUpdated -> {
        if (deliveryUpdated.remotelySettled()) {
            if (Accepted.class.isInstance(deliveryUpdated.getRemoteState())) {
                LOG.trace("message [message ID: {}] accepted by peer", messageId);
            } else if (Rejected.class.isInstance(deliveryUpdated.getRemoteState())) {
                Rejected remoteState = (Rejected) deliveryUpdated.getRemoteState();
                if (remoteState.getError() == null) {
                    LOG.debug("message [message ID: {}] rejected by peer", messageId);
                } else {
                    LOG.debug("message [message ID: {}] rejected by peer: {}, {}", messageId, remoteState.getError().getCondition(), remoteState.getError().getDescription());
                }
            } else {
                LOG.debug("message [message ID: {}] not accepted by peer: {}", messageId, deliveryUpdated.getRemoteState());
            }
        } else {
            LOG.warn("peer did not settle telemetry message [message ID: {}, remote state: {}]", messageId, deliveryUpdated.getRemoteState());
        }
    });
    LOG.trace("sent telemetry message [ID: {}], remaining credit: {}, queued messages: {}", messageId, sender.getCredit(), sender.getQueued());
    return Future.succeededFuture(result);
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected)

Aggregations

Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)59 Message (org.apache.qpid.proton.message.Message)32 ProtonDelivery (io.vertx.proton.ProtonDelivery)28 Handler (io.vertx.core.Handler)27 DeliveryState (org.apache.qpid.proton.amqp.transport.DeliveryState)23 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)23 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)21 Span (io.opentracing.Span)19 Test (org.junit.jupiter.api.Test)19 ProtonHelper (io.vertx.proton.ProtonHelper)18 ProtonSender (io.vertx.proton.ProtonSender)18 Released (org.apache.qpid.proton.amqp.messaging.Released)18 Future (io.vertx.core.Future)17 HttpURLConnection (java.net.HttpURLConnection)17 MessageHelper (org.eclipse.hono.util.MessageHelper)17 ProtonQoS (io.vertx.proton.ProtonQoS)16 ClientErrorException (org.eclipse.hono.client.ClientErrorException)16 ProtonReceiver (io.vertx.proton.ProtonReceiver)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)15 Truth.assertThat (com.google.common.truth.Truth.assertThat)14