From 3af17ece6aa248c939184e1101c3d0cd55c71bdd Mon Sep 17 00:00:00 2001 From: Mustapha Zorgati <15628173+mustaphazorgati@users.noreply.github.com> Date: Thu, 16 Feb 2023 14:58:17 +0100 Subject: [PATCH] TSK-2003: removed reset of schema name in SqlConRunner#runWithConnection --- .../jobs/helper/SqlConnectionRunner.java | 10 +-- .../helper/SqlConnectionRunnerAccTest.java | 72 +++++-------------- 2 files changed, 19 insertions(+), 63 deletions(-) diff --git a/lib/taskana-core/src/main/java/pro/taskana/task/internal/jobs/helper/SqlConnectionRunner.java b/lib/taskana-core/src/main/java/pro/taskana/task/internal/jobs/helper/SqlConnectionRunner.java index a2080142d..859d80ec4 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/task/internal/jobs/helper/SqlConnectionRunner.java +++ b/lib/taskana-core/src/main/java/pro/taskana/task/internal/jobs/helper/SqlConnectionRunner.java @@ -26,14 +26,8 @@ public class SqlConnectionRunner { public void runWithConnection(CheckedConsumer consumer) { try (Connection connection = getConnection()) { - String originalSchema = connection.getSchema(); - try { - connection.setSchema(taskanaEngine.getConfiguration().getSchemaName()); - consumer.accept(connection); - } finally { - connection.setSchema(originalSchema); - } - + connection.setSchema(taskanaEngine.getConfiguration().getSchemaName()); + consumer.accept(connection); } catch (SQLException e) { throw new SystemException("SQL error while running low level SQL", e); } diff --git a/lib/taskana-core/src/test/java/acceptance/jobs/helper/SqlConnectionRunnerAccTest.java b/lib/taskana-core/src/test/java/acceptance/jobs/helper/SqlConnectionRunnerAccTest.java index 22a16daa2..92369161b 100644 --- a/lib/taskana-core/src/test/java/acceptance/jobs/helper/SqlConnectionRunnerAccTest.java +++ b/lib/taskana-core/src/test/java/acceptance/jobs/helper/SqlConnectionRunnerAccTest.java @@ -7,92 +7,54 @@ import acceptance.AbstractAccTest; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; +import java.util.concurrent.atomic.AtomicReference; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.Test; 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.TaskanaRuntimeException; import pro.taskana.common.test.security.JaasExtension; -import pro.taskana.common.test.security.WithAccessId; import pro.taskana.task.internal.jobs.helper.SqlConnectionRunner; /** Acceptance test for all "jobs tasks runner" scenarios. */ @ExtendWith(JaasExtension.class) 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 - void should_executeSimpleQuery() { + void should_executeSimpleQuery() throws Exception { // given SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine); String taskId = "TKI:000000000000000000000000000000000050"; // when + AtomicReference resultSet = new AtomicReference<>(); runner.runWithConnection( connection -> { PreparedStatement preparedStatement = connection.prepareStatement("select * from TASK where ID = ?"); preparedStatement.setString(1, taskId); - final ResultSet resultSet = preparedStatement.executeQuery(); - - // then - assertThat(resultSet.next()).isTrue(); + resultSet.set(preparedStatement.executeQuery()); }); + + // then + assertThat(resultSet.get().next()).isTrue(); } @Test void should_catchSqlExceptionAndThrowSystemException() { // given SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine); + SQLException expectedException = new SQLException("test"); // when - assertThatThrownBy( - () -> - runner.runWithConnection( - connection -> { - throw new SQLException("test"); - })) - .isInstanceOf(SystemException.class); - } + ThrowingCallable code = + () -> + runner.runWithConnection( + connection -> { + throw expectedException; + }); - @Test - @WithAccessId(user = "admin") - @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"); + // then + assertThatThrownBy(code).isInstanceOf(SystemException.class).hasCause(expectedException); } }