diff --git a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/service/ProductServiceDefault.java b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/service/ProductServiceDefault.java index c388d58ec..16928b850 100644 --- a/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/service/ProductServiceDefault.java +++ b/libs/onboarding-sdk-product/src/main/java/it/pagopa/selfcare/product/service/ProductServiceDefault.java @@ -40,14 +40,17 @@ public ProductServiceDefault(String productString, ObjectMapper mapper) throws J } private Map constructProductsMap(String productString, ObjectMapper mapper) throws JsonProcessingException { - List productList = mapper.readValue(productString, new TypeReference>(){}); - if(Objects.isNull(productList) || productList.isEmpty()) throw new ProductNotFoundException("json string is empty!"); + List productList = mapper.readValue(productString, new TypeReference>() { + }); + if (Objects.isNull(productList) || productList.isEmpty()) + throw new ProductNotFoundException("json string is empty!"); return productList.stream() .collect(Collectors.toMap(Product::getId, Function.identity())); } /** * Returns the list of PagoPA products tree which are not INACTIVE + * * @param rootOnly if true only product that has parent is null are returned * @return List of PagoPA products */ @@ -55,11 +58,11 @@ private Map constructProductsMap(String productString, ObjectMa public List getProducts(boolean rootOnly, boolean valid) { return rootOnly - ? productsMap.values().stream() + ? productsMap.values().stream() .filter(product -> Objects.isNull(product.getParentId())) .filter(product -> !valid || !statusIsNotValid(product.getStatus())) .collect(Collectors.toList()) - : productsMap.values().stream() + : productsMap.values().stream() .filter(product -> !valid || !statusIsNotValid(product.getStatus())) .collect(Collectors.toList()); } @@ -67,19 +70,20 @@ public List getProducts(boolean rootOnly, boolean valid) { /** * Utility method for validating role mappings that contains associations between Selfcare role and Product role. * Each Selfcare role must be only one Product role except OPERATOR. + * * @param roleMappings - * @throws IllegalArgumentException roleMappings is null or empty + * @throws IllegalArgumentException roleMappings is null or empty * @throws InvalidRoleMappingException Selfcare role have more than one Product role */ @Override public void validateRoleMappings(Map roleMappings) { - if(Objects.isNull(roleMappings) || roleMappings.isEmpty()) + if (Objects.isNull(roleMappings) || roleMappings.isEmpty()) throw new IllegalArgumentException("A product role mappings is required"); roleMappings.forEach((partyRole, productRoleInfo) -> { - if(Objects.isNull(productRoleInfo)) + if (Objects.isNull(productRoleInfo)) throw new IllegalArgumentException("A product role info is required"); - if(Objects.isNull(productRoleInfo.getRoles()) || productRoleInfo.getRoles().isEmpty()) + if (Objects.isNull(productRoleInfo.getRoles()) || productRoleInfo.getRoles().isEmpty()) throw new IllegalArgumentException("At least one Product role are required"); if (productRoleInfo.getRoles().size() > 1 && !PartyRole.OPERATOR.equals(partyRole)) { throw new InvalidRoleMappingException(String.format("Only '%s' Party-role can have more than one Product-role, %s", @@ -92,11 +96,11 @@ public void validateRoleMappings(Map roleM /** * Return a product by productId without any filter * retrieving data from institutionContractMappings map + * * @param productId * @return Product * @throws IllegalArgumentException if @param id is null * @throws ProductNotFoundException if product is not found - * */ @Override public Product getProduct(String productId) { @@ -105,7 +109,7 @@ public Product getProduct(String productId) { private Product getProduct(String productId, boolean filterValid) { - if(Objects.isNull(productId)) { + if (Objects.isNull(productId)) { throw new IllegalArgumentException(REQUIRED_PRODUCT_ID_MESSAGE); } Product product = Optional.ofNullable(productsMap.get(productId)) @@ -131,7 +135,8 @@ private Product getProduct(String productId, boolean filterValid) { * Fills contractTemplatePath and ContractTemplateVersion based on @param institutionType. * If institutionContractMappings contains institutionType, it take value from that setting inside * contractTemplatePath and contractTemplateVersion of product - * @param product Product + * + * @param product Product * @param institutionType InstitutionType */ @Override @@ -145,6 +150,7 @@ public void fillContractTemplatePathAndVersion(Product product, InstitutionType /** * Returns the information for a single product if it has not PHASE_OUT,INACTIVE status and its parent has not PHASE_OUT,INACTIVE status + * * @param productId * @return Product if it is valid or null if it has PHASE_OUT,INACTIVE status * @throws IllegalArgumentException product id is null @@ -166,14 +172,25 @@ public ProductRole validateProductRole(String productId, String productRole, Par } Product product = getProduct(productId); + return Optional.ofNullable(product.getRoleMappings()) + .map(partyRoleProductRoleInfoMap -> getProductRole(productRole, role, product)) + .orElseThrow(() -> new IllegalArgumentException(String.format("RoleMappings map for product %s not found", productId))); + } + + /** + * The getProductRole function takes a productRole, role, and product as parameters. + * It returns the ProductRole object that matches the given role and product. + */ + private static ProductRole getProductRole(String productRole, PartyRole role, Product product) { ProductRoleInfo productRoleInfo = product.getRoleMappings().get(role); if (productRoleInfo == null) { throw new IllegalArgumentException(String.format("Role %s not found", role)); } - - return productRoleInfo.getRoles().stream().filter(prodRole -> prodRole.getCode().equals(productRole)) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException(String.format("ProductRole %s not found for role %s", productRole, role))); + return Optional.ofNullable(productRoleInfo.getRoles()) + .map(productRoles -> productRoles.stream().filter(prodRole -> prodRole.getCode().equals(productRole)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(String.format("ProductRole %s not found for role %s", productRole, role)))) + .orElseThrow(() -> new IllegalArgumentException(String.format("Roles of ProductRoleInfo related to role %s not found", role))); } private static boolean statusIsNotValid(ProductStatus status) {