refactor name extractors into entity classes

This commit is contained in:
Michael Hoennig 2022-09-13 14:54:57 +02:00
parent 8041553734
commit bc27e6dc89
14 changed files with 50 additions and 85 deletions

View File

@ -59,7 +59,10 @@ public class RbacGrantEntity {
} }
public String toDisplay() { public String toDisplay() {
return "{ grant " + (assumed ? "assumed " : "") + return "{ grant role " + grantedRoleIdName +
"role " + grantedRoleIdName + " to user " + granteeUserName + " by role " + grantedByRoleIdName + " }"; " to user " + granteeUserName +
" by role " + grantedByRoleIdName +
(assumed ? " and assume" : "") +
" }";
} }
} }

View File

@ -23,8 +23,8 @@ import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.hsOfficeContact; 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.rbacgrant.RawRbacGrantEntity.grantDisplaysOf;
import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleNameExtractor.roleNamesOf; import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf;
import static net.hostsharing.test.JpaAttempt.attempt; import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;

View File

@ -26,8 +26,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantDisplayExtractor.grantDisplaysOf; import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantEntity.grantDisplaysOf;
import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleNameExtractor.roleNamesOf; import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf;
import static net.hostsharing.test.JpaAttempt.attempt; import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;

View File

@ -22,8 +22,8 @@ import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import static net.hostsharing.hsadminng.hs.office.person.TestHsOfficePerson.hsOfficePerson; 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.rbacgrant.RawRbacGrantEntity.grantDisplaysOf;
import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleNameExtractor.roleNamesOf; import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf;
import static net.hostsharing.test.JpaAttempt.attempt; import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;

View File

@ -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<String> grantDisplaysOf(final List<RawRbacGrantEntity> roles) {
return roles.stream().map(RawRbacGrantEntity::toDisplay).collect(Collectors.toList());
}
}

View File

@ -1,10 +1,13 @@
package net.hostsharing.hsadminng.rbac.rbacgrant; package net.hostsharing.hsadminng.rbac.rbacgrant;
import lombok.*; import lombok.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.annotation.Immutable; import org.springframework.data.annotation.Immutable;
import javax.persistence.*; import javax.persistence.*;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@Entity @Entity
@Table(name = "rbacgrants_ev") @Table(name = "rbacgrants_ev")
@ -52,4 +55,10 @@ public class RawRbacGrantEntity {
" }"; " }";
// @formatter:on // @formatter:on
} }
@NotNull
public static List<String> grantDisplaysOf(final List<RawRbacGrantEntity> roles) {
return roles.stream().map(RawRbacGrantEntity::toDisplay).collect(Collectors.toList());
}
} }

View File

@ -272,9 +272,9 @@ class RbacGrantControllerAcceptanceTest extends ContextBasedTest {
.body("granteeUserName", is(givenNewUser.getName())); .body("granteeUserName", is(givenNewUser.getName()));
assertThat(findAllGrantsOf(givenCurrentUserAsPackageAdmin)) assertThat(findAllGrantsOf(givenCurrentUserAsPackageAdmin))
.extracting(RbacGrantEntity::toDisplay) .extracting(RbacGrantEntity::toDisplay)
.contains("{ grant assumed role " + givenOwnPackageAdminRole.getRoleName() + .contains("{ grant role " + givenOwnPackageAdminRole.getRoleName() +
" to user " + givenNewUser.getName() + " to user " + givenNewUser.getName() +
" by role " + givenRoleToGrant + " }"); " by role " + givenRoleToGrant + " and assume }");
} }
@Test @Test
@ -323,7 +323,7 @@ class RbacGrantControllerAcceptanceTest extends ContextBasedTest {
.toUser(givenArbitraryUser)); .toUser(givenArbitraryUser));
assumeGrantExists( assumeGrantExists(
givenCurrentUserAsPackageAdmin, 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(), givenOwnPackageAdminRole.getRoleName(),
givenArbitraryUser.getName(), givenArbitraryUser.getName(),
givenCurrentUserAsPackageAdmin.assumedRole)); givenCurrentUserAsPackageAdmin.assumedRole));

View File

@ -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<String> grantDisplaysOf(final List<RbacGrantEntity> roles) {
return roles.stream().map(RbacGrantEntity::toDisplay).collect(Collectors.toList());
}
}

View File

