Search in sources :

Example 1 with PhasePrice

use of org.killbill.billing.client.model.gen.PhasePrice in project killbill by killbill.

the class TestEntitlement method testOverridePrice.

@Test(groups = "slow", description = "Can override a price when creating a subscription")
public void testOverridePrice() throws Exception {
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account accountJson = createAccountWithDefaultPaymentMethod();
    final String productName = "Shotgun";
    final BillingPeriod term = BillingPeriod.ANNUAL;
    final Subscription input = new Subscription();
    input.setAccountId(accountJson.getAccountId());
    input.setBundleExternalKey("identical");
    input.setProductName(productName);
    input.setProductCategory(ProductCategory.BASE);
    input.setBillingPeriod(BillingPeriod.MONTHLY);
    input.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final List<PhasePrice> overrides = new ArrayList<PhasePrice>();
    overrides.add(new PhasePrice(null, null, PhaseType.TRIAL.toString(), BigDecimal.TEN, null, null));
    input.setPriceOverrides(overrides);
    callbackServlet.pushExpectedEvents(ExtBusEventType.ACCOUNT_CHANGE, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
    final Subscription subscription = subscriptionApi.createSubscription(input, null, null, null, null, false, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, NULL_PLUGIN_PROPERTIES, requestOptions);
    callbackServlet.assertListenerStatus();
    Assert.assertEquals(subscription.getPrices().size(), 2);
    Assert.assertEquals(subscription.getEvents().size(), 3);
    Assert.assertEquals(subscription.getEvents().get(0).getEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertMatches(subscription.getEvents().get(0).getPlan(), "shotgun-monthly-[1-9]+");
    assertMatches(subscription.getEvents().get(0).getPhase(), "shotgun-monthly-[1-9]+-trial");
    Assert.assertEquals(subscription.getEvents().get(0).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME);
    Assert.assertEquals(subscription.getEvents().get(0).getProduct(), "Shotgun");
    Assert.assertEquals(subscription.getEvents().get(1).getEventType(), SubscriptionEventType.START_BILLING);
    assertMatches(subscription.getEvents().get(1).getPlan(), "shotgun-monthly-[1-9]+");
    assertMatches(subscription.getEvents().get(1).getPhase(), "shotgun-monthly-[1-9]+-trial");
    Assert.assertEquals(subscription.getEvents().get(1).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME);
    Assert.assertEquals(subscription.getEvents().get(1).getProduct(), "Shotgun");
    Assert.assertEquals(subscription.getEvents().get(2).getEventType(), SubscriptionEventType.PHASE);
    assertMatches(subscription.getEvents().get(2).getPlan(), "shotgun-monthly-[1-9]+");
    assertMatches(subscription.getEvents().get(2).getPhase(), "shotgun-monthly-[1-9]+-evergreen");
    Assert.assertEquals(subscription.getEvents().get(2).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME);
    Assert.assertEquals(subscription.getEvents().get(2).getProduct(), "Shotgun");
    final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.FULL, requestOptions);
    assertEquals(invoices.size(), 1);
    assertEquals(invoices.get(0).getAmount().compareTo(BigDecimal.TEN), 0);
    // Move clock after phase
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
    clock.addDays(30);
    callbackServlet.assertListenerStatus();
    final Subscription subscription2 = subscriptionApi.getSubscription(subscription.getSubscriptionId(), requestOptions);
    Assert.assertEquals(subscription2.getEvents().size(), 3);
    clock.addDays(3);
    // Change Plan
    final Subscription newInput = new Subscription();
    newInput.setSubscriptionId(subscription2.getSubscriptionId());
    newInput.setPlanName("pistol-monthly");
    subscriptionApi.changeSubscriptionPlan(subscription2.getSubscriptionId(), newInput, null, BillingActionPolicy.IMMEDIATE, NULL_PLUGIN_PROPERTIES, requestOptions);
    final Subscription subscription3 = subscriptionApi.getSubscription(subscription.getSubscriptionId(), requestOptions);
    Assert.assertEquals(subscription3.getEvents().size(), 4);
    Assert.assertEquals(subscription3.getEvents().get(0).getEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertMatches(subscription3.getEvents().get(0).getPlan(), "shotgun-monthly-[1-9]+");
    assertMatches(subscription3.getEvents().get(0).getPhase(), "shotgun-monthly-[1-9]+-trial");
    Assert.assertEquals(subscription3.getEvents().get(0).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME.toString());
    Assert.assertEquals(subscription3.getEvents().get(0).getProduct(), "Shotgun");
    Assert.assertEquals(subscription3.getEvents().get(1).getEventType(), SubscriptionEventType.START_BILLING);
    assertMatches(subscription3.getEvents().get(1).getPlan(), "shotgun-monthly-[1-9]+");
    assertMatches(subscription3.getEvents().get(1).getPhase(), "shotgun-monthly-[1-9]+-trial");
    Assert.assertEquals(subscription3.getEvents().get(1).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME.toString());
    Assert.assertEquals(subscription3.getEvents().get(1).getProduct(), "Shotgun");
    Assert.assertEquals(subscription3.getEvents().get(2).getEventType(), SubscriptionEventType.PHASE);
    assertMatches(subscription3.getEvents().get(2).getPlan(), "shotgun-monthly-[1-9]+");
    assertMatches(subscription3.getEvents().get(2).getPhase(), "shotgun-monthly-[1-9]+-evergreen");
    Assert.assertEquals(subscription3.getEvents().get(2).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME.toString());
    Assert.assertEquals(subscription3.getEvents().get(2).getProduct(), "Shotgun");
    Assert.assertEquals(subscription3.getEvents().get(3).getEventType(), SubscriptionEventType.CHANGE);
    Assert.assertEquals(subscription3.getEvents().get(3).getPlan(), "pistol-monthly");
    Assert.assertEquals(subscription3.getEvents().get(3).getPhase(), "pistol-monthly-evergreen");
    Assert.assertEquals(subscription3.getEvents().get(3).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME.toString());
    Assert.assertEquals(subscription3.getEvents().get(3).getProduct(), "Pistol");
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) ArrayList(java.util.ArrayList) PhasePrice(org.killbill.billing.client.model.gen.PhasePrice) Subscription(org.killbill.billing.client.model.gen.Subscription) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)1 Account (org.killbill.billing.client.model.gen.Account)1 Invoice (org.killbill.billing.client.model.gen.Invoice)1 PhasePrice (org.killbill.billing.client.model.gen.PhasePrice)1 Subscription (org.killbill.billing.client.model.gen.Subscription)1 Test (org.testng.annotations.Test)1