Search in sources :

Example 21 with DefaultEntitlementSpecifier

use of org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier in project killbill by killbill.

the class TestBlockingApi method testCreateBaseSubscriptionOnBlockedChangeAcount.

@Test(groups = "slow")
public void testCreateBaseSubscriptionOnBlockedChangeAcount() throws AccountApiException, EntitlementApiException, SubscriptionApiException {
    final LocalDate initialDate = new LocalDate(2017, 5, 1);
    clock.setDay(initialDate);
    final Account account = createAccount(getAccountData(1));
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState blockChangeAccount = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "State1", "Service1", true, false, false, clock.getUTCNow());
    subscriptionApi.addBlockingState(blockChangeAccount, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Try create subscription right now
    try {
        final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("shotgun-monthly", null);
        entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "xyzqe", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
        fail("Should fail to create entitlement when ACCOUNT has been 'change' blocked");
    } catch (final EntitlementApiException e) {
        assertEquals(e.getCode(), ErrorCode.BLOCK_BLOCKED_ACTION.getCode());
    }
    // Try create subscription in the future
    try {
        final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("shotgun-monthly", null);
        entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "xyzqe", null, initialDate.plusDays(3), false, true, ImmutableList.<PluginProperty>of(), callContext);
        fail("Should fail to create entitlement when ACCOUNT has been 'change' blocked");
    } catch (final EntitlementApiException e) {
        assertEquals(e.getCode(), ErrorCode.BLOCK_BLOCKED_ACTION.getCode());
    }
    // Try create subscription in the past
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("shotgun-monthly", null);
    testListener.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE);
    entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "xyzqe", initialDate.minusDays(3), null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) LocalDate(org.joda.time.LocalDate) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 22 with DefaultEntitlementSpecifier

use of org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier in project killbill by killbill.

the class TestDefaultBlockingStateDao method testUnnecessaryEventsAreNotAdded.

@Test(groups = "slow", description = "Verify we don't insert extra add-on events")
public void testUnnecessaryEventsAreNotAdded() throws Exception {
    // This is a simple smoke test at the dao level only to make sure we do sane
    // things in case there are no future add-on cancellation events to add in the stream.
    // See TestEntitlementUtils for a more comprehensive test
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
    final BlockingStateType type = BlockingStateType.SUBSCRIPTION;
    final String state = "state";
    final String service = "service";
    // Verify initial state
    Assert.assertEquals(blockingStateDao.getBlockingAllForAccountRecordId(catalog, internalCallContext).size(), 1);
    // Set a state in the future so no event
    final DateTime stateDateTime = new DateTime(2013, 5, 6, 10, 11, 12, DateTimeZone.UTC);
    final BlockingState blockingState = new DefaultBlockingState(entitlement.getId(), type, state, service, false, false, false, stateDateTime);
    blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState, Optional.<UUID>of(entitlement.getBundleId())), internalCallContext);
    assertListenerStatus();
    Assert.assertEquals(blockingStateDao.getBlockingAllForAccountRecordId(catalog, internalCallContext).size(), 2);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) BlockingStateType(org.killbill.billing.entitlement.api.BlockingStateType) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) Entitlement(org.killbill.billing.entitlement.api.Entitlement) DateTime(org.joda.time.DateTime) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 23 with DefaultEntitlementSpecifier

use of org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier in project killbill by killbill.

the class TestEntitlementUtils method testChangePlanIMM.

