@Before public void before(TestContext context) { vertx = Vertx.vertx(); Async async = context.async(); vertx.deployVerticle(TestVerticle.class.getName(), context.asyncAssertSuccess(event -> async.complete())); }
@After public void after(TestContext context) { vertx.close(context.asyncAssertSuccess()); }
/** * Creates and returns a new async handler, the returned handler controls the completion of the test.<p/> * * When the returned handler is called back with a succeeded result it completes the async operation.<p/> * * When the returned handler is called back with a failed result it fails the test with the cause of the failure.<p/> * @return the async result handler */ public <T> Handler<AsyncResult<T>> asyncAssertSuccess() { Handler<AsyncResult<T>> ret = delegate.asyncAssertSuccess(); return ret; }
@Test public void test(TestContext testContext) { HttpClient client = vertx.createHttpClient(new HttpClientOptions() .setConnectTimeout(10000)); Async async = testContext.async(); client.getNow(PORT, "127.0.0.1", "/test", testContext.asyncAssertSuccess(httpClientResponse -> { testContext.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, httpClientResponse.statusCode()); async.complete(); })); }
static void deleteFromTestTable(TestContext ctx, PgClient client, Runnable completionHandler) { client.query( "DELETE FROM Test", ctx.asyncAssertSuccess(result -> completionHandler.run())); }
@Override public void start(Future<Void> startFuture) throws Exception { connector.accept(ctx.asyncAssertSuccess(conn -> { conn.closeHandler(v -> { done.complete(); }); startFuture.complete(); })); } }, ctx.asyncAssertSuccess(id -> {
static void insertIntoTestTable(TestContext ctx, PgClient client, int amount, Runnable completionHandler) { AtomicInteger count = new AtomicInteger(); for (int i = 0;i < 10;i++) { client.query("INSERT INTO Test (id, val) VALUES (" + i + ", 'Whatever-" + i + "')", ctx.asyncAssertSuccess(r1 -> { ctx.assertEquals(1, r1.rowCount()); if (count.incrementAndGet() == amount) { completionHandler.run(); } })); } }
@After public void teardown(TestContext ctx) { vertx.close(ctx.asyncAssertSuccess()); } }
@Test public void testInsert(TestContext ctx) { Async async = ctx.async(); connector.accept(ctx.asyncAssertSuccess(client -> { deleteFromTestTable(ctx, client, () -> { client.query("INSERT INTO Test (id, val) VALUES (1, 'Whatever')", ctx.asyncAssertSuccess(r1 -> { ctx.assertEquals(1, r1.rowCount()); async.complete(); })); }); })); }
private void testValidationError(TestContext ctx, BiConsumer<PgConnection, Handler<Throwable>> test) { Async async = ctx.async(); PgClient.connect(vertx, options(), ctx.asyncAssertSuccess(conn -> { test.accept(conn, failure -> { ctx.assertEquals(Util.buildInvalidArgsError(Stream.of("invalid-id"), Stream.of(Number.class)), failure.getMessage()); async.complete(); }); conn.preparedQuery("SELECT * FROM Fortune WHERE id=$1", Tuple.of("invalid-id"), ctx.asyncAssertFailure(failure -> { })); })); }
@Test public void testPrepareCursorValidationError(TestContext ctx) { testValidationError(ctx, (conn, cont) -> { conn.prepare("SELECT * FROM Fortune WHERE id=$1", ctx.asyncAssertSuccess(ps -> { try { ps.cursor(Tuple.of("invalid-id")); } catch (Exception e) { cont.handle(e); } })); }); }
@Test public void testCoerceSingleParam(TestContext ctx) { Async async = ctx.async(VALUES_TO_COERCE.length * SQL_TYPES_TO_COERCE_TO.length); PgClient.connect(vertx, options, ctx.asyncAssertSuccess(conn -> { for (String sqlType : SQL_TYPES_TO_COERCE_TO) { for (Object value: VALUES_TO_COERCE) { assertCoerceParam(conn, ctx, "SELECT 1 \"result\" WHERE $1::" + sqlType + "=5", value, async::countDown); } } })); }
@Test public void testPrepareBatchValidationError(TestContext ctx) { testValidationError(ctx, (conn, cont) -> { conn.prepare("SELECT * FROM Fortune WHERE id=$1", ctx.asyncAssertSuccess(ps -> { ps.batch(Collections.singletonList(Tuple.of("invalid-id")), ctx.asyncAssertFailure(cont)); })); }); }
@Test public void testGetColumnNameRows(TestContext ctx) { Async async = ctx.async(); PgClient.connect(vertx, options, ctx.asyncAssertSuccess(conn -> { conn.query("SELECT 2 \"foo\"", ctx.asyncAssertSuccess(result -> { Row row = result.iterator().next(); ctx.assertEquals("foo",row.getColumnName(0)); async.complete(); })); })); }
@Test public void testNotEqualGetColumnNameRows(TestContext ctx) { Async async = ctx.async(); PgClient.connect(vertx, options, ctx.asyncAssertSuccess(conn -> { conn.query("SELECT 2 \"foo\"", ctx.asyncAssertSuccess(result -> { Row row = result.iterator().next(); ctx.assertNotEquals("bar",row.getColumnName(0)); async.complete(); })); })); }
@Test public void testNegativeGetColumnNameRows(TestContext ctx) { Async async = ctx.async(); PgClient.connect(vertx, options, ctx.asyncAssertSuccess(conn -> { conn.query("SELECT 2 \"foo\"", ctx.asyncAssertSuccess(result -> { Row row = result.iterator().next(); ctx.assertNull(row.getColumnName(-1)); async.complete(); })); })); }
@Test public void testPrepareExecuteValidationError(TestContext ctx) { testValidationError(ctx, (conn, cont) -> { conn.prepare("SELECT * FROM Fortune WHERE id=$1", ctx.asyncAssertSuccess(ps -> { ps.execute(Tuple.of("invalid-id"), ctx.asyncAssertFailure(cont)); })); }); }
@Test public void testSslModeDisable(TestContext ctx) { Async async = ctx.async(); PgConnectOptions options = new PgConnectOptions(TLSTest.options) .setSslMode(SslMode.DISABLE); PgClient.connect(vertx, new PgConnectOptions(options), ctx.asyncAssertSuccess(conn -> { ctx.assertFalse(conn.isSSL()); async.complete(); })); }
@Test public void testSslModeAllow(TestContext ctx) { Async async = ctx.async(); PgConnectOptions options = new PgConnectOptions(TLSTest.options) .setSslMode(SslMode.ALLOW); PgClient.connect(vertx, new PgConnectOptions(options), ctx.asyncAssertSuccess(conn -> { ctx.assertFalse(conn.isSSL()); async.complete(); })); }
@Test public void testQueryParseError(TestContext ctx) { Async async = ctx.async(); PgClient.connect(vertx, options(), ctx.asyncAssertSuccess(conn -> { conn.prepare("invalid", ctx.asyncAssertFailure(err -> { PgException pgErr = (PgException) err; ctx.assertEquals(ErrorCodes.syntax_error, pgErr.getCode()); async.complete(); })); })); }