Search in sources :

Example 36 with ProtonReceiver

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;
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver)

Example 37 with ProtonReceiver

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();
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Test(org.junit.jupiter.api.Test)

Example 38 with ProtonReceiver

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);
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonQoS(io.vertx.proton.ProtonQoS) HonoConnection(org.eclipse.hono.client.HonoConnection) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) EventBus(io.vertx.core.eventbus.EventBus) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 39 with ProtonReceiver

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();
    }));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) SaslSystemException(io.vertx.proton.sasl.SaslSystemException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Mockito.doAnswer(org.mockito.Mockito.doAnswer) DisconnectListener(org.eclipse.hono.client.DisconnectListener) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Predicate(java.util.function.Predicate) ProtonQoS(io.vertx.proton.ProtonQoS) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) AdditionalAnswers(org.mockito.AdditionalAnswers) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) ProtonSender(io.vertx.proton.ProtonSender) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Target(org.apache.qpid.proton.amqp.messaging.Target) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CompositeFuture(io.vertx.core.CompositeFuture) ProtonSession(io.vertx.proton.ProtonSession) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Symbol(org.apache.qpid.proton.amqp.Symbol) BiConsumer(java.util.function.BiConsumer) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Source(org.apache.qpid.proton.amqp.transport.Source) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Source(org.apache.qpid.proton.amqp.transport.Source) Test(org.junit.jupiter.api.Test)

Example 40 with ProtonReceiver

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();
    }));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) SaslSystemException(io.vertx.proton.sasl.SaslSystemException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Mockito.doAnswer(org.mockito.Mockito.doAnswer) DisconnectListener(org.eclipse.hono.client.DisconnectListener) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Predicate(java.util.function.Predicate) ProtonQoS(io.vertx.proton.ProtonQoS) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) AdditionalAnswers(org.mockito.AdditionalAnswers) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) ProtonSender(io.vertx.proton.ProtonSender) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Target(org.apache.qpid.proton.amqp.messaging.Target) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CompositeFuture(io.vertx.core.CompositeFuture) ProtonSession(io.vertx.proton.ProtonSession) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Symbol(org.apache.qpid.proton.amqp.Symbol) BiConsumer(java.util.function.BiConsumer) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Source(org.apache.qpid.proton.amqp.transport.Source) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

ProtonReceiver (io.vertx.proton.ProtonReceiver)45 ProtonConnection (io.vertx.proton.ProtonConnection)19 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)19 Vertx (io.vertx.core.Vertx)17 ProtonQoS (io.vertx.proton.ProtonQoS)16 Handler (io.vertx.core.Handler)13 ProtonSender (io.vertx.proton.ProtonSender)13 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)13 Message (org.apache.qpid.proton.message.Message)12 Future (io.vertx.core.Future)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 ProtonDelivery (io.vertx.proton.ProtonDelivery)10 HttpURLConnection (java.net.HttpURLConnection)10 ClientErrorException (org.eclipse.hono.client.ClientErrorException)10 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)10 Test (org.junit.jupiter.api.Test)10 AsyncResult (io.vertx.core.AsyncResult)9 Promise (io.vertx.core.Promise)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9