SecondaryMarketplaceAccessor extends AbstractMarketplaceAccessor<ParticipationDescriptor> { @Override public Collection<ParticipationDescriptor> getMarketplace() { var cache = SoldParticipationCache.forTenant(tenant); Stream<Participation> participations = tenant .call(zonky -> zonky.getAvailableParticipations(getIncrementalFilter())) .filter(p -> { final int loanId = p.getLoanId(); if (cache.wasOnceSold(p.getInvestmentId())) { LOGGER.debug("Loan #{} already sold before, ignoring.", loanId); return false; } else { return true; } }); if (getMaximumItemsToRead().isPresent()) { int limit = getMaximumItemsToRead().orElseThrow(); LOGGER.trace("Enforcing read limit of {} latest items.", limit); participations = participations.limit(limit); } return participations .map(p -> { var loanId = p.getLoanId(); return new ParticipationDescriptor(p, () -> tenant.getLoan(loanId), () -> tenant.call(a -> a.getParticipationDetail(loanId))); }) .collect(Collectors.toList()); } SecondaryMarketplaceAccessor(final PowerTenant tenant,
            final UnaryOperator<LastPublishedItem> stateAccessor); @Override Duration getForcedMarketplaceCheckInterval(); @Override Collection<ParticipationDescriptor> getMarketplace(); @Override boolean hasUpdates();  }
@Test void readsMarketplace() { final Loan l = new MockLoanBuilder().build(); int loanId = l.getId(); final Participation p = mock(ParticipationImpl.class); when(p.getId()).thenReturn(1l); when(p.getLoanId()).thenReturn(loanId); when(p.getLoanHealthInfo()).thenReturn(LoanHealth.HEALTHY); final Zonky zonky = harmlessZonky(); when(zonky.getLoan(eq(loanId))).thenReturn(l); when(zonky.getAvailableParticipations(any())).thenReturn(Stream.of(p)); final PowerTenant tenant = mockTenant(zonky); final AbstractMarketplaceAccessor<ParticipationDescriptor> d = new SecondaryMarketplaceAccessor(tenant, UnaryOperator.identity()); final Collection<ParticipationDescriptor> pd = d.getMarketplace(); assertThat(pd).hasSize(1) .element(0) .extracting(ParticipationDescriptor::item) .isSameAs(p); assertThat(pd) .element(0) .extracting(ParticipationDescriptor::related) .isSameAs(l); }