Skip to content

Commit

Permalink
[SELC-4185] feat: refactor logic on retrieve user email based on user…
Browse files Browse the repository at this point in the history
…MailUuid instead of institutionId (#50)
  • Loading branch information
KevinSi96 authored Feb 20, 2024
1 parent 339c10b commit a392667
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public Uni<List<String>> getUsersEmailByInstitutionAndProduct(@NotNull @QueryPar
public Uni<UserResponse> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, WorkContactResource> map, String institutionId){
if(map!=null && !map.isEmpty() && map.containsKey(institutionId)){
return map.get(institutionId).getEmail().getValue();
default String retrieveMailFromWorkContacts(Map<String, WorkContactResource> map, String userMailUuid){
if(map!=null && !map.isEmpty() && map.containsKey(userMailUuid)){
return map.get(userMailUuid).getEmail().getValue();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +19,7 @@ public interface UserService {

Multi<UserProductResponse> getUserProductsByInstitution(String institutionId);

Uni<UserResource> retrievePerson(String userId, String productId, String institutionId);
Uni<UserResponse> retrievePerson(String userId, String productId, String institutionId);

Uni<UserInfo> retrieveBindings(String institutionId, String userId, String[] states);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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";

Expand Down Expand Up @@ -84,14 +85,13 @@ public Uni<List<String>> getUsersEmails(String institutionId, String productId)
var productFilters = OnboardedProductFilter.builder().productId(productId).build().constructMap();
Multi<UserInstitution> 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
Expand All @@ -110,7 +110,7 @@ public Multi<UserProductResponse> getUserProductsByInstitution(String institutio
}

@Override
public Uni<UserResource> retrievePerson(String userId, String productId, String institutionId) {
public Uni<UserResponse> 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<String, Object> queryParameter = userUtils.retrieveMapForFilter(userInstitutionFilters, productFilters);
Expand All @@ -119,7 +119,8 @@ public Uni<UserResource> 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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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("[email protected]").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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
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;
import it.pagopa.selfcare.user.entity.UserInstitutionRole;
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;
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -72,8 +68,9 @@ class UserServiceTest {
@InjectMock
private UserApi userRegistryApi;

@InjectMock
private UserMapper userMapper;

@Spy
private UserMapper userMapper = new UserMapperImpl();

@InjectMock
private ProductService productService;
Expand All @@ -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");
Expand Down Expand Up @@ -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());
Expand All @@ -157,19 +157,22 @@ void testRetrievePerson() {
userResource.setEmail(CertifiableFieldResourceOfstring.builder().value("[email protected]").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<UserResource> subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create());
subscriber.assertItem(userResource);
UniAssertSubscriber<UserResponse> 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<UserResource> subscriber = userService
UniAssertSubscriber<UserResponse> subscriber = userService
.retrievePerson("test-user", "test-product", "test-institutionId")
.subscribe()
.withSubscriber(UniAssertSubscriber.create());
Expand All @@ -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<UserResource> subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create());
UniAssertSubscriber<UserResponse> subscriber = userService.retrievePerson("test-user", "test-product", "test-institutionId").subscribe().withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(ResourceNotFoundException.class);
}
Expand Down Expand Up @@ -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<List<UserNotificationToSend>> subscriber = userService
.findPaginatedUserNotificationToSend(10, 0, "productId")
Expand All @@ -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<List<UserNotificationToSend>> subscriber = userService
.findPaginatedUserNotificationToSend(10, 0, null)
Expand Down

0 comments on commit a392667

Please sign in to comment.