Search in sources :

Example 16 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class TestBlockingDao method testWithAuditAndHistory.

@Test(groups = "slow")
public void testWithAuditAndHistory() throws Exception {
    final UUID uuid = createAccount(getAccountData(1)).getId();
    final String overdueStateName = "StateBlock";
    final String service = "auditAndHistory";
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState blockingState = new DefaultBlockingState(uuid, BlockingStateType.ACCOUNT, overdueStateName, service, false, true, false, clock.getUTCNow());
    blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState, Optional.<UUID>absent()), internalCallContext);
    assertListenerStatus();
    final List<AuditLogWithHistory> h1 = blockingStateDao.getBlockingStateAuditLogsWithHistoryForId(blockingState.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(h1.size(), 1);
    final AuditLogWithHistory firstHistoryRow = h1.get(0);
    Assert.assertEquals(firstHistoryRow.getChangeType(), ChangeType.INSERT);
    final BlockingStateModelDao firstBlockingState = (BlockingStateModelDao) firstHistoryRow.getEntity();
    Assert.assertFalse(firstBlockingState.getBlockChange());
    Assert.assertTrue(firstBlockingState.getBlockEntitlement());
    Assert.assertFalse(firstBlockingState.getBlockBilling());
    Assert.assertTrue(firstBlockingState.isActive());
    blockingStateDao.unactiveBlockingState(blockingState.getId(), internalCallContext);
    final List<AuditLogWithHistory> h2 = blockingStateDao.getBlockingStateAuditLogsWithHistoryForId(blockingState.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(h2.size(), 2);
    final AuditLogWithHistory secondHistoryRow = h2.get(1);
    Assert.assertEquals(secondHistoryRow.getChangeType(), ChangeType.DELETE);
    final BlockingStateModelDao secondBlockingState = (BlockingStateModelDao) secondHistoryRow.getEntity();
    Assert.assertFalse(secondBlockingState.getBlockChange());
    Assert.assertTrue(secondBlockingState.getBlockEntitlement());
    Assert.assertFalse(secondBlockingState.getBlockBilling());
    Assert.assertFalse(secondBlockingState.isActive());
}
Also used : DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 17 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class TestDefaultSubscriptionApi method testBlockingStatesInTimelineApi.

@Test(groups = "slow", description = "Verify blocking states are exposed in SubscriptionBundle")
public void testBlockingStatesInTimelineApi() throws Exception {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    final Account account = createAccount(getAccountData(7));
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID entitlement1Id = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), UUID.randomUUID().toString(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    final Entitlement entitlement1 = entitlementApi.getEntitlementForId(entitlement1Id, callContext);
    // Move the clock 1.5 sec so the (truncated) created date are apart from each other and ordering in the bundle does not default on the UUID which is random.
    clock.addDeltaFromReality(1500);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID entitlement2Id = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), UUID.randomUUID().toString(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement entitlement2 = entitlementApi.getEntitlementForId(entitlement2Id, callContext);
    testListener.pushExpectedEvents(NextEvent.BLOCK);
    entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "stateName", "service", false, false, false, clock.getUTCNow()), internalCallContextFactory.createInternalCallContext(account.getId(), callContext));
    assertListenerStatus();
    final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForAccountId(account.getId(), callContext);
    Assert.assertEquals(bundles.size(), 2);
    // This will test the ordering as well
    subscriptionBundleChecker(bundles, initialDate, entitlement1, 0);
    subscriptionBundleChecker(bundles, initialDate, entitlement2, 1);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 18 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class TestDefaultSubscriptionApi method testAddBlockingState.

