Search in sources :

Example 6 with MockAccountBuilder

use of org.killbill.billing.mock.MockAccountBuilder in project killbill by killbill.

the class TestWithTimeZones method testWithDayLightSaving.

// Verify that recurring invoice items are correctly computed although we went through and out of daylight saving transitions
@Test(groups = "slow")
public void testWithDayLightSaving() throws Exception {
    // Start with a date in daylight saving period  and make sure we use a time of 8 hour so that we we reach standard time
    // the next month where the difference is 9 hours, a transformation from DateTime to LocalDate with the account time zone would bring us a day earlier
    clock.setTime(new DateTime("2015-09-01T08:01:01.000Z"));
    final DateTimeZone tz = DateTimeZone.forID("America/Juneau");
    final AccountData accountData = new MockAccountBuilder().name(UUID.randomUUID().toString().substring(1, 8)).firstNameLength(6).email(UUID.randomUUID().toString().substring(1, 8)).phone(UUID.randomUUID().toString().substring(1, 8)).migrated(false).externalKey(UUID.randomUUID().toString().substring(1, 8)).billingCycleDayLocal(1).currency(Currency.USD).paymentMethodId(UUID.randomUUID()).referenceTime(clock.getUTCNow()).timeZone(tz).build();
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    final TestDryRunArguments dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, null, null, SubscriptionEventType.START_BILLING, null, null, null, null);
    final Invoice dryRunInvoice = invoiceUserApi.triggerDryRunInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 9, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, expectedInvoices);
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    // Check bundle after BP got created otherwise we get an error from auditApi.
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, expectedInvoices);
    expectedInvoices.clear();
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2015, 10, 1), new LocalDate(2015, 11, 1), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    LocalDate startDate = new LocalDate(2015, 11, 1);
    // We loop 18 times to go over a year and transitions several times between winter and summer (daylight saving)
    for (int i = 0; i < 18; i++) {
        final LocalDate endDate = startDate.plusMonths(1);
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.addMonths(1);
        assertListenerStatus();
        invoiceChecker.checkInvoice(account.getId(), i + 3, callContext, new ExpectedInvoiceItemCheck(startDate, endDate, InvoiceItemType.RECURRING, new BigDecimal("249.95")));
        startDate = endDate;
    }
}
Also used : Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) Invoice(org.killbill.billing.invoice.api.Invoice) ArrayList(java.util.ArrayList) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) BigDecimal(java.math.BigDecimal) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Test(org.testng.annotations.Test)

Example 7 with MockAccountBuilder

use of org.killbill.billing.mock.MockAccountBuilder in project killbill by killbill.

the class TestWithTimeZones method testReferenceTimeInDSTGap.

@Test(groups = "slow")
public void testReferenceTimeInDSTGap() throws Exception {
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    clock.setTime(new DateTime(2015, 3, 7, 2, 0, 0, tz));
    final AccountData accountData = new MockAccountBuilder().currency(Currency.USD).referenceTime(clock.getUTCNow()).timeZone(tz).build();
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    Assert.assertEquals(account.getTimeZone(), tz);
    Assert.assertEquals(account.getFixedOffsetTimeZone(), DateTimeZone.forOffsetHours(-8));
    // Note the gap: 2015-03-07T02:00:00.000-08:00 to 2015-03-08T03:00:00.000-07:00
    clock.addDays(1);
    try {
        // See TimeAwareContext#toUTCDateTime (which uses account.getFixedOffsetTimeZone() instead)
        new DateTime(clock.getUTCToday().getYear(), clock.getUTCToday().getMonthOfYear(), clock.getUTCToday().getDayOfMonth(), account.getReferenceTime().toDateTime(tz).getHourOfDay(), account.getReferenceTime().toDateTime(tz).getMinuteOfHour(), account.getReferenceTime().toDateTime(tz).getSecondOfMinute(), account.getTimeZone());
        Assert.fail();
    } catch (final IllegalInstantException e) {
    // Illegal instant due to time zone offset transition (daylight savings time 'gap'): 2015-03-08T10:00:00.000 (America/Los_Angeles)
    }
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Blowdart", BillingPeriod.MONTHLY, "notrial", null);
    // Pass a date of today, to trigger TimeAwareContext#toUTCDateTime
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "Something", clock.getUTCToday(), clock.getUTCToday(), false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
    Assert.assertEquals(entitlement.getEffectiveStartDate().compareTo(new LocalDate("2015-03-08")), 0);
    Assert.assertEquals(((DefaultEntitlement) entitlement).getBasePlanSubscriptionBase().getStartDate().compareTo(new DateTime("2015-03-08T02:00:00.000-08:00")), 0);
    invoiceChecker.checkChargedThroughDate(entitlement.getId(), new LocalDate("2015-04-08"), callContext);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(31);
    assertListenerStatus();
    invoiceChecker.checkChargedThroughDate(entitlement.getId(), new LocalDate("2015-05-08"), callContext);
    for (int i = 0; i < 25; i++) {
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.addMonths(1);
        assertListenerStatus();
        invoiceChecker.checkChargedThroughDate(entitlement.getId(), new LocalDate("2015-03-08").plusMonths(3 + i), callContext);
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) AccountData(org.killbill.billing.account.api.AccountData) IllegalInstantException(org.joda.time.IllegalInstantException) UUID(java.util.UUID) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 8 with MockAccountBuilder

use of org.killbill.billing.mock.MockAccountBuilder in project killbill by killbill.

the class TestWithTimeZones method testIntoDaylightSavingTransition.

@Test(groups = "slow")
public void testIntoDaylightSavingTransition() throws Exception {
    // Daylight saving happened on March 12th.
    // 
    // Because we use 30 days trial, we start a bit before and that way we can check that computation of BCD crossing into daylight saving works as expected.
    // 
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    clock.setTime(new DateTime(2017, 3, 1, 23, 30, 0, tz));
    final AccountData accountData = new MockAccountBuilder().currency(Currency.USD).referenceTime(clock.getUTCNow()).timeZone(tz).build();
    // Create account with non BCD to force junction BCD logic to activate
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final Account accountWithBCD = accountUserApi.getAccountById(account.getId(), callContext);
    assertEquals(accountWithBCD.getBillCycleDayLocal().intValue(), 31);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2017, 3, 31), new LocalDate(2017, 4, 30), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    expectedInvoices.clear();
}
Also used : Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) AccountData(org.killbill.billing.account.api.AccountData) ArrayList(java.util.ArrayList) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 9 with MockAccountBuilder

