diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java index c145b1a5f..d079bab71 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java @@ -76,39 +76,51 @@ private PageImpl applyPaging(final Pageable pageable, final List< final int end = Math.min((pageable.getPageNumber() + 1) * pageable.getPageSize(), policies.size()); final List pagedPolicies = policies.subList(start, end); - final String sortField = getSortField(pageable); - final Sort sort = isFieldSortedAscending(pageable, sortField) - ? Sort.by(sortField).ascending() - : Sort.by(sortField).descending(); - return new PageImpl<>(pagedPolicies, PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort), - policies.size()); + return new PageImpl<>(pagedPolicies, + PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort()), policies.size()); } private Comparator getComparator(final Pageable pageable) { - Comparator comparator; + Comparator comparator = null; - final String sortField = getSortField(pageable); + final List sort = pageable.getSort().stream().toList(); + for (final Sort.Order order : sort) { + + Comparator fieldComparator; + final String property = order.getProperty(); + if (property.equals("bpn")) { + fieldComparator = Comparator.comparing(PolicyWithBpn::bpn); + } else if (property.equals("validUntil")) { + fieldComparator = Comparator.comparing(p -> p.policy().getValidUntil()); + } else if (property.equals("policyId")) { + fieldComparator = Comparator.comparing(p -> p.policy().getPolicyId()); + } else if (property.equals("createdOn")) { + fieldComparator = Comparator.comparing(p -> p.policy().getCreatedOn()); + } else if (property.equals("action")) { + fieldComparator = Comparator.comparing(p -> { + final List permissions = p.policy().getPermissions(); + return permissions.isEmpty() ? null : permissions.get(0).getAction(); + }); + } else { + log.warn("Sorting by field '{}' is not supported", order.getProperty()); + throw new IllegalArgumentException("Sorting by this field is not supported"); + } + + if (getSortDirection(pageable, order.getProperty()) == Sort.Direction.DESC) { + fieldComparator = fieldComparator.reversed(); + } + + if (comparator == null) { + comparator = fieldComparator; + } else { + comparator = comparator.thenComparing(fieldComparator); + } - if ("bpn".equals(sortField)) { - comparator = Comparator.comparing(PolicyWithBpn::bpn); - } else if ("validUntil".equals(sortField)) { - comparator = Comparator.comparing(p -> p.policy().getValidUntil()); - } else if ("policyId".equals(sortField)) { - comparator = Comparator.comparing(p -> p.policy().getPolicyId()); - } else if ("createdOn".equals(sortField)) { - comparator = Comparator.comparing(p -> p.policy().getCreatedOn()); - } else if ("action".equals(sortField)) { - comparator = Comparator.comparing(p -> { - final List permissions = p.policy().getPermissions(); - return permissions.isEmpty() ? null : permissions.get(0).getAction(); - }); - } else { - throw new IllegalArgumentException("Sorting by this field is not supported"); } - if (!isFieldSortedAscending(pageable, sortField)) { - comparator = comparator.reversed(); + if (comparator == null) { + comparator = Comparator.comparing(PolicyWithBpn::bpn); } return comparator; @@ -129,18 +141,20 @@ private static String getSortField(final Pageable pageable) { return sortField; } - public boolean isFieldSortedAscending(final Pageable pageable, final String fieldName) { + public Sort.Direction getSortDirection(final Pageable pageable, final String fieldName) { if (pageable.getSort().isUnsorted()) { - return true; + return Sort.Direction.ASC; } final Sort sort = pageable.getSort(); for (final Sort.Order order : sort) { - if (order.getProperty().equals(fieldName) && order.getDirection() == Sort.Direction.ASC) { - return true; + if (order.getProperty().equals(fieldName)) { + return order.getDirection(); } } - return false; + + log.warn("Sort field '{}' not found", fieldName); + throw new IllegalArgumentException("Property not found"); } } diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingServiceTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingServiceTest.java index cd4769e86..c03c37eda 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingServiceTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingServiceTest.java @@ -53,23 +53,26 @@ void setUp() { } private final Map> policiesMap = Map.of( // - "BPN1", Arrays.asList( // - createPolicy("policy-1"), // - createPolicy("policy-4") // - ), // "BPN2", Arrays.asList( // - createPolicy("policy-2"), // createPolicy("policy-3"), // + createPolicy("policy-2"), // createPolicy("policy-5") // - )); + ), "BPN1", Arrays.asList( // + createPolicy("policy-4"), // + createPolicy("policy-1") // + ) // + ); @Test public void whenUnsorted_shouldUseDefaultSortingBpnAscending() { final Page result = testee.getPolicies(policiesMap, PageRequest.of(0, 10)); - assertThat(result.getContent().stream().map(PolicyWithBpn::bpn).toList()).containsExactly("BPN1", "BPN1", - "BPN2", "BPN2", "BPN2"); + assertThat(result.getContent().stream().map(p -> p.policy().getPolicyId()).toList()).containsExactly( + // BPN1 + "policy-4", "policy-1", + // BPN2 + "policy-3", "policy-2", "policy-5"); } @Test @@ -78,8 +81,11 @@ public void whenSortedByBpnAsc() { final Page result = testee.getPolicies(policiesMap, PageRequest.of(0, 10, Sort.by("bpn").ascending())); - assertThat(result.getContent().stream().map(PolicyWithBpn::bpn).toList()).containsExactly("BPN1", "BPN1", - "BPN2", "BPN2", "BPN2"); + assertThat(result.getContent().stream().map(p -> p.policy().getPolicyId()).toList()).containsExactly( + // BPN1 + "policy-4", "policy-1", + // BPN2 + "policy-3", "policy-2", "policy-5"); } @Test @@ -92,6 +98,32 @@ public void whenSortedByBpnDesc() { "BPN2", "BPN1", "BPN1"); } + @Test + public void whenSortedByBpnAscAndPolicyIdDesc() { + + final Page result = testee.getPolicies(policiesMap, + PageRequest.of(0, 10, Sort.by("bpn").ascending().and(Sort.by("policyId").descending()))); + + assertThat(result.getContent().stream().map(p -> p.policy().getPolicyId()).toList()).containsExactly( + // BPN1 + "policy-4", "policy-1", + // BPN2 + "policy-5", "policy-3", "policy-2"); + } + + @Test + public void whenSortedByBpnDescAndPolicyIdAsc() { + + final Page result = testee.getPolicies(policiesMap, + PageRequest.of(0, 10, Sort.by("bpn").descending().and(Sort.by("policyId").ascending()))); + + assertThat(result.getContent().stream().map(p -> p.policy().getPolicyId()).toList()).containsExactly( + // BPN2 + "policy-2", "policy-3", "policy-5", + // BPN1 + "policy-1", "policy-4"); + } + @Test public void whenRequestedPageIsAvailable_thenCorrectPageIsReturned() {