PrimaryMarketplaceAccessor extends AbstractMarketplaceAccessor<LoanDescriptor> { @Override public boolean hasUpdates() { try { final LastPublishedItem current = tenant.call(Zonky::getLastPublishedLoanInfo); final LastPublishedItem previous = stateAccessor.apply(current); LOGGER.trace("Current is {}, previous is {}.", current, previous); return !Objects.equals(previous, current); } catch (final Exception ex) { LOGGER.debug("Zonky primary marketplace status endpoint failed, forcing live marketplace check.", ex); return true; } } PrimaryMarketplaceAccessor(final Tenant tenant, final UnaryOperator<LastPublishedItem> stateAccessor); @Override Duration getForcedMarketplaceCheckInterval(); @Override Collection<LoanDescriptor> getMarketplace(); @Override boolean hasUpdates();  }
@Test void detectsUpdates() { final Zonky z = harmlessZonky(); when(z.getLastPublishedLoanInfo()).thenReturn(mock(LastPublishedItemImpl.class)); final Tenant t = mockTenant(z); final AtomicReference<LastPublishedItem> state = new AtomicReference<>(null); final AbstractMarketplaceAccessor<LoanDescriptor> a = new PrimaryMarketplaceAccessor(t, state::getAndSet); assertThat(a.hasUpdates()).isTrue(); assertThat(a.hasUpdates()).isFalse(); }
@Test void failsDetection() { final Zonky z = harmlessZonky(); when(z.getLastPublishedLoanInfo()).thenThrow(IllegalStateException.class); final Tenant t = mockTenant(z); final AtomicReference<LastPublishedItem> state = new AtomicReference<>(null); final AbstractMarketplaceAccessor<LoanDescriptor> a = new PrimaryMarketplaceAccessor(t, state::getAndSet); assertThat(a.hasUpdates()).isTrue(); assertThat(a.hasUpdates()).isTrue(); }