Skip to content

Commit

Permalink
fix validateProductRole method
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo committed Jan 25, 2024
1 parent 2c589d2 commit dd3b05c
Showing 1 changed file with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,50 @@ public ProductServiceDefault(String productString, ObjectMapper mapper) throws J
}

private Map<String, Product> constructProductsMap(String productString, ObjectMapper mapper) throws JsonProcessingException {
List<Product> productList = mapper.readValue(productString, new TypeReference<List<Product>>(){});
if(Objects.isNull(productList) || productList.isEmpty()) throw new ProductNotFoundException("json string is empty!");
List<Product> productList = mapper.readValue(productString, new TypeReference<List<Product>>() {
});
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
*/
@Override
public List<Product> 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());
}

/**
* 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<PartyRole, ? extends ProductRoleInfo> 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",
Expand All @@ -92,11 +96,11 @@ public void validateRoleMappings(Map<PartyRole, ? extends ProductRoleInfo> 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) {
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit dd3b05c

Please sign in to comment.