1

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)));

3
  • Pass the query as a parameter in your function Commented Apr 17, 2018 at 14:27
  • @Codeer I would agree with you, though it is only acceptable (still not recommended) if the values are not user provided. Commented Apr 17, 2018 at 14:36
  • @killjoy definitely true. I forgot about that for a moment Commented Apr 17, 2018 at 14:37

2 Answers 2

2

If I see it correctly, those methods only differ in the parameter you give to count(). Just introduce a method that takes this as a parameter and call it with the different values.

default void checkTableHasData(Duration atMost, String tableName) throws Exception {
    check(atMost, "SELECT COUNT(*) FROM " + tableName);
}

default void checkTableRowExistSearchOnColumn(Duration atMost, String tableName, String columnName,
                                              String columnValue) throws Exception {
    check(atMost, "SELECT COUNT(*) FROM " + tableName + " where " + columnName +
                               " = " + columnValue);
}

private void check(Duration atMost, String countStatement) throws Exception {
    try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {

        await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
        () -> mysqlQuery.count(countStatement),
        is(Matchers.greaterThan(0)));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have rewarded you the answer. Just one small change - can you see my edit on the bottom of the question?
@Saffik just add another parameter of type Matcher.
1

You can simply extract the common behaviour in a separate method:

default void checkTableHasData(Duration atMost, String tableName) throws Exception {
    checkExistsQuery("SELECT COUNT(*) FROM " + tableName),
}

default void checkTableRowExistSearchOnColumn(Duration atMost, String tableName, String columnName,
                                              String columnValue) throws Exception {
    checkExistsQuery("SELECT COUNT(*) FROM " + tableName + " where " + columnName +
                               " = " + columnValue),
}

private static void checkExistsQuery(Duration atMost, String query) {
    try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {

        await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
        () -> mysqlQuery.count(query),
        is(Matchers.greaterThan(0)));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.