use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class RequestResponseClientTest method testCreateAndSendRequestFailsOnRejectedMessage.
private void testCreateAndSendRequestFailsOnRejectedMessage(final VertxTestContext ctx, final Symbol errorCondition, final Consumer<Throwable> failureAssertions) {
// WHEN sending a request message with some headers and payload
final JsonObject payload = new JsonObject().put("key", "value");
client.compose(c -> c.createAndSendRequest("get", null, payload.toBuffer(), "application/json", SimpleRequestResponseResult::from, span)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the result handler is failed with the expected error
failureAssertions.accept(t);
verify(sample).completed(isA(Rejected.class));
// and a timer has been set to time out the request
final ArgumentCaptor<Handler<Long>> timeoutHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
verify(vertx).setTimer(eq(clientConfig.getRequestTimeout()), timeoutHandlerCaptor.capture());
// triggering the timer now that the request has been handled should not invoke the sampler timeout method
timeoutHandlerCaptor.getValue().handle(1L);
verify(sample, never()).timeout();
});
ctx.completeNow();
}));
// and the peer rejects the message
final Rejected rejected = new Rejected();
rejected.setError(ProtonHelper.condition(errorCondition, "request message cannot be processed"));
final ProtonDelivery delivery = mock(ProtonDelivery.class);
when(delivery.getRemoteState()).thenReturn(rejected);
final ArgumentCaptor<Handler<ProtonDelivery>> dispositionHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
verify(sender).send(any(Message.class), dispositionHandlerCaptor.capture());
dispositionHandlerCaptor.getValue().handle(delivery);
}
use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class ProtonBasedApplicationClientTest method testCreateTelemetryConsumerReleasesMessageOnException.
/**
* Verifies that the message consumer created by the factory catches an exception
* thrown by the client provided handler and releases the message.
*
* @param ctx The vert.x test context.
*/
@Test
@SuppressWarnings("unchecked")
void testCreateTelemetryConsumerReleasesMessageOnException(final VertxTestContext ctx) {
// GIVEN a client provided message handler that throws an exception on
// each message received
final Handler<DownstreamMessage<AmqpMessageContext>> consumer = VertxMockSupport.mockHandler();
doThrow(new IllegalArgumentException("message does not contain required properties"), new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)).when(consumer).handle(any(DownstreamMessage.class));
client.createTelemetryConsumer("tenant", consumer, t -> {
}).onComplete(ctx.succeeding(mc -> {
final ArgumentCaptor<ProtonMessageHandler> messageHandler = ArgumentCaptor.forClass(ProtonMessageHandler.class);
ctx.verify(() -> {
verify(connection).createReceiver(eq("telemetry/tenant"), eq(ProtonQoS.AT_LEAST_ONCE), messageHandler.capture(), anyInt(), anyBoolean(), VertxMockSupport.anyHandler());
final var msg = ProtonHelper.message();
// WHEN a message is received and the client provided consumer
// throws an IllegalArgumentException
var delivery = mock(ProtonDelivery.class);
messageHandler.getValue().handle(delivery, msg);
// THEN the message is forwarded to the client provided handler
verify(consumer).handle(any(DownstreamMessage.class));
// AND the AMQP message is being released
verify(delivery).disposition(any(Released.class), eq(Boolean.TRUE));
// WHEN a message is received and the client provided consumer
// throws a ClientErrorException
delivery = mock(ProtonDelivery.class);
messageHandler.getValue().handle(delivery, msg);
// THEN the message is forwarded to the client provided handler
verify(consumer, times(2)).handle(any(DownstreamMessage.class));
// AND the AMQP message is being rejected
verify(delivery).disposition(any(Rejected.class), eq(Boolean.TRUE));
});
ctx.completeNow();
}));
}
use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class ProtonBasedCommandContext method reject.
@Override
public void reject(final String error) {
TracingHelper.logError(getTracingSpan(), "client error trying to deliver or process command: " + error);
Tags.HTTP_STATUS.set(getTracingSpan(), HttpURLConnection.HTTP_BAD_REQUEST);
final ErrorCondition errorCondition = ProtonHelper.condition(Constants.AMQP_BAD_REQUEST, error);
final Rejected rejected = new Rejected();
rejected.setError(errorCondition);
updateDelivery(rejected);
}
use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class AmqpUploadTestBase method testAdapterRejectsBadInboundMessage.
/**
* Verifies that a message containing a payload which has the <em>empty notification</em>
* content type is rejected by the adapter.
*
* @param context The Vert.x context for running asynchronous tests.
* @throws InterruptedException if test is interrupted while running.
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 10)
public void testAdapterRejectsBadInboundMessage(final VertxTestContext context) throws InterruptedException {
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final VertxTestContext setup = new VertxTestContext();
setupProtocolAdapter(tenantId, new Tenant(), deviceId, ProtonQoS.AT_LEAST_ONCE).map(s -> {
setup.verify(() -> {
final UnsignedLong maxMessageSize = s.getRemoteMaxMessageSize();
assertWithMessage("max-message-size included in adapter's attach frame").that(maxMessageSize).isNotNull();
assertWithMessage("max-message-size").that(maxMessageSize.longValue()).isGreaterThan(0);
});
sender = s;
return s;
}).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
context.failNow(setup.causeOfFailure());
return;
}
final Message msg = ProtonHelper.message("some payload");
msg.setContentType(EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION);
msg.setAddress(getEndpointName());
sender.send(msg, delivery -> {
context.verify(() -> {
assertThat(delivery.getRemoteState()).isInstanceOf(Rejected.class);
final Rejected rejected = (Rejected) delivery.getRemoteState();
final ErrorCondition error = rejected.getError();
assertThat((Object) error.getCondition()).isEqualTo(Constants.AMQP_BAD_REQUEST);
});
context.completeNow();
});
}
use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testOneWayCommandRejected.
/**
* Verifies that the adapter signals a rejected one-way command
* back to the sender of the command.
*/
@Test
public void testOneWayCommandRejected() {
final DeliveryState remoteState = new Rejected();
final ProtonDelivery unsuccessfulDelivery = mock(ProtonDelivery.class);
when(unsuccessfulDelivery.remotelySettled()).thenReturn(true);
when(unsuccessfulDelivery.getRemoteState()).thenReturn(remoteState);
testOneWayCommandOutcome(unsuccessfulDelivery, ctx -> verify(ctx).reject((String) any()), ProcessingOutcome.UNPROCESSABLE);
}
Aggregations