Skip to content

Commit

Permalink
adjust sorting to be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbranham committed Oct 30, 2024
1 parent ea9e7cd commit a6bdd10
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
8 changes: 7 additions & 1 deletion module-audited-entity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<dependencies>


<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nexus-security-module</artifactId>
Expand All @@ -58,6 +58,12 @@

<!-- TESTS -->

<dependency>
<groupId>org.apache.causeway.testing</groupId>
<artifactId>causeway-testing-unittestsupport-applib</artifactId>
<scope>test</scope>
</dependency>


<!-- IDE support (optional) -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@ public class AuditedEntitySortedByDateDesc implements Comparator<AuditedEntity>

@Override
public int compare(AuditedEntity o1, AuditedEntity o2) {
if (o1.getCreatedDate() == null || o2.getCreatedDate() == null) {

// sort with nulls last
if (o1.getCreatedDate() == null && o2.getCreatedDate() == null) {
return 0;
}

if (o1.getCreatedDate() == null) {
return 1;
}

if (o2.getCreatedDate() == null) {
return -1;
}

return o2.getCreatedDate().compareTo(o1.getCreatedDate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package net.savantly.nexus.audited.api;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.ZonedDateTime;

import org.junit.jupiter.api.Test;

public class AuditedEntitySortedByDateDesc_test {

@Test
public void testCompare() {
// given
AuditedEntitySortedByDateDesc comparator = new AuditedEntitySortedByDateDesc();
AuditedEntity o1 = new MockAuditedEntity();
AuditedEntity o2 = new MockAuditedEntity();
o1.setCreatedDate(ZonedDateTime.now().minusDays(1));
o2.setCreatedDate(ZonedDateTime.now());

// when
int result = comparator.compare(o1, o2);

// then
assertThat(result).isPositive();

}

@Test
public void testCompareWithNulls() {
// given
AuditedEntitySortedByDateDesc comparator = new AuditedEntitySortedByDateDesc();
AuditedEntity o1 = new MockAuditedEntity();
AuditedEntity o2 = new MockAuditedEntity();
o1.setCreatedDate(ZonedDateTime.now().minusDays(1));
o2.setCreatedDate(null);

// when
int result = comparator.compare(o1, o2);

// then
assertThat(result).isNegative();

}

@Test
public void testCompareWithNullsBoth() {
// given
AuditedEntitySortedByDateDesc comparator = new AuditedEntitySortedByDateDesc();
AuditedEntity o1 = new MockAuditedEntity();
AuditedEntity o2 = new MockAuditedEntity();
o1.setCreatedDate(null);
o2.setCreatedDate(null);

// when
int result = comparator.compare(o1, o2);

// then
assertThat(result).isZero();

}

@Test
public void testCompareWithNullsFirst() {
// given
AuditedEntitySortedByDateDesc comparator = new AuditedEntitySortedByDateDesc();
AuditedEntity o1 = new MockAuditedEntity();
AuditedEntity o2 = new MockAuditedEntity();
o1.setCreatedDate(null);
o2.setCreatedDate(ZonedDateTime.now().minusDays(1));

// when
int result = comparator.compare(o1, o2);

// then
assertThat(result).isPositive();

}

static class MockAuditedEntity extends AuditedEntity {
}
}

0 comments on commit a6bdd10

Please sign in to comment.