Search in sources :

Example 41 with BillingPeriod

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

the class TestUserApiAddOn method testCreateCancelAddon.

@Test(groups = "slow")
public void testCreateCancelAddon() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.ANNUAL;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final String aoProduct = "Telescopic-Scope";
    final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
    final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
    assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    final DateTime now = clock.getUTCNow();
    aoSubscription.cancel(callContext);
    assertListenerStatus();
    aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
    assertEquals(aoSubscription.getState(), EntitlementState.CANCELLED);
    assertListenerStatus();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 42 with BillingPeriod

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

the class TestUserApiChangePlan method testChangePlanOnPendingSubscription.

@Test(groups = "slow")
public void testChangePlanOnPendingSubscription() throws SubscriptionBaseApiException {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    final LocalDate startDate = clock.getUTCToday().plusDays(5);
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList, startDate);
    assertEquals(subscription.getState(), Entitlement.EntitlementState.PENDING);
    assertEquals(subscription.getStartDate().compareTo(startDate.toDateTime(accountData.getReferenceTime())), 0);
    final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", baseTerm, basePriceList);
    final EntitlementSpecifier spec = new DefaultEntitlementSpecifier(planPhaseSpecifier, null, null, null);
    // First try with default api (no date -> IMM) => Call should fail because subscription is PENDING
    final DryRunArguments dryRunArguments1 = testUtil.createDryRunArguments(subscription.getId(), subscription.getBundleId(), spec, null, SubscriptionEventType.CHANGE, null);
    final List<SubscriptionBase> result1 = subscriptionInternalApi.getSubscriptionsForBundle(subscription.getBundleId(), dryRunArguments1, internalCallContext);
    // Check we are seeing the right PENDING transition (pistol-monthly), not the START but the CHANGE on the same date
    assertEquals(((DefaultSubscriptionBase) result1.get(0)).getCurrentOrPendingPlan().getName(), "pistol-monthly");
    assertEquals(((DefaultSubscriptionBase) result1.get(0)).getPendingTransition().getTransitionType(), SubscriptionBaseTransitionType.CREATE);
    // Second try with date prior to startDate => Call should fail because subscription is PENDING
    try {
        final DryRunArguments dryRunArguments2 = testUtil.createDryRunArguments(subscription.getId(), subscription.getBundleId(), spec, startDate.minusDays(1), SubscriptionEventType.CHANGE, null);
        subscriptionInternalApi.getSubscriptionsForBundle(subscription.getBundleId(), dryRunArguments2, internalCallContext);
        fail("Change plan should have failed : subscription PENDING");
    } catch (final SubscriptionBaseApiException e) {
        assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_NON_ACTIVE.getCode());
    }
    try {
        subscription.changePlanWithDate(spec, subscription.getStartDate().minusDays(1), callContext);
        fail("Change plan should have failed : subscription PENDING");
    } catch (final SubscriptionBaseApiException e) {
        assertEquals(e.getCode(), ErrorCode.SUB_INVALID_REQUESTED_DATE.getCode());
    }
    // Third try with date equals to startDate  Call should succeed, but no event because action in future
    final DryRunArguments dryRunArguments3 = testUtil.createDryRunArguments(subscription.getId(), subscription.getBundleId(), spec, startDate, SubscriptionEventType.CHANGE, null);
    final List<SubscriptionBase> result2 = subscriptionInternalApi.getSubscriptionsForBundle(subscription.getBundleId(), dryRunArguments3, internalCallContext);
    // Check we are seeing the right PENDING transition (pistol-monthly), not the START but the CHANGE on the same date
    assertEquals(((DefaultSubscriptionBase) result2.get(0)).getCurrentOrPendingPlan().getName(), "pistol-monthly");
    subscription.changePlanWithDate(spec, subscription.getStartDate(), callContext);
    assertListenerStatus();
    // Move clock to startDate
    testListener.pushExpectedEvents(NextEvent.CREATE);
    clock.addDays(5);
    assertListenerStatus();
    final DefaultSubscriptionBase subscription2 = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
    assertEquals(subscription2.getStartDate().compareTo(subscription.getStartDate()), 0);
    assertEquals(subscription2.getState(), Entitlement.EntitlementState.ACTIVE);
    assertEquals(subscription2.getCurrentPlan().getProduct().getName(), "Pistol");
    // Same original # active events
    assertEquals(subscription2.getEvents().size(), subscription.getEvents().size());
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) DryRunArguments(org.killbill.billing.invoice.api.DryRunArguments) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 43 with BillingPeriod

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

the class TestUserApiCancel method testCancelSubscriptionEOTWithNoChargeThroughDate.

@Test(groups = "slow")
public void testCancelSubscriptionEOTWithNoChargeThroughDate() throws SubscriptionBaseApiException {
    final String prod = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSet = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, prod, term, planSet);
    PlanPhase trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.TRIAL);
    // NEXT PHASE
    final DateTime expectedPhaseTrialChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), trialPhase.getDuration());
    testUtil.checkNextPhaseChange(subscription, 1, expectedPhaseTrialChange);
    // MOVE TO NEXT PHASE
    testListener.pushExpectedEvent(NextEvent.PHASE);
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    assertListenerStatus();
    trialPhase = subscription.getCurrentPhase();
    assertEquals(trialPhase.getPhaseType(), PhaseType.EVERGREEN);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    // CANCEL
    subscription.cancel(callContext);
    assertListenerStatus();
    final PlanPhase currentPhase = subscription.getCurrentPhase();
    assertNull(currentPhase);
    testUtil.checkNextPhaseChange(subscription, 0, null);
    assertListenerStatus();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 44 with BillingPeriod

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

