diff --git a/apps/onboarding-cdc/src/test/java/it/pagopa/selfcare/onboarding/event/NotificationServiceTest.java b/apps/onboarding-cdc/src/test/java/it/pagopa/selfcare/onboarding/event/NotificationServiceTest.java index 0bbb2456c..8cc8af717 100644 --- a/apps/onboarding-cdc/src/test/java/it/pagopa/selfcare/onboarding/event/NotificationServiceTest.java +++ b/apps/onboarding-cdc/src/test/java/it/pagopa/selfcare/onboarding/event/NotificationServiceTest.java @@ -1,8 +1,10 @@ package it.pagopa.selfcare.onboarding.event; import io.quarkus.test.InjectMock; +import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; +import io.quarkus.test.mongodb.MongoTestResource; import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; import it.pagopa.selfcare.onboarding.common.OnboardingStatus; @@ -25,6 +27,7 @@ import static org.mockito.Mockito.*; @QuarkusTest +@QuarkusTestResource(MongoTestResource.class) public class NotificationServiceTest { @Mock private OnboardingMapper onboardingMapper; diff --git a/apps/onboarding-functions/pom.xml b/apps/onboarding-functions/pom.xml index 735c2cbd5..ad02f89e0 100644 --- a/apps/onboarding-functions/pom.xml +++ b/apps/onboarding-functions/pom.xml @@ -193,17 +193,17 @@ it.pagopa.selfcare onboarding-sdk-crypto - 0.2.2 + 0.2.3 it.pagopa.selfcare onboarding-sdk-azure-storage - 0.2.2 + 0.2.3 it.pagopa.selfcare onboarding-sdk-product - 0.2.2 + 0.2.3 diff --git a/apps/onboarding-ms/pom.xml b/apps/onboarding-ms/pom.xml index 4b207d701..40bb1b7c6 100644 --- a/apps/onboarding-ms/pom.xml +++ b/apps/onboarding-ms/pom.xml @@ -237,12 +237,12 @@ it.pagopa.selfcare onboarding-sdk-azure-storage - 0.2.2 + 0.2.3 it.pagopa.selfcare onboarding-sdk-product - 0.2.2 + 0.2.3 diff --git a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefault.java b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefault.java index 0288802e3..49ef8f420 100644 --- a/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefault.java +++ b/apps/onboarding-ms/src/main/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefault.java @@ -33,6 +33,7 @@ import it.pagopa.selfcare.onboarding.service.util.OnboardingUtils; import it.pagopa.selfcare.onboarding.util.QueryUtils; import it.pagopa.selfcare.onboarding.util.SortEnum; +import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED; import it.pagopa.selfcare.product.entity.Product; import it.pagopa.selfcare.product.entity.ProductRoleInfo; import it.pagopa.selfcare.product.service.ProductService; @@ -72,6 +73,7 @@ import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_PAGOPA; import static it.pagopa.selfcare.onboarding.constants.CustomError.*; import static it.pagopa.selfcare.onboarding.util.ErrorMessage.*; +import static it.pagopa.selfcare.product.utils.ProductUtils.validRoles; @ApplicationScoped public class OnboardingServiceDefault implements OnboardingService { @@ -303,8 +305,8 @@ private Uni getRegistryResource(Onboarding onboar private Uni persistOnboarding(Onboarding onboarding, List userRequests, Product product, Listaggregates) { /* I have to retrieve onboarding id for saving reference to pdv */ return Panache.withTransaction(() -> Onboarding.persist(onboarding).replaceWith(onboarding) - .onItem().transformToUni(onboardingPersisted -> validationRole(userRequests) - .onItem().transformToUni(ignore -> validateUserAggregatesRoles(aggregates)) + .onItem().transformToUni(onboardingPersisted -> validationRole(userRequests, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING)) + .onItem().transformToUni(ignore -> validateUserAggregatesRoles(aggregates, validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING))) .onItem().transformToUni(ignore -> retrieveAndSetUserAggregatesResources(onboardingPersisted, product, aggregates)) .onItem().transformToUni(ignore -> retrieveUserResources(userRequests, product)) .onItem().invoke(onboardingPersisted::setUsers).replaceWith(onboardingPersisted))); @@ -548,9 +550,7 @@ private String retrieveProductRole(UserRequest userInfo, Map> validationRole(List users) { - - List validRoles = List.of(PartyRole.MANAGER, PartyRole.DELEGATE); + private Uni> validationRole(List users, List validRoles) { List usersNotValidRole = users.stream() .filter(user -> !validRoles.contains(user.getRole())) @@ -566,13 +566,13 @@ private Uni> validationRole(List users) { return Uni.createFrom().item(users); } - private Uni validateUserAggregatesRoles(List aggregates) { + private Uni validateUserAggregatesRoles(List aggregates, List validRoles) { LOG.debug("starting validateUserAggregatesRoles"); if (!CollectionUtils.isEmpty(aggregates)) { return Multi.createFrom().iterable(aggregates) .filter(aggregate -> !CollectionUtils.isEmpty(aggregate.getUsers())) .onItem().invoke(aggregate -> LOG.debugf("Validating role for users of aggregate: %s", aggregate.getTaxCode())) - .onItem().transformToUniAndMerge(aggregate -> validationRole(aggregate.getUsers()) + .onItem().transformToUniAndMerge(aggregate -> validationRole(aggregate.getUsers(), validRoles) .onFailure().invoke(throwable -> LOG.error("Error during validation role for aggregate: %s", aggregate.getTaxCode(), throwable))) .collect().asList().replaceWithVoid(); } diff --git a/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefaultTest.java b/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefaultTest.java index 5b1110d59..9a46a50ac 100644 --- a/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefaultTest.java +++ b/apps/onboarding-ms/src/test/java/it/pagopa/selfcare/onboarding/service/OnboardingServiceDefaultTest.java @@ -33,6 +33,7 @@ import it.pagopa.selfcare.onboarding.service.profile.OnboardingTestProfile; import it.pagopa.selfcare.onboarding.service.strategy.OnboardingValidationStrategy; import it.pagopa.selfcare.onboarding.service.util.OnboardingUtils; +import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED; import it.pagopa.selfcare.product.entity.Product; import it.pagopa.selfcare.product.entity.ProductRole; import it.pagopa.selfcare.product.entity.ProductRoleInfo; @@ -969,6 +970,7 @@ Product createDummyProduct(String productId, boolean hasParent) { productRole.setCode("admin"); ProductRoleInfo productRoleInfo = new ProductRoleInfo(); productRoleInfo.setRoles(List.of(productRole)); + productRoleInfo.setPhasesAdditionAllowed(List.of(PHASE_ADDITION_ALLOWED.ONBOARDING.value)); roleMapping.put(manager.getRole(), productRoleInfo); productResource.setRoleMappings(roleMapping); diff --git a/apps/pom.xml b/apps/pom.xml index 786906827..0722cbadf 100644 --- a/apps/pom.xml +++ b/apps/pom.xml @@ -16,7 +16,7 @@ it.pagopa.selfcare onboarding-sdk-common - 0.2.2 + 0.2.3 diff --git a/libs/onboarding-sdk-azure-storage/pom.xml b/libs/onboarding-sdk-azure-storage/pom.xml index c44460f1c..5e3ae2767 100644 --- a/libs/onboarding-sdk-azure-storage/pom.xml +++ b/libs/onboarding-sdk-azure-storage/pom.xml @@ -6,7 +6,7 @@ it.pagopa.selfcare onboarding-sdk-pom - 0.2.2 + 0.2.3 ../onboarding-sdk-pom diff --git a/libs/onboarding-sdk-common/pom.xml b/libs/onboarding-sdk-common/pom.xml index a21aefd70..f9ee1c1b2 100644 --- a/libs/onboarding-sdk-common/pom.xml +++ b/libs/onboarding-sdk-common/pom.xml @@ -4,7 +4,7 @@ it.pagopa.selfcare onboarding-sdk-pom - 0.2.2 + 0.2.3 ../onboarding-sdk-pom onboarding-sdk-common diff --git a/libs/onboarding-sdk-crypto/pom.xml b/libs/onboarding-sdk-crypto/pom.xml index ce8f5a039..3e589278b 100644 --- a/libs/onboarding-sdk-crypto/pom.xml +++ b/libs/onboarding-sdk-crypto/pom.xml @@ -4,7 +4,7 @@ it.pagopa.selfcare onboarding-sdk-pom - 0.2.2 + 0.2.3 ../onboarding-sdk-pom onboarding-sdk-crypto diff --git a/libs/onboarding-sdk-pom/pom.xml b/libs/onboarding-sdk-pom/pom.xml index d560a7564..9cd168c99 100644 --- a/libs/onboarding-sdk-pom/pom.xml +++ b/libs/onboarding-sdk-pom/pom.xml @@ -6,7 +6,7 @@ onboarding-sdk-pom pom onboarding-sdk-pom - 0.2.2 + 0.2.3 17 diff --git a/libs/onboarding-sdk-product/pom.xml b/libs/onboarding-sdk-product/pom.xml index a8432354b..e39fcc5b2 100644 --- a/libs/onboarding-sdk-product/pom.xml +++ b/libs/onboarding-sdk-product/pom.xml @@ -4,12 +4,12 @@ it.pagopa.selfcare onboarding-sdk-pom - 0.2.2 + 0.2.3 ../onboarding-sdk-pom onboarding-sdk-product onboarding-sdk-product - 0.2.2 + 0.2.3 2.15.2 diff --git a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/PHASE_ADDITION_ALLOWED.java b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/PHASE_ADDITION_ALLOWED.java new file mode 100644 index 000000000..6c4cb863e --- /dev/null +++ b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/PHASE_ADDITION_ALLOWED.java @@ -0,0 +1,19 @@ +package it.pagopa.selfcare.product.entity; + +public enum PHASE_ADDITION_ALLOWED { + + //Phase on onboarding process + ONBOARDING("onboarding"), + + //Phase on dashboard "Aggiunta Utenti" with any constraints + DASHBOARD("dashboard"), + + //Phase on dashboard "Aggiunta Utenti" when a sign contract is needed + DASHBOARD_ASYNC("dashboard-async"); + + public final String value; + + PHASE_ADDITION_ALLOWED(String value){ + this.value = value; + } +} diff --git a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/ProductRoleInfo.java b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/ProductRoleInfo.java index 05f4da849..1cb9cdbe5 100644 --- a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/ProductRoleInfo.java +++ b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/entity/ProductRoleInfo.java @@ -5,6 +5,11 @@ public class ProductRoleInfo { private boolean multiroleAllowed; + + /** + * List of phases where addition of the role is allowed + */ + private List phasesAdditionAllowed; private List roles; public boolean isMultiroleAllowed() { @@ -15,6 +20,14 @@ public void setMultiroleAllowed(boolean multiroleAllowed) { this.multiroleAllowed = multiroleAllowed; } + public List getPhasesAdditionAllowed() { + return phasesAdditionAllowed; + } + + public void setPhasesAdditionAllowed(List phasesAdditionAllowed) { + this.phasesAdditionAllowed = phasesAdditionAllowed; + } + public List getRoles() { return roles; } diff --git a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java new file mode 100644 index 000000000..511cf5255 --- /dev/null +++ b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/utils/ProductUtils.java @@ -0,0 +1,59 @@ +package it.pagopa.selfcare.product.utils; + +import it.pagopa.selfcare.onboarding.common.PartyRole; +import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED; +import it.pagopa.selfcare.product.entity.Product; +import it.pagopa.selfcare.product.entity.ProductRoleInfo; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +public class ProductUtils { + + /** + * Returns list of product's PartyRole associates with that PHASE_ADDITION_ALLOWED + * @param product Product + * @param phase PHASE_ADDITION_ALLOWED + * @return List + */ + public static List validRoles(Product product, PHASE_ADDITION_ALLOWED phase) { + return validEntryRoles(product, phase) + .stream() + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + /** + * Returns list of product's PartyRole associates with that PHASE_ADDITION_ALLOWED and filtering for productRole + * @param product Product + * @param phase PHASE_ADDITION_ALLOWED + * @param productRole String + * @return List + */ + public static List validRolesByProductRole(Product product, PHASE_ADDITION_ALLOWED phase, String productRole) { + if(Objects.isNull(product)) return List.of(); + return validEntryRoles(product, phase) + .stream() + .filter(entry -> Objects.nonNull(entry.getValue().getRoles()) && + entry.getValue().getRoles().stream() + .anyMatch(item -> productRole.equals(item.getCode()))) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + private static List> validEntryRoles(Product product, PHASE_ADDITION_ALLOWED phase) { + if (Objects.isNull(product)) { + throw new IllegalArgumentException("Product must not be null!"); + } + return Optional.ofNullable(product.getRoleMappings()) + .orElse(Map.of()) + .entrySet().stream() + .filter(entry -> Objects.nonNull(entry.getValue().getPhasesAdditionAllowed()) && + entry.getValue().getPhasesAdditionAllowed().contains(phase.value)) + .collect(Collectors.toList()); + + } +} diff --git a/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/service/ProductServiceDefaultTest.java b/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/service/ProductServiceDefaultTest.java index ce1b11631..d57d49a65 100644 --- a/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/service/ProductServiceDefaultTest.java +++ b/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/service/ProductServiceDefaultTest.java @@ -7,7 +7,6 @@ import it.pagopa.selfcare.product.entity.ProductRoleInfo; import it.pagopa.selfcare.product.exception.InvalidRoleMappingException; import it.pagopa.selfcare.product.exception.ProductNotFoundException; -import it.pagopa.selfcare.product.service.ProductServiceDefault; import org.junit.jupiter.api.Test; import java.util.HashMap; @@ -24,7 +23,7 @@ public class ProductServiceDefaultTest { "{\"id\":\"prod-inactive\",\"status\":\"INACTIVE\"}]"; final private String PRODUCT_JSON_STRING_WITH_ROLEMAPPING = "[{\"id\":\"prod-test-parent\",\"status\":\"ACTIVE\"}," + - "{\"id\":\"prod-test\", \"parentId\":\"prod-test-parent\",\"status\":\"ACTIVE\", \"roleMappings\" : {\"MANAGER\":{\"roles\":[{\"code\":\"operatore\"}]}}}," + + "{\"id\":\"prod-test\", \"parentId\":\"prod-test-parent\",\"status\":\"ACTIVE\", \"roleMappings\" : {\"MANAGER\":{\"roles\":[{\"code\":\"operatore\"}], \"phasesAdditionAllowed\":[\"onboarding\"]}}}," + "{\"id\":\"prod-inactive\",\"status\":\"INACTIVE\"}]"; @Test diff --git a/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/utils/ProductUtilsTest.java b/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/utils/ProductUtilsTest.java new file mode 100644 index 000000000..85890ee3e --- /dev/null +++ b/libs/onboarding-sdk-product/src/test/java/it/pagopa/selfcare/product/utils/ProductUtilsTest.java @@ -0,0 +1,59 @@ +package it.pagopa.selfcare.product.utils; + +import it.pagopa.selfcare.onboarding.common.PartyRole; +import it.pagopa.selfcare.product.entity.PHASE_ADDITION_ALLOWED; +import it.pagopa.selfcare.product.entity.Product; +import it.pagopa.selfcare.product.entity.ProductRole; +import it.pagopa.selfcare.product.entity.ProductRoleInfo; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ProductUtilsTest { + + private final static String productRoleManager = "admin"; + + @Test + void validRoles() { + Product product = dummyProduct(); + List partyRoles = ProductUtils.validRoles(product, PHASE_ADDITION_ALLOWED.ONBOARDING); + assertEquals(1, partyRoles.size()); + assertEquals(PartyRole.MANAGER, partyRoles.get(0)); + } + + @Test + void validRolesByProductRole() { + Product product = dummyProduct(); + List partyRoles = ProductUtils.validRolesByProductRole(product, PHASE_ADDITION_ALLOWED.ONBOARDING, productRoleManager); + assertEquals(1, partyRoles.size()); + assertEquals(PartyRole.MANAGER, partyRoles.get(0)); + } + + Product dummyProduct() { + + ProductRole productRole = new ProductRole(); + productRole.setCode(productRoleManager); + ProductRoleInfo productRoleInfo = new ProductRoleInfo(); + productRoleInfo.setRoles(List.of(productRole)); + productRoleInfo.setPhasesAdditionAllowed(List.of(PHASE_ADDITION_ALLOWED.ONBOARDING.value)); + + ProductRole productRoleOperator = new ProductRole(); + productRoleOperator.setCode("operator"); + ProductRoleInfo productRoleInfoOperator = new ProductRoleInfo(); + productRoleInfoOperator.setRoles(List.of(productRoleOperator)); + productRoleInfoOperator.setPhasesAdditionAllowed(List.of(PHASE_ADDITION_ALLOWED.DASHBOARD.value)); + + Map roleMapping = new HashMap<>(); + roleMapping.put(PartyRole.MANAGER, productRoleInfo); + roleMapping.put(PartyRole.OPERATOR, productRoleInfoOperator); + + Product productResource = new Product(); + productResource.setId("productId"); + productResource.setRoleMappings(roleMapping); + return productResource; + } +} diff --git a/test-coverage/pom.xml b/test-coverage/pom.xml index 33c9be6d9..ac320a966 100644 --- a/test-coverage/pom.xml +++ b/test-coverage/pom.xml @@ -77,22 +77,22 @@ it.pagopa.selfcare onboarding-sdk-product - 0.2.2 + 0.2.3 it.pagopa.selfcare onboarding-sdk-common - 0.2.2 + 0.2.3 it.pagopa.selfcare onboarding-sdk-azure-storage - 0.2.2 + 0.2.3 it.pagopa.selfcare onboarding-sdk-crypto - 0.2.2 + 0.2.3