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