the class TestUserApiCreate method testCreateBundlesWithSameExternalKeys.

@Test(groups = "slow")
public void testCreateBundlesWithSameExternalKeys() throws SubscriptionBaseApiException {
    final LocalDate init = clock.getUTCToday();
    final LocalDate requestedDate = init.minusYears(1);
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
    testListener.pushExpectedEvents(NextEvent.PHASE);
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, productName, term, planSetName, requestedDate);
    assertListenerStatus();
    assertNotNull(subscription);
    // Verify we can't create a second bundle with the same key
    try {
        subscriptionInternalApi.createBundleForAccount(bundle.getAccountId(), DefaultSubscriptionTestInitializer.DEFAULT_BUNDLE_KEY, true, internalCallContext);
        Assert.fail("Should not be able to create a bundle with same externalKey");
    } catch (final SubscriptionBaseApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.SUB_CREATE_ACTIVE_BUNDLE_KEY_EXISTS.getCode());
    }
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    subscription.cancelWithDate(clock.getUTCNow(), callContext);
    assertListenerStatus();
    try {
        subscriptionInternalApi.createBundleForAccount(bundle.getAccountId(), DefaultSubscriptionTestInitializer.DEFAULT_BUNDLE_KEY, false, internalCallContext);
        Assert.fail("createBundleForAccount should fail because key already exists");
    } catch (final RuntimeException e) {
        assertTrue(e.getCause() instanceof SQLException && (e.getCause() instanceof SQLIntegrityConstraintViolationException || "23505".compareTo(((SQLException) e.getCause()).getSQLState()) == 0));
    }
    final SubscriptionBaseBundle newBundle = subscriptionInternalApi.createBundleForAccount(bundle.getAccountId(), DefaultSubscriptionTestInitializer.DEFAULT_BUNDLE_KEY, true, internalCallContext);
    assertNotNull(newBundle);
    assertNotEquals(newBundle.getId(), subscription.getBundleId());
    assertEquals(newBundle.getExternalKey(), DefaultSubscriptionTestInitializer.DEFAULT_BUNDLE_KEY);
    assertEquals(newBundle.getOriginalCreatedDate().compareTo(bundle.getCreatedDate()), 0, String.format("OriginalCreatedDate=%s != CreatedDate=%s", newBundle.getOriginalCreatedDate(), bundle.getCreatedDate()));
    subscriptionInternalApi.updateExternalKey(newBundle.getId(), "myNewSuperKey", internalCallContext);
    final SubscriptionBaseBundle bundleWithNewKey = subscriptionInternalApi.getBundleFromId(newBundle.getId(), internalCallContext);
    assertEquals(bundleWithNewKey.getExternalKey(), "myNewSuperKey");
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SQLException(java.sql.SQLException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 45 with BillingPeriod

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

the class TestUserApiCreate method testSimpleCreateSubscription.

@Test(groups = "slow")
public void testSimpleCreateSubscription() throws SubscriptionBaseApiException {
    final DateTime init = clock.getUTCNow();
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.MONTHLY;
    final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
    final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, productName, term, planSetName);
    assertNotNull(subscription);
    assertEquals(subscription.getBundleExternalKey(), bundle.getExternalKey());
    testUtil.assertDateWithin(subscription.getStartDate(), init, clock.getUTCNow());
    testUtil.assertDateWithin(subscription.getBundleStartDate(), init, clock.getUTCNow());
    final Plan currentPlan = subscription.getCurrentPlan();
    assertNotNull(currentPlan);
    assertEquals(currentPlan.getProduct().getName(), productName);
    assertEquals(currentPlan.getProduct().getCategory(), ProductCategory.BASE);
    assertEquals(currentPlan.getRecurringBillingPeriod(), BillingPeriod.MONTHLY);
    final PlanPhase currentPhase = subscription.getCurrentPhase();
    assertNotNull(currentPhase);
    assertEquals(currentPhase.getPhaseType(), PhaseType.TRIAL);
    assertListenerStatus();
    final List<SubscriptionBaseEvent> events = dao.getPendingEventsForSubscription(subscription.getId(), internalCallContext);
    assertNotNull(events);
    testUtil.printEvents(events);
    assertTrue(events.size() == 1);
    assertTrue(events.get(0) instanceof PhaseEvent);
    final DateTime nextPhaseChange = ((PhaseEvent) events.get(0)).getEffectiveDate();
    final DateTime nextExpectedPhaseChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), currentPhase.getDuration());
    assertEquals(nextPhaseChange, nextExpectedPhaseChange);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
    clock.addDeltaFromReality(it.toDurationMillis());
    final DateTime futureNow = clock.getUTCNow();
    assertTrue(futureNow.isAfter(nextPhaseChange));
    assertListenerStatus();
}
Also used : PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Aggregations

BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)111 Test (org.testng.annotations.Test)91 LocalDate (org.joda.time.LocalDate)61 DateTime (org.joda.time.DateTime)59 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)45 BigDecimal (java.math.BigDecimal)44 Account (org.killbill.billing.account.api.Account)44 Invoice (org.killbill.billing.invoice.api.Invoice)40 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)31 Plan (org.killbill.billing.catalog.api.Plan)20 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)20 Interval (org.joda.time.Interval)17 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)17 UUID (java.util.UUID)15 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)15 ArrayList (java.util.ArrayList)14 Duration (org.killbill.billing.catalog.api.Duration)10 Entitlement (org.killbill.billing.entitlement.api.Entitlement)10 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)10 HashMap (java.util.HashMap)9