@Override public DefaultOverdueState calculateOverdueState(final BillingState billingState, final LocalDate now) throws OverdueApiException { for (final DefaultOverdueState overdueState : getStates()) { if (overdueState.getCondition() != null && overdueState.getCondition().evaluate(billingState, now)) { return overdueState; } } return getClearState(); }
public Duration getTimeOffset() { if (timeSinceEarliestUnpaidInvoiceEqualsOrExceeds != null) { return timeSinceEarliestUnpaidInvoiceEqualsOrExceeds; } else { return new DefaultDuration().setUnit(TimeUnit.DAYS).setNumber(0); // zero time } } }
@Override public Period getInitialReevaluationInterval() { if (initialReevaluationInterval == null || initialReevaluationInterval.getUnit() == TimeUnit.UNLIMITED || initialReevaluationInterval.getNumber() == 0) { return null; } return initialReevaluationInterval.toJodaPeriod(); }
@Override public boolean evaluate(final BillingState state, final LocalDate date) { LocalDate unpaidInvoiceTriggerDate = null; if (timeSinceEarliestUnpaidInvoiceEqualsOrExceeds != null && state.getDateOfEarliestUnpaidInvoice() != null) { // no date => no unpaid invoices unpaidInvoiceTriggerDate = state.getDateOfEarliestUnpaidInvoice().plus(timeSinceEarliestUnpaidInvoiceEqualsOrExceeds.toJodaPeriod()); } return (numberOfUnpaidInvoicesEqualsOrExceeds == null || state.getNumberOfUnpaidInvoices() >= numberOfUnpaidInvoicesEqualsOrExceeds) && (totalUnpaidInvoiceBalanceEqualsOrExceeds == null || totalUnpaidInvoiceBalanceEqualsOrExceeds.compareTo(state.getBalanceOfUnpaidInvoices()) <= 0) && (timeSinceEarliestUnpaidInvoiceEqualsOrExceeds == null || (unpaidInvoiceTriggerDate != null && !unpaidInvoiceTriggerDate.isAfter(date))) && (responseForLastFailedPayment == null || responseIsIn(state.getResponseForLastFailedPayment(), responseForLastFailedPayment)) && (controlTag == null || isTagIn(controlTag, state.getTags())); }
final InputStream is = new ByteArrayInputStream(xml.getBytes()); final OverdueConfig c = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); Assert.assertEquals(c.getStateSet().size(), 2); Assert.assertNull(c.getStateSet().getStates()[0].getEnterStateEmailNotification()); Assert.assertNotNull(c.getStateSet().getInitialReevaluationInterval()); Assert.assertEquals(c.getStateSet().getInitialReevaluationInterval().getDays(), 1); final EmailNotification secondNotification = c.getStateSet().getStates()[1].getEnterStateEmailNotification(); Assert.assertEquals(secondNotification.getSubject(), "ToTo"); Assert.assertEquals(secondNotification.getTemplateName(), "Titi");
@SuppressWarnings("unchecked") public MockOverdueRules() { final OverdueStatesAccount bundleODS = new OverdueStatesAccount(); bundleODS.setAccountOverdueStates(new DefaultOverdueState[]{new DefaultOverdueState().setName(CLEAR_STATE)}); setOverdueStates(bundleODS); } }
@Override public ValidationErrors validate(final OverdueConfig root, final ValidationErrors errors) { for (final DefaultOverdueState state : getStates()) { state.validate(root, errors); } try { getClearState(); } catch (OverdueApiException e) { if (e.getCode() == ErrorCode.CAT_MISSING_CLEAR_STATE.getCode()) { errors.add("Overdue state set is missing a clear state.", root.getURI(), this.getClass(), ""); } } return errors; }
@Test(groups = "slow") public void testApplicator() throws Exception { final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes()); final OverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); overdueWrapperFactory.setOverdueConfig(config); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(UUID.randomUUID()); final OverdueStateSet overdueStateSet = config.getStateSet(); final OverdueState clearState = config.getStateSet().findState(DefaultBlockingState.CLEAR_STATE_NAME); OverdueState state; state = config.getStateSet().findState("OD1"); applicator.apply(overdueStateSet, null, account, clearState, state, internalCallContext); testOverdueHelper.checkStateApplied(state); checkBussEvent("OD1"); state = config.getStateSet().findState("OD2"); applicator.apply(overdueStateSet, null, account, clearState, state, internalCallContext); testOverdueHelper.checkStateApplied(state); checkBussEvent("OD2"); state = config.getStateSet().findState("OD3"); applicator.apply(overdueStateSet, null, account, clearState, state, internalCallContext); testOverdueHelper.checkStateApplied(state); checkBussEvent("OD3"); }
@SuppressWarnings("unchecked") @Override public OverdueState getOverdueStateFor(final Account overdueable, final TenantContext context) throws OverdueException { try { final String stateName = accessApi.getBlockingStateForService(overdueable.getId(), BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, internalCallContextFactory.createInternalTenantContext(context)).getStateName(); final OverdueStateSet states = overdueConfig.getStateSet(); return states.findState(stateName); } catch (OverdueApiException e) { throw new OverdueException(e, ErrorCode.OVERDUE_CAT_ERROR_ENCOUNTERED, overdueable.getId(), overdueable.getClass().getSimpleName()); } }
@Override public OverdueState findState(final String stateName) throws OverdueApiException { if (stateName.equals(DefaultBlockingState.CLEAR_STATE_NAME)) { return clearState; } for (final DefaultOverdueState state : getStates()) { if (state.getName().equals(stateName)) { return state; } } throw new OverdueApiException(ErrorCode.CAT_NO_SUCH_OVEDUE_STATE, stateName); }
public MockOverdueState(final String name, final boolean blockChanges, final boolean disableEntitlementAndBlockChanges) { setName(name); setBlockChanges(blockChanges); setDisableEntitlement(disableEntitlementAndBlockChanges); } }
@Override public int size() { return getStates().length; }
private OverdueStateSet getOverdueStateSet() { if (config == null || config.getStateSet() == null) { return new DefaultOverdueStateSet() { @SuppressWarnings("unchecked") @Override protected DefaultOverdueState[] getStates() { return new DefaultOverdueState[0]; } @Override public Period getInitialReevaluationInterval() { return null; } }; } else { return config.getStateSet(); } }
@Override public ValidationErrors validate(final OverdueConfig root, final ValidationErrors errors) { return accountOverdueStates.validate(root, errors); }
public MockOverdueState() { setName(MockOverdueRules.CLEAR_STATE); }
@Override public ValidationErrors validate(final OverdueConfig root, final ValidationErrors errors) { if (name.length() > MAX_NAME_LENGTH) { errors.add(new ValidationError(String.format("Name of state '%s' exceeds the maximum length of %d", name, MAX_NAME_LENGTH), root.getURI(), DefaultOverdueState.class, name)); } return errors; }
@Test(groups = "slow") public void testWrapperBasic() throws Exception { final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes()); final OverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); overdueWrapperFactory.setOverdueConfig(config); Account account; OverdueWrapper wrapper; OverdueState state; state = config.getStateSet().findState("OD1"); account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(31)); wrapper = overdueWrapperFactory.createOverdueWrapperFor(account); wrapper.refresh(internalCallContext); testOverdueHelper.checkStateApplied(state); state = config.getStateSet().findState("OD2"); account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(41)); wrapper = overdueWrapperFactory.createOverdueWrapperFor(account); wrapper.refresh(internalCallContext); testOverdueHelper.checkStateApplied(state); state = config.getStateSet().findState("OD3"); account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(51)); wrapper = overdueWrapperFactory.createOverdueWrapperFor(account); wrapper.refresh(internalCallContext); testOverdueHelper.checkStateApplied(state); }
@Override public Period getReevaluationInterval() throws OverdueApiException { if (autoReevaluationInterval == null || autoReevaluationInterval.getUnit() == TimeUnit.UNLIMITED || autoReevaluationInterval.getNumber() == 0) { throw new OverdueApiException(ErrorCode.OVERDUE_NO_REEVALUATION_INTERVAL, name); } return autoReevaluationInterval.toJodaPeriod(); }
@Override public OverdueState getFirstState() { return getStates()[0]; } }
@Test(groups = "slow") public void testWrapperNoConfig() throws Exception { overdueWrapperFactory.setOverdueConfig(null); final Account account; final OverdueWrapper wrapper; final OverdueState state; final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes()); final OverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); state = config.getStateSet().findState(DefaultBlockingState.CLEAR_STATE_NAME); account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(31)); wrapper = overdueWrapperFactory.createOverdueWrapperFor(account); final OverdueState result = wrapper.refresh(internalCallContext); Assert.assertEquals(result.getName(), state.getName()); Assert.assertEquals(result.blockChanges(), state.blockChanges()); Assert.assertEquals(result.disableEntitlementAndChangesBlocked(), state.disableEntitlementAndChangesBlocked()); } }