diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java index 5a93ad4d..9f9aed59 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java @@ -62,8 +62,7 @@ public Uni> getUsersEmailByInstitutionAndProduct(@NotNull @QueryPar public Uni getUserInfo(@PathParam(value = "id") String userId, @QueryParam(value = "institutionId") String institutionId, @QueryParam(value = "productId") String productId) { - return userService.retrievePerson(userId, productId, institutionId) - .map(user -> userMapper.toUserResponse(user, institutionId)); + return userService.retrievePerson(userId, productId, institutionId); } @Operation(summary = "Retrieves products info and role which the user is enabled") diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java index 91b87c31..e73b1563 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java @@ -1,6 +1,8 @@ package it.pagopa.selfcare.user.mapper; -import it.pagopa.selfcare.user.controller.response.*; +import it.pagopa.selfcare.user.controller.response.OnboardedProductResponse; +import it.pagopa.selfcare.user.controller.response.UserNotificationResponse; +import it.pagopa.selfcare.user.controller.response.UserResponse; import it.pagopa.selfcare.user.controller.response.product.InstitutionProducts; import it.pagopa.selfcare.user.controller.response.product.UserProductsResponse; import it.pagopa.selfcare.user.entity.UserInfo; @@ -22,16 +24,16 @@ public interface UserMapper { @Mapping(source = "userResource.fiscalCode", target = "taxCode") @Mapping(source = "userResource.familyName", target = "surname") - @Mapping(target = "email", expression = "java(retrieveMailFromWorkContacts(userResource.getWorkContacts(), institutionId))") - UserResponse toUserResponse(UserResource userResource, String institutionId); + @Mapping(target = "email", expression = "java(retrieveMailFromWorkContacts(userResource.getWorkContacts(), userMailUuid))") + UserResponse toUserResponse(UserResource userResource, String userMailUuid); default String fromCertifiabletoString(CertifiableFieldResourceOfstring certifiableFieldResourceOfstring) { return certifiableFieldResourceOfstring.getValue(); } @Named("retrieveMailFromWorkContacts") - default String retrieveMailFromWorkContacts(Map map, String institutionId){ - if(map!=null && !map.isEmpty() && map.containsKey(institutionId)){ - return map.get(institutionId).getEmail().getValue(); + default String retrieveMailFromWorkContacts(Map map, String userMailUuid){ + if(map!=null && !map.isEmpty() && map.containsKey(userMailUuid)){ + return map.get(userMailUuid).getEmail().getValue(); } return null; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java index 01939647..cd2cad2d 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java @@ -6,9 +6,9 @@ import it.pagopa.selfcare.user.constant.OnboardedProductState; import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse; import it.pagopa.selfcare.user.controller.response.UserProductResponse; +import it.pagopa.selfcare.user.controller.response.UserResponse; import it.pagopa.selfcare.user.entity.UserInfo; import it.pagopa.selfcare.user.model.notification.UserNotificationToSend; -import org.openapi.quarkus.user_registry_json.model.UserResource; import java.time.LocalDateTime; import java.util.List; @@ -19,7 +19,7 @@ public interface UserService { Multi getUserProductsByInstitution(String institutionId); - Uni retrievePerson(String userId, String productId, String institutionId); + Uni retrievePerson(String userId, String productId, String institutionId); Uni retrieveBindings(String institutionId, String userId, String[] states); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java index d17860ec..46f428ba 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java @@ -6,6 +6,7 @@ import it.pagopa.selfcare.user.constant.OnboardedProductState; import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse; import it.pagopa.selfcare.user.controller.response.UserProductResponse; +import it.pagopa.selfcare.user.controller.response.UserResponse; import it.pagopa.selfcare.user.entity.UserInfo; import it.pagopa.selfcare.user.entity.UserInstitution; import it.pagopa.selfcare.user.entity.filter.OnboardedProductFilter; @@ -14,6 +15,7 @@ import it.pagopa.selfcare.user.exception.ResourceNotFoundException; import it.pagopa.selfcare.user.mapper.OnboardedProductMapper; import it.pagopa.selfcare.user.mapper.UserInstitutionMapper; +import it.pagopa.selfcare.user.mapper.UserMapper; import it.pagopa.selfcare.user.model.notification.UserNotificationToSend; import it.pagopa.selfcare.user.util.UserUtils; import jakarta.enterprise.context.ApplicationScoped; @@ -23,7 +25,6 @@ import org.apache.commons.lang3.StringUtils; import org.eclipse.microprofile.rest.client.inject.RestClient; import org.openapi.quarkus.user_registry_json.api.UserApi; -import org.openapi.quarkus.user_registry_json.model.UserResource; import java.time.LocalDateTime; import java.util.ArrayList; @@ -34,7 +35,6 @@ import static it.pagopa.selfcare.user.constant.CustomError.*; import static it.pagopa.selfcare.user.util.GeneralUtils.formatQueryParameterList; -import static it.pagopa.selfcare.user.constant.CustomError.USER_NOT_FOUND_ERROR; import static it.pagopa.selfcare.user.util.UserUtils.VALID_USER_PRODUCT_STATES_FOR_NOTIFICATION; @RequiredArgsConstructor @@ -50,6 +50,7 @@ public class UserServiceImpl implements UserService { private final UserUtils userUtils; private final UserInstitutionService userInstitutionService; private final UserInstitutionMapper userInstitutionMapper; + private final UserMapper userMapper; private static final String WORK_CONTACTS = "workContacts"; @@ -84,14 +85,13 @@ public Uni> getUsersEmails(String institutionId, String productId) var productFilters = OnboardedProductFilter.builder().productId(productId).build().constructMap(); Multi userInstitutions = userInstitutionService.findAllWithFilter(userUtils.retrieveMapForFilter(userInstitutionFilters, productFilters)); return userInstitutions.onItem() - .transformToUni(obj -> userRegistryApi.findByIdUsingGET(WORK_CONTACTS, obj.getUserId())) - .merge() - .filter(userResource -> Objects.nonNull(userResource.getWorkContacts()) - && userResource.getWorkContacts().containsKey(institutionId)) - .map(user -> user.getWorkContacts().get(institutionId)) - .filter(workContract -> StringUtils.isNotBlank(workContract.getEmail().getValue())) - .map(workContract -> workContract.getEmail().getValue()) + .transformToUni(userInstitution -> userRegistryApi.findByIdUsingGET(WORK_CONTACTS, userInstitution.getUserId()) + .map(userResource -> Objects.nonNull(userResource.getWorkContacts()) && userResource.getWorkContacts().containsKey(userInstitution.getUserMailUuid()) + ? userResource.getWorkContacts().get(userInstitution.getUserMailUuid()) : null)).merge() + .filter(workContactResource -> Objects.nonNull(workContactResource) && StringUtils.isNotBlank(workContactResource.getEmail().getValue())) + .map(workContactResource -> workContactResource.getEmail().getValue()) .collect().asList(); + } @Override @@ -110,7 +110,7 @@ public Multi getUserProductsByInstitution(String institutio } @Override - public Uni retrievePerson(String userId, String productId, String institutionId) { + public Uni retrievePerson(String userId, String productId, String institutionId) { var userInstitutionFilters = UserInstitutionFilter.builder().userId(userId).institutionId(institutionId).build().constructMap(); var productFilters = OnboardedProductFilter.builder().productId(productId).build().constructMap(); Map queryParameter = userUtils.retrieveMapForFilter(userInstitutionFilters, productFilters); @@ -119,7 +119,8 @@ public Uni retrievePerson(String userId, String productId, String log.error(String.format(USER_NOT_FOUND_ERROR.getMessage(), userId)); return new ResourceNotFoundException(String.format(USER_NOT_FOUND_ERROR.getMessage(), userId), USER_NOT_FOUND_ERROR.getCode()); }) - .onItem().transformToUni(userInstitution -> userRegistryApi.findByIdUsingGET(USERS_WORKS_FIELD_LIST, userInstitution.getUserId())) + .onItem().transformToUni(userInstitution -> userRegistryApi.findByIdUsingGET(USERS_WORKS_FIELD_LIST, userInstitution.getUserId()) + .map(userResource -> userMapper.toUserResponse(userResource, userInstitution.getUserMailUuid()))) .onFailure(UserUtils::checkIfNotFoundException).transform(t -> new ResourceNotFoundException(String.format(USER_NOT_FOUND_ERROR.getMessage(), userId), USER_NOT_FOUND_ERROR.getCode())); } diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java index 23ab676e..5ad1e076 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java @@ -9,6 +9,7 @@ import io.smallrye.mutiny.Uni; import it.pagopa.selfcare.user.constant.OnboardedProductState; import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse; +import it.pagopa.selfcare.user.controller.response.UserResponse; import it.pagopa.selfcare.user.entity.UserInfo; import it.pagopa.selfcare.user.entity.UserInstitutionRole; import it.pagopa.selfcare.user.exception.InvalidRequestException; @@ -19,16 +20,13 @@ import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfLocalDate; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto; -import org.openapi.quarkus.user_registry_json.model.UserResource; -import java.time.LocalDate; import java.util.*; import static io.restassured.RestAssured.given; -import static org.mockito.ArgumentMatchers.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; @QuarkusTest @TestHTTPEndpoint(UserController.class) @@ -97,13 +95,12 @@ void getUsersEmailByInstitutionNotAuthorized() { @Test @TestSecurity(user = "userJwt") void testGetUserInfoOk() { - UserResource userResource = new UserResource(); - userResource.setId(UUID.randomUUID()); - userResource.setFiscalCode("test"); - userResource.setBirthDate(CertifiableFieldResourceOfLocalDate.builder().value(LocalDate.now()).build()); - userResource.setEmail(CertifiableFieldResourceOfstring.builder().value("test@test.com").build()); - userResource.setName(CertifiableFieldResourceOfstring.builder().value("testName").build()); - userResource.setFamilyName(CertifiableFieldResourceOfstring.builder().value("testFamilyName").build()); + UserResponse userResource = new UserResponse(); + userResource.setId(UUID.randomUUID().toString()); + userResource.setTaxCode("test"); + userResource.setEmail("email"); + userResource.setName("name"); + userResource.setSurname("testFamilyName"); Mockito.when(userService.retrievePerson(any(), any(), any())) .thenReturn(Uni.createFrom().item(userResource)); diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java index 0476792e..6a947f75 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java @@ -14,7 +14,9 @@ import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; import it.pagopa.selfcare.product.service.ProductService; import it.pagopa.selfcare.user.constant.OnboardedProductState; -import it.pagopa.selfcare.user.controller.response.*; +import it.pagopa.selfcare.user.controller.response.UserInstitutionResponse; +import it.pagopa.selfcare.user.controller.response.UserProductResponse; +import it.pagopa.selfcare.user.controller.response.UserResponse; import it.pagopa.selfcare.user.entity.OnboardedProduct; import it.pagopa.selfcare.user.entity.UserInfo; import it.pagopa.selfcare.user.entity.UserInstitution; @@ -22,6 +24,7 @@ import it.pagopa.selfcare.user.exception.InvalidRequestException; import it.pagopa.selfcare.user.exception.ResourceNotFoundException; import it.pagopa.selfcare.user.mapper.UserMapper; +import it.pagopa.selfcare.user.mapper.UserMapperImpl; import it.pagopa.selfcare.user.model.notification.UserNotificationToSend; import jakarta.inject.Inject; import org.apache.http.HttpStatus; @@ -30,6 +33,7 @@ import org.jboss.resteasy.reactive.client.api.WebClientApplicationException; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.mockito.Spy; import org.openapi.quarkus.user_registry_json.api.UserApi; import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfLocalDate; import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; @@ -38,22 +42,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; import java.util.*; -import static it.pagopa.selfcare.user.constant.CustomError.*; import static it.pagopa.selfcare.onboarding.common.PartyRole.MANAGER; -import static it.pagopa.selfcare.user.constant.CustomError.STATUS_IS_MANDATORY; -import static it.pagopa.selfcare.user.constant.CustomError.USER_TO_UPDATE_NOT_FOUND; +import static it.pagopa.selfcare.user.constant.CustomError.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.when; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @QuarkusTest @QuarkusTestResource(MongoTestResource.class) @@ -72,8 +68,9 @@ class UserServiceTest { @InjectMock private UserApi userRegistryApi; - @InjectMock - private UserMapper userMapper; + + @Spy + private UserMapper userMapper = new UserMapperImpl(); @InjectMock private ProductService productService; @@ -94,12 +91,13 @@ class UserServiceTest { WorkContactResource workContactResource = new WorkContactResource(); workContactResource.setEmail(certifiedEmail); userResource.setEmail(certifiedEmail); - userResource.setWorkContacts(Map.of("institutionId", workContactResource)); + userResource.setWorkContacts(Map.of("userMailUuid", workContactResource)); userInstitution = new UserInstitution(); userInstitution.setId(ObjectId.get()); userInstitution.setUserId("userId"); userInstitution.setInstitutionId("institutionId"); + userInstitution.setUserMailUuid("userMailUuid"); userInstitution.setInstitutionRootName("institutionRootName"); OnboardedProduct product = new OnboardedProduct(); product.setProductId("test"); @@ -149,6 +147,8 @@ void getUserProductsByInstitutionTest() { void testRetrievePerson() { UserInstitution userInstitution = new UserInstitution(); userInstitution.setUserId("test-user"); + String userMailUuId = UUID.randomUUID().toString(); + userInstitution.setUserMailUuid(userMailUuId); UserResource userResource = new UserResource(); userResource.setId(UUID.randomUUID()); @@ -157,19 +157,22 @@ void testRetrievePerson() { userResource.setEmail(CertifiableFieldResourceOfstring.builder().value("test@test.com").build()); userResource.setName(CertifiableFieldResourceOfstring.builder().value("testName").build()); userResource.setFamilyName(CertifiableFieldResourceOfstring.builder().value("testFamilyName").build()); + WorkContactResource workContactResource = new WorkContactResource(); + workContactResource.setEmail(CertifiableFieldResourceOfstring.builder().value("userMail").build()); + userResource.setWorkContacts(Map.of(userMailUuId, workContactResource)); when(userInstitutionService.retrieveFirstFilteredUserInstitution(any())).thenReturn(Uni.createFrom().item(userInstitution)); when(userRegistryApi.findByIdUsingGET(any(), any())).thenReturn(Uni.createFrom().item(userResource)); - UniAssertSubscriber subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create()); - subscriber.assertItem(userResource); + UniAssertSubscriber subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create()); + assertEquals("userMail", subscriber.getItem().getEmail()); } @Test void testRetrievePersonFailsWhenUserIsNotPresent() { when(userInstitutionService.retrieveFirstFilteredUserInstitution(any())).thenReturn(Uni.createFrom().nullItem()); - UniAssertSubscriber subscriber = userService + UniAssertSubscriber subscriber = userService .retrievePerson("test-user", "test-product", "test-institutionId") .subscribe() .withSubscriber(UniAssertSubscriber.create()); @@ -184,7 +187,7 @@ void testRetrievePersonFailsWhenPdvFails() { when(userInstitutionService.retrieveFirstFilteredUserInstitution(any())).thenReturn(Uni.createFrom().item(userInstitution)); when(userRegistryApi.findByIdUsingGET(any(), any())).thenReturn(Uni.createFrom().failure(new WebClientApplicationException(HttpStatus.SC_NOT_FOUND))); - UniAssertSubscriber subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create()); + UniAssertSubscriber subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create()); subscriber.assertFailedWith(ResourceNotFoundException.class); } @@ -397,9 +400,6 @@ void findPaginatedUserNotificationToSend() { UserResource userResource = mock(UserResource.class); when(userRegistryApi.findByIdUsingGET(any(), any())) .thenReturn(Uni.createFrom().item(userResource)); - UserNotificationResponse userNotificationResponse = mock(UserNotificationResponse.class); - when(userMapper.toUserNotification(any())) - .thenReturn(userNotificationResponse); UniAssertSubscriber> subscriber = userService .findPaginatedUserNotificationToSend(10, 0, "productId") @@ -416,9 +416,6 @@ void findPaginatedUserNotificationToSendQueryWithoutProductId() { UserResource userResource = mock(UserResource.class); when(userRegistryApi.findByIdUsingGET(any(), any())) .thenReturn(Uni.createFrom().item(userResource)); - UserNotificationResponse userNotificationResponse = mock(UserNotificationResponse.class); - when(userMapper.toUserNotification(any())) - .thenReturn(userNotificationResponse); UniAssertSubscriber> subscriber = userService .findPaginatedUserNotificationToSend(10, 0, null)