Search in sources :

Example 46 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestInvoiceHelper method generateRegularInvoice.

public UUID generateRegularInvoice(final Account account, final BigDecimal fixedPrice, final BigDecimal recurringPrice, final LocalDate targetDate, final CallContext callContext) throws Exception {
    final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
    Mockito.when(subscription.getId()).thenReturn(UUID.randomUUID());
    Mockito.when(subscription.getBundleId()).thenReturn(new UUID(0L, 0L));
    final BillingEventSet events = new MockBillingEventSet();
    final Plan plan = MockPlan.createBicycleNoTrialEvergreen1USD();
    final PlanPhase planPhase = MockPlanPhase.create1USDMonthlyEvergreen();
    final DateTime effectiveDate = new DateTime().minusDays(1);
    final Currency currency = Currency.USD;
    events.add(createMockBillingEvent(account, subscription, effectiveDate, plan, planPhase, fixedPrice, recurringPrice, currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "", 1L, SubscriptionBaseTransitionType.CREATE));
    Mockito.when(billingApi.getBillingEventsForAccountAndUpdateAccountBCD(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<LocalDate>any(), Mockito.<InternalCallContext>any())).thenReturn(events);
    final InternalCallContext context = internalCallContextFactory.createInternalCallContext(account.getId(), callContext);
    Invoice invoice = generateInvoice(account.getId(), targetDate, new DryRunFutureDateArguments(), context);
    Assert.assertNotNull(invoice);
    List<InvoiceModelDao> invoices = invoiceDao.getInvoicesByAccount(false, context);
    Assert.assertEquals(invoices.size(), 0);
    invoice = generateInvoice(account.getId(), targetDate, null, context);
    Assert.assertNotNull(invoice);
    invoices = invoiceDao.getInvoicesByAccount(false, context);
    Assert.assertEquals(invoices.size(), 1);
    return invoice.getId();
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) MutableInternalCallContext(org.killbill.billing.callcontext.MutableInternalCallContext) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) BillingEventSet(org.killbill.billing.junction.BillingEventSet) Currency(org.killbill.billing.catalog.api.Currency) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) UUID(java.util.UUID)

Example 47 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestInvoiceDao method testInvoiceForFreeTrial.

@Test(groups = "slow")
public void testInvoiceForFreeTrial() throws InvoiceApiException, CatalogApiException {
    final Currency currency = Currency.USD;
    final DefaultPrice price = new DefaultPrice(BigDecimal.ZERO, Currency.USD);
    final MockInternationalPrice fixedPrice = new MockInternationalPrice(price);
    final MockPlanPhase phase = new MockPlanPhase(null, fixedPrice);
    final MockPlan plan = new MockPlan(phase);
    final SubscriptionBase subscription = getZombieSubscription();
    final DateTime effectiveDate = invoiceUtil.buildDate(2011, 1, 1).toDateTimeAtStartOfDay();
    final BillingEvent event = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate, plan, phase, fixedPrice.getPrice(currency), null, currency, BillingPeriod.MONTHLY, 15, BillingMode.IN_ADVANCE, "testEvent", 1L, SubscriptionBaseTransitionType.CREATE);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(event);
    final LocalDate targetDate = invoiceUtil.buildDate(2011, 1, 15);
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, targetDate, Currency.USD, null, context);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNotNull(invoice);
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) Currency(org.killbill.billing.catalog.api.Currency) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 48 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestInvoiceDao method getZombieSubscription.

private SubscriptionBase getZombieSubscription(UUID subscriptionId) {
    final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
    Mockito.when(subscription.getId()).thenReturn(UUID.randomUUID());
    Mockito.when(subscription.getBundleId()).thenReturn(UUID.randomUUID());
    return subscription;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase)

Example 49 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestInvoiceDao method testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair.

