use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class AmqpClientUnitTestHelper method mockProtonReceiver.
/**
* Creates a mocked Proton receiver which always returns {@code true} when its isOpen method is called
* and whose connection is not disconnected.
*
* @return The mocked receiver.
*/
public static ProtonReceiver mockProtonReceiver() {
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.isOpen()).thenReturn(Boolean.TRUE);
mockLinkConnection(receiver);
return receiver;
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testAdapterAcceptsAnonymousRelayReceiverOnly.
/**
* Verifies that the AMQP Adapter rejects (closes) AMQP links that contains a target address.
*/
@Test
public void testAdapterAcceptsAnonymousRelayReceiverOnly() {
// GIVEN an AMQP adapter with a configured server.
givenAnAdapter(properties);
// WHEN the adapter receives a link that contains a target address
final ResourceIdentifier targetAddress = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, TEST_DEVICE);
final ProtonReceiver link = getReceiver(ProtonQoS.AT_LEAST_ONCE, getTarget(targetAddress));
adapter.handleRemoteReceiverOpen(getConnection(null), link);
// THEN the adapter closes the link.
verify(link).close();
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class ProtonBasedNotificationSenderTest method setUp.
/**
* Sets up the fixture.
*/
@BeforeEach
void setUp() {
final var vertx = mock(Vertx.class);
when(vertx.eventBus()).thenReturn(mock(EventBus.class));
final HonoConnection connection = AmqpClientUnitTestHelper.mockHonoConnection(vertx);
final ProtonReceiver receiver = AmqpClientUnitTestHelper.mockProtonReceiver();
when(connection.createReceiver(anyString(), any(ProtonQoS.class), any(ProtonMessageHandler.class), anyInt(), anyBoolean(), VertxMockSupport.anyHandler())).thenReturn(Future.succeededFuture(receiver));
protonDelivery = mock(ProtonDelivery.class);
when(protonDelivery.remotelySettled()).thenReturn(true);
when(protonDelivery.getRemoteState()).thenReturn(new Accepted());
sender = AmqpClientUnitTestHelper.mockProtonSender();
when(sender.send(any(Message.class), VertxMockSupport.anyHandler())).thenReturn(protonDelivery);
when(connection.createSender(anyString(), any(), any())).thenReturn(Future.succeededFuture(sender));
notificationSender = new ProtonBasedNotificationSender(connection);
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class HonoConnectionImplTest method testCreateReceiverFailsOnDisconnectBeforeOpen.
/**
* Verifies that the attempt to create a receiver fails with a
* {@code ServerErrorException} if the connection gets disconnected
* before the remote peer has sent its attach frame. It is verified
* that this is done before the link establishment timeout.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateReceiverFailsOnDisconnectBeforeOpen(final VertxTestContext ctx) {
// choose a distinct value here
final long linkEstablishmentTimeout = 444L;
props.setLinkEstablishmentTimeout(linkEstablishmentTimeout);
// don't run linkEstablishmentTimeout timer handler
when(vertx.setTimer(eq(linkEstablishmentTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> 0L);
final Source source = mock(Source.class);
when(source.getAddress()).thenReturn("source/address");
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.isOpen()).thenReturn(Boolean.TRUE);
when(receiver.getSource()).thenReturn(source);
when(receiver.getRemoteSource()).thenReturn(source);
when(session.createReceiver(anyString())).thenReturn(receiver);
final Handler<String> remoteCloseHook = VertxMockSupport.mockHandler();
// GIVEN an established connection
honoConnection.connect().compose(c -> {
// WHEN creating a receiver link with a close hook
final Future<ProtonReceiver> result = honoConnection.createReceiver("source", ProtonQoS.AT_LEAST_ONCE, mock(ProtonMessageHandler.class), remoteCloseHook);
// THEN the result is not completed at first
ctx.verify(() -> assertThat(result.isComplete()).isFalse());
// WHEN the downstream connection fails
connectionFactory.getDisconnectHandler().handle(con);
return result;
}).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(((ServerErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE));
ctx.completeNow();
}));
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class HonoConnectionImplTest method testCreateReceiverFailsOnTimeout.
/**
* Verifies that the attempt to create a receiver fails with a
* {@code ServerErrorException} if the remote peer doesn't
* send its attach frame in time.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateReceiverFailsOnTimeout(final VertxTestContext ctx) {
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.isOpen()).thenReturn(Boolean.TRUE);
when(session.createReceiver(anyString())).thenReturn(receiver);
final Handler<String> remoteCloseHook = VertxMockSupport.mockHandler();
// GIVEN an established connection
honoConnection.connect().compose(c -> honoConnection.createReceiver("source", ProtonQoS.AT_LEAST_ONCE, (delivery, msg) -> {
}, remoteCloseHook)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
assertThat(((ServerErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE);
verify(receiver).open();
verify(receiver).close();
verify(remoteCloseHook, never()).handle(anyString());
});
ctx.completeNow();
}));
}
Aggregations