Skip to content

Commit

Permalink
feat(policy-api):[eclipse-tractusx#639] Implement multi-sort
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Jul 3, 2024
1 parent da6a7a6 commit fa980bc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,51 @@ private PageImpl<PolicyWithBpn> applyPaging(final Pageable pageable, final List<
final int end = Math.min((pageable.getPageNumber() + 1) * pageable.getPageSize(), policies.size());
final List<PolicyWithBpn> 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<PolicyWithBpn> getComparator(final Pageable pageable) {

Comparator<PolicyWithBpn> comparator;
Comparator<PolicyWithBpn> comparator = null;

final String sortField = getSortField(pageable);
final List<Sort.Order> sort = pageable.getSort().stream().toList();
for (final Sort.Order order : sort) {

Comparator<PolicyWithBpn> 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<Permission> 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<Permission> 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;
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,26 @@ void setUp() {
}

private final Map<String, List<Policy>> 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<PolicyWithBpn> 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
Expand All @@ -78,8 +81,11 @@ public void whenSortedByBpnAsc() {
final Page<PolicyWithBpn> 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
Expand All @@ -92,6 +98,32 @@ public void whenSortedByBpnDesc() {
"BPN2", "BPN1", "BPN1");
}

@Test
public void whenSortedByBpnAscAndPolicyIdDesc() {

final Page<PolicyWithBpn> 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<PolicyWithBpn> 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() {

Expand Down

0 comments on commit fa980bc

Please sign in to comment.