From fea8ee8c7ea0adaf945dd650c6342522e4a14dfe Mon Sep 17 00:00:00 2001 From: Richard Richter Date: Fri, 10 Sep 2021 00:31:16 +0200 Subject: [PATCH] repo-sqale tests: shortcuts for new JDBC sessions/transactions --- .../repo/sqale/SqaleRepoBaseTest.java | 22 +++++++++++++------ .../repo/sqale/func/AuditCleanupTest.java | 2 +- .../func/SqaleRepoSearchIterativeTest.java | 2 +- .../repo/sqale/func/SqaleRepoSmokeTest.java | 8 +++---- .../repo/sqale/perf/OrgHierarchyPerfTest.java | 2 +- .../sqale/slow/SequenceTestConcurrency.java | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/SqaleRepoBaseTest.java b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/SqaleRepoBaseTest.java index 1ea1aa69a6c..40520dd30a1 100644 --- a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/SqaleRepoBaseTest.java +++ b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/SqaleRepoBaseTest.java @@ -59,7 +59,7 @@ public class SqaleRepoBaseTest extends AbstractSpringTest @BeforeClass public void clearDatabase() { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { // object delete cascades to sub-rows of the "object aggregate" jdbcSession.executeStatement("TRUNCATE m_object CASCADE;"); @@ -78,7 +78,7 @@ public void clearDatabase() { // this is "suite" scope code, but @BeforeSuite can't use injected fields if (!cacheTablesCleared) { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { // We could skip default relation ID with .where(u.id.gt(0)), but it must work. jdbcSession.newDelete(QUri.DEFAULT).execute(); jdbcSession.newDelete(QExtItem.DEFAULT).execute(); @@ -97,7 +97,7 @@ public void clearDatabase() { // Called on demand public void clearAudit() { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { jdbcSession.executeStatement("TRUNCATE ma_audit_event CASCADE;"); jdbcSession.commit(); display("AUDIT tables cleared"); @@ -141,7 +141,7 @@ protected > long count( protected > long count( Q path, Predicate... conditions) { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) { + try (JdbcSession jdbcSession = startReadOnlyTransaction()) { SQLQuery query = jdbcSession.newQuery() .from(path) .where(conditions); @@ -156,7 +156,7 @@ protected > List select( protected > List select( Q path, Predicate... conditions) { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) { + try (JdbcSession jdbcSession = startReadOnlyTransaction()) { return jdbcSession.newQuery() .from(path) .where(conditions) @@ -174,7 +174,7 @@ protected > List select( protected > @Nullable R selectOneNullable( Q path, Predicate... conditions) { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) { + try (JdbcSession jdbcSession = startReadOnlyTransaction()) { return jdbcSession.newQuery() .from(path) .where(conditions) @@ -335,7 +335,7 @@ private String extKey(Containerable extContainer, String itemName, MExtItemHolde PrismContainerValue pcv = extContainer.asPrismContainerValue(); ItemDefinition def = pcv.getDefinition().findItemDefinition(new ItemName(itemName)); MExtItem.Key key = MExtItem.keyFrom(def, holder); - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) { + try (JdbcSession jdbcSession = startReadOnlyTransaction()) { QExtItem ei = QExtItem.DEFAULT; return jdbcSession.newQuery() .from(ei) @@ -418,4 +418,12 @@ public ItemDefinition getDefinition(ItemName attributeName) { } } // endregion + + protected JdbcSession startTransaction() { + return sqlRepoContext.newJdbcSession().startTransaction(); + } + + protected JdbcSession startReadOnlyTransaction() { + return sqlRepoContext.newJdbcSession().startReadOnlyTransaction(); + } } diff --git a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/AuditCleanupTest.java b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/AuditCleanupTest.java index 5c7cee27d3b..0d41422f33c 100644 --- a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/AuditCleanupTest.java +++ b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/AuditCleanupTest.java @@ -116,7 +116,7 @@ public void test200CleanupByAge() throws SchemaException { } private long selectMinMaxId(QAuditEventRecord qae, NumberExpression minMaxPath) { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) { + try (JdbcSession jdbcSession = startReadOnlyTransaction()) { return jdbcSession.newQuery() .select(minMaxPath) .from(qae) diff --git a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSearchIterativeTest.java b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSearchIterativeTest.java index c6076c47be9..e9388928aec 100644 --- a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSearchIterativeTest.java +++ b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSearchIterativeTest.java @@ -228,7 +228,7 @@ public void test125SearchIterativeWithCustomOrdering() throws Exception { and("all objects were processed in proper order"); QUser u = aliasFor(QUser.class); - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) { + try (JdbcSession jdbcSession = startReadOnlyTransaction()) { List result = jdbcSession.newQuery() .from(u) .orderBy(u.costCenter.asc(), u.oid.asc()) diff --git a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSmokeTest.java b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSmokeTest.java index ee2234bd04e..250fba1ec62 100644 --- a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSmokeTest.java +++ b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/func/SqaleRepoSmokeTest.java @@ -89,7 +89,7 @@ public void test020TestOrgClosureConsistency() throws Exception { OperationResult result = createOperationResult(); given("reset closure"); - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { jdbcSession.executeStatement("CALL m_refresh_org_closure(true)"); jdbcSession.commit(); } @@ -126,7 +126,7 @@ public void test021OrgClosureIsRefreshedBeforeOrgFilterQuery() throws Exception OperationResult result = createOperationResult(); given("reset closure"); - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { jdbcSession.executeStatement("CALL m_refresh_org_closure(true)"); jdbcSession.commit(); } @@ -365,7 +365,7 @@ public void test900WorkingWithPgArraysJsonbAndBytea() { user.subtypes = new String[] { "subtype1", "subtype2" }; user.ext = new Jsonb("{\"key\" : \"value\",\n\"number\": 47} "); // more whitespaces/lines user.photo = new byte[] { 0, 1, 0, 1 }; - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { jdbcSession.newInsert(u).populate(user).execute(); jdbcSession.commit(); } @@ -378,7 +378,7 @@ public void test900WorkingWithPgArraysJsonbAndBytea() { assertThat(row.photo).hasSize(4); // setting NULLs - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { jdbcSession.newUpdate(u) .setNull(u.policySituations) .set(u.subtypes, (String[]) null) // this should do the same diff --git a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/perf/OrgHierarchyPerfTest.java b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/perf/OrgHierarchyPerfTest.java index c3d61fa0ae3..c72ac7137a2 100644 --- a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/perf/OrgHierarchyPerfTest.java +++ b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/perf/OrgHierarchyPerfTest.java @@ -45,7 +45,7 @@ public class OrgHierarchyPerfTest extends SqaleRepoBaseTest { public void initObjects() throws Exception { OperationResult result = createOperationResult(); - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { jdbcSession.executeStatement("CALL m_refresh_org_closure(true)"); jdbcSession.commit(); } diff --git a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/slow/SequenceTestConcurrency.java b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/slow/SequenceTestConcurrency.java index e40b5534f4f..d9ea37d640f 100644 --- a/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/slow/SequenceTestConcurrency.java +++ b/repo/repo-sqale/src/test/java/com/evolveum/midpoint/repo/sqale/slow/SequenceTestConcurrency.java @@ -103,7 +103,7 @@ public void test210TenThreadsReturning() throws Exception { private void concurrencyUniversal( long duration, WorkerThread[] workerThreads, boolean alwaysOrder) throws Exception { - try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) { + try (JdbcSession jdbcSession = startTransaction()) { System.out.println(">>>>" + jdbcSession.connection().getTransactionIsolation()); }