Search in sources :

Example 16 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class TelemetrySenderImplTest method testSendMessageDoesNotWaitForAcceptedOutcome.

/**
 * Verifies that the sender does not wait for the peer to settle and
 * accept a message before succeeding.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked" })
@Test
public void testSendMessageDoesNotWaitForAcceptedOutcome(final TestContext ctx) {
    // GIVEN a sender that has credit
    when(sender.sendQueueFull()).thenReturn(Boolean.FALSE);
    MessageSender messageSender = new TelemetrySenderImpl(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");
    // which gets rejected by the peer
    ProtonDelivery rejected = mock(ProtonDelivery.class);
    when(rejected.remotelySettled()).thenReturn(Boolean.TRUE);
    when(rejected.getRemoteState()).thenReturn(new Rejected());
    handlerRef.get().handle(rejected);
    // THEN the resulting future is succeeded nevertheless
    assertTrue(result.succeeded());
    // and the message has been sent
    verify(sender).send(any(Message.class), eq(handlerRef.get()));
}
Also used : Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) MessageSender(org.eclipse.hono.client.MessageSender) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Test(org.junit.Test)

Example 17 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventFailsForRejectedOutcome.

/**
 * Verifies that the adapter fails the upload of an event with a 400
 * result if it is rejected by the downstream peer.
 */
@Test
public void testUploadEventFailsForRejectedOutcome() {
    // GIVEN an adapter with a downstream event consumer attached
    final Future<ProtonDelivery> outcome = Future.future();
    givenAnEventSenderForOutcome(outcome);
    HttpServer server = getHttpServer(false);
    AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
    // WHEN a device publishes an event that is not accepted by the peer
    final Buffer payload = Buffer.buffer("some payload");
    final RoutingContext ctx = newRoutingContext(payload);
    adapter.uploadEventMessage(ctx, "tenant", "device", payload, "application/text");
    outcome.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "malformed message"));
    // THEN the device gets a 400
    verify(ctx).fail(HttpURLConnection.HTTP_BAD_REQUEST);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) ProtonDelivery(io.vertx.proton.ProtonDelivery) HttpServer(io.vertx.core.http.HttpServer) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 18 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventWaitsForAcceptedOutcome.

/**
 * Verifies that the adapter waits for an event being settled and accepted
 * by a downstream peer before responding with a 202 status to the device.
 */
@Test
public void testUploadEventWaitsForAcceptedOutcome() {
    // GIVEN an adapter with a downstream event consumer attached
    final Future<ProtonDelivery> outcome = Future.future();
    givenAnEventSenderForOutcome(outcome);
    HttpServer server = getHttpServer(false);
    AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
    // WHEN a device publishes an event
    final Buffer payload = Buffer.buffer("some payload");
    final HttpServerResponse response = mock(HttpServerResponse.class);
    final RoutingContext ctx = newRoutingContext(payload, response);
    adapter.uploadEventMessage(ctx, "tenant", "device", payload, "application/text");
    // THEN the device does not get a response
    verify(response, never()).end();
    // until the event has been accepted
    outcome.complete(mock(ProtonDelivery.class));
    verify(response).setStatusCode(202);
    verify(response).end();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) ProtonDelivery(io.vertx.proton.ProtonDelivery) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 19 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class AbstractRequestResponseClientTest method testCreateAndSendRequestAddsResponseToCache.

/**
 * Verifies that the adapter puts the response from the service to the cache
 * using the default cache timeout if the response does not contain a
 * <em>no-cache</em> cache directive.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testCreateAndSendRequestAddsResponseToCache(final TestContext ctx) {
    // GIVEN an adapter with an empty cache
    client.setResponseCache(cache);
    // WHEN sending a request
    client.createAndSendRequest("get", (JsonObject) null, ctx.asyncAssertSuccess(result -> {
        // THEN the response has been put to the cache
        verify(cache).put(eq("cacheKey"), any(SimpleRequestResponseResult.class), eq(Duration.ofSeconds(RequestResponseClientConfigProperties.DEFAULT_RESPONSE_CACHE_TIMEOUT)));
    }), "cacheKey");
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender).send(messageCaptor.capture(), any(Handler.class));
    final Message response = ProtonHelper.message("result");
    MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
    response.setCorrelationId(messageCaptor.getValue().getMessageId());
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    client.handleResponse(delivery, response);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Target(org.apache.qpid.proton.amqp.transport.Target) Duration(java.time.Duration) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 20 with ProtonDelivery

use of io.vertx.proton.ProtonDelivery in project hono by eclipse.

the class AbstractRequestResponseClientTest method testHandleResponseInvokesHandlerForMatchingCorrelationId.

/**
 * Verifies that the client passes a response message to the handler registered for the request that
 * the response correlates with.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleResponseInvokesHandlerForMatchingCorrelationId(final TestContext ctx) {
    // GIVEN a request message that has been sent to a peer
    final Async responseReceived = ctx.async();
    client.createAndSendRequest("request", null, (JsonObject) null, ctx.asyncAssertSuccess(s -> {
        ctx.assertEquals(200, s.getStatus());
        ctx.assertEquals("payload", s.getPayload());
        responseReceived.complete();
    }));
    // WHEN a response is received for the request
    final Message response = ProtonHelper.message("payload");
    response.setCorrelationId(MESSAGE_ID);
    MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, 200);
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    client.handleResponse(delivery, response);
    // THEN the response is passed to the handler registered with the request
    responseReceived.await();
    verify(vertx, never()).setTimer(anyLong(), any(Handler.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) CoreMatchers(org.hamcrest.CoreMatchers) Async(io.vertx.ext.unit.Async) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Target(org.apache.qpid.proton.amqp.transport.Target) Duration(java.time.Duration) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

ProtonDelivery (io.vertx.proton.ProtonDelivery)38 Test (org.junit.Test)31 Message (org.apache.qpid.proton.message.Message)29 Handler (io.vertx.core.Handler)21 ProtonSender (io.vertx.proton.ProtonSender)15 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)15 ProtonReceiver (io.vertx.proton.ProtonReceiver)13 Async (io.vertx.ext.unit.Async)12 Vertx (io.vertx.core.Vertx)11 ProtonHelper (io.vertx.proton.ProtonHelper)11 JsonObject (io.vertx.core.json.JsonObject)10 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 Before (org.junit.Before)10 Rule (org.junit.Rule)10 RunWith (org.junit.runner.RunWith)10 ArgumentCaptor (org.mockito.ArgumentCaptor)10 Mockito (org.mockito.Mockito)10 Context (io.vertx.core.Context)9 HttpURLConnection (java.net.HttpURLConnection)9