From bc27e6dc895dcc7a9a87380230daa09fe037d37d Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 13 Sep 2022 14:54:57 +0200 Subject: [PATCH] refactor name extractors into entity classes --- .../rbac/rbacgrant/RbacGrantEntity.java | 7 ++++-- .../rbac/rbacrole/RbacRoleEntity.java | 8 +++---- ...fficeContactRepositoryIntegrationTest.java | 4 ++-- ...fficePartnerRepositoryIntegrationTest.java | 4 ++-- ...OfficePersonRepositoryIntegrationTest.java | 4 ++-- .../RawRbacGrantDisplayExtractor.java | 14 ----------- .../rbac/rbacgrant/RawRbacGrantEntity.java | 9 +++++++ .../RbacGrantControllerAcceptanceTest.java | 6 ++--- .../rbacgrant/RbacGrantDisplayExtractor.java | 14 ----------- .../rbacgrant/RbacGrantEntityUnitTest.java | 2 +- .../RbacGrantRepositoryIntegrationTest.java | 24 ++++++++++--------- .../rbac/rbacrole/RawRbacRoleEntity.java | 9 +++++++ .../rbacrole/RawRbacRoleNameExtractor.java | 15 ------------ .../rbac/rbacrole/RbacRoleNameExtractor.java | 15 ------------ 14 files changed, 50 insertions(+), 85 deletions(-) delete mode 100644 src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantDisplayExtractor.java delete mode 100644 src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantDisplayExtractor.java delete mode 100644 src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleNameExtractor.java delete mode 100644 src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleNameExtractor.java diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntity.java b/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntity.java index 901d6266..1f01eac6 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntity.java @@ -59,7 +59,10 @@ public class RbacGrantEntity { } public String toDisplay() { - return "{ grant " + (assumed ? "assumed " : "") + - "role " + grantedRoleIdName + " to user " + granteeUserName + " by role " + grantedByRoleIdName + " }"; + return "{ grant role " + grantedRoleIdName + + " to user " + granteeUserName + + " by role " + grantedByRoleIdName + + (assumed ? " and assume" : "") + + " }"; } } diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleEntity.java b/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleEntity.java index d0dc4d93..53264d7a 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleEntity.java @@ -20,16 +20,16 @@ public class RbacRoleEntity { @Id private UUID uuid; - @Column(name="objectuuid") + @Column(name = "objectuuid") private UUID objectUuid; - @Column(name="objecttable") + @Column(name = "objecttable") private String objectTable; - @Column(name="objectidname") + @Column(name = "objectidname") private String objectIdName; - @Column(name="roletype") + @Column(name = "roletype") @Enumerated(EnumType.STRING) private RbacRoleType roleType; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java index 4987b10b..e5e43a67 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/contact/HsOfficeContactRepositoryIntegrationTest.java @@ -23,8 +23,8 @@ import java.util.List; import java.util.function.Supplier; import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.hsOfficeContact; -import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantDisplayExtractor.grantDisplaysOf; -import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleNameExtractor.roleNamesOf; +import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantEntity.grantDisplaysOf; +import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf; import static net.hostsharing.test.JpaAttempt.attempt; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assumptions.assumeThat; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java index 506ba6bc..8c5a0885 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java @@ -26,8 +26,8 @@ import java.util.List; import java.util.Set; import java.util.UUID; -import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantDisplayExtractor.grantDisplaysOf; -import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleNameExtractor.roleNamesOf; +import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantEntity.grantDisplaysOf; +import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf; import static net.hostsharing.test.JpaAttempt.attempt; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assumptions.assumeThat; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java index da0a07ab..af13ea8c 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePersonRepositoryIntegrationTest.java @@ -22,8 +22,8 @@ import java.util.List; import java.util.function.Supplier; import static net.hostsharing.hsadminng.hs.office.person.TestHsOfficePerson.hsOfficePerson; -import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantDisplayExtractor.grantDisplaysOf; -import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleNameExtractor.roleNamesOf; +import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantEntity.grantDisplaysOf; +import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf; import static net.hostsharing.test.JpaAttempt.attempt; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assumptions.assumeThat; diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantDisplayExtractor.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantDisplayExtractor.java deleted file mode 100644 index 1e362e84..00000000 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantDisplayExtractor.java +++ /dev/null @@ -1,14 +0,0 @@ -package net.hostsharing.hsadminng.rbac.rbacgrant; - -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.stream.Collectors; - -public class RawRbacGrantDisplayExtractor { - - @NotNull - public static List grantDisplaysOf(final List roles) { - return roles.stream().map(RawRbacGrantEntity::toDisplay).collect(Collectors.toList()); - } -} diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantEntity.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantEntity.java index 6effb682..fa9fc7ad 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantEntity.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RawRbacGrantEntity.java @@ -1,10 +1,13 @@ package net.hostsharing.hsadminng.rbac.rbacgrant; import lombok.*; +import org.jetbrains.annotations.NotNull; import org.springframework.data.annotation.Immutable; import javax.persistence.*; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Entity @Table(name = "rbacgrants_ev") @@ -52,4 +55,10 @@ public class RawRbacGrantEntity { " }"; // @formatter:on } + + + @NotNull + public static List grantDisplaysOf(final List roles) { + return roles.stream().map(RawRbacGrantEntity::toDisplay).collect(Collectors.toList()); + } } diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java index 1e4497ce..222b7a0a 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantControllerAcceptanceTest.java @@ -272,9 +272,9 @@ class RbacGrantControllerAcceptanceTest extends ContextBasedTest { .body("granteeUserName", is(givenNewUser.getName())); assertThat(findAllGrantsOf(givenCurrentUserAsPackageAdmin)) .extracting(RbacGrantEntity::toDisplay) - .contains("{ grant assumed role " + givenOwnPackageAdminRole.getRoleName() + + .contains("{ grant role " + givenOwnPackageAdminRole.getRoleName() + " to user " + givenNewUser.getName() + - " by role " + givenRoleToGrant + " }"); + " by role " + givenRoleToGrant + " and assume }"); } @Test @@ -323,7 +323,7 @@ class RbacGrantControllerAcceptanceTest extends ContextBasedTest { .toUser(givenArbitraryUser)); assumeGrantExists( givenCurrentUserAsPackageAdmin, - "{ grant assumed role %s to user %s by role %s }".formatted( + "{ grant role %s to user %s by role %s and assume }".formatted( givenOwnPackageAdminRole.getRoleName(), givenArbitraryUser.getName(), givenCurrentUserAsPackageAdmin.assumedRole)); diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantDisplayExtractor.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantDisplayExtractor.java deleted file mode 100644 index ad43beb9..00000000 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantDisplayExtractor.java +++ /dev/null @@ -1,14 +0,0 @@ -package net.hostsharing.hsadminng.rbac.rbacgrant; - -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.stream.Collectors; - -public class RbacGrantDisplayExtractor { - - @NotNull - public static List grantDisplaysOf(final List roles) { - return roles.stream().map(RbacGrantEntity::toDisplay).collect(Collectors.toList()); - } -} diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntityUnitTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntityUnitTest.java index 22729d32..eea18932 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntityUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantEntityUnitTest.java @@ -40,7 +40,7 @@ class RbacGrantEntityUnitTest { final var display = entity.toDisplay(); // then - assertThat(display).isEqualTo("{ grant assumed role GrantED to user GrantEE by role GrantER }"); + assertThat(display).isEqualTo("{ grant role GrantED to user GrantEE by role GrantER and assume }"); } @Test diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java index 58772f6e..f9b304af 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacgrant/RbacGrantRepositoryIntegrationTest.java @@ -41,6 +41,9 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { @Autowired RbacGrantRepository rbacGrantRepository; + @Autowired + RawRbacGrantRepository rawRbacGrantRepository; + @Autowired RbacUserRepository rbacUserRepository; @@ -68,7 +71,7 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { // then exactlyTheseRbacGrantsAreReturned( result, - "{ grant assumed role test_package#xxx00.admin to user pac-admin-xxx00@xxx.example.com by role test_customer#xxx.admin }"); + "{ grant role test_package#xxx00.admin to user pac-admin-xxx00@xxx.example.com by role test_customer#xxx.admin and assume }"); } @Test @@ -83,10 +86,10 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { // then exactlyTheseRbacGrantsAreReturned( result, - "{ grant assumed role test_customer#xxx.admin to user customer-admin@xxx.example.com by role global#global.admin }", - "{ grant assumed role test_package#xxx00.admin to user pac-admin-xxx00@xxx.example.com by role test_customer#xxx.admin }", - "{ grant assumed role test_package#xxx01.admin to user pac-admin-xxx01@xxx.example.com by role test_customer#xxx.admin }", - "{ grant assumed role test_package#xxx02.admin to user pac-admin-xxx02@xxx.example.com by role test_customer#xxx.admin }"); + "{ grant role test_customer#xxx.admin to user customer-admin@xxx.example.com by role global#global.admin and assume }", + "{ grant role test_package#xxx00.admin to user pac-admin-xxx00@xxx.example.com by role test_customer#xxx.admin and assume }", + "{ grant role test_package#xxx01.admin to user pac-admin-xxx01@xxx.example.com by role test_customer#xxx.admin and assume }", + "{ grant role test_package#xxx02.admin to user pac-admin-xxx02@xxx.example.com by role test_customer#xxx.admin and assume }"); } @Test @@ -101,7 +104,7 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { // then exactlyTheseRbacGrantsAreReturned( result, - "{ grant assumed role test_package#xxx00.admin to user pac-admin-xxx00@xxx.example.com by role test_customer#xxx.admin }"); + "{ grant role test_package#xxx00.admin to user pac-admin-xxx00@xxx.example.com by role test_customer#xxx.admin and assume }"); } } @@ -129,7 +132,7 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { assertThat(rbacGrantRepository.findAll()) .extracting(RbacGrantEntity::toDisplay) .contains( - "{ grant assumed role test_package#xxx00.admin to user pac-admin-zzz00@zzz.example.com by role test_customer#xxx.admin }"); + "{ grant role test_package#xxx00.admin to user pac-admin-zzz00@zzz.example.com by role test_customer#xxx.admin and assume }"); } @Test @@ -167,7 +170,6 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { // finally, we use the new user to make sure, no roles were granted context(given.arbitraryUser.getName(), null); assertThat(rbacGrantRepository.findAll()) - .extracting(RbacGrantEntity::toDisplay) .hasSize(0); }); } @@ -255,9 +257,9 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { ); assumeThat(grantAttempt.caughtException()).isNull(); - assumeThat(rbacGrantRepository.findAll()) - .extracting(RbacGrantEntity::toDisplay) - .contains("{ grant assumed role %s to user %s by role %s }".formatted( + assumeThat(rawRbacGrantRepository.findAll()) + .extracting(RawRbacGrantEntity::toDisplay) + .contains("{ grant role %s to user %s by role %s and assume }".formatted( with.grantedRole, with.granteeUserName, with.assumedRole )); diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleEntity.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleEntity.java index ed35a474..c6b90c47 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleEntity.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleEntity.java @@ -2,10 +2,13 @@ package net.hostsharing.hsadminng.rbac.rbacrole; import lombok.*; import org.hibernate.annotations.Formula; +import org.jetbrains.annotations.NotNull; import org.springframework.data.annotation.Immutable; import javax.persistence.*; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Entity @Table(name = "rbacrole_ev") @@ -35,4 +38,10 @@ public class RawRbacRoleEntity { @Formula("objectTable||'#'||objectIdName||'.'||roleType") private String roleName; + + @NotNull + public static List roleNamesOf(@NotNull final List roles) { + return roles.stream().map(RawRbacRoleEntity::getRoleName).collect(Collectors.toList()); + } + } diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleNameExtractor.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleNameExtractor.java deleted file mode 100644 index 949f3f37..00000000 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RawRbacRoleNameExtractor.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.hostsharing.hsadminng.rbac.rbacrole; - -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.stream.Collectors; - -public class RawRbacRoleNameExtractor { - - @NotNull - public static List roleNamesOf(@NotNull final List roles) { - return roles.stream().map(RawRbacRoleEntity::getRoleName).collect(Collectors.toList()); - } - -} diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleNameExtractor.java b/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleNameExtractor.java deleted file mode 100644 index 047de283..00000000 --- a/src/test/java/net/hostsharing/hsadminng/rbac/rbacrole/RbacRoleNameExtractor.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.hostsharing.hsadminng.rbac.rbacrole; - -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.stream.Collectors; - -public class RbacRoleNameExtractor { - - @NotNull - public static List roleNamesOf(@NotNull final List roles) { - return roles.stream().map(RbacRoleEntity::getRoleName).collect(Collectors.toList()); - } - -}