Search in sources :

Example 21 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase 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, new AccountInvoices(), null, targetDate, Currency.USD, null, 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);
}
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) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) 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) Test(org.testng.annotations.Test)

Example 22 with SubscriptionBase

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

the class TestDefaultSubscriptionBundleTimeline method createEntitlement.

private Entitlement createEntitlement(final UUID entitlementId, final List<SubscriptionBaseTransition> allTransitions, final Collection<BlockingState> blockingStates) {
    final DefaultEntitlement result = Mockito.mock(DefaultEntitlement.class);
    Mockito.when(result.getId()).thenReturn(entitlementId);
    final EventsStream eventsStream = Mockito.mock(EventsStream.class);
    Mockito.when(eventsStream.getBlockingStates()).thenReturn(blockingStates);
    Mockito.when(result.getEventsStream()).thenReturn(eventsStream);
    final SubscriptionBase base = Mockito.mock(SubscriptionBase.class);
    Mockito.when(base.getAllTransitions()).thenReturn(allTransitions);
    Mockito.when(result.getSubscriptionBase()).thenReturn(base);
    Mockito.when(result.getSubscriptionBase().getStartDate()).thenReturn(new DateTime(DateTimeZone.UTC));
    return result;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) EventsStream(org.killbill.billing.entitlement.EventsStream) DateTime(org.joda.time.DateTime)

Example 23 with SubscriptionBase

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

the class EventsStreamBuilder method buildForBundle.

public List<EventsStream> buildForBundle(final UUID bundleId, final InternalTenantContext internalTenantContext) throws EntitlementApiException {
    final SubscriptionBaseBundle bundle;
    final List<SubscriptionBase> subscriptionsForBundle;
    try {
        bundle = subscriptionInternalApi.getBundleFromId(bundleId, internalTenantContext);
        subscriptionsForBundle = subscriptionInternalApi.getSubscriptionsForBundle(bundleId, null, internalTenantContext);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
    final List<EventsStream> eventsStreams = new LinkedList<EventsStream>();
    for (final SubscriptionBase subscription : subscriptionsForBundle) {
        eventsStreams.add(buildForEntitlement(bundle, subscription, subscriptionsForBundle, internalTenantContext));
    }
    return eventsStreams;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) EventsStream(org.killbill.billing.entitlement.EventsStream) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) LinkedList(java.util.LinkedList)

Example 24 with SubscriptionBase

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

the class EventsStreamBuilder method buildForBaseSubscription.

public EventsStream buildForBaseSubscription(final UUID bundleId, final TenantContext tenantContext) throws EntitlementApiException {
    final SubscriptionBase baseSubscription;
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(bundleId, ObjectType.BUNDLE, tenantContext);
        baseSubscription = subscriptionInternalApi.getBaseSubscription(bundleId, internalTenantContext);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
    return buildForEntitlement(baseSubscription.getId(), tenantContext);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 25 with SubscriptionBase

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

the class EventsStreamBuilder method buildForEntitlement.

private EventsStream buildForEntitlement(final SubscriptionBase subscription, final InternalTenantContext internalTenantContext) throws EntitlementApiException {
    final SubscriptionBaseBundle bundle;
    final List<SubscriptionBase> subscriptionsForBundle;
    try {
        bundle = subscriptionInternalApi.getBundleFromId(subscription.getBundleId(), internalTenantContext);
        subscriptionsForBundle = subscriptionInternalApi.getSubscriptionsForBundle(subscription.getBundleId(), null, internalTenantContext);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
    return buildForEntitlement(bundle, subscription, subscriptionsForBundle, internalTenantContext);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

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