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());
}
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());
}
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);
}
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));
}
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));
}
Aggregations