use of org.killbill.billing.mock.MockAccountBuilder in project killbill by killbill.

the class TestWithTimeZones method testOutOfDaylightSavingTransition1.

@Test(groups = "slow")
public void testOutOfDaylightSavingTransition1() throws Exception {
    // Transition out of daylight saving is set for Nov 5
    // 
    // Because we use 30 days trial, we start a bit before and that way we can check that computation of BCD crossing out of of daylight saving works as expected.
    // 
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    clock.setTime(new DateTime(2017, 11, 1, 00, 30, 0, tz));
    final AccountData accountData = new MockAccountBuilder().currency(Currency.USD).timeZone(tz).referenceTime(clock.getUTCNow()).build();
    // Create account with non BCD to force junction BCD logic to activate
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final Account accountWithBCD = accountUserApi.getAccountById(account.getId(), callContext);
    assertEquals(accountWithBCD.getBillCycleDayLocal().intValue(), 1);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2017, 12, 1), new LocalDate(2018, 1, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    expectedInvoices.clear();
}
Also used : Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) AccountData(org.killbill.billing.account.api.AccountData) ArrayList(java.util.ArrayList) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 10 with MockAccountBuilder

use of org.killbill.billing.mock.MockAccountBuilder in project killbill by killbill.

the class TestWithTimeZones method testIntoDaylightSavingTransition2.

@Test(groups = "slow")
public void testIntoDaylightSavingTransition2() throws Exception {
    // 
    // Nov 6th Transition date from DST -> ST
    // 
    // Because we use 30 days trial, we start a bit before and that way we can check that computation of BCD crossing into daylight saving works as expected.
    // 
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    clock.setTime(new DateTime(2016, 11, 5, 23, 30, 0, tz));
    final AccountData accountData = new MockAccountBuilder().currency(Currency.USD).referenceTime(clock.getUTCNow()).timeZone(tz).build();
    // Create account with non BCD to force junction BCD logic to activate
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    clock.setTime(new DateTime(2017, 3, 1, 23, 30, 0, tz));
    createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    final Account accountWithBCD = accountUserApi.getAccountById(account.getId(), callContext);
    // 
    // Ok, it's a little bit tricky here:
    // 
    // Intuitively we would expect a BCD of 31 (same as previous test testIntoDaylightSavingTransition1), however our implementation relies on TimeAwareContext for all TZ
    // computation. This context stores an offset from UTC which is then used as a reference and because we created an account before Nov 6 (DST), the offest is (-7)
    // so the result is different than previous test (where offset was -8)
    // What 's important is the fact that BCD and invoiceDate align in such a way that:
    // 1. we see no leading pro-ration
    // 2. Invoice is correctly generated (don't miss it because too early or don't invoice loop)
    // 
    assertEquals(accountWithBCD.getBillCycleDayLocal().intValue(), 1);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2017, 4, 1), new LocalDate(2017, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    expectedInvoices.clear();
}
Also used : Account(org.killbill.billing.account.api.Account) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) AccountData(org.killbill.billing.account.api.AccountData) ArrayList(java.util.ArrayList) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

MockAccountBuilder (org.killbill.billing.mock.MockAccountBuilder)25 Account (org.killbill.billing.account.api.Account)21 AccountData (org.killbill.billing.account.api.AccountData)14 Test (org.testng.annotations.Test)13 DateTime (org.joda.time.DateTime)11 DateTimeZone (org.joda.time.DateTimeZone)11 LocalDate (org.joda.time.LocalDate)9 BigDecimal (java.math.BigDecimal)6 ArrayList (java.util.ArrayList)6 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)6 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)6 UUID (java.util.UUID)5 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)5 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)5 Entitlement (org.killbill.billing.entitlement.api.Entitlement)3 Invoice (org.killbill.billing.invoice.api.Invoice)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 PluginProperty (org.killbill.billing.payment.api.PluginProperty)2 Handle (org.skife.jdbi.v2.Handle)2