@ -40,7 +40,7 @@ class RbacGrantEntityUnitTest {
final var display = entity.toDisplay(); final var display = entity.toDisplay();
// then // 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 @Test

View File

@ -41,6 +41,9 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired
RbacGrantRepository rbacGrantRepository; RbacGrantRepository rbacGrantRepository;
@Autowired
RawRbacGrantRepository rawRbacGrantRepository;
@Autowired @Autowired
RbacUserRepository rbacUserRepository; RbacUserRepository rbacUserRepository;
@ -68,7 +71,7 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
// then // then
exactlyTheseRbacGrantsAreReturned( exactlyTheseRbacGrantsAreReturned(
result, 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 @Test
@ -83,10 +86,10 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
// then // then
exactlyTheseRbacGrantsAreReturned( exactlyTheseRbacGrantsAreReturned(
result, result,
"{ grant assumed role test_customer#xxx.admin to user customer-admin@xxx.example.com by role global#global.admin }", "{ grant role test_customer#xxx.admin to user customer-admin@xxx.example.com by role global#global.admin and assume }",
"{ 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 }",
"{ grant assumed role test_package#xxx01.admin to user pac-admin-xxx01@xxx.example.com by role test_customer#xxx.admin }", "{ grant role test_package#xxx01.admin to user pac-admin-xxx01@xxx.example.com by role test_customer#xxx.admin and assume }",
"{ grant assumed role test_package#xxx02.admin to user pac-admin-xxx02@xxx.example.com by role test_customer#xxx.admin }"); "{ grant role test_package#xxx02.admin to user pac-admin-xxx02@xxx.example.com by role test_customer#xxx.admin and assume }");
} }
@Test @Test
@ -101,7 +104,7 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
// then // then
exactlyTheseRbacGrantsAreReturned( exactlyTheseRbacGrantsAreReturned(
result, 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()) assertThat(rbacGrantRepository.findAll())
.extracting(RbacGrantEntity::toDisplay) .extracting(RbacGrantEntity::toDisplay)
.contains( .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 @Test
@ -167,7 +170,6 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
// finally, we use the new user to make sure, no roles were granted // finally, we use the new user to make sure, no roles were granted
context(given.arbitraryUser.getName(), null); context(given.arbitraryUser.getName(), null);
assertThat(rbacGrantRepository.findAll()) assertThat(rbacGrantRepository.findAll())
.extracting(RbacGrantEntity::toDisplay)
.hasSize(0); .hasSize(0);
}); });
} }
@ -255,9 +257,9 @@ class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
); );
assumeThat(grantAttempt.caughtException()).isNull(); assumeThat(grantAttempt.caughtException()).isNull();
assumeThat(rbacGrantRepository.findAll()) assumeThat(rawRbacGrantRepository.findAll())
.extracting(RbacGrantEntity::toDisplay) .extracting(RawRbacGrantEntity::toDisplay)
.contains("{ grant assumed role %s to user %s by role %s }".formatted( .contains("{ grant role %s to user %s by role %s and assume }".formatted(
with.grantedRole, with.granteeUserName, with.assumedRole with.grantedRole, with.granteeUserName, with.assumedRole
)); ));

View File

@ -2,10 +2,13 @@ package net.hostsharing.hsadminng.rbac.rbacrole;
import lombok.*; import lombok.*;
import org.hibernate.annotations.Formula; import org.hibernate.annotations.Formula;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.annotation.Immutable; import org.springframework.data.annotation.Immutable;
import javax.persistence.*; import javax.persistence.*;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@Entity @Entity
@Table(name = "rbacrole_ev") @Table(name = "rbacrole_ev")
@ -35,4 +38,10 @@ public class RawRbacRoleEntity {
@Formula("objectTable||'#'||objectIdName||'.'||roleType") @Formula("objectTable||'#'||objectIdName||'.'||roleType")
private String roleName; private String roleName;
@NotNull
public static List<String> roleNamesOf(@NotNull final List<RawRbacRoleEntity> roles) {
return roles.stream().map(RawRbacRoleEntity::getRoleName).collect(Collectors.toList());
}
} }

View File

@ -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<String> roleNamesOf(@NotNull final List<RawRbacRoleEntity> roles) {
return roles.stream().map(RawRbacRoleEntity::getRoleName).collect(Collectors.toList());
}
}

View File

@ -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<String> roleNamesOf(@NotNull final List<RbacRoleEntity> roles) {
return roles.stream().map(RbacRoleEntity::getRoleName).collect(Collectors.toList());
}
}