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);
}
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();
}
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();
}
Aggregations