NaturalLanguageReservationStrategy implements ReservationStrategy { @Override public Stream<RecommendedReservation> recommend(final Collection<ReservationDescriptor> 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("Reservation #{} skipped due to an undesirable rating.", d.item() .getId()); } return isAcceptable; }); return strategy.getApplicableReservations(withoutUndesirable, portfolio) .sorted(preferences.getReservationComparator()) .flatMap(d -> { final Money amount = d.item() .getMyReservation() .getReservedAmount(); return d.recommend(amount) .stream(); }); } NaturalLanguageReservationStrategy(final ParsedStrategy p); @Override ReservationMode getMode(); @Override Stream<RecommendedReservation> recommend(final Collection<ReservationDescriptor> 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, Collections.emptyList(), Collections.emptyMap(), Collections.emptyMap()); final ReservationStrategy s = new NaturalLanguageReservationStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize()); final Stream<RecommendedReservation> result = s.recommend( singletonList(new ReservationDescriptor(mockReservation(200), () -> null)), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); }
@Test void noReservationsApplicable() { final MarketplaceFilter filter = MarketplaceFilter.of(MarketplaceFilterCondition.alwaysAccepting()); final ParsedStrategy p = new ParsedStrategy(DefaultPortfolio.PROGRESSIVE, Collections.singleton(filter)); final ReservationStrategy s = new NaturalLanguageReservationStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getShareOnInvestment(any())).thenReturn(Ratio.ZERO); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize() .subtract(1)); final Stream<RecommendedReservation> result = s.recommend( singletonList(new ReservationDescriptor(mockReservation(200), () -> null)), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); }
@Test void nothingRecommendedDueToRatingOverinvested() { final ParsedStrategy p = new ParsedStrategy(DefaultPortfolio.EMPTY, Collections.emptySet()); final ReservationStrategy s = new NaturalLanguageReservationStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize() .subtract(1)); when(portfolio.getShareOnInvestment(any())).thenReturn(Ratio.ZERO); final Stream<RecommendedReservation> result = s.recommend( singletonList(new ReservationDescriptor(mockReservation(200), () -> null)), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); assertThatThrownBy(s::getMode).isInstanceOf(IllegalStateException.class); }
@Test void recommendationIsMade() { final ParsedStrategy p = new ParsedStrategy(DefaultPortfolio.PROGRESSIVE, Collections.emptySet()); final ReservationStrategy s = new NaturalLanguageReservationStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); when(portfolio.getInvested()).thenReturn(p.getMaximumInvestmentSize() .subtract(1)); when(portfolio.getShareOnInvestment(any())).thenReturn(Ratio.ZERO); final Reservation l = mockReservation(200); final ReservationDescriptor ld = new ReservationDescriptor(l, () -> null); final List<RecommendedReservation> result = s .recommend(Collections.singleton(ld), portfolio, mockSessionInfo()) .collect(Collectors.toList()); assertThat(result).hasSize(1); final RecommendedReservation r = result.get(0); assertSoftly(softly -> { softly.assertThat(r.descriptor()) .isEqualTo(ld); softly.assertThat(r.amount()) .isEqualTo(Money.from(200)); }); }