From 2c91c88df51ad70f1df0c77cd6876a4ac8c6ef95 Mon Sep 17 00:00:00 2001 From: Manuel Rafeli Date: Thu, 9 May 2024 11:34:51 +0200 Subject: [PATCH] [SELC-4849] hotfix: filter data after retrieve from db on GET /users (#123) --- .../user/service/UserServiceImpl.java | 17 ++++++ .../user/service/UserServiceTest.java | 52 +++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) 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 49dacd10..79a5b71f 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 @@ -41,6 +41,7 @@ import java.time.LocalDateTime; import java.util.*; +import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Stream; @@ -153,6 +154,7 @@ public Multi findAllUserInstitutions(String institution var userInstitutionFilters = UserInstitutionFilter.builder().userId(userId).institutionId(institutionId).build().constructMap(); var productFilters = OnboardedProductFilter.builder().productId(products).status(states).role(roles).productRole(productRoles).build().constructMap(); return userInstitutionService.findAllWithFilter(userUtils.retrieveMapForFilter(userInstitutionFilters, productFilters)) + .onItem().transform(filterAndSetProducts(roles, states, products, productRoles)) .onItem().transform(userInstitutionMapper::toResponse); } @@ -160,10 +162,25 @@ public Multi findAllUserInstitutions(String institution public Multi findPaginatedUserInstitutions(String institutionId, String userId, List roles, List states, List products, List productRoles, Integer page, Integer size) { var userInstitutionFilters = UserInstitutionFilter.builder().userId(userId).institutionId(institutionId).build().constructMap(); var productFilters = OnboardedProductFilter.builder().productId(products).status(states).role(roles).productRole(productRoles).build().constructMap(); + var rolesString = Optional.ofNullable(roles).map(items -> items.stream().map(PartyRole::name).toList()).orElse(null); return userInstitutionService.paginatedFindAllWithFilter(userUtils.retrieveMapForFilter(userInstitutionFilters, productFilters), page, size) + .onItem().transform(filterAndSetProducts(rolesString, states, products, productRoles)) .onItem().transform(userInstitutionMapper::toResponse); } + private static Function filterAndSetProducts(List roles, List states, List products, List productRoles) { + + return userInstitution -> { + userInstitution.setProducts(userInstitution.getProducts().stream() + .filter(product -> CollectionUtils.isNullOrEmpty(products) || products.contains(product.getProductId())) + .filter(product -> CollectionUtils.isNullOrEmpty(states) || states.contains(Optional.ofNullable(product.getStatus()).map(OnboardedProductState::name).orElse(null))) + .filter(product -> CollectionUtils.isNullOrEmpty(roles) || roles.contains(Optional.ofNullable(product.getRole()).map(PartyRole::name).orElse(null))) + .filter(product -> CollectionUtils.isNullOrEmpty(productRoles) || productRoles.contains(product.getProductRole())) + .toList()); + return userInstitution; + }; + } + @Override public Uni deleteUserInstitutionProduct(String userId, String institutionId, String productId) { return userInstitutionService.deleteUserInstitutionProduct(userId, institutionId, productId) 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 2db20e98..f0eaa593 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 @@ -12,6 +12,7 @@ import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.helpers.test.AssertSubscriber; import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; +import it.pagopa.selfcare.onboarding.common.PartyRole; import it.pagopa.selfcare.product.entity.Product; import it.pagopa.selfcare.product.service.ProductService; import it.pagopa.selfcare.user.constant.OnboardedProductState; @@ -118,12 +119,20 @@ private UserInstitution createUserInstitution(){ userInstitution.setInstitutionId("institutionId"); userInstitution.setUserMailUuid(workContractsKey); userInstitution.setInstitutionRootName("institutionRootName"); + OnboardedProduct product = new OnboardedProduct(); product.setProductId("test"); product.setProductRole("admin"); + product.setRole(MANAGER); product.setStatus(OnboardedProductState.ACTIVE); + + OnboardedProduct productTest = new OnboardedProduct(); + productTest.setProductId("prod-test"); + productTest.setProductRole("operator"); + productTest.setStatus(OnboardedProductState.DELETED); List products = new ArrayList<>(); products.add(product); + products.add(productTest); userInstitution.setProducts(products); return userInstitution; } @@ -409,11 +418,47 @@ void retrieveUsersTest() { @Test - void findAllUserInstitutionsPaged() { + void findAllUserInstitutionsPaged_whenFilterProductIdAndProductRole() { + UserInstitution userInstitution = createUserInstitution(); + final String productId = userInstitution.getProducts().get(0).getProductId(); + final String productRole = userInstitution.getProducts().get(0).getProductRole(); + when(userInstitutionService.paginatedFindAllWithFilter(anyMap(), anyInt(), anyInt())).thenReturn(Multi.createFrom().item(userInstitution)); + AssertSubscriber subscriber = userService + .findPaginatedUserInstitutions("institutionId", "userId", null, null, List.of(productId), List.of(productRole), 0, 100) + .subscribe() + .withSubscriber(AssertSubscriber.create(10)); + + List actual = subscriber.assertCompleted().getItems(); + Assert.assertNotNull(actual); + assertEquals(1, actual.size()); + assertEquals(userInstitution.getUserId(), actual.get(0).getUserId()); + assertEquals(productId, actual.get(0).getProducts().get(0).getProductId()); + } + + + @Test + void findAllUserInstitutionsPaged_whenFilterStatus() { + UserInstitution userInstitution = createUserInstitution(); + when(userInstitutionService.paginatedFindAllWithFilter(anyMap(), anyInt(), anyInt())).thenReturn(Multi.createFrom().item(userInstitution)); + AssertSubscriber subscriber = userService + .findPaginatedUserInstitutions("institutionId", "userId", null, List.of(OnboardedProductState.ACTIVE.name()), null, null, 0, 100) + .subscribe() + .withSubscriber(AssertSubscriber.create(10)); + + List actual = subscriber.assertCompleted().getItems(); + Assert.assertNotNull(actual); + assertEquals(1, actual.size()); + assertEquals(userInstitution.getUserId(), actual.get(0).getUserId()); + assertEquals(OnboardedProductState.ACTIVE, actual.get(0).getProducts().get(0).getStatus()); + } + + + @Test + void findAllUserInstitutionsPaged_whenFilterRole() { UserInstitution userInstitution = createUserInstitution(); when(userInstitutionService.paginatedFindAllWithFilter(anyMap(), anyInt(), anyInt())).thenReturn(Multi.createFrom().item(userInstitution)); AssertSubscriber subscriber = userService - .findPaginatedUserInstitutions("institutionId", "userId", null, null, null, null, 0, 100) + .findPaginatedUserInstitutions("institutionId", "userId", List.of(MANAGER), null, null, null, 0, 100) .subscribe() .withSubscriber(AssertSubscriber.create(10)); @@ -421,6 +466,7 @@ void findAllUserInstitutionsPaged() { Assert.assertNotNull(actual); assertEquals(1, actual.size()); assertEquals(userInstitution.getUserId(), actual.get(0).getUserId()); + assertEquals(MANAGER, actual.get(0).getProducts().get(0).getRole()); } @Test @@ -680,7 +726,7 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() { UniAssertSubscriber subscriber = userService.createOrUpdateUserByFiscalCode(createUserDto, loggedUser) .subscribe().withSubscriber(UniAssertSubscriber.create()); - Assertions.assertEquals(3, userInstitution.getProducts().size()); + Assertions.assertEquals(4, userInstitution.getProducts().size()); // Verify the result subscriber.awaitItem().assertItem(userId.toString()); verify(userRegistryApi).updateUsingPATCH(any(), any());