public Either<InvestmentFailure, BigDecimal> invest(final RecommendedLoan r, final boolean alreadySeenBefore) { final boolean confirmationRequired = r.isConfirmationRequired(); if (alreadySeenBefore) { LOGGER.debug("Loan seen before."); final boolean protectedByCaptcha = r.descriptor().getLoanCaptchaProtectionEndDateTime() .map(date -> DateUtil.offsetNow().isBefore(date)) .orElse(false); if (!protectedByCaptcha && !confirmationRequired) { /* * investment is no longer protected by CAPTCHA and no confirmation is required. therefore we invest. */ return this.investLocallyFailingOnCaptcha(r); } else { /* * protected by captcha or confirmation required. yet already seen from a previous investment session. * this must mean that the previous response was DELEGATED and the user did not respond in the * meantime. we therefore keep the investment as delegated. */ return Either.left(InvestmentFailure.SEEN_BEFORE); } } else if (confirmationRequired) { if (this.provider == null) { throw new IllegalStateException("Confirmation required but no confirmation provider specified."); } else { return this.delegateOrReject(r); } } else { return this.investOrDelegateOnCaptcha(r); } }
public Either<InvestmentFailure, BigDecimal> invest(final RecommendedLoan r, final boolean alreadySeenBefore) { final boolean confirmationRequired = r.isConfirmationRequired(); if (alreadySeenBefore) { LOGGER.debug("Loan seen before."); final boolean protectedByCaptcha = r.descriptor().getLoanCaptchaProtectionEndDateTime() .map(date -> DateUtil.offsetNow().isBefore(date)) .orElse(false); if (!protectedByCaptcha && !confirmationRequired) { /* * investment is no longer protected by CAPTCHA and no confirmation is required. therefore we invest. */ return this.investLocallyFailingOnCaptcha(r); } else { /* * protected by captcha or confirmation required. yet already seen from a previous investment session. * this must mean that the previous response was DELEGATED and the user did not respond in the * meantime. we therefore keep the investment as delegated. */ return Either.left(InvestmentFailure.SEEN_BEFORE); } } else if (confirmationRequired) { if (this.provider == null) { throw new IllegalStateException("Confirmation required but no confirmation provider specified."); } else { return this.delegateOrReject(r); } } else { return this.investOrDelegateOnCaptcha(r); } }
case DELEGATED: tenant.fire(investmentDelegated(recommendation, providerId)); if (recommendation.isConfirmationRequired()) {
case DELEGATED: tenant.fire(investmentDelegated(recommendation, providerId)); if (recommendation.isConfirmationRequired()) {
@Test void recommendAmount() { final Loan mockedLoan = LoanDescriptorTest.mockLoan(); final LoanDescriptor ld = new LoanDescriptor(mockedLoan); final Optional<RecommendedLoan> r = ld.recommend(200); assertThat(r).isPresent(); final RecommendedLoan recommendation = r.get(); assertSoftly(softly -> { softly.assertThat(recommendation.descriptor()).isSameAs(ld); softly.assertThat(recommendation.amount()).isEqualTo(BigDecimal.valueOf(200)); softly.assertThat(recommendation.isConfirmationRequired()).isFalse(); }); }
@Test void constructor() { final LoanDescriptor ld = RecommendedLoanTest.mockLoanDescriptor(); final int amount = 200; final RecommendedLoan r = new RecommendedLoan(ld, amount, true); SoftAssertions.assertSoftly(softly -> { softly.assertThat(r.descriptor()).isSameAs(ld); softly.assertThat(r.amount()).isEqualTo(BigDecimal.valueOf(amount)); softly.assertThat(r.isConfirmationRequired()).isTrue(); }); }