PrimaryMarketplaceComparator implements Comparator<LoanDescriptor> { @Override public int compare(final LoanDescriptor loan1, final LoanDescriptor loan2) { return comparator.compare(loan1.item(), loan2.item()); } PrimaryMarketplaceComparator(Comparator<Rating> ratingByDemandComparator); @Override int compare(final LoanDescriptor loan1, final LoanDescriptor loan2);  }
@Test void sortByRating() { final OffsetDateTime first = OffsetDateTime.ofInstant(Instant.EPOCH, Defaults.ZONE_ID); final OffsetDateTime second = first.plus(Duration.ofMillis(1)); final LoanImpl l1 = mockLoan(Rating.D, 100000, first); final LoanImpl l2 = mockLoan(Rating.A, l1.getNonReservedRemainingInvestment() .getValue() .intValue(), second); final LoanDescriptor ld1 = new LoanDescriptor(l1), ld2 = new LoanDescriptor(l2); assertSoftly(softly -> { softly.assertThat(c.compare(ld1, ld2)) .isGreaterThan(0); softly.assertThat(c.compare(ld2, ld1)) .isLessThan(0); softly.assertThat(c.compare(ld1, ld1)) .isEqualTo(0); }); }
@Test void sortByRecencyIfSameRating() { final OffsetDateTime first = OffsetDateTime.ofInstant(Instant.EPOCH, Defaults.ZONE_ID); final OffsetDateTime second = first.plus(Duration.ofMillis(1)); final LoanImpl l1 = mockLoan(Rating.A, 100000, first); final LoanImpl l2 = mockLoan(Rating.A, l1.getNonReservedRemainingInvestment() .getValue() .intValue(), second); final LoanDescriptor ld1 = new LoanDescriptor(l1), ld2 = new LoanDescriptor(l2); assertSoftly(softly -> { softly.assertThat(c.compare(ld1, ld2)) .isGreaterThan(0); softly.assertThat(c.compare(ld2, ld1)) .isLessThan(0); softly.assertThat(c.compare(ld1, ld1)) .isEqualTo(0); }); }
@Test void sortByRemainingIfAsRecent() { final OffsetDateTime first = OffsetDateTime.ofInstant(Instant.EPOCH, Defaults.ZONE_ID); final LoanImpl l1 = mockLoan(Rating.A, 100000, first); final LoanImpl l2 = mockLoan(Rating.A, l1.getNonReservedRemainingInvestment() .getValue() .intValue() + 1, l1.getDatePublished()); final LoanDescriptor ld1 = new LoanDescriptor(l1), ld2 = new LoanDescriptor(l2); assertSoftly(softly -> { softly.assertThat(c.compare(ld1, ld2)) .isGreaterThan(0); softly.assertThat(c.compare(ld2, ld1)) .isLessThan(0); }); }