Search in sources :

Example 31 with VersionedCatalog

use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.

the class TestInArrearWithCatalogVersions method testWithSubscriptionBCD.

@Test(groups = "slow")
public void testWithSubscriptionBCD() throws Exception {
    // 30 days month
    clock.setDay(new LocalDate(2016, 4, 1));
    final VersionedCatalog catalog = catalogUserApi.getCatalog("foo", callContext);
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("electricity-monthly");
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), null, null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    recordUsageData(entitlementId, "t1", "kilowatt-hour", new LocalDate(2016, 4, 5), 1L, callContext);
    recordUsageData(entitlementId, "t2", "kilowatt-hour", new LocalDate(2016, 4, 5), 99L, callContext);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    Invoice curInvoice = invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2016, 4, 1), new LocalDate(2016, 5, 1), InvoiceItemType.USAGE, new BigDecimal("150.00")));
    invoiceChecker.checkTrackingIds(curInvoice, ImmutableSet.of("t1", "t2"), internalCallContext);
    final Entitlement bp = entitlementApi.getEntitlementForId(entitlementId, callContext);
    recordUsageData(entitlementId, "t3", "kilowatt-hour", new LocalDate(2016, 5, 2), 100L, callContext);
    // Update subscription BCD
    bp.updateBCD(9, new LocalDate(2016, 5, 9), callContext);
    recordUsageData(entitlementId, "t4", "kilowatt-hour", new LocalDate(2016, 5, 10), 10L, callContext);
    busHandler.pushExpectedEvents(NextEvent.BCD_CHANGE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(9);
    assertListenerStatus();
    curInvoice = invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2016, 5, 1), new LocalDate(2016, 5, 8), InvoiceItemType.USAGE, new BigDecimal("150.00")), // Reach the catalog version change date for this subscription
    new ExpectedInvoiceItemCheck(new LocalDate(2016, 5, 8), new LocalDate(2016, 5, 9), InvoiceItemType.USAGE, new BigDecimal("0.00")));
    invoiceChecker.checkTrackingIds(curInvoice, ImmutableSet.of("t3"), internalCallContext);
    // Check items catalogEffectiveDate are correctly marked against each version
    assertEquals(curInvoice.getInvoiceItems().get(0).getCatalogEffectiveDate().toDate().compareTo(catalog.getVersions().get(0).getEffectiveDate()), 0);
    assertEquals(curInvoice.getInvoiceItems().get(1).getCatalogEffectiveDate().toDate().compareTo(catalog.getVersions().get(1).getEffectiveDate()), 0);
    // Original notification before we change BCD
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE);
    clock.addDays(23);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    clock.addDays(8);
    assertListenerStatus();
    // NOTE: Is using the new version of the catalog Utility-v2 (effectiveDateForExistingSubscriptions = 2016-05-08T00:00:00+00:00) what we want or is this a bug?
    curInvoice = invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2016, 5, 9), new LocalDate(2016, 6, 9), InvoiceItemType.USAGE, new BigDecimal("25.00")));
    invoiceChecker.checkTrackingIds(curInvoice, ImmutableSet.of("t4"), internalCallContext);
    // Check items catalogEffectiveDate is correctly set against last version
    assertEquals(curInvoice.getInvoiceItems().get(0).getCatalogEffectiveDate().toDate().compareTo(catalog.getVersions().get(1).getEffectiveDate()), 0);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) UUID(java.util.UUID) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 32 with VersionedCatalog

use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.

the class TestBillingApi method beforeMethod.

