private boolean blockChanges(final OverdueState nextOverdueState) { return nextOverdueState.blockChanges(); }
private boolean blockBilling(final OverdueState nextOverdueState) { return nextOverdueState.disableEntitlementAndChangesBlocked(); }
@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(); }
@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()); } }
public OverdueStateJson(final OverdueState overdueState) { this.name = overdueState.getName(); this.externalMessage = overdueState.getExternalMessage(); this.daysBetweenPaymentRetries = overdueState.getDaysBetweenPaymentRetries(); this.disableEntitlementAndChangesBlocked = overdueState.disableEntitlementAndChangesBlocked(); this.blockChanges = overdueState.blockChanges(); this.isClearState = overdueState.isClearState(); Period reevaluationIntervalPeriod = null; try { reevaluationIntervalPeriod = overdueState.getReevaluationInterval(); } catch (OverdueApiException ignored) { } if (reevaluationIntervalPeriod != null) { this.reevaluationIntervalDays = reevaluationIntervalPeriod.getDays(); } else { this.reevaluationIntervalDays = null; } }
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); } }
public void checkStateApplied(final BlockingState result, final OverdueState state) { Assert.assertEquals(result.getStateName(), state.getName()); Assert.assertEquals(result.isBlockChange(), state.blockChanges()); Assert.assertEquals(result.isBlockEntitlement(), state.disableEntitlementAndChangesBlocked()); Assert.assertEquals(result.isBlockBilling(), state.disableEntitlementAndChangesBlocked()); }
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()); } }
@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; }
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); } }
public String generateEmail(final Account account, final BillingState billingState, final Account overdueable, final OverdueState nextOverdueState) throws IOException { final Map<String, Object> data = new HashMap<String, Object>(); // TODO raw objects for now. We eventually should respect the account locale and support translations data.put("account", account); data.put("billingState", overdueEmailFormatterFactory.createBillingStateFormatter(billingState)); data.put("overdueable", overdueable); data.put("nextOverdueState", nextOverdueState); // TODO single template for all languages for now return templateEngine.executeTemplate(nextOverdueState.getEnterStateEmailNotification().getTemplateName(), data); } }
@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); }
@Override @AfterMethod(groups = "slow") public void afterMethod() throws Exception { notifierForMock.stop(); super.afterMethod(); }
public void checkStateApplied(final OverdueState state) { final BlockingState result = ((ApplicatorBlockingApi) blockingInternalApi).getBlockingState(); checkStateApplied(result, state); }
private void set_AUTO_INVOICE_OFF_on_blockedBilling(final UUID accountId, final InternalCallContext context) throws OverdueApiException { try { tagApi.addTag(accountId, ObjectType.ACCOUNT, ControlTagType.AUTO_INVOICING_OFF.getId(), context); } catch (TagApiException e) { throw new OverdueApiException(e); } }
@GET @Path("/{accountId:" + UUID_PATTERN + "}/" + OVERDUE) @Produces(APPLICATION_JSON) public Response getOverdueAccount(@PathParam("accountId") final String accountId, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, OverdueException, OverdueApiException { final TenantContext tenantContext = context.createContext(request); final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), tenantContext); final OverdueState overdueState = overdueApi.getOverdueStateFor(account, tenantContext); return Response.status(Status.OK).entity(new OverdueStateJson(overdueState)).build(); }
@Override @BeforeMethod(groups = "fast") public void beforeMethod() throws Exception { super.beforeMethod(); final Account account = Mockito.mock(Account.class); Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC); Mockito.when(accountApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account); }
@Override @BeforeMethod(groups = "slow") public void beforeMethod() throws Exception { super.beforeMethod(); entitySqlDaoTransactionalJdbiWrapper = new EntitySqlDaoTransactionalJdbiWrapper(dbi, clock, cacheControllerDispatcher, nonEntityDao); overdueQueue = notificationQueueService.getNotificationQueue(DefaultOverdueService.OVERDUE_SERVICE_NAME, OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE); Assert.assertTrue(overdueQueue.isStarted()); testReferenceTime = clock.getUTCNow(); }
private boolean blockEntitlement(final OverdueState nextOverdueState) { return nextOverdueState.disableEntitlementAndChangesBlocked(); }
private void remove_AUTO_INVOICE_OFF_on_clear(final UUID accountId, final InternalCallContext context) throws OverdueApiException { try { tagApi.removeTag(accountId, ObjectType.ACCOUNT, ControlTagType.AUTO_INVOICING_OFF.getId(), context); } catch (TagApiException e) { if (e.getCode() != ErrorCode.TAG_DOES_NOT_EXIST.getCode()) { throw new OverdueApiException(e); } } }