@Test(groups = "slow", description = "Verify add-ons blocking states are added for IMM change plans")
public void testChangePlanIMM() throws Exception {
    // Approximate check, as the blocking state check (checkBlockingStatesDAO) could be a bit off
    final DateTime changeDateTime = clock.getUTCNow();
    final LocalDate changeDate = clock.getUTCToday();
    // Change plan IMM (upgrade) to Assault-Rifle (Telescopic-Scope is included)
    testListener.pushExpectedEvents(NextEvent.CHANGE, NextEvent.CANCEL, NextEvent.BLOCK);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME);
    final DefaultEntitlement changedBaseEntitlement = (DefaultEntitlement) baseEntitlement.changePlan(new DefaultEntitlementSpecifier(spec), ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // We need to add a 1s delay before invoking the eventsStreamBuilder in the checks below, because
    // the ClockMock truncates milliseconds. Otherwise, utcNow is equal to the changeDateTime, and
    // the change is considered as pending (see DefaultEventsStream#getPendingSubscriptionEvents)
    clock.addDeltaFromReality(1000);
    // Refresh the add-on state
    final DefaultEntitlement cancelledAddOnEntitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(addOnEntitlement.getId(), callContext);
    // Verify we compute the right blocking states for the "read" path...
    checkFutureBlockingStatesToCancel(changedBaseEntitlement, null, null);
    checkFutureBlockingStatesToCancel(cancelledAddOnEntitlement, null, null);
    checkFutureBlockingStatesToCancel(changedBaseEntitlement, cancelledAddOnEntitlement, null);
    // ...and for the "write" path (which has been exercised in the change call above).
    checkActualBlockingStatesToCancel(changedBaseEntitlement, cancelledAddOnEntitlement, changeDateTime, true);
    // Verify also the blocking states DAO doesn't add too many events (all on disk)
    checkBlockingStatesDAO(changedBaseEntitlement, cancelledAddOnEntitlement, changeDate, false);
    clock.addDays(30);
    // No new event
    assertListenerStatus();
    checkFutureBlockingStatesToCancel(changedBaseEntitlement, null, null);
    checkFutureBlockingStatesToCancel(cancelledAddOnEntitlement, null, null);
    checkFutureBlockingStatesToCancel(changedBaseEntitlement, cancelledAddOnEntitlement, null);
    checkActualBlockingStatesToCancel(changedBaseEntitlement, cancelledAddOnEntitlement, changeDateTime, true);
    checkBlockingStatesDAO(changedBaseEntitlement, cancelledAddOnEntitlement, changeDate, false);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) PluginProperty(org.killbill.billing.payment.api.PluginProperty) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 24 with DefaultEntitlementSpecifier

use of org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier in project killbill by killbill.

the class TestDefaultInternalBillingApi method testRecurringInArrear.

@Test(groups = "slow")
public void testRecurringInArrear() throws Exception {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    // Account with no BCD
    final Account account = createAccount(getAccountData(0));
    Assert.assertEquals(account.getBillCycleDayLocal(), (Integer) 0);
    // Create base entitlement
    final String bundleKey = UUID.randomUUID().toString();
    final EntitlementSpecifier entitlementSpecifierBase = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Cannon", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null));
    final BaseEntitlementWithAddOnsSpecifier specifier = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey, ImmutableList.of(entitlementSpecifierBase), null, null, false);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), ImmutableList.of(specifier), false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final List<Entitlement> entitlements = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlements.size(), 1);
    Assert.assertEquals(entitlements.get(0).getLastActiveProduct().getName(), "Cannon");
    Assert.assertNull(entitlements.get(0).getBillCycleDayLocal());
    // Account still has no BCD
    final Account accountNoBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountNoBCD.getBillCycleDayLocal(), (Integer) 0);
    List<BillingEvent> events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 1);
    Assert.assertEquals(events.get(0).getBillCycleDayLocal(), 7);
    // Verify BCD
    final Account accountWithBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountWithBCD.getBillCycleDayLocal(), (Integer) 7);
    // Verify GET
    final List<Entitlement> entitlementsUpdated = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlementsUpdated.size(), 1);
    Assert.assertEquals(entitlementsUpdated.get(0).getLastActiveProduct().getName(), "Cannon");
    Assert.assertEquals(entitlementsUpdated.get(0).getBillCycleDayLocal(), (Integer) 7);
    events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 1);
    Assert.assertEquals(events.get(0).getBillCycleDayLocal(), 7);
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) BillingEvent(org.killbill.billing.junction.BillingEvent) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) Test(org.testng.annotations.Test)

