Search in sources :

Example 1 with PgPool

use of io.vertx.pgclient.PgPool in project raml-module-builder by folio-org.

the class PostgresClientIT method postgresClientConnectionThrowsException.

/**
 * @return a PostgresClient where invoking SQLConnection::update(...) or SQLConnection::updateWithParams
 * throws an RuntimeException.
 */
private PostgresClient postgresClientConnectionThrowsException() {
    PgConnection pgConnection = new PgConnection() {

        @Override
        public PgConnection notificationHandler(Handler<PgNotification> handler) {
            throw new RuntimeException();
        }

        @Override
        public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
            throw new RuntimeException();
        }

        @Override
        public int processId() {
            throw new RuntimeException();
        }

        @Override
        public int secretKey() {
            throw new RuntimeException();
        }

        @Override
        public PgConnection prepare(String s, Handler<AsyncResult<PreparedStatement>> handler) {
            throw new RuntimeException();
        }

        @Override
        public Future<PreparedStatement> prepare(String s) {
            return null;
        }

        @Override
        public SqlConnection prepare(String sql, PrepareOptions options, Handler<AsyncResult<PreparedStatement>> handler) {
            return null;
        }

        @Override
        public Future<PreparedStatement> prepare(String s, PrepareOptions prepareOptions) {
            return null;
        }

        @Override
        public PgConnection exceptionHandler(Handler<Throwable> handler) {
            return null;
        }

        @Override
        public PgConnection closeHandler(Handler<Void> handler) {
            return null;
        }

        @Override
        public void begin(Handler<AsyncResult<Transaction>> handler) {
        }

        @Override
        public Future<Transaction> begin() {
            return null;
        }

        @Override
        public boolean isSSL() {
            return false;
        }

        @Override
        public void close(Handler<AsyncResult<Void>> handler) {
            close().onComplete(handler);
        }

        @Override
        public Future<Void> close() {
            return Future.succeededFuture();
        }

        @Override
        public Query<RowSet<Row>> query(String s) {
            throw new RuntimeException();
        }

        @Override
        public PreparedQuery<RowSet<Row>> preparedQuery(String s, PrepareOptions options) {
            throw new RuntimeException();
        }

        @Override
        public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
            throw new RuntimeException();
        }

        @Override
        public DatabaseMetadata databaseMetadata() {
            throw new RuntimeException();
        }
    };
    PgPool client = new PgPoolBase() {

        @Override
        public Future<SqlConnection> getConnection() {
            return Future.succeededFuture(pgConnection);
        }
    };
    try {
        PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
        postgresClient.setClient(client);
        return postgresClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PgPool(io.vertx.pgclient.PgPool) PgConnection(io.vertx.pgclient.PgConnection) RowSet(io.vertx.sqlclient.RowSet) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) PreparedStatement(io.vertx.sqlclient.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Transaction(io.vertx.sqlclient.Transaction) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection)

Example 2 with PgPool

use of io.vertx.pgclient.PgPool in project raml-module-builder by folio-org.

the class PostgresClientIT method postgresClientGetConnectionFails.

/**
 * @return a PostgresClient where getConnection(handler) invokes the handler with a failure.
 */
