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(); } }
@LifecycleHandlerType(LifecycleLevel.LOAD_CATALOG) public synchronized void loadConfig() throws ServiceException { if (!isConfigLoaded) { try { final URI u = new URI(properties.getConfigURI()); overdueConfig = XMLLoader.getObjectFromUri(u, OverdueConfig.class); // File not found? if (overdueConfig == null) { log.warn("Unable to load the overdue config from " + properties.getConfigURI()); overdueConfig = new OverdueConfig(); } isConfigLoaded = true; } catch (final URISyntaxException e) { overdueConfig = new OverdueConfig(); } catch (final IllegalArgumentException e) { overdueConfig = new OverdueConfig(); } catch (final Exception e) { throw new ServiceException(e); } factory.setOverdueConfig(overdueConfig); ((DefaultOverdueUserApi) userApi).setOverdueConfig(overdueConfig); } }
@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; }
@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 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; }
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");
@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"); }
@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()); } }
@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); }