Search in sources :

Example 26 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultEntitlementApi method testCreateBaseWithBillingInThePast.

@Test(groups = "slow")
public void testCreateBaseWithBillingInThePast() throws AccountApiException, EntitlementApiException, SubscriptionApiException {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    final Account account = createAccount(getAccountData(7));
    final LocalDate entitlementDate = null;
    final LocalDate billingDate = initialDate.minusDays(5);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    testListener.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE);
    final Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, account.getExternalKey(), null, entitlementDate, billingDate, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    assertEquals(entitlement.getState(), EntitlementState.ACTIVE);
    assertEquals(entitlement.getEffectiveStartDate(), initialDate);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 27 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultEntitlementApi method testUncancelEffectiveCancelledEntitlement.

@Test(groups = "slow")
public void testUncancelEffectiveCancelledEntitlement() throws AccountApiException, EntitlementApiException, SubscriptionBaseApiException {
    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);
    // Keep the same object for the whole test, to make sure we refresh its state before r/w calls
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, account.getExternalKey(), null, null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    subscriptionInternalApi.setChargedThroughDate(entitlement.getId(), clock.getUTCNow().plusMonths(1), internalCallContext);
    final LocalDate entitlementCancelledDate = clock.getToday(account.getTimeZone());
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final Entitlement cancelledEntitlement = entitlement.cancelEntitlementWithDateOverrideBillingPolicy(clock.getToday(account.getTimeZone()), BillingActionPolicy.END_OF_TERM, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Assert.assertEquals(cancelledEntitlement.getEffectiveEndDate(), entitlementCancelledDate);
    testListener.pushExpectedEvent(NextEvent.UNCANCEL);
    cancelledEntitlement.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement reactivatedEntitlement = entitlementApi.getEntitlementForId(cancelledEntitlement.getId(), callContext);
    Assert.assertNull(reactivatedEntitlement.getEffectiveEndDate());
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 28 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultEntitlementApi method testCheckStaleStates.

@Test(groups = "slow")
public void testCheckStaleStates() throws AccountApiException, EntitlementApiException {
    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);
    // Keep the same object for the whole test, to make sure we refresh its state before r/w calls
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, account.getExternalKey(), null, null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Add ADD_ON
    // Keep the same object for the whole test, to make sure we refresh its state before r/w calls
    final PlanPhaseSpecifier addOnSpec = new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final Entitlement addOnEntitlement = entitlementApi.addEntitlement(entitlement.getBundleId(), addOnSpec, null, initialDate, initialDate, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    try {
        entitlement.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
        Assert.fail("Entitlement hasn't been cancelled yet");
    } catch (final EntitlementApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.ENT_UNCANCEL_BAD_STATE.getCode());
    }
    clock.addDays(3);
    // Cancelling the base entitlement will cancel the add-on
    testListener.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.BLOCK);
    entitlement.cancelEntitlementWithDateOverrideBillingPolicy(clock.getUTCToday(), BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    try {
        entitlement.cancelEntitlementWithDateOverrideBillingPolicy(clock.getUTCToday(), BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
        Assert.fail("Entitlement is already cancelled");
    } catch (final EntitlementApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.SUB_CANCEL_BAD_STATE.getCode());
    }
    try {
        addOnEntitlement.cancelEntitlementWithDateOverrideBillingPolicy(clock.getUTCToday(), BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
        Assert.fail("Add-On Entitlement is already cancelled");
    } catch (final EntitlementApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.SUB_CANCEL_BAD_STATE.getCode());
    }
    try {
        entitlement.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
        Assert.fail("Entitlement is already cancelled");
    } catch (final EntitlementApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.SUB_UNCANCEL_BAD_STATE.getCode());
    }
    try {
        addOnEntitlement.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
        Assert.fail("Add-On Entitlement is already cancelled");
    } catch (final EntitlementApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.SUB_UNCANCEL_BAD_STATE.getCode());
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 29 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier 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 Entitlement entitlement1 = entitlementApi.createBaseEntitlement(account.getId(), spec, UUID.randomUUID().toString(), null, null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    // Sleep 1 sec so created date are apart from each other and ordering in the bundle does not default on the UUID which is random.
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ignore) {
    }
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final Entitlement entitlement2 = entitlementApi.createBaseEntitlement(account.getId(), spec, UUID.randomUUID().toString(), null, null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    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) LocalDate(org.joda.time.LocalDate) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Example 30 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultSubscriptionApi method testWithMultipleBundle.

