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