TSK-2003: removed reset of schema name in SqlConRunner#runWithConnection

This commit is contained in:
Mustapha Zorgati 2023-02-16 14:58:17 +01:00
parent b96c68ac6f
commit 3af17ece6a
2 changed files with 19 additions and 63 deletions

View File

@ -26,14 +26,8 @@ public class SqlConnectionRunner {
public void runWithConnection(CheckedConsumer<Connection, SQLException> consumer) { public void runWithConnection(CheckedConsumer<Connection, SQLException> consumer) {
try (Connection connection = getConnection()) { try (Connection connection = getConnection()) {
String originalSchema = connection.getSchema(); connection.setSchema(taskanaEngine.getConfiguration().getSchemaName());
try { consumer.accept(connection);
connection.setSchema(taskanaEngine.getConfiguration().getSchemaName());
consumer.accept(connection);
} finally {
connection.setSchema(originalSchema);
}
} catch (SQLException e) { } catch (SQLException e) {
throw new SystemException("SQL error while running low level SQL", e); throw new SystemException("SQL error while running low level SQL", e);
} }

View File

@ -7,92 +7,54 @@ import acceptance.AbstractAccTest;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import org.junit.jupiter.api.BeforeEach; import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Disabled; import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import pro.taskana.TaskanaConfiguration;
import pro.taskana.common.api.TaskanaEngine;
import pro.taskana.common.api.exceptions.SystemException; import pro.taskana.common.api.exceptions.SystemException;
import pro.taskana.common.api.exceptions.TaskanaRuntimeException;
import pro.taskana.common.test.security.JaasExtension; import pro.taskana.common.test.security.JaasExtension;
import pro.taskana.common.test.security.WithAccessId;
import pro.taskana.task.internal.jobs.helper.SqlConnectionRunner; import pro.taskana.task.internal.jobs.helper.SqlConnectionRunner;
/** Acceptance test for all "jobs tasks runner" scenarios. */ /** Acceptance test for all "jobs tasks runner" scenarios. */
@ExtendWith(JaasExtension.class) @ExtendWith(JaasExtension.class)
class SqlConnectionRunnerAccTest extends AbstractAccTest { class SqlConnectionRunnerAccTest extends AbstractAccTest {
@BeforeEach
void before() throws Exception {
// required if single tests modify database
// TODO split test class into readOnly & modifying tests to improve performance
resetDb(false);
}
@Test @Test
void should_executeSimpleQuery() { void should_executeSimpleQuery() throws Exception {
// given // given
SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine); SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine);
String taskId = "TKI:000000000000000000000000000000000050"; String taskId = "TKI:000000000000000000000000000000000050";
// when // when
AtomicReference<ResultSet> resultSet = new AtomicReference<>();
runner.runWithConnection( runner.runWithConnection(
connection -> { connection -> {
PreparedStatement preparedStatement = PreparedStatement preparedStatement =
connection.prepareStatement("select * from TASK where ID = ?"); connection.prepareStatement("select * from TASK where ID = ?");
preparedStatement.setString(1, taskId); preparedStatement.setString(1, taskId);
final ResultSet resultSet = preparedStatement.executeQuery(); resultSet.set(preparedStatement.executeQuery());
// then
assertThat(resultSet.next()).isTrue();
}); });
// then
assertThat(resultSet.get().next()).isTrue();
} }
@Test @Test
void should_catchSqlExceptionAndThrowSystemException() { void should_catchSqlExceptionAndThrowSystemException() {
// given // given
SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine); SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine);
SQLException expectedException = new SQLException("test");
// when // when
assertThatThrownBy( ThrowingCallable code =
() -> () ->
runner.runWithConnection( runner.runWithConnection(
connection -> { connection -> {
throw new SQLException("test"); throw expectedException;
})) });
.isInstanceOf(SystemException.class);
}
@Test // then
@WithAccessId(user = "admin") assertThatThrownBy(code).isInstanceOf(SystemException.class).hasCause(expectedException);
@Disabled("This Test does not make sense anymore")
void should_SetOriginalSchema_When_ExceptionThrown() throws Exception {
TaskanaEngine taskanaEngine =
TaskanaEngine.buildTaskanaEngine(
new TaskanaConfiguration.Builder(AbstractAccTest.taskanaEngine.getConfiguration())
.schemaName("NotExisting")
.build());
SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine);
// TSK-1749
assertThat(runner.getConnection().getSchema()).matches("TASKANA\\s|TASKANA|taskana");
String taskId = "TKI:000000000000000000000000000000000000";
assertThatThrownBy(
() ->
runner.runWithConnection(
connection -> {
PreparedStatement preparedStatement =
connection.prepareStatement("select * from TASK where ID = ?");
preparedStatement.setString(1, taskId);
preparedStatement.executeQuery();
}))
.isInstanceOf(TaskanaRuntimeException.class);
// TSK-1749
assertThat(runner.getConnection().getSchema()).matches("TASKANA\\s|TASKANA|taskana");
} }
} }