@BeforeMethod(groups = "fast")
public void beforeMethod() throws Exception {
    if (hasFailed()) {
        return;
    }
    super.beforeMethod();
    final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class);
    Mockito.when(bundle.getId()).thenReturn(bunId);
    final List<SubscriptionBaseBundle> bundles = ImmutableList.<SubscriptionBaseBundle>of(bundle);
    billingTransitions = new LinkedList<SubscriptionBillingEvent>();
    effectiveSubscriptionTransitions = new LinkedList<EffectiveSubscriptionInternalEvent>();
    final DateTime subscriptionStartDate = clock.getUTCNow().minusDays(3);
    subscription = new MockSubscription(subId, bunId, null, null, subscriptionStartDate, subscriptionStartDate);
    final List<SubscriptionBase> subscriptions = ImmutableList.<SubscriptionBase>of(subscription);
    // Mockito.when(subscription.getBillingAlignment(Mockito.<PlanPhaseSpecifier>any(), Mockito.<DateTime>any(), Mockito.<Catalog>any())).thenReturn(BillingAlignment.ACCOUNT);
    Mockito.when(subscriptionInternalApi.getBundlesForAccount(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(bundles);
    Mockito.when(subscriptionInternalApi.getSubscriptionsForBundle(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscriptions);
    Mockito.when(subscriptionInternalApi.getSubscriptionsForAccount(Mockito.<VersionedCatalog>any(), Mockito.<LocalDate>any(), Mockito.<InternalTenantContext>any())).thenReturn(ImmutableMap.<UUID, List<SubscriptionBase>>builder().put(bunId, subscriptions).build());
    Mockito.when(subscriptionInternalApi.getSubscriptionFromId(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscription);
    Mockito.when(subscriptionInternalApi.getBundleFromId(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(bundle);
    Mockito.when(subscriptionInternalApi.getBaseSubscription(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscription);
    Mockito.when(subscriptionInternalApi.getSubscriptionBillingEvents(Mockito.<VersionedCatalog>any(), Mockito.<SubscriptionBase>any(), Mockito.<InternalTenantContext>any())).thenReturn(billingTransitions);
    Mockito.when(subscriptionInternalApi.getAllTransitions(Mockito.<SubscriptionBase>any(), Mockito.<InternalTenantContext>any())).thenReturn(effectiveSubscriptionTransitions);
    final VersionedCatalog versionedCatalog = catalogService.getFullCatalog(true, true, internalCallContext);
    catalog = (MockCatalog) Iterables.getLast(versionedCatalog.getVersions());
    Mockito.when(catalogService.getFullCatalog(true, true, internalCallContext)).thenReturn(versionedCatalog);
    // Set a default alignment
    ((MockSubscription) subscription).setBillingAlignment(BillingAlignment.ACCOUNT);
    // Cleanup mock daos
    ((MockBlockingStateDao) blockingStateDao).clear();
    ((MockTagDao) tagDao).clear();
}
Also used : EffectiveSubscriptionInternalEvent(org.killbill.billing.events.EffectiveSubscriptionInternalEvent) MockSubscription(org.killbill.billing.mock.MockSubscription) MockBlockingStateDao(org.killbill.billing.entitlement.dao.MockBlockingStateDao) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) MockTagDao(org.killbill.billing.util.tag.dao.MockTagDao) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent) DefaultSubscriptionBillingEvent(org.killbill.billing.subscription.api.user.DefaultSubscriptionBillingEvent) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 33 with VersionedCatalog

use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.

the class CatalogResource method getCatalogJson.

@TimedResource
@GET
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve the catalog as JSON", responseContainer = "List", response = CatalogJson.class)
@ApiResponses(value = {})
public Response getCatalogJson(@QueryParam(QUERY_REQUESTED_DT) final String requestedDate, @QueryParam(QUERY_ACCOUNT_ID) final UUID accountId, @javax.ws.rs.core.Context final HttpServletRequest request) throws Exception {
    final TenantContext tenantContext = accountId != null ? context.createTenantContextWithAccountId(accountId, request) : context.createTenantContextNoAccountId(request);
    final DateTime catalogDateVersion = requestedDate != null ? DATE_TIME_FORMATTER.parseDateTime(requestedDate).toDateTime(DateTimeZone.UTC) : null;
    final VersionedCatalog catalog = catalogUserApi.getCatalog(catalogName, tenantContext);
    final Collection<CatalogJson> result = new ArrayList<CatalogJson>();
    if (catalogDateVersion == null) {
        for (final StaticCatalog v : catalog.getVersions()) {
            result.add(new CatalogJson(v));
        }
    } else {
        final StaticCatalog target = catalog.getVersion(catalogDateVersion.toDate());
        result.add(new CatalogJson(target));
    }
    return Response.status(Status.OK).entity(result).build();
}
Also used : VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) ArrayList(java.util.ArrayList) TenantContext(org.killbill.billing.util.callcontext.TenantContext) CatalogJson(org.killbill.billing.jaxrs.json.CatalogJson) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) DateTime(org.joda.time.DateTime) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)33 Test (org.testng.annotations.Test)14 DefaultVersionedCatalog (org.killbill.billing.catalog.DefaultVersionedCatalog)11 UUID (java.util.UUID)10 DateTime (org.joda.time.DateTime)8 LocalDate (org.joda.time.LocalDate)8 BigDecimal (java.math.BigDecimal)7 ArrayList (java.util.ArrayList)7 Account (org.killbill.billing.account.api.Account)7 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)7 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)7 Invoice (org.killbill.billing.invoice.api.Invoice)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)6 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)6 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)6 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4