Example 25 with DefaultEntitlementSpecifier

use of org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier in project killbill by killbill.

the class TestDefaultInternalBillingApi method testBCDUpdateMultipleSubscriptionsAccountAndSubscriptionAligned.

@Test(groups = "slow")
public void testBCDUpdateMultipleSubscriptionsAccountAndSubscriptionAligned() throws Exception {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    // Account with no BCD
    final Account account = createAccount(getAccountData(0));
    Assert.assertEquals(account.getBillCycleDayLocal(), (Integer) 0);
    // Create 2 BASE entitlements
    final String bundleKey1 = UUID.randomUUID().toString();
    final String bundleKey2 = UUID.randomUUID().toString();
    final EntitlementSpecifier entitlementSpecifierBase1 = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Pistol", BillingPeriod.ANNUAL, "gunclubDiscountNoTrial", null));
    final EntitlementSpecifier entitlementSpecifierBase2 = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null));
    final BaseEntitlementWithAddOnsSpecifier specifier1 = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey1, ImmutableList.of(entitlementSpecifierBase1), null, null, false);
    final BaseEntitlementWithAddOnsSpecifier specifier2 = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey2, ImmutableList.of(entitlementSpecifierBase2), null, null, false);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK);
    entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), ImmutableList.of(specifier1, specifier2), false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final List<Entitlement> entitlements = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlements.size(), 2);
    for (final Entitlement entitlement : entitlements) {
        if ("pistol-annual-gunclub-discount-notrial".equals(entitlement.getLastActivePlan().getName())) {
            // SUBSCRIPTION aligned
            Assert.assertEquals(entitlement.getBillCycleDayLocal(), (Integer) 7);
        } else {
            // ACCOUNT aligned
            Assert.assertNull(entitlement.getBillCycleDayLocal());
        }
    }
    // Account still has no BCD
    final Account accountNoBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountNoBCD.getBillCycleDayLocal(), (Integer) 0);
    List<BillingEvent> events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 4);
    for (final BillingEvent billingEvent : events) {
        if ("pistol-annual-gunclub-discount-notrial".equals(billingEvent.getPlan().getName())) {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 7);
        } else {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 6);
        }
    }
    // Verify BCD
    final Account accountWithBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountWithBCD.getBillCycleDayLocal(), (Integer) 6);
    // Verify GET
    final List<Entitlement> entitlementsUpdated = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlementsUpdated.size(), 2);
    for (final Entitlement entitlement : entitlementsUpdated) {
        if ("pistol-annual-gunclub-discount-notrial".equals(entitlement.getLastActivePlan().getName())) {
            Assert.assertEquals(entitlement.getBillCycleDayLocal(), (Integer) 7);
        } else {
            Assert.assertEquals(entitlement.getBillCycleDayLocal(), (Integer) 6);
        }
    }
    events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 4);
    for (final BillingEvent billingEvent : events) {
        if ("pistol-annual-gunclub-discount-notrial".equals(billingEvent.getPlan().getName())) {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 7);
        } else {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 6);
        }
    }
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) BillingEvent(org.killbill.billing.junction.BillingEvent) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) Test(org.testng.annotations.Test)

Aggregations

DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)141 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)140 Test (org.testng.annotations.Test)129 LocalDate (org.joda.time.LocalDate)105 Account (org.killbill.billing.account.api.Account)98 UUID (java.util.UUID)87 Entitlement (org.killbill.billing.entitlement.api.Entitlement)64 BigDecimal (java.math.BigDecimal)62 DateTime (org.joda.time.DateTime)61 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)59 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)56 Invoice (org.killbill.billing.invoice.api.Invoice)40 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)27 ArrayList (java.util.ArrayList)24 AccountData (org.killbill.billing.account.api.AccountData)23 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)20 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)14 BlockingState (org.killbill.billing.entitlement.api.BlockingState)13 Interval (org.joda.time.Interval)12 Period (org.joda.time.Period)12