From df9b90714f0a96fc046d8b8dfa278328767b726c Mon Sep 17 00:00:00 2001 From: macacia Date: Thu, 24 Oct 2024 14:46:16 +0200 Subject: [PATCH 1/9] P4ADEV-1303 modified UserInfo mapper --- .../user/IamUserInfoDTO2UserInfoMapper.java | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index b29c05ff..43c87f2d 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -1,17 +1,21 @@ package it.gov.pagopa.payhub.auth.service.user; import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO; +import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO; import it.gov.pagopa.payhub.auth.exception.custom.UserNotFoundException; import it.gov.pagopa.payhub.auth.model.Operator; import it.gov.pagopa.payhub.auth.model.User; import it.gov.pagopa.payhub.auth.repository.OperatorsRepository; import it.gov.pagopa.payhub.auth.repository.UsersRepository; +import it.gov.pagopa.payhub.auth.utils.Constants; import it.gov.pagopa.payhub.model.generated.UserInfo; import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles; import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Function; @Service @@ -27,27 +31,36 @@ public IamUserInfoDTO2UserInfoMapper(UsersRepository usersRepository, OperatorsR @Override public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) { - User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId())); - List userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId()); UserInfo userInfo = UserInfo.builder() - .userId(user.getUserId()) - .mappedExternalUserId(user.getMappedExternalUserId()) .fiscalCode(iamUserInfoDTO.getFiscalCode()) .familyName(iamUserInfoDTO.getFamilyName()) .name(iamUserInfoDTO.getName()) .issuer(iamUserInfoDTO.getIssuer()) - .organizations(userRoles.stream() - .map(r -> UserOrganizationRoles.builder() - .operatorId(r.getOperatorId()) - .organizationIpaCode(r.getOrganizationIpaCode()) - .roles(new ArrayList<>(r.getRoles())) - .email(r.getEmail()) - .build()) - .toList()) .build(); - - if(iamUserInfoDTO.getOrganizationAccess()!=null){ - userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()); + if (iamUserInfoDTO.isSystemUser()) { + userInfo.setUserId(iamUserInfoDTO.getUserId()); + userInfo.setMappedExternalUserId(iamUserInfoDTO.getFiscalCode()); + UserOrganizationRoles userOrgRoles = UserOrganizationRoles.builder() + .organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()) + .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .build(); + userInfo.setOrganizations(Collections.singletonList(userOrgRoles)); + } else { + User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId())); + List userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId()); + userInfo.setUserId(user.getUserId()); + userInfo.setMappedExternalUserId(user.getMappedExternalUserId()); + userInfo.setOrganizations(userRoles.stream() + .map(r -> UserOrganizationRoles.builder() + .operatorId(r.getOperatorId()) + .organizationIpaCode(r.getOrganizationIpaCode()) + .roles(new ArrayList<>(r.getRoles())) + .email(r.getEmail()) + .build()) + .toList()); + Optional.ofNullable(iamUserInfoDTO.getOrganizationAccess()) + .map(IamUserOrganizationRolesDTO::getOrganizationIpaCode) + .ifPresent(userInfo::setOrganizationAccess); } return userInfo; } From 231d5c34b52389631073c01e4b058edd7254dd85 Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 11:01:00 +0200 Subject: [PATCH 2/9] P4ADEV-1303 fixed splittin string --- .../a2a/AuthorizeClientCredentialsRequestService.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java index e40d6537..0ecae810 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java @@ -4,13 +4,15 @@ import it.gov.pagopa.payhub.auth.mapper.ClientMapper; import it.gov.pagopa.payhub.model.generated.ClientDTO; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service @Slf4j public class AuthorizeClientCredentialsRequestService { - private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria_"; + private static final String PIATTAFORMA_UNITARIA = "piattaforma-unitaria"; + private static final String SEPARATOR = "_"; private final ClientService clientService; private final ClientMapper clientMapper; private final String piattaformaUnitariaClientSecret; @@ -25,7 +27,7 @@ public AuthorizeClientCredentialsRequestService( } public ClientDTO authorizeCredentials(String clientId, String clientSecret) { - if (clientId.startsWith(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX)) { + if (clientId.startsWith(PIATTAFORMA_UNITARIA + SEPARATOR)) { return authorizePiattaformaUnitariaCredentials(clientId, clientSecret); } return authorizeSilCredentials(clientId, clientSecret); @@ -41,11 +43,10 @@ private ClientDTO authorizeSilCredentials(String clientId, String clientSecret) private ClientDTO authorizePiattaformaUnitariaCredentials(String clientId, String clientSecret) { if (!clientSecret.equals(piattaformaUnitariaClientSecret)) throw new ClientUnauthorizedException("Unauthorized client for piattaforma-unitaria client-credentials"); - String[] splittedClientId = clientId.split("_"); return ClientDTO.builder() .clientId(clientId) - .clientName(splittedClientId[0]) - .organizationIpaCode(splittedClientId[1]) + .clientName(PIATTAFORMA_UNITARIA) + .organizationIpaCode(StringUtils.substringAfter(clientId, PIATTAFORMA_UNITARIA + SEPARATOR)) .clientSecret(clientSecret) .build(); } From 4ae963bd5b0954752ef542b64fc674f3cf144103 Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 11:58:58 +0200 Subject: [PATCH 3/9] P4ADEV-1303 resolved change request --- .../user/IamUserInfoDTO2UserInfoMapper.java | 71 ++++++++++------- ...zeClientCredentialsRequestServiceTest.java | 9 ++- .../IamUserInfoDTO2UserInfoMapperTest.java | 79 +++++++++++++------ 3 files changed, 104 insertions(+), 55 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index 43c87f2d..1865f2ac 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -31,36 +31,49 @@ public IamUserInfoDTO2UserInfoMapper(UsersRepository usersRepository, OperatorsR @Override public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) { - UserInfo userInfo = UserInfo.builder() - .fiscalCode(iamUserInfoDTO.getFiscalCode()) - .familyName(iamUserInfoDTO.getFamilyName()) - .name(iamUserInfoDTO.getName()) - .issuer(iamUserInfoDTO.getIssuer()) - .build(); if (iamUserInfoDTO.isSystemUser()) { - userInfo.setUserId(iamUserInfoDTO.getUserId()); - userInfo.setMappedExternalUserId(iamUserInfoDTO.getFiscalCode()); - UserOrganizationRoles userOrgRoles = UserOrganizationRoles.builder() - .organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()) - .roles(Collections.singletonList(Constants.ROLE_ADMIN)) - .build(); - userInfo.setOrganizations(Collections.singletonList(userOrgRoles)); - } else { - User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId())); - List userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId()); - userInfo.setUserId(user.getUserId()); - userInfo.setMappedExternalUserId(user.getMappedExternalUserId()); - userInfo.setOrganizations(userRoles.stream() - .map(r -> UserOrganizationRoles.builder() - .operatorId(r.getOperatorId()) - .organizationIpaCode(r.getOrganizationIpaCode()) - .roles(new ArrayList<>(r.getRoles())) - .email(r.getEmail()) - .build()) - .toList()); - Optional.ofNullable(iamUserInfoDTO.getOrganizationAccess()) - .map(IamUserOrganizationRolesDTO::getOrganizationIpaCode) - .ifPresent(userInfo::setOrganizationAccess); + return systemUserMapper(iamUserInfoDTO); + } + return userInfoMapper(iamUserInfoDTO); + } + + private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) { + return UserInfo.builder() + .userId(iamUserInfoDTO.getUserId()) + .mappedExternalUserId(iamUserInfoDTO.getFiscalCode()) + .fiscalCode(iamUserInfoDTO.getFiscalCode()) + .familyName(iamUserInfoDTO.getFamilyName()) + .name(iamUserInfoDTO.getName()) + .issuer(iamUserInfoDTO.getIssuer()) + .organizations(Collections.singletonList(UserOrganizationRoles.builder() + .organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()) + .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .build())) + .build(); + } + + private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO) { + User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId())); + List userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId()); + UserInfo userInfo = UserInfo.builder() + .userId(user.getUserId()) + .mappedExternalUserId(user.getMappedExternalUserId()) + .fiscalCode(iamUserInfoDTO.getFiscalCode()) + .familyName(iamUserInfoDTO.getFamilyName()) + .name(iamUserInfoDTO.getName()) + .issuer(iamUserInfoDTO.getIssuer()) + .organizations(userRoles.stream() + .map(r -> UserOrganizationRoles.builder() + .operatorId(r.getOperatorId()) + .organizationIpaCode(r.getOrganizationIpaCode()) + .roles(new ArrayList<>(r.getRoles())) + .email(r.getEmail()) + .build()) + .toList()) + .build(); + + if(iamUserInfoDTO.getOrganizationAccess() != null){ + userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()); } return userInfo; } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java index 937ac58f..439b9223 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java @@ -4,6 +4,7 @@ import it.gov.pagopa.payhub.auth.mapper.ClientMapper; import it.gov.pagopa.payhub.auth.model.Client; import it.gov.pagopa.payhub.model.generated.ClientDTO; +import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -93,16 +94,16 @@ void givenSystemUserWhenMatcherThenAssertionOk() { // Given String clientId = "piattaforma-unitaria_IPA_TEST"; String clientSecret = "SECRET"; - String[] splitted = clientId.split("_"); - + String piattaformaUnitaria = "piattaforma-unitaria"; + String separator = "_"; // When ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecret); // Then Assertions.assertEquals( ClientDTO.builder() .clientId(clientId) - .clientName(splitted[0]) - .organizationIpaCode(splitted[1]) + .clientName(piattaformaUnitaria) + .organizationIpaCode(StringUtils.substringAfter(clientId, piattaformaUnitaria + separator)) .clientSecret(clientSecret) .build(), actualClientDTO); } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java index c5c555e9..e8764dc7 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java @@ -7,6 +7,7 @@ import it.gov.pagopa.payhub.auth.model.User; import it.gov.pagopa.payhub.auth.repository.OperatorsRepository; import it.gov.pagopa.payhub.auth.repository.UsersRepository; +import it.gov.pagopa.payhub.auth.utils.Constants; import it.gov.pagopa.payhub.model.generated.UserInfo; import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles; import org.junit.jupiter.api.AfterEach; @@ -136,33 +137,67 @@ void givenNoOrganizationAccessWhenApplyThenOk(){ testApplyOk(iamUserInfo, user, organizationRoles); } + @Test + void givenSystemUserWhenApplyThenOk(){ + // Given + IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder() + .systemUser(Boolean.TRUE) + .userId("EXTERNALUSERID") + .innerUserId("INNERUSERID") + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .organizationAccess(IamUserOrganizationRolesDTO.builder() + .organizationIpaCode("IPA_CODE") + .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .build()) + .build(); + + testApplyOk(iamUserInfo, null, null); + } + private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List organizationRoles) { - Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); - Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); + UserInfo userInfo; + if (iamUserInfo.isSystemUser()) { + userInfo = UserInfo.builder() + .userId(iamUserInfo.getUserId()) + .mappedExternalUserId(iamUserInfo.getFiscalCode()) + .fiscalCode(iamUserInfo.getFiscalCode()) + .familyName(iamUserInfo.getFamilyName()) + .name(iamUserInfo.getName()) + .issuer(iamUserInfo.getIssuer()) + .organizations(Collections.singletonList(UserOrganizationRoles.builder() + .organizationIpaCode(iamUserInfo.getOrganizationAccess().getOrganizationIpaCode()) + .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .build())) + .build(); + } else { + Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); + Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); + userInfo = UserInfo.builder() + .userId(user.getUserId()) + .mappedExternalUserId(user.getMappedExternalUserId()) + .fiscalCode(iamUserInfo.getFiscalCode()) + .familyName(iamUserInfo.getFamilyName()) + .name(iamUserInfo.getName()) + .issuer(iamUserInfo.getIssuer()) + .organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null) + .organizations(organizationRoles.stream() + .map(r -> UserOrganizationRoles.builder() + .operatorId(r.getOperatorId()) + .organizationIpaCode(r.getOrganizationIpaCode()) + .roles(new ArrayList<>(r.getRoles())) + .email(r.getEmail()) + .build()) + .toList()) + .build(); + } // When UserInfo result = mapper.apply(iamUserInfo); // Then - Assertions.assertEquals( - UserInfo.builder() - .userId(user.getUserId()) - .mappedExternalUserId(user.getMappedExternalUserId()) - .fiscalCode(iamUserInfo.getFiscalCode()) - .familyName(iamUserInfo.getFamilyName()) - .name(iamUserInfo.getName()) - .issuer(iamUserInfo.getIssuer()) - .organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null) - .organizations(organizationRoles.stream() - .map(r -> UserOrganizationRoles.builder() - .operatorId(r.getOperatorId()) - .organizationIpaCode(r.getOrganizationIpaCode()) - .roles(new ArrayList<>(r.getRoles())) - .email(r.getEmail()) - .build()) - .toList()) - .build(), - result - ); + Assertions.assertEquals(userInfo, result); } } From c68c2e22926568c2b8b226e01cf0b6d3cc9b61ca Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 12:04:31 +0200 Subject: [PATCH 4/9] P4ADEV-1303 resolved sonarcloud issue --- .../payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index 1865f2ac..ec816cd3 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -1,7 +1,6 @@ package it.gov.pagopa.payhub.auth.service.user; import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO; -import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO; import it.gov.pagopa.payhub.auth.exception.custom.UserNotFoundException; import it.gov.pagopa.payhub.auth.model.Operator; import it.gov.pagopa.payhub.auth.model.User; @@ -15,7 +14,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Optional; import java.util.function.Function; @Service From 0e5c232ad9174627f54058dd389e78a9214e02c7 Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 15:01:36 +0200 Subject: [PATCH 5/9] P4ADEV-1303 resolved change request --- .../a2a/AuthorizeClientCredentialsRequestService.java | 6 +++--- .../auth/service/user/IamUserInfoDTO2UserInfoMapper.java | 6 ++++-- .../a2a/AuthorizeClientCredentialsRequestServiceTest.java | 3 +-- .../service/user/IamUserInfoDTO2UserInfoMapperTest.java | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java index 0ecae810..956157fe 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java @@ -4,7 +4,6 @@ import it.gov.pagopa.payhub.auth.mapper.ClientMapper; import it.gov.pagopa.payhub.model.generated.ClientDTO; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -13,6 +12,7 @@ public class AuthorizeClientCredentialsRequestService { private static final String PIATTAFORMA_UNITARIA = "piattaforma-unitaria"; private static final String SEPARATOR = "_"; + private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = PIATTAFORMA_UNITARIA + SEPARATOR; private final ClientService clientService; private final ClientMapper clientMapper; private final String piattaformaUnitariaClientSecret; @@ -27,7 +27,7 @@ public AuthorizeClientCredentialsRequestService( } public ClientDTO authorizeCredentials(String clientId, String clientSecret) { - if (clientId.startsWith(PIATTAFORMA_UNITARIA + SEPARATOR)) { + if (clientId.startsWith(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX)) { return authorizePiattaformaUnitariaCredentials(clientId, clientSecret); } return authorizeSilCredentials(clientId, clientSecret); @@ -46,7 +46,7 @@ private ClientDTO authorizePiattaformaUnitariaCredentials(String clientId, Strin return ClientDTO.builder() .clientId(clientId) .clientName(PIATTAFORMA_UNITARIA) - .organizationIpaCode(StringUtils.substringAfter(clientId, PIATTAFORMA_UNITARIA + SEPARATOR)) + .organizationIpaCode(clientId.substring(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX.length())) .clientSecret(clientSecret) .build(); } diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index ec816cd3..b3948a17 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -18,7 +18,9 @@ @Service public class IamUserInfoDTO2UserInfoMapper implements Function { - + private static final String WS_USER = "WS_USER"; + private static final String SEPARATOR = "-"; + private static final String WS_USER_SUFFIX = SEPARATOR + WS_USER; private final UsersRepository usersRepository; private final OperatorsRepository operatorsRepository; @@ -38,7 +40,7 @@ public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) { private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) { return UserInfo.builder() .userId(iamUserInfoDTO.getUserId()) - .mappedExternalUserId(iamUserInfoDTO.getFiscalCode()) + .mappedExternalUserId(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode() + WS_USER_SUFFIX) .fiscalCode(iamUserInfoDTO.getFiscalCode()) .familyName(iamUserInfoDTO.getFamilyName()) .name(iamUserInfoDTO.getName()) diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java index 439b9223..d0c36b72 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java @@ -4,7 +4,6 @@ import it.gov.pagopa.payhub.auth.mapper.ClientMapper; import it.gov.pagopa.payhub.auth.model.Client; import it.gov.pagopa.payhub.model.generated.ClientDTO; -import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -103,7 +102,7 @@ void givenSystemUserWhenMatcherThenAssertionOk() { ClientDTO.builder() .clientId(clientId) .clientName(piattaformaUnitaria) - .organizationIpaCode(StringUtils.substringAfter(clientId, piattaformaUnitaria + separator)) + .organizationIpaCode(clientId.substring((piattaformaUnitaria + separator).length())) .clientSecret(clientSecret) .build(), actualClientDTO); } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java index e8764dc7..32823052 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java @@ -163,7 +163,7 @@ private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List o if (iamUserInfo.isSystemUser()) { userInfo = UserInfo.builder() .userId(iamUserInfo.getUserId()) - .mappedExternalUserId(iamUserInfo.getFiscalCode()) + .mappedExternalUserId(iamUserInfo.getOrganizationAccess().getOrganizationIpaCode() + "-WS_USER") .fiscalCode(iamUserInfo.getFiscalCode()) .familyName(iamUserInfo.getFamilyName()) .name(iamUserInfo.getName()) From dcb3cdaf17ee0bc87b0d90d66da7e232c0683bdf Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 15:50:38 +0200 Subject: [PATCH 6/9] P4ADEV-1303 resolved change request --- ...horizeClientCredentialsRequestService.java | 6 ++-- .../user/IamUserInfoDTO2UserInfoMapper.java | 4 +-- .../IamUserInfoDTO2UserInfoMapperTest.java | 36 ++++++++++--------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java index 956157fe..7fb80064 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java @@ -10,9 +10,7 @@ @Service @Slf4j public class AuthorizeClientCredentialsRequestService { - private static final String PIATTAFORMA_UNITARIA = "piattaforma-unitaria"; - private static final String SEPARATOR = "_"; - private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = PIATTAFORMA_UNITARIA + SEPARATOR; + private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria"; private final ClientService clientService; private final ClientMapper clientMapper; private final String piattaformaUnitariaClientSecret; @@ -45,7 +43,7 @@ private ClientDTO authorizePiattaformaUnitariaCredentials(String clientId, Strin throw new ClientUnauthorizedException("Unauthorized client for piattaforma-unitaria client-credentials"); return ClientDTO.builder() .clientId(clientId) - .clientName(PIATTAFORMA_UNITARIA) + .clientName(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX) .organizationIpaCode(clientId.substring(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX.length())) .clientSecret(clientSecret) .build(); diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index b3948a17..6b8d62cb 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -18,9 +18,7 @@ @Service public class IamUserInfoDTO2UserInfoMapper implements Function { - private static final String WS_USER = "WS_USER"; - private static final String SEPARATOR = "-"; - private static final String WS_USER_SUFFIX = SEPARATOR + WS_USER; + private static final String WS_USER_SUFFIX = "-WS_USER"; private final UsersRepository usersRepository; private final OperatorsRepository operatorsRepository; diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java index 32823052..462868e4 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java @@ -83,7 +83,7 @@ void givenCompleteDataWhenApplyThenOk(){ .roles(Set.of("ROLE")) .build()); - testApplyOk(iamUserInfo, user, organizationRoles); + testApplyOk(iamUserInfo, user, organizationRoles, null); } @Test @@ -107,7 +107,7 @@ void givenNotOperatorsWhenApplyThenOk(){ .mappedExternalUserId("MAPPEDEXTERNALUSERID") .build(); - testApplyOk(iamUserInfo, user, Collections.emptyList()); + testApplyOk(iamUserInfo, user, Collections.emptyList(), null); } @Test @@ -134,7 +134,7 @@ void givenNoOrganizationAccessWhenApplyThenOk(){ .email("EMAIL") .build()); - testApplyOk(iamUserInfo, user, organizationRoles); + testApplyOk(iamUserInfo, user, organizationRoles, null); } @Test @@ -154,25 +154,27 @@ void givenSystemUserWhenApplyThenOk(){ .build()) .build(); - testApplyOk(iamUserInfo, null, null); + UserInfo expected = UserInfo.builder() + .userId("EXTERNALUSERID") + .mappedExternalUserId("IPA_CODE-WS_USER") + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("IPA_CODE") + .organizations(Collections.singletonList(UserOrganizationRoles.builder() + .organizationIpaCode("IPA_CODE") + .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .build())) + .build(); + + testApplyOk(iamUserInfo, null, null, expected); } - private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List organizationRoles) { + private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List organizationRoles, UserInfo expected) { UserInfo userInfo; if (iamUserInfo.isSystemUser()) { - userInfo = UserInfo.builder() - .userId(iamUserInfo.getUserId()) - .mappedExternalUserId(iamUserInfo.getOrganizationAccess().getOrganizationIpaCode() + "-WS_USER") - .fiscalCode(iamUserInfo.getFiscalCode()) - .familyName(iamUserInfo.getFamilyName()) - .name(iamUserInfo.getName()) - .issuer(iamUserInfo.getIssuer()) - .organizations(Collections.singletonList(UserOrganizationRoles.builder() - .organizationIpaCode(iamUserInfo.getOrganizationAccess().getOrganizationIpaCode()) - .roles(Collections.singletonList(Constants.ROLE_ADMIN)) - .build())) - .build(); + userInfo = expected; } else { Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); From 0df19a94f7025b03bd6d471c51ab0e619a590ca4 Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 15:51:12 +0200 Subject: [PATCH 7/9] P4ADEV-1303 resolved change request --- .../service/a2a/AuthorizeClientCredentialsRequestService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java index 7fb80064..0b0767cb 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java @@ -10,7 +10,7 @@ @Service @Slf4j public class AuthorizeClientCredentialsRequestService { - private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria"; + private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria_"; private final ClientService clientService; private final ClientMapper clientMapper; private final String piattaformaUnitariaClientSecret; From e23afd5a89ede725801621a7f5c2643e2fbd544c Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 16:43:34 +0200 Subject: [PATCH 8/9] P4ADEV-1303 resolved change request --- ...horizeClientCredentialsRequestService.java | 2 + .../user/IamUserInfoDTO2UserInfoMapper.java | 27 ++++---- .../IamUserInfoDTO2UserInfoMapperTest.java | 65 +++++++++++-------- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java index 0b0767cb..4b88005a 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestService.java @@ -10,7 +10,9 @@ @Service @Slf4j public class AuthorizeClientCredentialsRequestService { + private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria_"; + private final ClientService clientService; private final ClientMapper clientMapper; private final String piattaformaUnitariaClientSecret; diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index 6b8d62cb..0f37978e 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -18,7 +18,9 @@ @Service public class IamUserInfoDTO2UserInfoMapper implements Function { + private static final String WS_USER_SUFFIX = "-WS_USER"; + private final UsersRepository usersRepository; private final OperatorsRepository operatorsRepository; @@ -36,18 +38,19 @@ public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) { } private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) { - return UserInfo.builder() - .userId(iamUserInfoDTO.getUserId()) - .mappedExternalUserId(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode() + WS_USER_SUFFIX) - .fiscalCode(iamUserInfoDTO.getFiscalCode()) - .familyName(iamUserInfoDTO.getFamilyName()) - .name(iamUserInfoDTO.getName()) - .issuer(iamUserInfoDTO.getIssuer()) - .organizations(Collections.singletonList(UserOrganizationRoles.builder() - .organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()) - .roles(Collections.singletonList(Constants.ROLE_ADMIN)) - .build())) - .build(); + String organizationIpaCode = iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode(); + return UserInfo.builder() + .userId(iamUserInfoDTO.getUserId()) + .mappedExternalUserId(organizationIpaCode + WS_USER_SUFFIX) + .fiscalCode(iamUserInfoDTO.getFiscalCode()) + .familyName(iamUserInfoDTO.getFamilyName()) + .name(iamUserInfoDTO.getName()) + .issuer(iamUserInfoDTO.getIssuer()) + .organizations(Collections.singletonList(UserOrganizationRoles.builder() + .organizationIpaCode(organizationIpaCode) + .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .build())) + .build(); } private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO) { diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java index 462868e4..dbebfdf2 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java @@ -83,7 +83,15 @@ void givenCompleteDataWhenApplyThenOk(){ .roles(Set.of("ROLE")) .build()); - testApplyOk(iamUserInfo, user, organizationRoles, null); + UserInfo expected = UserInfo.builder() + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .organizationAccess("ORG") + .build(); + + testApplyOk(iamUserInfo, user, organizationRoles, expected); } @Test @@ -107,7 +115,15 @@ void givenNotOperatorsWhenApplyThenOk(){ .mappedExternalUserId("MAPPEDEXTERNALUSERID") .build(); - testApplyOk(iamUserInfo, user, Collections.emptyList(), null); + UserInfo expected = UserInfo.builder() + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .organizationAccess("ORG") + .build(); + + testApplyOk(iamUserInfo, user, Collections.emptyList(), expected); } @Test @@ -133,8 +149,15 @@ void givenNoOrganizationAccessWhenApplyThenOk(){ .roles(Set.of("ROLE")) .email("EMAIL") .build()); + + UserInfo expected = UserInfo.builder() + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .build(); - testApplyOk(iamUserInfo, user, organizationRoles, null); + testApplyOk(iamUserInfo, user, organizationRoles, expected); } @Test @@ -147,7 +170,7 @@ void givenSystemUserWhenApplyThenOk(){ .fiscalCode("FISCALCODE") .familyName("FAMILYNAME") .name("NAME") - .issuer("ISSUER") + .issuer("IPA_CODE") .organizationAccess(IamUserOrganizationRolesDTO.builder() .organizationIpaCode("IPA_CODE") .roles(Collections.singletonList(Constants.ROLE_ADMIN)) @@ -172,34 +195,24 @@ void givenSystemUserWhenApplyThenOk(){ private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List organizationRoles, UserInfo expected) { - UserInfo userInfo; - if (iamUserInfo.isSystemUser()) { - userInfo = expected; - } else { + if (!iamUserInfo.isSystemUser()) { Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); - userInfo = UserInfo.builder() - .userId(user.getUserId()) - .mappedExternalUserId(user.getMappedExternalUserId()) - .fiscalCode(iamUserInfo.getFiscalCode()) - .familyName(iamUserInfo.getFamilyName()) - .name(iamUserInfo.getName()) - .issuer(iamUserInfo.getIssuer()) - .organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null) - .organizations(organizationRoles.stream() - .map(r -> UserOrganizationRoles.builder() - .operatorId(r.getOperatorId()) - .organizationIpaCode(r.getOrganizationIpaCode()) - .roles(new ArrayList<>(r.getRoles())) - .email(r.getEmail()) - .build()) - .toList()) - .build(); + expected.setUserId(user.getUserId()); + expected.setMappedExternalUserId(user.getMappedExternalUserId()); + expected.setOrganizations(organizationRoles.stream() + .map(r -> UserOrganizationRoles.builder() + .operatorId(r.getOperatorId()) + .organizationIpaCode(r.getOrganizationIpaCode()) + .roles(new ArrayList<>(r.getRoles())) + .email(r.getEmail()) + .build()) + .toList()); } // When UserInfo result = mapper.apply(iamUserInfo); // Then - Assertions.assertEquals(userInfo, result); + Assertions.assertEquals(expected, result); } } From fddd94e36bb4b0130fd1f3251de6786552beffa0 Mon Sep 17 00:00:00 2001 From: macacia Date: Fri, 25 Oct 2024 17:20:00 +0200 Subject: [PATCH 9/9] P4ADEV-1303 resolved change request --- ...zeClientCredentialsRequestServiceTest.java | 5 +- .../IamUserInfoDTO2UserInfoMapperTest.java | 171 ++++++++++-------- 2 files changed, 99 insertions(+), 77 deletions(-) diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java index d0c36b72..c557185e 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/a2a/AuthorizeClientCredentialsRequestServiceTest.java @@ -93,8 +93,7 @@ void givenSystemUserWhenMatcherThenAssertionOk() { // Given String clientId = "piattaforma-unitaria_IPA_TEST"; String clientSecret = "SECRET"; - String piattaformaUnitaria = "piattaforma-unitaria"; - String separator = "_"; + String piattaformaUnitaria = "piattaforma-unitaria_"; // When ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecret); // Then @@ -102,7 +101,7 @@ void givenSystemUserWhenMatcherThenAssertionOk() { ClientDTO.builder() .clientId(clientId) .clientName(piattaformaUnitaria) - .organizationIpaCode(clientId.substring((piattaformaUnitaria + separator).length())) + .organizationIpaCode(clientId.substring((piattaformaUnitaria).length())) .clientSecret(clientSecret) .build(), actualClientDTO); } diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java index dbebfdf2..1ab2b3d2 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java @@ -46,9 +46,9 @@ void verifyNotMoreInteractions() { void givenNotUserWhenApplyThenUserNotFoundException(){ // Given IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder() - .userId("EXTERNALUSERID") - .innerUserId("INNERUSERID") - .build(); + .userId("EXTERNALUSERID") + .innerUserId("INNERUSERID") + .build(); Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.empty()); @@ -60,104 +60,145 @@ void givenNotUserWhenApplyThenUserNotFoundException(){ void givenCompleteDataWhenApplyThenOk(){ // Given IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder() - .userId("EXTERNALUSERID") - .innerUserId("INNERUSERID") - .fiscalCode("FISCALCODE") - .familyName("FAMILYNAME") - .name("NAME") - .issuer("ISSUER") - .organizationAccess(IamUserOrganizationRolesDTO.builder() - .organizationIpaCode("ORG") - .email("EMAIL") - .build()) - .build(); + .userId("EXTERNALUSERID") + .innerUserId("INNERUSERID") + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .organizationAccess(IamUserOrganizationRolesDTO.builder() + .organizationIpaCode("ORG") + .email("EMAIL") + .build()) + .build(); User user = User.builder() - .userId(iamUserInfo.getInnerUserId()) - .mappedExternalUserId("MAPPEDEXTERNALUSERID") - .build(); + .userId(iamUserInfo.getInnerUserId()) + .mappedExternalUserId("MAPPEDEXTERNALUSERID") + .build(); List organizationRoles = List.of(Operator.builder() - .operatorId("OPERATORID") - .organizationIpaCode("ORG") - .roles(Set.of("ROLE")) - .build()); + .operatorId("OPERATORID") + .organizationIpaCode("ORG") + .roles(Set.of("ROLE")) + .email("EMAIL") + .build()); UserInfo expected = UserInfo.builder() + .userId("INNERUSERID") + .mappedExternalUserId("MAPPEDEXTERNALUSERID") .fiscalCode("FISCALCODE") .familyName("FAMILYNAME") .name("NAME") .issuer("ISSUER") .organizationAccess("ORG") + .organizations(List.of(UserOrganizationRoles.builder() + .operatorId("OPERATORID") + .organizationIpaCode("ORG") + .roles(List.of("ROLE")) + .email("EMAIL") + .build())) .build(); - testApplyOk(iamUserInfo, user, organizationRoles, expected); + Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); + Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); + + // When + UserInfo result = mapper.apply(iamUserInfo); + + // Then + Assertions.assertEquals(expected, result); } @Test void givenNotOperatorsWhenApplyThenOk(){ // Given IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder() - .userId("EXTERNALUSERID") - .innerUserId("INNERUSERID") - .fiscalCode("FISCALCODE") - .familyName("FAMILYNAME") - .name("NAME") - .issuer("ISSUER") - .organizationAccess(IamUserOrganizationRolesDTO.builder() - .organizationIpaCode("ORG") - .email("EMAIL") - .build()) - .build(); + .userId("EXTERNALUSERID") + .innerUserId("INNERUSERID") + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .organizationAccess(IamUserOrganizationRolesDTO.builder() + .organizationIpaCode("ORG") + .email("EMAIL") + .build()) + .build(); User user = User.builder() - .userId(iamUserInfo.getInnerUserId()) - .mappedExternalUserId("MAPPEDEXTERNALUSERID") - .build(); + .userId(iamUserInfo.getInnerUserId()) + .mappedExternalUserId("MAPPEDEXTERNALUSERID") + .build(); UserInfo expected = UserInfo.builder() + .userId("INNERUSERID") + .mappedExternalUserId("MAPPEDEXTERNALUSERID") .fiscalCode("FISCALCODE") .familyName("FAMILYNAME") .name("NAME") .issuer("ISSUER") .organizationAccess("ORG") + .organizations(Collections.emptyList()) .build(); - testApplyOk(iamUserInfo, user, Collections.emptyList(), expected); + Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); + Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(Collections.emptyList()); + + // When + UserInfo result = mapper.apply(iamUserInfo); + + // Then + Assertions.assertEquals(expected, result); } @Test void givenNoOrganizationAccessWhenApplyThenOk(){ // Given IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder() - .userId("EXTERNALUSERID") - .innerUserId("INNERUSERID") - .fiscalCode("FISCALCODE") - .familyName("FAMILYNAME") - .name("NAME") - .issuer("ISSUER") - .build(); + .userId("EXTERNALUSERID") + .innerUserId("INNERUSERID") + .fiscalCode("FISCALCODE") + .familyName("FAMILYNAME") + .name("NAME") + .issuer("ISSUER") + .build(); User user = User.builder() - .userId(iamUserInfo.getInnerUserId()) - .mappedExternalUserId("MAPPEDEXTERNALUSERID") - .build(); + .userId(iamUserInfo.getInnerUserId()) + .mappedExternalUserId("MAPPEDEXTERNALUSERID") + .build(); List organizationRoles = List.of(Operator.builder() - .operatorId("OPERATORID") - .organizationIpaCode("ORG") - .roles(Set.of("ROLE")) - .email("EMAIL") - .build()); - + .operatorId("OPERATORID") + .organizationIpaCode("ORG") + .roles(Set.of("ROLE")) + .email("EMAIL") + .build()); + UserInfo expected = UserInfo.builder() + .userId("INNERUSERID") + .mappedExternalUserId("MAPPEDEXTERNALUSERID") .fiscalCode("FISCALCODE") .familyName("FAMILYNAME") .name("NAME") .issuer("ISSUER") + .organizations(List.of(UserOrganizationRoles.builder() + .operatorId("OPERATORID") + .organizationIpaCode("ORG") + .roles(List.of("ROLE")) + .email("EMAIL") + .build())) .build(); - testApplyOk(iamUserInfo, user, organizationRoles, expected); + Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); + Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); + + // When + UserInfo result = mapper.apply(iamUserInfo); + + // Then + Assertions.assertEquals(expected, result); } @Test @@ -186,33 +227,15 @@ void givenSystemUserWhenApplyThenOk(){ .issuer("IPA_CODE") .organizations(Collections.singletonList(UserOrganizationRoles.builder() .organizationIpaCode("IPA_CODE") - .roles(Collections.singletonList(Constants.ROLE_ADMIN)) + .roles(List.of(Constants.ROLE_ADMIN)) .build())) .build(); - testApplyOk(iamUserInfo, null, null, expected); - } - - private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List organizationRoles, UserInfo expected) { - - if (!iamUserInfo.isSystemUser()) { - Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user)); - Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles); - expected.setUserId(user.getUserId()); - expected.setMappedExternalUserId(user.getMappedExternalUserId()); - expected.setOrganizations(organizationRoles.stream() - .map(r -> UserOrganizationRoles.builder() - .operatorId(r.getOperatorId()) - .organizationIpaCode(r.getOrganizationIpaCode()) - .roles(new ArrayList<>(r.getRoles())) - .email(r.getEmail()) - .build()) - .toList()); - } // When UserInfo result = mapper.apply(iamUserInfo); // Then Assertions.assertEquals(expected, result); } + }