use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.
the class TestBlockingDao method testDaoWithMultipleServices.
@Test(groups = "slow", description = "Check BlockingStateDao with multiple services")
public void testDaoWithMultipleServices() throws Exception {
final UUID uuid = UUID.randomUUID();
final String overdueStateName = "WayPassedItMan";
final String service1 = "TEST";
final boolean blockChange = true;
final boolean blockEntitlement = false;
final boolean blockBilling = false;
testListener.pushExpectedEvent(NextEvent.BLOCK);
final BlockingState state1 = new DefaultBlockingState(uuid, BlockingStateType.ACCOUNT, overdueStateName, service1, blockChange, blockEntitlement, blockBilling, clock.getUTCNow());
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(state1, Optional.<UUID>absent()), internalCallContext);
assertListenerStatus();
clock.setDeltaFromReality(1000 * 3600 * 24);
final String service2 = "TEST2";
testListener.pushExpectedEvent(NextEvent.BLOCK);
final String overdueStateName2 = "NoReallyThisCantGoOn";
final BlockingState state2 = new DefaultBlockingState(uuid, BlockingStateType.ACCOUNT, overdueStateName2, service2, blockChange, blockEntitlement, blockBilling, clock.getUTCNow());
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(state2, Optional.<UUID>absent()), internalCallContext);
assertListenerStatus();
final List<BlockingState> history2 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(history2.size(), 2);
Assert.assertEquals(history2.get(0).getStateName(), overdueStateName);
Assert.assertEquals(history2.get(1).getStateName(), overdueStateName2);
}
use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.
the class TestDefaultBlockingStateDao method testSetBlockingState.
// See https://github.com/killbill/killbill/issues/111
@Test(groups = "slow", description = "Verify we don't insert duplicate blocking states")
public void testSetBlockingState() throws Exception {
final UUID blockableId = UUID.randomUUID();
final BlockingStateType type = BlockingStateType.ACCOUNT;
final String state = "state";
final String state2 = "state-2";
final String serviceA = "service-A";
final String serviceB = "service-B";
// Verify initial state
Assert.assertEquals(blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext).size(), 0);
// Note: the checkers below rely on record_id ordering, not effective date
// Set a state for service A
final DateTime stateDateTime = new DateTime(2013, 5, 6, 10, 11, 12, DateTimeZone.UTC);
final BlockingState blockingState1 = new DefaultBlockingState(blockableId, type, state, serviceA, false, false, false, stateDateTime);
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState1, Optional.<UUID>absent()), internalCallContext);
final List<BlockingState> blockingStates1 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(blockingStates1.size(), 1);
Assert.assertEquals(blockingStates1.get(0).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates1.get(0).getStateName(), state);
Assert.assertEquals(blockingStates1.get(0).getService(), serviceA);
Assert.assertEquals(blockingStates1.get(0).getEffectiveDate(), stateDateTime);
// Set the same state again - no change
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState1, Optional.<UUID>absent()), internalCallContext);
final List<BlockingState> blockingStates2 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(blockingStates2.size(), 1);
Assert.assertEquals(blockingStates2.get(0).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates2.get(0).getStateName(), state);
Assert.assertEquals(blockingStates2.get(0).getService(), serviceA);
Assert.assertEquals(blockingStates2.get(0).getEffectiveDate(), stateDateTime);
// Set the state for service B
final BlockingState blockingState2 = new DefaultBlockingState(blockableId, type, state, serviceB, false, false, false, stateDateTime);
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState2, Optional.<UUID>absent()), internalCallContext);
final List<BlockingState> blockingStates3 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(blockingStates3.size(), 2);
Assert.assertEquals(blockingStates3.get(0).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates3.get(0).getStateName(), state);
Assert.assertEquals(blockingStates3.get(0).getService(), serviceA);
Assert.assertEquals(blockingStates3.get(0).getEffectiveDate(), stateDateTime);
Assert.assertEquals(blockingStates3.get(1).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates3.get(1).getStateName(), state);
Assert.assertEquals(blockingStates3.get(1).getService(), serviceB);
Assert.assertEquals(blockingStates3.get(1).getEffectiveDate(), stateDateTime);
// Set the state for service A in the future - there should be no change (already effective)
final DateTime stateDateTime2 = new DateTime(2013, 6, 6, 10, 11, 12, DateTimeZone.UTC);
final BlockingState blockingState3 = new DefaultBlockingState(blockableId, type, state, serviceA, false, false, false, stateDateTime2);
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState3, Optional.<UUID>absent()), internalCallContext);
final List<BlockingState> blockingStates4 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(blockingStates4.size(), 2);
Assert.assertEquals(blockingStates4.get(0).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates4.get(0).getStateName(), state);
Assert.assertEquals(blockingStates4.get(0).getService(), serviceA);
Assert.assertEquals(blockingStates4.get(0).getEffectiveDate(), stateDateTime);
Assert.assertEquals(blockingStates4.get(1).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates4.get(1).getStateName(), state);
Assert.assertEquals(blockingStates4.get(1).getService(), serviceB);
Assert.assertEquals(blockingStates4.get(1).getEffectiveDate(), stateDateTime);
// Set the state for service A in the past - the new effective date should be respected
final DateTime stateDateTime3 = new DateTime(2013, 2, 6, 10, 11, 12, DateTimeZone.UTC);
final BlockingState blockingState4 = new DefaultBlockingState(blockableId, type, state, serviceA, false, false, false, stateDateTime3);
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState4, Optional.<UUID>absent()), internalCallContext);
final List<BlockingState> blockingStates5 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(blockingStates5.size(), 2);
Assert.assertEquals(blockingStates5.get(0).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates5.get(0).getStateName(), state);
Assert.assertEquals(blockingStates5.get(0).getService(), serviceA);
Assert.assertEquals(blockingStates5.get(0).getEffectiveDate(), stateDateTime3);
Assert.assertEquals(blockingStates5.get(1).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates5.get(1).getStateName(), state);
Assert.assertEquals(blockingStates5.get(1).getService(), serviceB);
Assert.assertEquals(blockingStates5.get(1).getEffectiveDate(), stateDateTime);
// Set a new state for service A
final DateTime state2DateTime = new DateTime(2013, 12, 6, 10, 11, 12, DateTimeZone.UTC);
final BlockingState blockingState5 = new DefaultBlockingState(blockableId, type, state2, serviceA, false, false, false, state2DateTime);
blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState5, Optional.<UUID>absent()), internalCallContext);
final List<BlockingState> blockingStates6 = blockingStateDao.getBlockingAllForAccountRecordId(internalCallContext);
Assert.assertEquals(blockingStates6.size(), 3);
Assert.assertEquals(blockingStates6.get(0).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates6.get(0).getStateName(), state);
Assert.assertEquals(blockingStates6.get(0).getService(), serviceA);
Assert.assertEquals(blockingStates6.get(0).getEffectiveDate(), stateDateTime3);
Assert.assertEquals(blockingStates6.get(1).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates6.get(1).getStateName(), state);
Assert.assertEquals(blockingStates6.get(1).getService(), serviceB);
Assert.assertEquals(blockingStates6.get(1).getEffectiveDate(), stateDateTime);
Assert.assertEquals(blockingStates6.get(2).getBlockedId(), blockableId);
Assert.assertEquals(blockingStates6.get(2).getStateName(), state2);
Assert.assertEquals(blockingStates6.get(2).getService(), serviceA);
Assert.assertEquals(blockingStates6.get(2).getEffectiveDate(), state2DateTime);
}
use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.
the class OverdueWrapper method refreshWithLock.
private OverdueState refreshWithLock(final DateTime effectiveDate, final InternalCallContext context) throws OverdueException, OverdueApiException {
final BillingState billingState = billingState(context);
final BlockingState blockingStateForService = api.getBlockingStateForService(overdueable.getId(), BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, context);
final String previousOverdueStateName = blockingStateForService != null ? blockingStateForService.getStateName() : OverdueWrapper.CLEAR_STATE_NAME;
final OverdueState currentOverdueState = overdueStateSet.findState(previousOverdueStateName);
final OverdueState nextOverdueState = overdueStateSet.calculateOverdueState(billingState, clock.getToday(billingState.getAccountTimeZone()));
overdueStateApplicator.apply(effectiveDate, overdueStateSet, billingState, overdueable, currentOverdueState, nextOverdueState, context);
return nextOverdueState;
}
use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.
the class OverdueWrapper method clearWithLock.
private void clearWithLock(final DateTime effectiveDate, final InternalCallContext context) throws OverdueException, OverdueApiException {
final BlockingState blockingStateForService = api.getBlockingStateForService(overdueable.getId(), BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, context);
final String previousOverdueStateName = blockingStateForService != null ? blockingStateForService.getStateName() : OverdueWrapper.CLEAR_STATE_NAME;
final OverdueState previousOverdueState = overdueStateSet.findState(previousOverdueStateName);
overdueStateApplicator.clear(effectiveDate, overdueable, previousOverdueState, overdueStateSet.getClearState(), context);
}
use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.
the class DefaultOverdueApi method getOverdueStateFor.
@Override
public OverdueState getOverdueStateFor(final UUID accountId, final TenantContext tenantContext) throws OverdueApiException {
final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
final BlockingState blockingStateForService = blockingInternalApi.getBlockingStateForService(accountId, BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, internalTenantContext);
final String stateName = blockingStateForService != null ? blockingStateForService.getStateName() : OverdueWrapper.CLEAR_STATE_NAME;
final OverdueConfig overdueConfig = overdueConfigCache.getOverdueConfig(internalTenantContext);
final OverdueStateSet states = ((DefaultOverdueConfig) overdueConfig).getOverdueStatesAccount();
return states.findState(stateName);
}
Aggregations