use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testOnePlan_TwoMonthlyPhases_ChangeImmediate.
@Test(groups = "fast")
public void testOnePlan_TwoMonthlyPhases_ChangeImmediate() throws InvoiceApiException, CatalogApiException {
final BillingEventSet events = new MockBillingEventSet();
final Plan plan1 = new MockPlan();
final BigDecimal rate1 = FIVE;
final PlanPhase phase1 = createMockMonthlyPlanPhase(rate1);
final SubscriptionBase sub = createSubscription();
final BillingEvent event1 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 9, 1), plan1, phase1, 1);
events.add(event1);
final BigDecimal rate2 = TEN;
final PlanPhase phase2 = createMockMonthlyPlanPhase(rate2);
final BillingEvent event2 = createBillingEvent(sub.getId(), sub.getBundleId(), invoiceUtil.buildDate(2011, 10, 15), plan1, phase2, 15);
events.add(event2);
final LocalDate targetDate = invoiceUtil.buildDate(2011, 12, 3);
final UUID accountId = UUID.randomUUID();
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertNotNull(invoice);
assertEquals(invoice.getNumberOfItems(), 4);
final BigDecimal numberOfCyclesEvent1;
numberOfCyclesEvent1 = ONE.add(FOURTEEN.divide(THIRTY_ONE, KillBillMoney.ROUNDING_METHOD));
final BigDecimal numberOfCyclesEvent2 = TWO;
BigDecimal expectedValue;
expectedValue = numberOfCyclesEvent1.multiply(rate1);
expectedValue = expectedValue.add(numberOfCyclesEvent2.multiply(rate2));
expectedValue = KillBillMoney.of(expectedValue, invoice.getCurrency());
assertEquals(invoice.getBalance(), expectedValue);
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testSimpleWithTimeZone.
@Test(groups = "fast")
public void testSimpleWithTimeZone() throws InvoiceApiException, CatalogApiException {
final SubscriptionBase sub = createSubscription();
final Plan plan = new MockPlan();
final BigDecimal rate = TEN;
final PlanPhase phase = createMockMonthlyPlanPhase(rate);
// Start date was the 16 local, but was the 17 UTC
final int bcdLocal = 16;
final LocalDate startDate = invoiceUtil.buildDate(2012, 7, bcdLocal);
final BillingEventSet events = new MockBillingEventSet();
final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, bcdLocal);
events.add(event);
// Target date is the next BCD, in local time
final LocalDate targetDate = invoiceUtil.buildDate(2012, 8, bcdLocal);
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertNotNull(invoice);
assertEquals(invoice.getNumberOfItems(), 2);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), invoiceUtil.buildDate(2012, 7, 16));
assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), invoiceUtil.buildDate(2012, 8, 16));
assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), invoiceUtil.buildDate(2012, 8, 16));
assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), invoiceUtil.buildDate(2012, 9, 16));
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testTooManyFixedInvoiceItemsForGivenSubscriptionAndStartDate.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testTooManyFixedInvoiceItemsForGivenSubscriptionAndStartDate() 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(null, price, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate.toDateTimeAtStartOfDay(), plan, planPhase, amount, null, account.getCurrency(), BillingPeriod.NO_BILLING_PERIOD, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
// Simulate a bunch of fixed items for that subscription and start date (simulate bad data on disk)
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
for (int i = 0; i < 20; i++) {
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
invoice.addInvoiceItem(new FixedPriceInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), "Buggy fixed item", startDate, amount, account.getCurrency()));
existingInvoices.add(invoice);
}
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
// There will be one proposed, but because it will match one of ones in the existing list and we don't repair, it won't be returned
assertEquals(generatedItems.size(), 0);
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testSafetyBoundsTooManyInvoiceItemsForGivenSubscriptionAndInvoiceDate.
@Test(groups = "fast")
public void testSafetyBoundsTooManyInvoiceItemsForGivenSubscriptionAndInvoiceDate() throws InvoiceApiException {
final int threshold = 15;
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(), planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
// Simulate a big catch-up
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
for (int i = 0; i < threshold; i++) {
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate.plusMonths(i), account.getCurrency());
invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), // Different days - should not trigger the safety bounds
startDate.plusMonths(i).toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate.plusMonths(i), startDate.plusMonths(1 + i), amount, amount, account.getCurrency()));
existingInvoices.add(invoice);
}
assertEquals(fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate.plusMonths(threshold), account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext).size(), 1);
// Simulate a big catch-up on that day
for (int i = threshold; i < 2 * threshold; i++) {
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate.plusMonths(i), account.getCurrency());
invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), // Same day
clock.getUTCNow(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate.plusMonths(i), startDate.plusMonths(1 + i), amount, amount, account.getCurrency()));
existingInvoices.add(invoice);
}
try {
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate.plusMonths(2 * threshold), account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
}
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testInvalidAdjustment.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testInvalidAdjustment() throws InvoiceApiException {
final LocalDate startDate = new LocalDate("2016-01-01");
final BillingEventSet events = new MockBillingEventSet();
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
// Dangling adjustment
invoice.addInvoiceItem(new ItemAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, "Dangling adjustment", BigDecimal.ONE.negate(), account.getCurrency(), UUID.randomUUID()));
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("Missing subscription id"));
}
}
Aggregations