import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.List;
import java.util.Map;
private static final Logger LOGGER = LoggerFactory.getLogger(DataBaseAccessImpl.class);
@Override
- public List<Map<String, String>> executeQuery(String query, ExecuteResultSet<ResultSet> executeResultSet) {
+ public List<Map<String, String>> executeQuery(
+ String query, ExecuteResultSet<ResultSet> executeResultSet, FillPreparedStatement fillStatement) {
List<Map<String, String>> result = null;
try {
- result = this.executeQueryThrowable(query, executeResultSet);
+ result = this.executeQueryThrowable(query, executeResultSet, fillStatement);
} catch (SQLException exception) {
LOGGER.error("Query error: ", exception);
}
return result;
}
- protected List<Map<String, String>> executeQueryThrowable(String query, ExecuteResultSet<ResultSet> executeResultSet) throws SQLException {
+ protected List<Map<String, String>> executeQueryThrowable(
+ String query, ExecuteResultSet<ResultSet> executeResultSet, FillPreparedStatement fillStatement) throws SQLException {
final DataSource dataSource = DoDataSourceContext.getInstance().getDataSource();
try {
final Connection connection = dataSource.getConnection();
return this.doExecuteQuery(
query,
connection,
- preparedStatement -> preparedStatement.executeQuery(),
+ preparedStatement ->
+ {
+ fillStatement.doFill(preparedStatement);
+
+ return preparedStatement.executeQuery();
+ },
executeResultSet
);
} finally {
import javax.sql.DataSource;
+import org.junit.Before;
import org.junit.Test;
import com.prueba.core.context.integration.database.DataBaseAccess;
private static final String PASSWORD = "PASSWORD";
private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE";
+ private DataSource dataSource;
+ private LiquibaseContext liquibaseContext;
+
+ @Before
+ public void init() {
+ dataSource = DoDataSourceContext.getInstance().getDataSource();
+ liquibaseContext = new LiquibaseContext(dataSource);
+ liquibaseContext.init();
+ }
+
@Test
public void whenLoadLiquibaseContextThenReturnResultsFromDatabase() {
- final DataSource dataSource = DoDataSourceContext.getInstance().getDataSource();
- final LiquibaseContext liquibaseContext = new LiquibaseContext(dataSource);
- liquibaseContext.init();
-
final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
final List<Map<String, String>> dataResult = dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT",
answer ->
}
return result;
+ },
+ preparedStatement -> {
+
+ });
+
+ final String expectedCode = "GUMARTIN";
+ final String expectedName = "Gustavo";
+ final String expectedSurname = "Martin Morcuende";
+ final String expectedPassword = "lame";
+ final String expectedAppRoleCode = "ROLE_APP_ADMIN";
+
+ final Map<String, String> row = dataResult.get(0);
+ assertNotNull(row);
+ assertEquals(expectedCode, row.get(CODE));
+ assertEquals(expectedName, row.get(NAME));
+ assertEquals(expectedSurname, row.get(SURNAME));
+ assertEquals(expectedPassword, row.get(PASSWORD));
+ assertEquals(expectedAppRoleCode, row.get(APP_ROLE_CODE));
+ }
+
+ @Test
+ public void whenLoadLiquibaseContextThenReturnResultsFromDatabaseUsingParameters() {
+ final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
+ final List<Map<String, String>> dataResult = dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?",
+ answer ->
+ {
+ final List<Map<String, String>> result = new ArrayList<>();
+ while (answer.next()) {
+ final Map<String, String> row = new HashMap<>();
+ row.put(CODE, answer.getString(CODE));
+ row.put(NAME, answer.getString(NAME));
+ row.put(SURNAME, answer.getString(SURNAME));
+ row.put(PASSWORD, answer.getString(PASSWORD));
+ row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE));
+ result.add(row);
+ }
+
+ return result;
+ },
+ preparedStatement -> {
+ preparedStatement.setString(1, "GUMARTIN");
});
final String expectedCode = "GUMARTIN";
assertEquals(expectedAppRoleCode, row.get(APP_ROLE_CODE));
}
+ @Test
+ public void whenLoadLiquibaseContextAndNotUserFoundThenDoNotReturResultsFromDatabase() {
+ final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
+ final List<Map<String, String>> dataResult = dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?",
+ answer ->
+ {
+ final List<Map<String, String>> result = new ArrayList<>();
+ while (answer.next()) {
+ final Map<String, String> row = new HashMap<>();
+ row.put(CODE, answer.getString(CODE));
+ row.put(NAME, answer.getString(NAME));
+ row.put(SURNAME, answer.getString(SURNAME));
+ row.put(PASSWORD, answer.getString(PASSWORD));
+ row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE));
+ result.add(row);
+ }
+
+ return result;
+ },
+ preparedStatement -> {
+ preparedStatement.setString(1, "DONOTEXIST");
+ });
+
+
+ assertTrue(dataResult.isEmpty());
+ }
+
}