Search in sources :

Example 11 with BlockingState

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);
}
Also used : DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 12 with BlockingState

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);
}
Also used : BlockingStateType(org.killbill.billing.entitlement.api.BlockingStateType) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 13 with BlockingState

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;
}
Also used : BlockingState(org.killbill.billing.entitlement.api.BlockingState) BillingState(org.killbill.billing.overdue.config.api.BillingState) OverdueState(org.killbill.billing.overdue.api.OverdueState)

Example 14 with BlockingState

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);
}
Also used : BlockingState(org.killbill.billing.entitlement.api.BlockingState) OverdueState(org.killbill.billing.overdue.api.OverdueState)

Example 15 with BlockingState

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);
}
Also used : OverdueStateSet(org.killbill.billing.overdue.config.api.OverdueStateSet) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) BlockingState(org.killbill.billing.entitlement.api.BlockingState) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig)

Aggregations

BlockingState (org.killbill.billing.entitlement.api.BlockingState)46 UUID (java.util.UUID)24 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)22 Test (org.testng.annotations.Test)15 DateTime (org.joda.time.DateTime)13 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)6 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)6 LocalDate (org.joda.time.LocalDate)5 ImmutableList (com.google.common.collect.ImmutableList)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Account (org.killbill.billing.account.api.Account)4 BillingEvent (org.killbill.billing.junction.BillingEvent)4 Optional (com.google.common.base.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 TreeSet (java.util.TreeSet)3 AccountApiException (org.killbill.billing.account.api.AccountApiException)3 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)3 BlockingStateType (org.killbill.billing.entitlement.api.BlockingStateType)3