NaturalLanguageSellStrategy implements SellStrategy { @Override public Stream<RecommendedInvestment> recommend(final Collection<InvestmentDescriptor> available, final PortfolioOverview portfolio, final SessionInfo sessionInfo) { return strategy.getSellingMode() .map(mode -> { switch (mode) { case SELL_FILTERS: return strategy.getMatchingSellFilters(available.stream(), portfolio); case FREE_AND_OUTSIDE_STRATEGY: return getFreeAndOutsideStrategy(available, portfolio); case FREE_UNDISCOUNTED_AND_OUTSIDE_STRATEGY: return getFreeAndOutsideStrategy(available, portfolio) .filter(NaturalLanguageSellStrategy::isUndiscounted); default: throw new IllegalStateException("Impossible."); } }) .orElse(Stream.empty()) .map(InvestmentDescriptor::recommend) .flatMap(Optional::stream); } NaturalLanguageSellStrategy(final ParsedStrategy p); @Override Stream<RecommendedInvestment> recommend(final Collection<InvestmentDescriptor> available,
            final PortfolioOverview portfolio, final SessionInfo sessionInfo);  }
@Test void noLoansApplicable() { final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE); v.setSellingMode(SellingMode.SELL_FILTERS); final ParsedStrategy p = spy(new ParsedStrategy(v)); doReturn(Stream.empty()).when(p) .getMatchingSellFilters(any(), any()); final SellStrategy s = new NaturalLanguageSellStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); final Stream<RecommendedInvestment> result = s.recommend(Collections.singletonList(mockDescriptor()), portfolio, mockSessionInfo()); assertThat(result).isEmpty(); }
@Test void someLoansApplicable() { final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE); v.setSellingMode(SellingMode.SELL_FILTERS); final ParsedStrategy p = spy(new ParsedStrategy(v)); doAnswer(e -> e.getArgument(0)).when(p) .getMatchingSellFilters(any(), any()); final SellStrategy s = new NaturalLanguageSellStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); final Stream<RecommendedInvestment> result = s.recommend(Collections.singletonList(mockDescriptor()), portfolio, mockSessionInfo()); assertThat(result).hasSize(1); }
@Test void feeBasedInvestmentsNotApplicableInSelloffStrategy() { final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE); v.setSellingMode(SellingMode.FREE_AND_OUTSIDE_STRATEGY); final ParsedStrategy p = spy(new ParsedStrategy(v)); doAnswer(e -> e.getArgument(0)).when(p) .getMatchingPrimaryMarketplaceFilters(any(), any()); final SellStrategy s = new NaturalLanguageSellStrategy(p); final PortfolioOverview portfolio = mock(PortfolioOverview.class); final Investment i1 = mockInvestment(); final Investment i2 = mockInvestment(BigDecimal.ZERO); final Stream<RecommendedInvestment> result = s.recommend(asList(mockDescriptor(i1), mockDescriptor(i2)), portfolio, mockSessionInfo()); assertThat(result).extracting(d -> d.descriptor() .item()) .containsOnly(i2); }