use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.
the class DefaultCatalogCache method initializeCacheLoaderArgument.
//
// Build the LoaderCallback that is required to build the catalog from the xml from a module that knows
// nothing about catalog.
//
// This is a contract between the TenantCatalogCacheLoader and the DefaultCatalogCache
private CacheLoaderArgument initializeCacheLoaderArgument(final boolean filterTemplateCatalog) {
final LoaderCallback loaderCallback = new LoaderCallback() {
@Override
public VersionedCatalog loadCatalog(final List<String> catalogXMLs, final Long tenantRecordId) throws CatalogApiException {
final VersionedCatalog versionedCatalog = loader.load(catalogXMLs, filterTemplateCatalog, tenantRecordId);
if (versionedCatalog != null) {
initializeCatalog(versionedCatalog);
}
return versionedCatalog;
}
};
final Object[] args = new Object[1];
args[0] = loaderCallback;
final ObjectType irrelevant = null;
final InternalTenantContext notUsed = null;
return new CacheLoaderArgument(irrelevant, args, notUsed);
}
use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.
the class DefaultCatalogCache method getCatalog.
@Override
public VersionedCatalog getCatalog(final boolean useDefaultCatalog, final boolean filterTemplateCatalog, final boolean internalUse, final InternalTenantContext tenantContext) throws CatalogApiException {
//
if (internalUse) {
Preconditions.checkState(tenantContext.getAccountRecordId() != null, "Unexpected null accountRecordId in context issued from internal Kill Bill service");
}
final VersionedCatalog pluginVersionedCatalog = getCatalogFromPlugins(tenantContext);
if (pluginVersionedCatalog != null) {
return pluginVersionedCatalog;
}
if (InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID.equals(tenantContext.getTenantRecordId())) {
return useDefaultCatalog ? defaultCatalog : null;
}
// but to be on the safe side;;
try {
DefaultVersionedCatalog tenantCatalog = cacheController.get(tenantContext.getTenantRecordId(), filterTemplateCatalog ? cacheLoaderArgumentWithTemplateFiltering : cacheLoaderArgument);
// for test purpose.
if (useDefaultCatalog && tenantCatalog == null) {
tenantCatalog = new DefaultVersionedCatalog();
for (final StaticCatalog cur : defaultCatalog.getVersions()) {
final StandaloneCatalogWithPriceOverride curWithOverride = new StandaloneCatalogWithPriceOverride(cur, priceOverride, tenantContext.getTenantRecordId(), internalCallContextFactory);
tenantCatalog.add(curWithOverride);
}
initializeCatalog(tenantCatalog);
cacheController.putIfAbsent(tenantContext.getTenantRecordId(), tenantCatalog);
}
return tenantCatalog;
} catch (final IllegalStateException e) {
throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, tenantContext.getTenantRecordId());
}
}
use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.
the class DefaultInvoiceUserApi method getInvoiceItemsByParentInvoice.
@Override
public List<InvoiceItem> getInvoiceItemsByParentInvoice(final UUID parentInvoiceId, final TenantContext context) throws InvoiceApiException {
final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(parentInvoiceId, ObjectType.INVOICE, context);
final VersionedCatalog catalog = getCatalogSafelyForPrettyNames(internalTenantContext);
return ImmutableList.copyOf(Collections2.transform(dao.getInvoiceItemsByParentInvoice(parentInvoiceId, internalTenantContext), new Function<InvoiceItemModelDao, InvoiceItem>() {
@Override
public InvoiceItem apply(final InvoiceItemModelDao input) {
return InvoiceItemFactory.fromModelDaoWithCatalog(input, catalog);
}
}));
}
use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.
the class TestWithCatalogPlugin method testWithMultipleVersions.
@Test(groups = "slow")
public void testWithMultipleVersions() throws Exception {
testCatalogPluginApi.addCatalogVersion("org/killbill/billing/catalog/versionedCatalog/WeaponsHireSmall-1.xml");
final VersionedCatalog catalog1 = catalogUserApi.getCatalog("whatever", callContext);
Assert.assertEquals(testCatalogPluginApi.getNbLatestCatalogVersionApiCalls(), 1);
Assert.assertEquals(testCatalogPluginApi.getNbVersionedPluginCatalogApiCalls(), 1);
Assert.assertEquals(catalog1.getVersions().get(0).getEffectiveDate().compareTo(testCatalogPluginApi.getLatestCatalogUpdate().toDate()), 0);
// Retrieve 3 more times
catalogUserApi.getCatalog("whatever", callContext);
catalogUserApi.getCatalog("whatever", callContext);
catalogUserApi.getCatalog("whatever", callContext);
Assert.assertEquals(testCatalogPluginApi.getNbLatestCatalogVersionApiCalls(), 4);
Assert.assertEquals(testCatalogPluginApi.getNbVersionedPluginCatalogApiCalls(), 1);
testCatalogPluginApi.addCatalogVersion("org/killbill/billing/catalog/versionedCatalog/WeaponsHireSmall-2.xml");
final VersionedCatalog catalog2 = catalogUserApi.getCatalog("whatever", callContext);
Assert.assertEquals(testCatalogPluginApi.getNbLatestCatalogVersionApiCalls(), 5);
Assert.assertEquals(testCatalogPluginApi.getNbVersionedPluginCatalogApiCalls(), 2);
Assert.assertEquals(catalog2.getVersions().get(1).getEffectiveDate().compareTo(testCatalogPluginApi.getLatestCatalogUpdate().toDate()), 0);
testCatalogPluginApi.addCatalogVersion("org/killbill/billing/catalog/versionedCatalog/WeaponsHireSmall-3.xml");
final VersionedCatalog catalog3 = catalogUserApi.getCatalog("whatever", callContext);
Assert.assertEquals(testCatalogPluginApi.getNbLatestCatalogVersionApiCalls(), 6);
Assert.assertEquals(testCatalogPluginApi.getNbVersionedPluginCatalogApiCalls(), 3);
Assert.assertEquals(catalog3.getVersions().get(2).getEffectiveDate().compareTo(testCatalogPluginApi.getLatestCatalogUpdate().toDate()), 0);
// Retrieve 4 more times
catalogUserApi.getCatalog("whatever", callContext);
catalogUserApi.getCatalog("whatever", callContext);
catalogUserApi.getCatalog("whatever", callContext);
catalogUserApi.getCatalog("whatever", callContext);
Assert.assertEquals(testCatalogPluginApi.getNbLatestCatalogVersionApiCalls(), 10);
Assert.assertEquals(testCatalogPluginApi.getNbVersionedPluginCatalogApiCalls(), 3);
}
use of org.killbill.billing.catalog.api.VersionedCatalog in project killbill by killbill.
the class TestCatalogWithEvents method testChangeWithUsagePlan.
@Test(groups = "slow")
public void testChangeWithUsagePlan() throws Exception {
final LocalDate today = new LocalDate(2020, 1, 1);
clock.setDay(today);
final VersionedCatalog catalog = catalogUserApi.getCatalog("foo", callContext);
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("water-monthly", null);
busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
final UUID subscriptionId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec, null, null, null), UUID.randomUUID().toString(), clock.getUTCToday(), clock.getUTCToday(), false, true, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
recordUsageData(subscriptionId, "t1", "liter", new LocalDate(2020, 1, 1), 10L, callContext);
recordUsageData(subscriptionId, "t2", "liter", new LocalDate(2020, 1, 23), 10L, callContext);
// 2020-2-1
busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
clock.addMonths(1);
assertListenerStatus();
final Invoice invoice1 = invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2020, 1, 1), new LocalDate(2020, 2, 1), InvoiceItemType.USAGE, new BigDecimal("30.00")));
invoiceChecker.checkTrackingIds(invoice1, ImmutableSet.of("t1", "t2"), internalCallContext);
Assert.assertTrue(invoice1.getInvoiceItems().get(0).getCatalogEffectiveDate().toDate().compareTo(catalog.getVersions().get(0).getEffectiveDate()) == 0);
final Subscription subscription1 = subscriptionApi.getSubscriptionForEntitlementId(subscriptionId, callContext);
final List<SubscriptionEvent> events1 = subscription1.getSubscriptionEvents();
Assert.assertEquals(events1.size(), 2);
Assert.assertTrue(events1.get(0).getNextPlan().getCatalog().getEffectiveDate().compareTo(catalog.getVersions().get(0).getEffectiveDate()) == 0);
// 2020-2-16 (V2 effDt = 2020-2-15)
clock.addDays(15);
busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE);
subscription1.changePlanWithDate(new DefaultEntitlementSpecifier(spec), clock.getUTCToday(), ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
final Subscription subscription2 = subscriptionApi.getSubscriptionForEntitlementId(subscriptionId, callContext);
final List<SubscriptionEvent> events2 = subscription2.getSubscriptionEvents();
Assert.assertEquals(events2.size(), 3);
Assert.assertTrue(events2.get(0).getNextPlan().getCatalog().getEffectiveDate().compareTo(catalog.getVersions().get(0).getEffectiveDate()) == 0);
// 2020-03-01
busHandler.pushExpectedEvents(NextEvent.INVOICE);
clock.addDays(14);
assertListenerStatus();
// 2020-3-16 (V3 effDt = 2020-3-15)
clock.addDays(15);
busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE);
subscription1.changePlanWithDate(new DefaultEntitlementSpecifier(spec), clock.getUTCToday(), ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
final Subscription subscription3 = subscriptionApi.getSubscriptionForEntitlementId(subscriptionId, callContext);
final List<SubscriptionEvent> events3 = subscription3.getSubscriptionEvents();
Assert.assertEquals(events3.size(), 4);
Assert.assertTrue(events3.get(0).getNextPlan().getCatalog().getEffectiveDate().compareTo(catalog.getVersions().get(0).getEffectiveDate()) == 0);
Assert.assertTrue(events3.get(1).getNextPlan().getCatalog().getEffectiveDate().compareTo(catalog.getVersions().get(0).getEffectiveDate()) == 0);
// Change catalog V2
Assert.assertTrue(events3.get(2).getNextPlan().getCatalog().getEffectiveDate().compareTo(catalog.getVersions().get(1).getEffectiveDate()) == 0);
// Change catalog V3
Assert.assertTrue(events3.get(3).getNextPlan().getCatalog().getEffectiveDate().compareTo(catalog.getVersions().get(2).getEffectiveDate()) == 0);
}
Aggregations