@Test(groups = "slow")
public void testAddBlockingState() throws AccountApiException, EntitlementApiException, SubscriptionApiException {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    final Account account = accountApi.createAccount(getAccountData(7), callContext);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID createdEntitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), initialDate, initialDate, false, true, ImmutableList.<PluginProperty>of(), callContext);
    final Entitlement createdEntitlement = entitlementApi.getEntitlementForId(createdEntitlementId, callContext);
    final Iterable<BlockingState> iterableForCreateState = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION), null, OrderingType.ASCENDING, SubscriptionApi.ALL_EVENTS, callContext);
    assertTrue(iterableForCreateState.iterator().hasNext());
    final BlockingState createState = iterableForCreateState.iterator().next();
    assertEquals(createState.getService(), KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName());
    assertListenerStatus();
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state1 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "accountBlock", "svc1", false, true, false, clock.getUTCNow());
    subscriptionApi.addBlockingState(state1, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Entitlement updateEntitlement = entitlementApi.getEntitlementForId(createdEntitlement.getId(), callContext);
    Assert.assertEquals(updateEntitlement.getState(), EntitlementState.BLOCKED);
    clock.addDays(1);
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state2 = new DefaultBlockingState(createdEntitlement.getId(), BlockingStateType.SUBSCRIPTION, "subscriptionBlock", "svc2", false, false, false, clock.getUTCNow());
    subscriptionApi.addBlockingState(state2, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Still blocked because this is a different service
    updateEntitlement = entitlementApi.getEntitlementForId(createdEntitlement.getId(), callContext);
    Assert.assertEquals(updateEntitlement.getState(), EntitlementState.BLOCKED);
    // Now we remove the blocking state for the same service but at the SUBSCRIPTION level
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state3 = new DefaultBlockingState(createdEntitlement.getId(), BlockingStateType.SUBSCRIPTION, "subscriptionUnBlock", "svc1", false, false, false, clock.getUTCNow());
    subscriptionApi.addBlockingState(state3, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    updateEntitlement = entitlementApi.getEntitlementForId(createdEntitlement.getId(), callContext);
    Assert.assertEquals(updateEntitlement.getState(), EntitlementState.BLOCKED);
    final DateTime futureEffectiveDate = clock.getUTCNow().plusDays(1);
    final BlockingState state4 = new DefaultBlockingState(createdEntitlement.getBundleId(), BlockingStateType.SUBSCRIPTION_BUNDLE, "blockBilling", "svc1", true, false, false, futureEffectiveDate);
    subscriptionApi.addBlockingState(state4, internalCallContext.toLocalDate(futureEffectiveDate), ImmutableList.<PluginProperty>of(), callContext);
    final Iterable<BlockingState> blockingStates1 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.ACCOUNT, BlockingStateType.SUBSCRIPTION), ImmutableList.of("svc1", "svc2"), OrderingType.ASCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext);
    verifyBlockingStates(blockingStates1, ImmutableList.<BlockingState>of(state1, state2, state3));
    final Iterable<BlockingState> blockingStates2 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION), ImmutableList.of("svc1", "svc2"), OrderingType.DESCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext);
    verifyBlockingStates(blockingStates2, ImmutableList.<BlockingState>of(state3, state2));
    final Iterable<BlockingState> blockingStates3 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION), ImmutableList.of("svc2"), OrderingType.DESCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext);
    verifyBlockingStates(blockingStates3, ImmutableList.<BlockingState>of(state2));
    final Iterable<BlockingState> blockingStates4 = subscriptionApi.getBlockingStates(account.getId(), null, null, OrderingType.DESCENDING, SubscriptionApi.ALL_EVENTS, callContext);
    verifyBlockingStates(blockingStates4, ImmutableList.<BlockingState>of(state4, state3, state2, state1, createState));
    final Iterable<BlockingState> blockingStates5 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION_BUNDLE), null, OrderingType.ASCENDING, SubscriptionApi.FUTURE_EVENTS, callContext);
    verifyBlockingStates(blockingStates5, ImmutableList.<BlockingState>of(state4));
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    clock.addDays(1);
    assertListenerStatus();
    final Iterable<BlockingState> blockingStates6 = subscriptionApi.getBlockingStates(account.getId(), null, null, OrderingType.ASCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext);
    verifyBlockingStates(blockingStates6, ImmutableList.<BlockingState>of(createState, state1, state2, state3, state4));
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 19 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class TestProxyBlockingStateDao method testComparisonSameEffectiveDate.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/174")
public void testComparisonSameEffectiveDate() throws Exception {
    final UUID blockedId = UUID.randomUUID();
    final BlockingStateType blockingStateType = BlockingStateType.ACCOUNT;
    final String service = "test";
    final DateTime effectiveDate = clock.getUTCNow();
    final BlockingState bs1 = new DefaultBlockingState(UUID.randomUUID(), blockedId, blockingStateType, "OD1", service, false, false, false, effectiveDate.minusDays(10), effectiveDate, effectiveDate, 1L);
    final BlockingState bs2 = new DefaultBlockingState(UUID.randomUUID(), blockedId, blockingStateType, "OD2", service, true, true, true, effectiveDate.minusDays(5), effectiveDate, effectiveDate, 2L);
    final BlockingState bs3 = new DefaultBlockingState(UUID.randomUUID(), blockedId, blockingStateType, "OD3", service, true, true, true, effectiveDate, effectiveDate, effectiveDate, 3L);
    final BlockingState bs4 = new DefaultBlockingState(UUID.randomUUID(), blockedId, blockingStateType, "OD4", service, false, false, false, effectiveDate, effectiveDate, effectiveDate, 4L);
    verifySortedCopy(bs1, bs2, bs3, bs4, bs1, bs2, bs3, bs4);
    verifySortedCopy(bs1, bs2, bs3, bs4, bs1, bs3, bs2, bs4);
    verifySortedCopy(bs1, bs2, bs3, bs4, bs2, bs3, bs1, bs4);
    verifySortedCopy(bs1, bs2, bs3, bs4, bs2, bs1, bs3, bs4);
    verifySortedCopy(bs1, bs2, bs3, bs4, bs3, bs1, bs2, bs4);
    verifySortedCopy(bs1, bs2, bs3, bs4, bs3, bs2, bs1, bs4);
}
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 20 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class DefaultSubscriptionApi method addBlockingState.