@Test(groups = "slow")
public void testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair() throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2010, 1, 1);
    ((ClockMock) clock).setDay(startDate);
    final LocalDate recuringStartDate = clock.getUTCNow().plusDays(30).toLocalDate();
    final LocalDate recuringEndDate = recuringStartDate.plusMonths(1);
    final LocalDate targetDate = recuringStartDate.plusDays(1);
    // FIRST CREATE INITIAL INVOICE WITH ONE RECURRING ITEM
    final Invoice invoice = new DefaultInvoice(accountId, targetDate, targetDate, Currency.USD);
    final UUID invoiceId = invoice.getId();
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "test product", "test-plan", "test-phase-rec", null, recuringStartDate, recuringEndDate, new BigDecimal("239.00"), new BigDecimal("239.00"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(invoice, context);
    ((ClockMock) clock).addDays(1);
    // SECOND CREATE THE PAYMENT
    final BigDecimal paymentAmount = new BigDecimal("239.00");
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoiceId, clock.getUTCNow(), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), UUID.randomUUID(), context);
    // AND THEN THIRD THE REFUND
    final Map<UUID, BigDecimal> invoiceItemMap = new HashMap<UUID, BigDecimal>();
    invoiceItemMap.put(invoiceItem.getId(), new BigDecimal("239.00"));
    invoiceDao.createRefund(paymentId, UUID.randomUUID(), paymentAmount, true, invoiceItemMap, UUID.randomUUID().toString(), true, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(savedInvoice);
    assertEquals(savedInvoice.getInvoiceItems().size(), 2);
    final List<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(new DefaultInvoice(savedInvoice));
    // NOW COMPUTE A DIFFERENT ITEM TO TRIGGER REPAIR
    final BillingEventSet events = new MockBillingEventSet();
    final SubscriptionBase subscription = getZombieSubscription(subscriptionId);
    final Product product = Mockito.mock(Product.class);
    Mockito.when(product.getName()).thenReturn("product");
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getName()).thenReturn("plan");
    Mockito.when(plan.getProduct()).thenReturn(product);
    Mockito.when(plan.getRecurringBillingMode()).thenReturn(BillingMode.IN_ADVANCE);
    final PlanPhase phase1 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase1.getName()).thenReturn("plan-phase1");
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, recuringStartDate.toDateTimeAtStartOfDay(), plan, phase1, null, TEN, Currency.USD, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "new-event", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event1);
    final InvoiceWithMetadata newInvoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, targetDate, Currency.USD, null, context);
    final Invoice newInvoice = newInvoiceWithMetadata.getInvoice();
    invoiceUtil.createInvoice(newInvoice, context);
    // VERIFY THAT WE STILL HAVE ONLY 2 ITEMS, MEANING THERE WERE NO REPAIR AND NO CBA GENERATED
    final Invoice firstInvoice = new DefaultInvoice(invoiceDao.getById(invoiceId, context));
    assertNotNull(firstInvoice);
    assertEquals(firstInvoice.getInvoiceItems().size(), 2);
}
Also used : 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) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) ClockMock(org.killbill.clock.ClockMock) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) 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) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 50 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestTransfer method testTransferWithAOCancelled.

@Test(groups = "slow")
public void testTransferWithAOCancelled() throws Exception {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    // MOVE 3 DAYS AND CREATE AO1
    clock.addDays(3);
    final String aoProduct1 = "Telescopic-Scope";
    final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY;
    final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, aoProduct1, aoTerm1, basePriceList);
    assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    // SET CTD TO TRIGGER CANCELLATION EOT
    final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
    setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
    // SET CTD TO TRIGGER CANCELLATION EOT
    setChargedThroughDate(aoSubscription1.getId(), ctd, internalCallContext);
    // CANCEL ADDON
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    aoSubscription1.cancelWithDate(clock.getUTCNow(), callContext);
    assertListenerStatus();
    clock.addDays(1);
    final DateTime transferRequestedDate = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.TRANSFER);
    transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, true, false, callContext);
    assertListenerStatus();
    final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
    assertEquals(bundlesForAccountAndKey.size(), 1);
    final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
    final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
    assertEquals(subscriptions.size(), 1);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)120 Test (org.testng.annotations.Test)47 UUID (java.util.UUID)46 DateTime (org.joda.time.DateTime)40 Plan (org.killbill.billing.catalog.api.Plan)37 ArrayList (java.util.ArrayList)35 LocalDate (org.joda.time.LocalDate)33 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)32 BillingEvent (org.killbill.billing.junction.BillingEvent)30 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)26 MockPlan (org.killbill.billing.catalog.MockPlan)24 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)24 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)23 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)23 BigDecimal (java.math.BigDecimal)22 LinkedList (java.util.LinkedList)21 Invoice (org.killbill.billing.invoice.api.Invoice)21 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)21 BillingEventSet (org.killbill.billing.junction.BillingEventSet)21 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)20