use of io.vertx.core.Context in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsIfReceiverIsNotOpen.
/**
* Verifies that a response handler is immediately failed with a
* {@link ServerErrorException} when the receiver link is not open (yet).
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateAndSendRequestFailsIfReceiverIsNotOpen(final TestContext ctx) {
// GIVEN a client whose sender and receiver are not open
when(receiver.isOpen()).thenReturn(Boolean.FALSE);
// WHEN sending a request
Async requestFailure = ctx.async();
client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
requestFailure.complete();
}));
// THEN the request fails immediately
requestFailure.await();
}
use of io.vertx.core.Context in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsIfSenderIsNotOpen.
/**
* Verifies that a response handler is immediately failed with a
* {@link ServerErrorException} when the sender link is not open (yet).
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateAndSendRequestFailsIfSenderIsNotOpen(final TestContext ctx) {
// GIVEN a client whose sender and receiver are not open
when(sender.isOpen()).thenReturn(Boolean.FALSE);
// WHEN sending a request
Async requestFailure = ctx.async();
client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
requestFailure.complete();
}));
// THEN the request fails immediately
requestFailure.await();
}
use of io.vertx.core.Context in project hono by eclipse.
the class HonoClientUnitTestHelper method mockContext.
/**
* Creates a mocked vert.x Context which immediately invokes any handler that is passed to its runOnContext method.
*
* @param vertx The vert.x instance that the mock of the context is created for.
* @return The mocked context.
*/
@SuppressWarnings("unchecked")
public static final Context mockContext(final Vertx vertx) {
final Context context = mock(Context.class);
when(context.owner()).thenReturn(vertx);
doAnswer(invocation -> {
Handler<Void> handler = invocation.getArgument(0);
handler.handle(null);
return null;
}).when(context).runOnContext(any(Handler.class));
return context;
}
use of io.vertx.core.Context in project hono by eclipse.
the class RegistrationClientImplTest method testGetRegistrationInfoReturnsValueFromCache.
/**
* Verifies that registration information is taken from cache.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetRegistrationInfoReturnsValueFromCache(final TestContext ctx) {
// GIVEN an adapter with a cache containing a registration assertion
// response for "device"
client.setResponseCache(cache);
final JsonObject registrationAssertion = newRegistrationAssertionResult();
final RegistrationResult regResult = RegistrationResult.from(HttpURLConnection.HTTP_OK, registrationAssertion);
when(cache.get(eq(TriTuple.of("assert", "device", "gateway")))).thenReturn(regResult);
// WHEN getting registration information
client.assertRegistration("device", "gateway").setHandler(ctx.asyncAssertSuccess(result -> {
// THEN the registration information is read from the cache
ctx.assertEquals(registrationAssertion, result);
verify(sender, never()).send(any(Message.class));
}));
}
use of io.vertx.core.Context in project hono by eclipse.
the class RegistrationClientImplTest method testAssertRegistrationAddsResponseToCacheOnCacheMiss.
/**
* Verifies that on a cache miss the adapter retrieves registration information
* from the Device Registration service and puts it to the cache.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testAssertRegistrationAddsResponseToCacheOnCacheMiss(final TestContext ctx) {
// GIVEN an adapter with an empty cache
client.setResponseCache(cache);
// WHEN getting registration information
final Async assertion = ctx.async();
client.assertRegistration("device").setHandler(ctx.asyncAssertSuccess(result -> assertion.complete()));
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
verify(sender).send(messageCaptor.capture(), any(Handler.class));
final JsonObject registrationAssertion = newRegistrationAssertionResult();
final Message response = ProtonHelper.message(registrationAssertion.encode());
MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60));
response.setCorrelationId(messageCaptor.getValue().getMessageId());
final ProtonDelivery delivery = mock(ProtonDelivery.class);
client.handleResponse(delivery, response);
// THEN the registration information has been added to the cache
verify(cache).put(eq(TriTuple.of("assert", "device", null)), any(RegistrationResult.class), any(Duration.class));
}
Aggregations