private PostgresClient postgresClientGetConnectionFails() {
    PgPool client = new PgPoolBase() {

        @Override
        public Future<SqlConnection> getConnection() {
            return Future.failedFuture("postgresClientGetConnectionFails");
        }
    };
    try {
        PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
        postgresClient.setClient(client);
        return postgresClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PgPool(io.vertx.pgclient.PgPool) SqlConnection(io.vertx.sqlclient.SqlConnection) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 3 with PgPool

use of io.vertx.pgclient.PgPool in project raml-module-builder by folio-org.

the class PostgresClientIT method postgresClientNullConnection.

/**
 * @return a PostgresClient where getConnection(handler) invokes the handler with
 * a null result value and success status.
 */
private PostgresClient postgresClientNullConnection() {
    PgPool client = new PgPoolBase();
    try {
        PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
        postgresClient.setClient(client);
        return postgresClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PgPool(io.vertx.pgclient.PgPool) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 4 with PgPool

use of io.vertx.pgclient.PgPool in project raml-module-builder by folio-org.

the class PostgresClientIT method postgresClientQueryReturnBadResults.

/**
 * @return a PostgresClient where invoking SQLConnection::queryWithParams will return null ResultSet
 */
private PostgresClient postgresClientQueryReturnBadResults() {
    PgConnection pgConnection = new PgConnection() {

        @Override
        public PgConnection notificationHandler(Handler<PgNotification> handler) {
            return null;
        }

        @Override
        public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
            return this;
        }

        @Override
        public int processId() {
            return 0;
        }

        @Override
        public int secretKey() {
            return 0;
        }

        @Override
        public PgConnection prepare(String s, Handler<AsyncResult<PreparedStatement>> handler) {
            return null;
        }

        @Override
        public Future<PreparedStatement> prepare(String s) {
            return null;
        }

        @Override
        public SqlConnection prepare(String sql, PrepareOptions options, Handler<AsyncResult<PreparedStatement>> handler) {
            return null;
        }

        @Override
        public Future<PreparedStatement> prepare(String sql, PrepareOptions options) {
            return null;
        }

        @Override
        public PgConnection exceptionHandler(Handler<Throwable> handler) {
            return null;
        }

        @Override
        public PgConnection closeHandler(Handler<Void> handler) {
            return null;
        }

        @Override
        public void begin(Handler<AsyncResult<Transaction>> handler) {
        }

        @Override
        public Future<Transaction> begin() {
            return null;
        }

        @Override
        public boolean isSSL() {
            return false;
        }

        @Override
        public void close(Handler<AsyncResult<Void>> handler) {
        }

        @Override
        public Query<RowSet<Row>> query(String s) {
            return null;
        }

        @Override
        public PreparedQuery<RowSet<Row>> preparedQuery(String s) {
            return null;
        }

        @Override
        public PreparedQuery<RowSet<Row>> preparedQuery(String sql, PrepareOptions options) {
            return preparedQuery(sql);
        }

        @Override
        public Future<Void> close() {
            return null;
        }

        @Override
        public DatabaseMetadata databaseMetadata() {
            return null;
        }
    };
    PgPool client = new PgPoolBase() {

        @Override
        public Future<SqlConnection> getConnection() {
            return Future.succeededFuture(pgConnection);
        }
    };
    try {
        PostgresClient postgresClient = new PostgresClient(vertx, TENANT);
        postgresClient.setClient(client);
        return postgresClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PgPool(io.vertx.pgclient.PgPool) PgConnection(io.vertx.pgclient.PgConnection) RowSet(io.vertx.sqlclient.RowSet) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) PreparedStatement(io.vertx.sqlclient.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) FieldException(org.folio.cql2pgjson.exception.FieldException) UncheckedIOException(java.io.UncheckedIOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Transaction(io.vertx.sqlclient.Transaction) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection)

Example 5 with PgPool

use of io.vertx.pgclient.PgPool in project raml-module-builder by folio-org.

the class PostgresClientMockTest method testGetById.

@Test
public void testGetById(TestContext context) {
    String table = "a";
    String id = UUID.randomUUID().toString();
    PostgresClient pc = spy(PostgresClient.testClient());
    // mock empty query result
    @SuppressWarnings("unchecked") RowSet<Row> mockRowSet = mock(RowSet.class);
    when(mockRowSet.size()).thenReturn(0);
    @SuppressWarnings("unchecked") PreparedQuery<RowSet<Row>> mockPreparedQuery = mock(PreparedQuery.class);
    when(mockPreparedQuery.execute(any(Tuple.class))).thenReturn(Future.succeededFuture(mockRowSet));
    PgConnection mockPgConnection = mock(PgConnection.class);
    when(mockPgConnection.preparedQuery(anyString())).thenReturn(mockPreparedQuery);
    PgPool mockPgPool = mock(PgPool.class);
    when(mockPgPool.getConnection()).thenReturn(Future.succeededFuture(mockPgConnection));
    when(pc.getClient()).thenReturn(mockPgPool);
    SQLConnection mockSQLConnection = new SQLConnection(mockPgConnection, null, null);
    AsyncResult<SQLConnection> mockConn = Future.succeededFuture(mockSQLConnection);
    // tests
    pc.getByIdAsString(table, id, context.asyncAssertSuccess());
    pc.getByIdAsString(mockConn, table, id, context.asyncAssertSuccess());
    pc.getByIdAsStringForUpdate(mockConn, table, id, context.asyncAssertSuccess());
    pc.getById(table, id, context.asyncAssertSuccess());
    pc.getById(mockConn, table, id, context.asyncAssertSuccess());
    pc.getByIdForUpdate(mockConn, table, id, context.asyncAssertSuccess());
    pc.getById(table, id, Map.class, context.asyncAssertSuccess());
    pc.getById(mockConn, table, id, Map.class, context.asyncAssertSuccess());
    pc.getByIdForUpdate(mockConn, table, id, Map.class, context.asyncAssertSuccess());
    // mock query result
    String jsonString = "{\"id\": \"abc\"}";
    when(mockRowSet.size()).thenReturn(1);
    @SuppressWarnings("unchecked") RowIterator<Row> mockRowIterator = mock(RowIterator.class);
    Row mockRow = mock(Row.class);
    when(mockRowSet.iterator()).thenReturn(mockRowIterator);
    when(mockRowIterator.next()).thenReturn(mockRow);
    when(mockRow.getValue(anyInt())).thenReturn(jsonString);
    // tests
    pc.getByIdAsString(table, id, assertGetByIdAsString(context));
    pc.getByIdAsString(mockConn, table, id, assertGetByIdAsString(context));
    pc.getByIdAsStringForUpdate(mockConn, table, id, assertGetByIdAsString(context));
    pc.getById(table, id, assertGetByIdAsJson(context));
    pc.getById(mockConn, table, id, assertGetByIdAsJson(context));
    pc.getByIdForUpdate(mockConn, table, id, assertGetByIdAsJson(context));
    pc.getById(table, id, Map.class, assertGetByIdAsObject(context));
    pc.getById(mockConn, table, id, Map.class, assertGetByIdAsObject(context));
    pc.getByIdForUpdate(mockConn, table, id, Map.class, assertGetByIdAsObject(context));
    // test exceptions
    pc.getByIdAsString(Future.failedFuture("fail"), table, id, context.asyncAssertFailure());
    when(mockPgPool.getConnection()).thenReturn(Future.failedFuture("fail"));
    pc.getByIdAsString(table, id, context.asyncAssertFailure());
    when(mockPreparedQuery.execute(any(Tuple.class))).thenReturn(Future.failedFuture("fail"));
    pc.getByIdAsString(mockConn, table, id, context.asyncAssertFailure());
    when(mockPreparedQuery.execute(any(Tuple.class))).thenReturn(Future.succeededFuture(mockRowSet));
    when(mockRow.getValue(anyInt())).thenThrow(new RuntimeException("fail"));
    pc.getByIdAsString(mockConn, table, id, context.asyncAssertFailure());
    pc.getByIdAsString(mockConn, table, "1", context.asyncAssertFailure());
}
Also used : PgPool(io.vertx.pgclient.PgPool) RowSet(io.vertx.sqlclient.RowSet) PgConnection(io.vertx.pgclient.PgConnection) Row(io.vertx.sqlclient.Row) Tuple(io.vertx.sqlclient.Tuple) Test(org.junit.Test)

Aggregations

PgPool (io.vertx.pgclient.PgPool)8 FieldException (org.folio.cql2pgjson.exception.FieldException)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 PgConnection (io.vertx.pgclient.PgConnection)5 RowSet (io.vertx.sqlclient.RowSet)5 SqlConnection (io.vertx.sqlclient.SqlConnection)5 TransactionRollbackException (io.vertx.sqlclient.TransactionRollbackException)5 IOException (java.io.IOException)5 UncheckedIOException (java.io.UncheckedIOException)5 Handler (io.vertx.core.Handler)3 PrepareOptions (io.vertx.sqlclient.PrepareOptions)3 PreparedStatement (io.vertx.sqlclient.PreparedStatement)3 Transaction (io.vertx.sqlclient.Transaction)3 LocalRowSet (org.folio.rest.persist.helpers.LocalRowSet)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Row (io.vertx.sqlclient.Row)2 PreparedQuery (io.vertx.sqlclient.PreparedQuery)1 Query (io.vertx.sqlclient.Query)1 SqlResult (io.vertx.sqlclient.SqlResult)1 Tuple (io.vertx.sqlclient.Tuple)1