diff --git a/src/main/java/de/ozgcloud/admin/RootModelAssembler.java b/src/main/java/de/ozgcloud/admin/RootModelAssembler.java index a2bfee632f155abf5c8e0b747bde49767050e8ff..479177c203e46590781de6cc4c6b824233547053 100644 --- a/src/main/java/de/ozgcloud/admin/RootModelAssembler.java +++ b/src/main/java/de/ozgcloud/admin/RootModelAssembler.java @@ -57,12 +57,13 @@ public class RootModelAssembler implements RepresentationModelAssembler<Root, En var rootLinkBuilder = WebMvcLinkBuilder.linkTo(RootController.class); links.add(rootLinkBuilder.withSelfRel()); if (currentUserService.hasRole(UserRole.ADMIN_USER)) { - links.add(buildConfigLink(rootLinkBuilder)); + links.add(buildConfigLink()); } return links; } - private Link buildConfigLink(WebMvcLinkBuilder rootLinkBuilder) { + private Link buildConfigLink() { + var rootLinkBuilder = WebMvcLinkBuilder.linkTo(RootController.class); return Link.of( rootLinkBuilder.toUriComponentsBuilder().replacePath(restProperties.getBasePath()).toUriString(), REL_CONFIGURATION diff --git a/src/main/java/de/ozgcloud/admin/common/user/CurrentUserHelper.java b/src/main/java/de/ozgcloud/admin/common/user/CurrentUserHelper.java index b41de92905942ccd5d72ed6e097ca5b1255299d9..16f4bf18d38516bbdf07cadfd58b9ce5fe793426 100644 --- a/src/main/java/de/ozgcloud/admin/common/user/CurrentUserHelper.java +++ b/src/main/java/de/ozgcloud/admin/common/user/CurrentUserHelper.java @@ -39,7 +39,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class CurrentUserHelper { +class CurrentUserHelper { static final String ROLE_PREFIX = "ROLE_"; private static final Predicate<String> IS_ROLE_PREFIX_MISSING = role -> !role.startsWith(ROLE_PREFIX); private static final AuthenticationTrustResolver TRUST_RESOLVER = new AuthenticationTrustResolverImpl(); @@ -65,7 +65,7 @@ public class CurrentUserHelper { static String addRolePrefixIfMissing(String roleToCheck) { return Optional.ofNullable(roleToCheck) .filter(IS_ROLE_PREFIX_MISSING) - .map(role -> String.format("%s%s", ROLE_PREFIX, role)) + .map(role -> ROLE_PREFIX + role) .orElse(roleToCheck); } diff --git a/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java b/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java index bd0696e7d2b94b6053cdb162c92de7cd39ad80e8..a08b15d54ddf45532608cb0fa77e7a8bc2227b40 100644 --- a/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java +++ b/src/test/java/de/ozgcloud/admin/RootModelAssemblerTest.java @@ -62,7 +62,7 @@ class RootModelAssemblerTest { } @Test - void entityRootModelHasRoot() { + void shouldHaveRoot() { var givenRoot = RootTestFactory.create(); List<Link> links = List.of(); Mockito.when(modelAssembler.buildRootModelLinks()).thenReturn(links); @@ -73,7 +73,7 @@ class RootModelAssemblerTest { } @Test - void entityRootModelHasLinks() { + void shouldHaveLinks() { List<Link> links = List.of(Link.of(RootController.PATH)); Mockito.when(modelAssembler.buildRootModelLinks()).thenReturn(links); diff --git a/src/test/java/de/ozgcloud/admin/common/user/CurrentUserHelperTest.java b/src/test/java/de/ozgcloud/admin/common/user/CurrentUserHelperTest.java index 272f51ca2db836f9e5a0d581703455a61bb762c7..ceffbadefbf8779eee19f63079566c5ac4f9b7a1 100644 --- a/src/test/java/de/ozgcloud/admin/common/user/CurrentUserHelperTest.java +++ b/src/test/java/de/ozgcloud/admin/common/user/CurrentUserHelperTest.java @@ -31,6 +31,8 @@ import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -51,7 +53,7 @@ class CurrentUserHelperTest { private final User mockPrincipal = Mockito.mock(User.class); @Test - void shouldNotHaveRoleIfNull() { + void shouldReturnFalseOnMissingAuthentication() { try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic( CurrentUserHelper.class, Mockito.CALLS_REAL_METHODS)) { @@ -64,7 +66,7 @@ class CurrentUserHelperTest { } @Test - void shouldNotHaveRoleIfPrincipalIsNull() { + void shouldReturnFalseOnMissingPrincipal() { Mockito.when(mockAuthentication.getPrincipal()).thenReturn(null); try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic( CurrentUserHelper.class, @@ -77,27 +79,9 @@ class CurrentUserHelperTest { } } - @Test - void shouldNotHaveRole() { - Mockito.when(mockAuthentication.getPrincipal()).thenReturn(mockPrincipal); - List<GrantedAuthority> authorities = List.of(); - Mockito.<Collection<? extends GrantedAuthority>>when(mockAuthentication.getAuthorities()).thenReturn(authorities); - - try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic( - CurrentUserHelper.class, - Mockito.CALLS_REAL_METHODS)) { - mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication); - mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString())) - .thenReturn(false); - - boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER); - - assertThat(hasRole).isFalse(); - } - } - - @Test - void shouldHaveRole() { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void shouldReturnValue(boolean containsRoleValue) { Mockito.when(mockAuthentication.getPrincipal()).thenReturn(mockPrincipal); List<GrantedAuthority> authorities = List.of(); Mockito.<Collection<? extends GrantedAuthority>>when(mockAuthentication.getAuthorities()).thenReturn(authorities); @@ -107,11 +91,12 @@ class CurrentUserHelperTest { Mockito.CALLS_REAL_METHODS)) { mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication); mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString())) - .thenReturn(true); + .thenReturn(containsRoleValue); boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER); - assertThat(hasRole).isTrue(); + mockUserHelper.verify(() -> CurrentUserHelper.containsRole(mockAuthentication.getAuthorities(), UserRole.ADMIN_USER)); + assertThat(hasRole).isEqualTo(containsRoleValue); } } } @@ -120,7 +105,7 @@ class CurrentUserHelperTest { @Nested class TestContainsRole { @Test - void shouldNotContainRoleIfNull() { + void shouldNotContainRoleIfAuthoritiesIsNull() { boolean containsRole = CurrentUserHelper.containsRole(null, UserRole.ADMIN_USER); assertThat(containsRole).isFalse(); @@ -147,9 +132,9 @@ class CurrentUserHelperTest { } } - @DisplayName("Prepare role for check") + @DisplayName("Add Role Prefix If Missing") @Nested - class TestPrepareRoleForCheck { + class TestAddRolePrefixIfMissing { @Test void shouldAddPrefixIfMissing() { @@ -157,12 +142,12 @@ class CurrentUserHelperTest { var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithoutPrefix); - assertThat(role).isEqualTo(String.format("%s%s", CurrentUserHelper.ROLE_PREFIX, UserRole.ADMIN_USER)); + assertThat(role).isEqualTo(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER); } @Test void shouldReturnRoleIfPrefixAlreadyExists() { - var roleWithPrefix = String.format("ROLE_%s", UserRole.ADMIN_USER); + var roleWithPrefix = CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER; var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithPrefix); @@ -184,7 +169,7 @@ class CurrentUserHelperTest { private final SecurityContext mockSecurityContext = Mockito.mock(SecurityContext.class); @Test - void shouldThrowIfNull() { + void shouldThrowIfNoAuthenticatedUser() { Mockito.when(mockSecurityContext.getAuthentication()).thenReturn(null); try (MockedStatic<SecurityContextHolder> contextHolder = Mockito.mockStatic(SecurityContextHolder.class)) { diff --git a/src/test/java/de/ozgcloud/admin/common/user/CurrentUserServiceTest.java b/src/test/java/de/ozgcloud/admin/common/user/CurrentUserServiceTest.java index 4e2f38123a3f02741cf940974756366a86b7ab8b..20e75bfc94b321b65e0884ecad643ed28192af33 100644 --- a/src/test/java/de/ozgcloud/admin/common/user/CurrentUserServiceTest.java +++ b/src/test/java/de/ozgcloud/admin/common/user/CurrentUserServiceTest.java @@ -25,43 +25,32 @@ package de.ozgcloud.admin.common.user; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.MockedStatic; import org.mockito.Mockito; import static org.assertj.core.api.Assertions.assertThat; -public class CurrentUserServiceTest { +class CurrentUserServiceTest { private final CurrentUserService currentUserService = new CurrentUserService(); @DisplayName("Has role") @Nested class TestHasRole { - @Test - void shouldNotHaveRole() { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void shouldReturnValue(boolean hasRoleValue) { try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic( CurrentUserHelper.class) ){ mockUserHelper.when(() -> CurrentUserHelper.hasRole(Mockito.anyString())) - .thenReturn(false); + .thenReturn(hasRoleValue); boolean hasRole = currentUserService.hasRole(UserRole.ADMIN_USER); - assertThat(hasRole).isFalse(); - } - } - - @Test - void shouldHaveRole() { - try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic( - CurrentUserHelper.class) - ){ - mockUserHelper.when(() -> CurrentUserHelper.hasRole(Mockito.anyString())) - .thenReturn(true); - - boolean hasRole = currentUserService.hasRole(UserRole.ADMIN_USER); - - assertThat(hasRole).isTrue(); + mockUserHelper.verify(() -> CurrentUserHelper.hasRole(UserRole.ADMIN_USER)); + assertThat(hasRole).isEqualTo(hasRoleValue); } } }