private boolean isBlockBillingTransition(final OverdueState prevOverdueState, final OverdueState nextOverdueState) { return !blockBilling(prevOverdueState) && blockBilling(nextOverdueState); }
private void avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(final Account account, final OverdueState previousOverdueState, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueApiException { if (isBlockBillingTransition(previousOverdueState, nextOverdueState)) { set_AUTO_INVOICE_OFF_on_blockedBilling(account.getId(), context); } else if (isUnblockBillingTransition(previousOverdueState, nextOverdueState)) { remove_AUTO_INVOICE_OFF_on_clear(account.getId(), context); } }
@Override public Boolean call() throws Exception { final List<OverdueChangeInternalEvent> events = listener.getEventsReceived(); return events.size() == 1; } });
try { if (isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF(context)) { log.debug("OverdueStateApplicator:apply returns because account (recordId = " + context.getAccountRecordId() + ") is set with OVERDUE_ENFORCEMENT_OFF "); return; createFutureNotification(account, clock.getUTCNow().plus(reevaluationInterval), context); clearFutureNotification(account, context); storeNewState(account, nextOverdueState, context); cancelSubscriptionsIfRequired(account, nextOverdueState, context); sendEmailIfRequired(billingState, account, nextOverdueState, context); avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(account, previousOverdueState, nextOverdueState, context); bus.post(createOverdueEvent(account, previousOverdueState.getName(), nextOverdueState.getName(), isBlockBillingTransition(previousOverdueState, nextOverdueState), isUnblockBillingTransition(previousOverdueState, nextOverdueState), context)); } catch (Exception e) { log.error("Error posting overdue change event to bus", e);
public void clear(final Account account, final OverdueState previousOverdueState, final OverdueState clearState, final InternalCallContext context) throws OverdueException { log.debug("OverdueStateApplicator:clear : time = " + clock.getUTCNow() + ", previousState = " + previousOverdueState.getName()); storeNewState(account, clearState, context); clearFutureNotification(account, context); try { avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(account, previousOverdueState, clearState, context); } catch (OverdueApiException e) { throw new OverdueException(e); } try { bus.post(createOverdueEvent(account, previousOverdueState.getName(), clearState.getName(), isBlockBillingTransition(previousOverdueState, clearState), isUnblockBillingTransition(previousOverdueState, clearState), context)); } catch (Exception e) { log.error("Error posting overdue change event to bus", e); } }
protected void storeNewState(final Account blockable, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueException { try { blockingApi.setBlockingState(new DefaultBlockingState(blockable.getId(), BlockingStateType.ACCOUNT, nextOverdueState.getName(), OverdueService.OVERDUE_SERVICE_NAME, blockChanges(nextOverdueState), blockEntitlement(nextOverdueState), blockBilling(nextOverdueState), clock.getUTCNow()), context); } catch (Exception e) { throw new OverdueException(e, ErrorCode.OVERDUE_CAT_ERROR_ENCOUNTERED, blockable.getId(), blockable.getClass().getName()); } }
@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"); }
private void checkBussEvent(final String state) throws Exception { await().atMost(10, SECONDS).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { final List<OverdueChangeInternalEvent> events = listener.getEventsReceived(); return events.size() == 1; } }); final List<OverdueChangeInternalEvent> events = listener.getEventsReceived(); Assert.assertEquals(1, events.size()); Assert.assertEquals(state, events.get(0).getNextOverdueStateName()); listener.clearEventsReceived(); } }
public OverdueState refresh(final InternalCallContext context) throws OverdueException, OverdueApiException { if (overdueStateSet.size() < 1) { // No configuration available return overdueStateSet.getClearState(); } final BillingState billingState = billingState(context); final String previousOverdueStateName = api.getBlockingStateForService(overdueable.getId(), BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, context).getStateName(); final OverdueState currentOverdueState = overdueStateSet.findState(previousOverdueStateName); final OverdueState nextOverdueState = overdueStateSet.calculateOverdueState(billingState, clock.getToday(billingState.getAccountTimeZone())); overdueStateApplicator.apply(overdueStateSet, billingState, overdueable, currentOverdueState, nextOverdueState, context); return nextOverdueState; }
private OverdueChangeInternalEvent createOverdueEvent(final Account overdueable, final String previousOverdueStateName, final String nextOverdueStateName, final boolean isBlockedBilling, final boolean isUnblockedBilling, final InternalCallContext context) throws BlockingApiException { return new DefaultOverdueChangeEvent(overdueable.getId(), previousOverdueStateName, nextOverdueStateName, isBlockedBilling, isUnblockedBilling, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); }
public void clear(final InternalCallContext context) throws OverdueException, OverdueApiException { final String previousOverdueStateName = api.getBlockingStateForService(overdueable.getId(), BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, context).getStateName(); final OverdueState previousOverdueState = overdueStateSet.findState(previousOverdueStateName); overdueStateApplicator.clear(overdueable, previousOverdueState, overdueStateSet.getClearState(), context); }
private void sendEmailIfRequired(final BillingState billingState, final Account account, final OverdueState nextOverdueState, final InternalTenantContext context) { // Note: we don't want to fail the full refresh call because sending the email failed. // That's the reason why we catch all exceptions here. // The alternative would be to: throw new OverdueApiException(e, ErrorCode.EMAIL_SENDING_FAILED); // If sending is not configured, skip if (nextOverdueState.getEnterStateEmailNotification() == null) { return; } final List<String> to = ImmutableList.<String>of(account.getEmail()); // TODO - should we look at the account CC: list? final List<String> cc = ImmutableList.<String>of(); final String subject = nextOverdueState.getEnterStateEmailNotification().getSubject(); try { // Generate and send the email final String emailBody = overdueEmailGenerator.generateEmail(account, billingState, account, nextOverdueState); if (nextOverdueState.getEnterStateEmailNotification().isHTML()) { emailSender.sendHTMLEmail(to, cc, subject, emailBody); } else { emailSender.sendPlainTextEmail(to, cc, subject, emailBody); } } catch (IOException e) { log.warn(String.format("Unable to generate or send overdue notification email for account %s and overdueable %s", account.getId(), account.getId()), e); } catch (EmailApiException e) { log.warn(String.format("Unable to send overdue notification email for account %s and overdueable %s", account.getId(), account.getId()), e); } catch (MustacheException e) { log.warn(String.format("Unable to generate overdue notification email for account %s and overdueable %s", account.getId(), account.getId()), e); } }
computeEntitlementsToCancel(account, toBeCancelled, context);
private boolean isUnblockBillingTransition(final OverdueState prevOverdueState, final OverdueState nextOverdueState) { return blockBilling(prevOverdueState) && !blockBilling(nextOverdueState); }