NaturalLanguagePurchaseStrategy implements PurchaseStrategy { @Override public Stream<RecommendedParticipation> recommend(final Collection<ParticipationDescriptor> available, final PortfolioOverview portfolio, final SessionInfo sessionInfo) { if (!Util.isAcceptable(strategy, portfolio)) { return Stream.empty(); } var preferences = Preferences.get(strategy, portfolio); var withoutUndesirable = available.parallelStream() .peek(d -> LOGGER.trace("Evaluating {}.", d.item())) .filter(d -> { boolean isAcceptable = preferences.getDesirableRatings() .contains(d.item() .getRating()); if (!isAcceptable) { LOGGER.debug("Participation #{} skipped due to an undesirable rating.", d.item() .getId()); } return isAcceptable; }); return strategy.getApplicableParticipations(withoutUndesirable, portfolio) .filter(d -> sizeMatchesStrategy(d.item())) .sorted(preferences.getSecondaryMarketplaceComparator()) .flatMap(d -> d.recommend() .stream()); } NaturalLanguagePurchaseStrategy(final ParsedStrategy p); @Override Stream<RecommendedParticipation> recommend(final Collection<ParticipationDescriptor> available,
            final PortfolioOverview portfolio, final SessionInfo sessionInfo);  }
@Test void unacceptablePortfolioDueToOverInvestment() { final DefaultValues v = new DefaultValues(DefaultPortfolio.EMPTY); v.setTargetPortfolioSize(1000); final ParsedStrategy p = new ParsedStrategy(v); final PurchaseStrategy s = new NaturalLanguagePurchaseStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize()); final Stream<RecommendedParticipation> result = s.recommend(Collections.singletonList(mockDescriptor()), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); }
@Test void noLoansApplicable() { final MarketplaceFilter filter = MarketplaceFilter.of(MarketplaceFilterCondition.alwaysAccepting()); final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE); final FilterSupplier w = new FilterSupplier(v, Collections.emptySet(), Collections.singleton(filter)); final ParsedStrategy p = new ParsedStrategy(v, Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), w); final PurchaseStrategy s = new NaturalLanguagePurchaseStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getShareOnInvestment(any())).thenReturn(Ratio.ZERO); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize() .subtract(1)); final Stream<RecommendedParticipation> result = s.recommend(Collections.singletonList(mockDescriptor()), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); }
@Test void nothingRecommendedDueToRatingOverinvested() { final ParsedStrategy p = new ParsedStrategy(DefaultPortfolio.EMPTY); final PurchaseStrategy s = new NaturalLanguagePurchaseStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize() .subtract(1)); when(portfolio.getShareOnInvestment(any())).thenReturn(Ratio.ZERO); final Participation l = mockParticipation(); doReturn(Rating.A).when(l) .getRating(); final Stream<RecommendedParticipation> result = s.recommend(Collections.singletonList(mockDescriptor(l)), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); }
@Test void recommendationIsMade() { final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE); final ParsedStrategy p = new ParsedStrategy(v, Collections.emptyList(), Collections.emptyMap(), Collections.emptyMap(), new FilterSupplier(v, null, Collections.emptySet())); final PurchaseStrategy s = new NaturalLanguagePurchaseStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize() .subtract(1)); when(portfolio.getShareOnInvestment(any())).thenReturn(Ratio.ZERO); final Participation participation = mockParticipation(); doReturn(Money.from(100_000)).when(participation) .getRemainingPrincipal(); doReturn(Rating.A).when(participation) .getRating(); final Participation p2 = mockParticipation(); final int amount = 199; doReturn(Money.from(amount)).when(p2) .getRemainingPrincipal(); doReturn(Rating.A).when(p2) .getRating(); final ParticipationDescriptor pd = mockDescriptor(p2); final List<RecommendedParticipation> result = s .recommend(asList(mockDescriptor(participation), pd), portfolio, mockSessionInfo()) .collect(Collectors.toList()); assertThat(result).hasSize(1); final RecommendedParticipation r = result.get(0); assertSoftly(softly -> { softly.assertThat(r.descriptor()) .isEqualTo(pd); softly.assertThat(r.amount()) .isEqualTo(pd.item() .getRemainingPrincipal()); }); }