From ae3ae26d030fbbc87324b6841aee717ee0555dd3 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 2 Jan 2025 09:16:48 +0100 Subject: [PATCH] test idempotence of procedure ..._rebuild_rbac_system --- .../RbacGrantRepositoryIntegrationTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/grant/RbacGrantRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/grant/RbacGrantRepositoryIntegrationTest.java index f4df3c6d..05b3bc73 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/grant/RbacGrantRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/grant/RbacGrantRepositoryIntegrationTest.java @@ -290,6 +290,32 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { } } + @Nested + class RebuildRbacSystem { + + @Test + public void rebuildingTheRbacSystemWitSameRbacSpecDoesNotChangeGrantNorRoleCount() { + final var grantCountBefore = sql("SELECT COUNT(*) FROM rbac.grants"); + final var roleCountBefore = sql("SELECT COUNT(*) FROM rbac.role"); + + jpaAttempt.transacted(() -> + em.createNativeQuery("CALL rbactest.package_rebuild_rbac_system()") + ); + + final var grantCountAfter = sql("SELECT COUNT(*) FROM rbac.grants"); + assertThat(grantCountBefore).as("grant count must not change").isEqualTo(grantCountAfter); + + final var roleCountAfter = sql("SELECT COUNT(*) FROM rbac.role"); + assertThat(roleCountBefore).as("role count must not change").isEqualTo(roleCountAfter); + } + + private Object sql(final String query) { + return jpaAttempt.transacted(() -> + em.createNativeQuery(query).getSingleResult() + ).assumeSuccessful().returnedValue(); + } + } + private RbacSubjectEntity createNewUserTransacted() { return jpaAttempt.transacted(() -> { final var newUserName = "test-user-" + System.currentTimeMillis() + "@example.com";