I have the following code in Java to query a database:
public interface MapReduceDAO {
String host = "mysql";
int port = 3306;
String user = "root";
String password = "root";
String dbName = "customers";
default String customersMysqlUrl(String name) {
return getDocker().containers().container(name).port(port).inFormat("$HOST:$EXTERNAL_PORT");
}
default void checkTableHasData(Duration atMost, String tableName) throws Exception {
try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {
await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
() -> mysqlQuery.count("SELECT COUNT(*) FROM " + tableName),
is(Matchers.greaterThan(0)));
}
}
default void checkExistsQuery(Duration atMost, String tableName, int countValueExpected) throws Exception {
try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {
await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
() -> mysqlQuery.count("SELECT COUNT(*) FROM " + tableName),
is(Matchers.equalTo(countValueExpected)));
}
}
DockerComposeRule getDocker();
}
How to avoid using repeated code. In method checkTableHasData and checkExistsQuery, I have mostly repeated code.
Edit: Forgot to mention, they may have different assert at the end, e.g:
is(Matchers.greaterThan(0)));
is(Matchers.equalTo(countValueExpected)));