Skip to content

Commit

Permalink
[SELC-4849] hotfix: filter data after retrieve from db on GET /users (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
manuraf committed May 9, 2024
1 parent b07a69c commit 2c91c88
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -153,17 +154,33 @@ public Multi<UserInstitutionResponse> 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);
}

@Override
public Multi<UserInstitutionResponse> findPaginatedUserInstitutions(String institutionId, String userId, List<PartyRole> roles, List<String> states, List<String> products, List<String> 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<UserInstitution, UserInstitution> filterAndSetProducts(List<String> roles, List<String> states, List<String> products, List<String> 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<Void> deleteUserInstitutionProduct(String userId, String institutionId, String productId) {
return userInstitutionService.deleteUserInstitutionProduct(userId, institutionId, productId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<OnboardedProduct> products = new ArrayList<>();
products.add(product);
products.add(productTest);
userInstitution.setProducts(products);
return userInstitution;
}
Expand Down Expand Up @@ -409,18 +418,55 @@ 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<UserInstitutionResponse> subscriber = userService
.findPaginatedUserInstitutions("institutionId", "userId", null, null, List.of(productId), List.of(productRole), 0, 100)
.subscribe()
.withSubscriber(AssertSubscriber.create(10));

List<UserInstitutionResponse> 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<UserInstitutionResponse> subscriber = userService
.findPaginatedUserInstitutions("institutionId", "userId", null, List.of(OnboardedProductState.ACTIVE.name()), null, null, 0, 100)
.subscribe()
.withSubscriber(AssertSubscriber.create(10));

List<UserInstitutionResponse> 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<UserInstitutionResponse> 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));

List<UserInstitutionResponse> actual = subscriber.assertCompleted().getItems();
Assert.assertNotNull(actual);
assertEquals(1, actual.size());
assertEquals(userInstitution.getUserId(), actual.get(0).getUserId());
assertEquals(MANAGER, actual.get(0).getProducts().get(0).getRole());
}

@Test
Expand Down Expand Up @@ -680,7 +726,7 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() {
UniAssertSubscriber<String> 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());
Expand Down

0 comments on commit 2c91c88

Please sign in to comment.