LoanDescriptor implements Descriptor<RecommendedLoan, LoanDescriptor, Loan> { @Override public Optional<RecommendedLoan> recommend(final Money toInvest) { final Money remaining = loan.getNonReservedRemainingInvestment(); if (toInvest.compareTo(remaining) <= 0) { return Optional.of(new RecommendedLoan(this, toInvest)); } else { LOGGER.warn("Can not recommend {} with {} remaining in loan #{}.", toInvest, remaining, loan.getId()); return Optional.empty(); } } LoanDescriptor(final Loan loan); @Override String toString(); @Override boolean equals(final Object o); @Override int hashCode(); @Override Loan item(); @Override Loan related(); @Override Optional<RecommendedLoan> recommend(final Money toInvest);  }
@Test void recommendAmount() { final LoanImpl mockedLoan = LoanDescriptorTest.mockLoan(); final LoanDescriptor ld = new LoanDescriptor(mockedLoan); final Optional<RecommendedLoan> r = ld.recommend(Money.from(200)); assertThat(r).isPresent(); final RecommendedLoan recommendation = r.get(); assertSoftly(softly -> { softly.assertThat(recommendation.descriptor()) .isSameAs(ld); softly.assertThat(recommendation.amount()) .isEqualTo(Money.from(200)); }); }
@Test void recommendWrongAmount() { final LoanImpl mockedLoan = LoanDescriptorTest.mockLoan(); final LoanDescriptor ld = new LoanDescriptor(mockedLoan); final Optional<RecommendedLoan> r = ld.recommend(mockedLoan.getNonReservedRemainingInvestment() .add(1)); assertThat(r).isEmpty(); }