@Override
public void addBlockingState(final BlockingState inputBlockingState, @Nullable final LocalDate inputEffectiveDate, final Iterable<PluginProperty> properties, final CallContext callContext) throws EntitlementApiException {
    logAddBlockingState(log, inputBlockingState, inputEffectiveDate);
    // This is in no way an exhaustive arg validation, but to to ensure plugin would not hijack private entitlement state or service name
    if (inputBlockingState.getService() == null || inputBlockingState.getService().equals(KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName())) {
        throw new EntitlementApiException(ErrorCode.SUB_BLOCKING_STATE_INVALID_ARG, "Need to specify a valid serviceName");
    }
    if (inputBlockingState.getStateName() == null || inputBlockingState.getStateName().equals(DefaultEntitlementApi.ENT_STATE_CANCELLED) || inputBlockingState.getStateName().equals(DefaultEntitlementApi.ENT_STATE_BLOCKED) || inputBlockingState.getStateName().equals(DefaultEntitlementApi.ENT_STATE_CLEAR)) {
        throw new EntitlementApiException(ErrorCode.SUB_BLOCKING_STATE_INVALID_ARG, "Need to specify a valid stateName");
    }
    final InternalCallContext internalCallContextWithValidAccountId;
    final ImmutableAccountData account;
    final UUID accountId;
    final UUID bundleId;
    final String externalKey;
    try {
        switch(inputBlockingState.getType()) {
            case ACCOUNT:
                internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(inputBlockingState.getBlockedId(), ObjectType.ACCOUNT, callContext);
                account = accountApi.getImmutableAccountDataById(inputBlockingState.getBlockedId(), internalCallContextWithValidAccountId);
                externalKey = account.getExternalKey();
                accountId = account.getId();
                bundleId = null;
                break;
            case SUBSCRIPTION_BUNDLE:
                internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(inputBlockingState.getBlockedId(), ObjectType.BUNDLE, callContext);
                final SubscriptionBaseBundle bundle = subscriptionBaseInternalApi.getBundleFromId(inputBlockingState.getBlockedId(), internalCallContextWithValidAccountId);
                externalKey = bundle.getExternalKey();
                bundleId = bundle.getId();
                accountId = bundle.getAccountId();
                break;
            case SUBSCRIPTION:
                internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(inputBlockingState.getBlockedId(), ObjectType.SUBSCRIPTION, callContext);
                final Entitlement entitlement = entitlementInternalApi.getEntitlementForId(inputBlockingState.getBlockedId(), internalCallContextWithValidAccountId);
                bundleId = entitlement.getBundleId();
                accountId = entitlement.getAccountId();
                externalKey = null;
                break;
            default:
                throw new IllegalStateException("Invalid blockingStateType " + inputBlockingState.getType());
        }
    } catch (final AccountApiException e) {
        throw new EntitlementApiException(e);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
    final DateTime effectiveDate = inputEffectiveDate == null ? callContext.getCreatedDate() : internalCallContextWithValidAccountId.toUTCDateTime(inputEffectiveDate);
    final DefaultBlockingState blockingState = new DefaultBlockingState(inputBlockingState, effectiveDate);
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(bundleId, externalKey, new ArrayList<EntitlementSpecifier>(), internalCallContextWithValidAccountId.toLocalDate(effectiveDate), internalCallContextWithValidAccountId.toLocalDate(effectiveDate), false);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.ADD_BLOCKING_STATE, accountId, null, baseEntitlementWithAddOnsSpecifierList, null, properties, callContext);
    final WithEntitlementPlugin<Void> addBlockingStateWithPlugin = new WithEntitlementPlugin<Void>() {

        @Override
        public Void doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
            entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(blockingState, internalCallContextWithValidAccountId);
            return null;
        }
    };
    pluginExecution.executeWithPlugin(addBlockingStateWithPlugin, pluginContext);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DateTime(org.joda.time.DateTime) AccountApiException(org.killbill.billing.account.api.AccountApiException) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext)

Aggregations

DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)63 UUID (java.util.UUID)46 DateTime (org.joda.time.DateTime)38 BlockingState (org.killbill.billing.entitlement.api.BlockingState)37 Test (org.testng.annotations.Test)35 LocalDate (org.joda.time.LocalDate)34 Account (org.killbill.billing.account.api.Account)23 ArrayList (java.util.ArrayList)21 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)19 DateTimeZone (org.joda.time.DateTimeZone)14 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)14 SubscriptionBaseTransition (org.killbill.billing.subscription.api.user.SubscriptionBaseTransition)11 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)9 Entitlement (org.killbill.billing.entitlement.api.Entitlement)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)7 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)7 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)7 BigDecimal (java.math.BigDecimal)6 BillingEvent (org.killbill.billing.junction.BillingEvent)6