DelinquencyNotificationPayload implements TenantPayload { @Override public void accept(final Tenant tenant) { PowerTenant powerTenant = (PowerTenant) tenant; SessionEvents sessionEvents = Events.forSession(powerTenant); boolean shouldTrigger = force || Stream.of(LoanDefaultedEvent.class, LoanDelinquent10DaysOrMoreEvent.class, LoanDelinquent30DaysOrMoreEvent.class, LoanDelinquent60DaysOrMoreEvent.class, LoanDelinquent90DaysOrMoreEvent.class, LoanNowDelinquentEvent.class, LoanNoLongerDelinquentEvent.class) .anyMatch(sessionEvents::isListenerRegistered); if (!shouldTrigger) { LOGGER.debug("Skipping on account of no event listener being configured to receive the results."); return; } powerTenant.inTransaction(this::process); } DelinquencyNotificationPayload();   DelinquencyNotificationPayload(final Function<Tenant, Registry> registryFunction, final boolean force); @Override void accept(final Tenant tenant);  }
@Test void initializesWithoutTriggeringEvents() { final Investment i1 = getDelinquentInvestment(1); final Investment i10 = getDelinquentInvestment(10); final Investment i30 = getDelinquentInvestment(30); final Investment i60 = getDelinquentInvestment(60); final Investment i90 = getDelinquentInvestment(90); final Investment defaulted = getDefaultedInvestment(); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1, i10, i30, i60, i90, defaulted)); payload.accept(tenant); assertSoftly(softly -> { softly.assertThat(r.getCategories(i1)) .containsExactly(Category.NEW); softly.assertThat(r.getCategories(i10)) .containsExactly(Category.NEW, Category.MILD); softly.assertThat(r.getCategories(i30)) .containsExactly(Category.NEW, Category.MILD, Category.SEVERE); softly.assertThat(r.getCategories(i60)) .containsExactly(Category.NEW, Category.MILD, Category.SEVERE, Category.CRITICAL); softly.assertThat(r.getCategories(i90)) .containsExactly(Category.NEW, Category.MILD, Category.SEVERE, Category.CRITICAL, Category.HOPELESS); softly.assertThat(r.getCategories(defaulted)) .containsExactly(Category.NEW, Category.MILD, Category.SEVERE, Category.CRITICAL, Category.HOPELESS, Category.DEFAULTED); softly.assertThat(getEventsRequested()) .isEmpty(); }); }
@Test void triggersEventsOnNewDelinquents() { final Investment i1 = getDelinquentInvestment(1); when(zonky.getInvestment(eq(i1.getId()))).thenReturn(i1); final Investment i10 = getDelinquentInvestment(10); when(zonky.getInvestment(eq(i10.getId()))).thenReturn(i10); final Investment i30 = getDelinquentInvestment(30); when(zonky.getInvestment(eq(i30.getId()))).thenReturn(i30); final Investment i60 = getDelinquentInvestment(60); when(zonky.getInvestment(eq(i60.getId()))).thenReturn(i60); final Investment i90 = getDelinquentInvestment(90); when(zonky.getInvestment(eq(i90.getId()))).thenReturn(i90); final Investment defaulted = getDefaultedInvestment(); when(zonky.getInvestment(eq(defaulted.getId()))).thenReturn(defaulted); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1)); payload.accept(tenant); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i10, i30, i60, i90, defaulted)); final Loan fresh = MockLoanBuilder.fresh(); when(zonky.getLoan(anyInt())).thenReturn(fresh); payload.accept(tenant); assertThat(getEventsRequested()) .extracting(e -> (Object) e.getClass() .getInterfaces()[0]) .containsOnly(LoanNoLongerDelinquentEvent.class, LoanDefaultedEvent.class, LoanDelinquent10DaysOrMoreEvent.class, LoanDelinquent30DaysOrMoreEvent.class, LoanDelinquent60DaysOrMoreEvent.class, LoanDelinquent90DaysOrMoreEvent.class); }
@Test void handlesRegularHealing() { final Investment i1 = getDelinquentInvestment(1); when(zonky.getInvestment(eq(i1.getId()))).thenReturn(i1); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1)); payload.accept(tenant); readPreexistingEvents(); final Investment i2 = getDelinquentInvestment(1); when(zonky.getInvestment(eq(i2.getId()))).thenReturn(i2); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1, i2)); final Loan fresh = MockLoanBuilder.fresh(); when(zonky.getLoan(anyInt())).thenReturn(fresh); payload.accept(tenant); assertThat(getEventsRequested()).hasSize(1) .extracting(e -> (Object) e.getClass() .getInterfaces()[0]) .containsOnly(LoanNowDelinquentEvent.class); readPreexistingEvents(); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i2)); payload.accept(tenant); assertThat(getEventsRequested()).hasSize(1) .first() .isInstanceOf(LoanNoLongerDelinquentEvent.class); }
@Test void handlesLoss() { final Investment i1 = getDefaultedInvestment(); when(zonky.getInvestment(eq(i1.getId()))).thenReturn(i1); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1)); payload.accept(tenant); final Investment i2 = getDefaultedInvestment(); when(zonky.getInvestment(eq(i2.getId()))).thenReturn(i2); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1, i2)); final Loan fresh = MockLoanBuilder.fresh(); when(zonky.getLoan(anyInt())).thenReturn(fresh); payload.accept(tenant); assertThat(getEventsRequested()).hasSize(1) .extracting(e -> (Object) e.getClass() .getInterfaces()[0]) .containsOnly(LoanDefaultedEvent.class); readPreexistingEvents(); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1)); payload.accept(tenant); assertThat(getEventsRequested()).hasSize(1) .first() .isInstanceOf(LoanLostEvent.class); }
@Test void handlesRepayment() { final Investment i1 = getDelinquentInvestment(1); when(zonky.getInvestment(eq(i1.getId()))).thenReturn(i1); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1)); payload.accept(tenant); final InvestmentImpl i2 = getDelinquentInvestment(1); when(zonky.getInvestment(eq(i2.getId()))).thenReturn(i2); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1, i2)); final Loan fresh = MockLoanBuilder.fresh(); when(zonky.getLoan(anyInt())).thenReturn(fresh); payload.accept(tenant); assertThat(getEventsRequested()) .extracting(e -> (Object) e.getClass() .getInterfaces()[0]) .containsExactly(LoanNowDelinquentEvent.class); readPreexistingEvents(); i2.setPrincipal(new AmountsImpl(Money.ZERO)); when(zonky.getDelinquentInvestments()).thenReturn(Stream.of(i1)); payload.accept(tenant); assertThat(getEventsRequested()).isEmpty(); }