Search in sources :

Example 41 with MockBillingEventSet

use of org.killbill.billing.invoice.MockBillingEventSet in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItems.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItems() throws InvoiceApiException {
    final LocalDate startDate = new LocalDate("2016-01-01");
    final BillingEventSet events = new MockBillingEventSet();
    final BigDecimal amount = BigDecimal.TEN;
    final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(amount, account.getCurrency()));
    final Plan plan = new MockPlan("my-plan");
    final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event);
    // Simulate a previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
    final List<Invoice> existingInvoices = new LinkedList<Invoice>();
    final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
    invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
    existingInvoices.add(invoice);
    // We will repair the wrong item and generate the correct recurring item
    final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
    assertEquals(generatedItems.size(), 2);
    assertTrue(generatedItems.get(0) instanceof RecurringInvoiceItem);
    assertEquals(generatedItems.get(0).getStartDate(), new LocalDate("2016-01-01"));
    assertEquals(generatedItems.get(0).getEndDate(), new LocalDate("2016-02-01"));
    assertEquals(generatedItems.get(0).getAmount().compareTo(amount), 0);
    assertTrue(generatedItems.get(1) instanceof RepairAdjInvoiceItem);
    assertEquals(generatedItems.get(1).getAmount().compareTo(amount.negate()), 0);
    assertEquals(generatedItems.get(1).getLinkedItemId(), invoice.getInvoiceItems().get(0).getId());
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) LinkedList(java.util.LinkedList) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 42 with MockBillingEventSet

use of org.killbill.billing.invoice.MockBillingEventSet in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItemsWithTooManyRepairs.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItemsWithTooManyRepairs() throws InvoiceApiException {
    final LocalDate startDate = new LocalDate("2016-01-01");
    final BillingEventSet events = new MockBillingEventSet();
    final BigDecimal amount = BigDecimal.TEN;
    final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(amount, account.getCurrency()));
    final Plan plan = new MockPlan("my-plan");
    final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event);
    // Simulate a previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
    final List<Invoice> existingInvoices = new LinkedList<Invoice>();
    final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
    invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
    // But the system has already repaired it
    invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
    BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
    // Twice!
    invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
    BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
    existingInvoices.add(invoice);
    try {
        final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
        fail();
    } catch (final InvoiceApiException e) {
        assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
        assertTrue(e.getCause().getMessage().startsWith("Too many repairs"));
    }
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) LinkedList(java.util.LinkedList) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Example 43 with MockBillingEventSet

use of org.killbill.billing.invoice.MockBillingEventSet in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testSimpleWithSingleDiscountEvent.

@Test(groups = "fast")
public void testSimpleWithSingleDiscountEvent() throws Exception {
    final UUID accountId = UUID.randomUUID();
    final SubscriptionBase sub = createSubscription();
    final Plan plan = new MockPlan("Plan with a single discount phase");
    final PlanPhase phaseEvergreen = createMockMonthlyPlanPhase(EIGHT, PhaseType.DISCOUNT);
    final int bcdLocal = 16;
    final LocalDate startDate = invoiceUtil.buildDate(2012, 7, 16);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phaseEvergreen, bcdLocal));
    // Set a target date of today (start date)
    final LocalDate targetDate = startDate;
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNotNull(invoice);
    assertEquals(invoice.getNumberOfItems(), 1);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), invoiceUtil.buildDate(2012, 7, 16));
    assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), invoiceUtil.buildDate(2012, 8, 16));
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockPlan(org.killbill.billing.catalog.MockPlan) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) UUID(java.util.UUID) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 44 with MockBillingEventSet

use of org.killbill.billing.invoice.MockBillingEventSet in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testWithEmptyEventSet.

@Test(groups = "fast")
public void testWithEmptyEventSet() throws InvoiceApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, clock.getUTCToday(), Currency.USD, internalCallContext);
    assertNull(invoiceWithMetadata.getInvoice());
}
Also used : MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) Test(org.testng.annotations.Test)

Example 45 with MockBillingEventSet

use of org.killbill.billing.invoice.MockBillingEventSet in project killbill by killbill.

the class TestDefaultInvoiceGenerator method testBuggyBillingEventsDoNotImpactInvoicing.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testBuggyBillingEventsDoNotImpactInvoicing() throws InvoiceApiException, CatalogApiException {
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final UUID subscriptionId1 = UUID.randomUUID();
    final BillingEventSet events = new MockBillingEventSet();
    final List<Invoice> invoices = new ArrayList<Invoice>();
    Invoice invoice;
    final Plan plan1 = new MockPlan("plan1");
    final PlanPhase plan1Phase1 = createMockMonthlyPlanPhase(null, EIGHT, PhaseType.TRIAL);
    final PlanPhase plan1Phase2 = createMockMonthlyPlanPhase(TWELVE, PhaseType.EVERGREEN);
    final LocalDate plan1StartDate = invoiceUtil.buildDate(2011, 1, 5);
    final LocalDate plan1PhaseChangeDate = invoiceUtil.buildDate(2011, 2, 5);
    // To simulate a bug, duplicate the billing events
    for (int i = 0; i < 10; i++) {
        events.add(createBillingEvent(subscriptionId1, bundleId, plan1StartDate, plan1, plan1Phase1, 5));
        events.add(createBillingEvent(subscriptionId1, bundleId, plan1PhaseChangeDate, plan1, plan1Phase2, 5));
    }
    assertEquals(events.size(), 20);
    // Fix for https://github.com/killbill/killbill/issues/467 will prevent duplicate fixed items
    testInvoiceGeneration(accountId, events, invoices, plan1StartDate, 1, EIGHT);
    invoice = invoices.get(0);
    assertEquals(invoice.getInvoiceItems().size(), 1);
    assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), subscriptionId1);
    assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 1, 5));
    assertNull(invoice.getInvoiceItems().get(0).getEndDate());
    assertEquals(invoice.getInvoiceItems().get(0).getAmount().compareTo(EIGHT), 0);
    // Intermediate billing intervals associated with recurring items will be less than a day, so only one recurring item will be generated
    testInvoiceGeneration(accountId, events, invoices, plan1PhaseChangeDate, 1, TWELVE);
    invoice = invoices.get(1);
    assertEquals(invoice.getInvoiceItems().size(), 1);
    assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), subscriptionId1);
    assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 2, 5));
    assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2011, 3, 5));
    assertEquals(invoice.getInvoiceItems().get(0).getAmount().compareTo(TWELVE), 0);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) MockPlan(org.killbill.billing.catalog.MockPlan) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) ArrayList(java.util.ArrayList) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) UUID(java.util.UUID) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)50 Test (org.testng.annotations.Test)50 LocalDate (org.joda.time.LocalDate)49 BillingEventSet (org.killbill.billing.junction.BillingEventSet)48 MockPlan (org.killbill.billing.catalog.MockPlan)45 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)45 Invoice (org.killbill.billing.invoice.api.Invoice)44 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)44 Plan (org.killbill.billing.catalog.api.Plan)41 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)40 BigDecimal (java.math.BigDecimal)36 BillingEvent (org.killbill.billing.junction.BillingEvent)35 UUID (java.util.UUID)31 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)22 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)22 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)21 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)21 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)21 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)20 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)20