use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class ProtonBasedMappingAndDelegatingCommandHandlerTest method testMapForMessageHavingAddressWithoutDeviceId.
/**
* Verifies that a command message with an address that doesn't contain a device ID
* gets rejected.
*/
@SuppressWarnings("unchecked")
@Test
public void testMapForMessageHavingAddressWithoutDeviceId() {
// GIVEN a command message with an address that does not
// contain a device ID
final String deviceId = "4711";
final Message message = getValidCommandMessage(deviceId);
message.setAddress(String.format("%s/%s", CommandConstants.COMMAND_ENDPOINT, tenantId));
// WHEN mapping and delegating the command
final ProtonDelivery delivery = mock(ProtonDelivery.class);
mappingAndDelegatingCommandHandler.mapAndDelegateIncomingCommandMessage(tenantId, delivery, message);
// THEN the disposition is REJECTED
verify(delivery).disposition(argThat(state -> Constants.AMQP_BAD_REQUEST.equals(((Rejected) state).getError().getCondition())), eq(true));
// and the message is not being delegated
verify(sender, never()).send(any(Message.class), any(Handler.class));
}
use of org.apache.qpid.proton.amqp.messaging.Rejected in project hono by eclipse.
the class ProtonBasedMappingAndDelegatingCommandHandlerTest method testMapForMessageHavingAddressWithInvalidTenant.
/**
* Verifies that a command message with an address that contains a tenant which doesn't
* match the scope of the command receiver link gets rejected.
*/
@SuppressWarnings("unchecked")
@Test
public void testMapForMessageHavingAddressWithInvalidTenant() {
// GIVEN a command message with an address that contains an
// invalid tenant
final String deviceId = "4711";
final Message message = getValidCommandMessage(deviceId);
message.setAddress(String.format("%s/%s/%s", CommandConstants.COMMAND_ENDPOINT, "wrong-tenant", deviceId));
// WHEN mapping and delegating the command
final ProtonDelivery delivery = mock(ProtonDelivery.class);
mappingAndDelegatingCommandHandler.mapAndDelegateIncomingCommandMessage(tenantId, delivery, message);
// THEN the disposition is REJECTED
verify(delivery).disposition(argThat(state -> AmqpError.UNAUTHORIZED_ACCESS.equals(((Rejected) state).getError().getCondition())), eq(true));
// and the message is not being delegated
verify(sender, never()).send(any(Message.class), any(Handler.class));
}
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 azure-iot-sdk-java by Azure.
the class AmqpsIotHubConnection method onMessageAcknowledged.
@Override
public void onMessageAcknowledged(Message message, DeliveryState deliveryState, String deviceId) {
if (deliveryState == Accepted.getInstance()) {
this.listener.onMessageSent(message, deviceId, null);
} else if (deliveryState instanceof Rejected) {
// The message was not accepted by the server, and the reason why is found within the nested error
TransportException ex = AmqpsExceptionTranslator.convertFromAmqpException(((Rejected) deliveryState).getError());
this.listener.onMessageSent(message, deviceId, ex);
} else if (deliveryState == Released.getInstance()) {
// As per AMQP spec, this state means the message should be re-delivered to the server at a later time
ProtocolException protocolException = new ProtocolException("Message was released by the amqp server");
protocolException.setRetryable(true);
this.listener.onMessageSent(message, deviceId, protocolException);
} else {
log.warn("Unexpected delivery state for sent message ({})", message);
}
}
Aggregations