@Test(groups = "slow")
public void testWithMultipleBundle() throws AccountApiException, SubscriptionApiException, EntitlementApiException {
    final String externalKey = "fooXXX";
    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);
    // Create entitlement and check each field
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, externalKey, null, null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    assertEquals(entitlement.getAccountId(), account.getId());
    assertEquals(entitlement.getExternalKey(), externalKey);
    assertEquals(entitlement.getEffectiveStartDate(), initialDate);
    assertNull(entitlement.getEffectiveEndDate());
    final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, callContext);
    assertEquals(bundles.size(), 1);
    final SubscriptionBundle activeBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey(externalKey, callContext);
    assertEquals(activeBundle.getId(), entitlement.getBundleId());
    // Cancel entitlement
    clock.addDays(3);
    testListener.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK);
    entitlement.cancelEntitlementWithDate(new LocalDate(clock.getUTCNow(), account.getTimeZone()), true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    try {
        subscriptionApi.getActiveSubscriptionBundleForExternalKey(externalKey, callContext);
        Assert.fail("Expected getActiveSubscriptionBundleForExternalKey to fail after cancellation");
    } catch (SubscriptionApiException e) {
        assertEquals(e.getCode(), ErrorCode.SUB_GET_INVALID_BUNDLE_KEY.getCode());
    }
    clock.addDays(1);
    // Re-create a new bundle with same externalKey
    final PlanPhaseSpecifier spec2 = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    // Create entitlement and check each field
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final Entitlement entitlement2 = entitlementApi.createBaseEntitlement(account.getId(), spec2, externalKey, null, null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    assertEquals(entitlement2.getAccountId(), account.getId());
    assertEquals(entitlement2.getExternalKey(), externalKey);
    final List<SubscriptionBundle> bundles2 = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, callContext);
    assertEquals(bundles2.size(), 2);
    SubscriptionBundle firstbundle = bundles2.get(0);
    assertEquals(firstbundle.getSubscriptions().size(), 1);
    assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 7));
    assertEquals(firstbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 7));
    assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveEndDate(), new LocalDate(2013, 8, 10));
    assertEquals(firstbundle.getSubscriptions().get(0).getBillingEndDate(), new LocalDate(2013, 8, 10));
    SubscriptionBundle secondbundle = bundles2.get(1);
    assertEquals(secondbundle.getSubscriptions().size(), 1);
    assertEquals(secondbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 11));
    assertEquals(secondbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 11));
    assertNull(secondbundle.getSubscriptions().get(0).getEffectiveEndDate());
    assertNull(secondbundle.getSubscriptions().get(0).getBillingEndDate());
    assertEquals(secondbundle.getOriginalCreatedDate().compareTo(firstbundle.getCreatedDate()), 0);
    final List<SubscriptionBundle> bundles2Again = subscriptionApi.getSubscriptionBundlesForAccountIdAndExternalKey(account.getId(), externalKey, callContext);
    assertEquals(bundles2Again.size(), 2);
    clock.addDays(3);
    final Account account2 = createAccount(getAccountData(7));
    testListener.pushExpectedEvents(NextEvent.TRANSFER, NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.BLOCK);
    entitlementApi.transferEntitlements(account.getId(), account2.getId(), externalKey, new LocalDate(clock.getUTCNow(), account.getTimeZone()), ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final List<SubscriptionBundle> bundles3 = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, callContext);
    assertEquals(bundles3.size(), 3);
    firstbundle = bundles3.get(0);
    assertEquals(firstbundle.getSubscriptions().size(), 1);
    assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 7));
    assertEquals(firstbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 7));
    assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveEndDate(), new LocalDate(2013, 8, 10));
    assertEquals(firstbundle.getSubscriptions().get(0).getBillingEndDate(), new LocalDate(2013, 8, 10));
    secondbundle = bundles3.get(1);
    assertEquals(secondbundle.getSubscriptions().size(), 1);
    assertEquals(secondbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 11));
    assertEquals(secondbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 11));
    assertEquals(secondbundle.getSubscriptions().get(0).getEffectiveEndDate(), new LocalDate(2013, 8, 14));
    assertEquals(secondbundle.getSubscriptions().get(0).getBillingEndDate(), new LocalDate(2013, 8, 14));
    assertEquals(secondbundle.getOriginalCreatedDate().compareTo(firstbundle.getCreatedDate()), 0);
    SubscriptionBundle thirdBundle = bundles3.get(2);
    assertEquals(thirdBundle.getSubscriptions().size(), 1);
    assertEquals(thirdBundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 14));
    assertEquals(thirdBundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 14));
    assertNull(thirdBundle.getSubscriptions().get(0).getEffectiveEndDate());
    assertNull(thirdBundle.getSubscriptions().get(0).getBillingEndDate());
    assertEquals(thirdBundle.getOriginalCreatedDate().compareTo(firstbundle.getCreatedDate()), 0);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)88 Test (org.testng.annotations.Test)69 LocalDate (org.joda.time.LocalDate)63 Account (org.killbill.billing.account.api.Account)59 Entitlement (org.killbill.billing.entitlement.api.Entitlement)27 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)18 DateTime (org.joda.time.DateTime)17 ArrayList (java.util.ArrayList)12 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)9 Invoice (org.killbill.billing.invoice.api.Invoice)9 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)9 AccountData (org.killbill.billing.account.api.AccountData)8 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)8 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)8 EntitlementSpecifier (org.killbill.billing.entitlement.api.EntitlementSpecifier)8 BigDecimal (java.math.BigDecimal)7 Plan (org.killbill.billing.catalog.api.Plan)7 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)7 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)6 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)6