use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class TenantStateMachineConfigCacheLoader method load.
@Override
public Object load(final Object key, final Object argument) {
checkCacheLoaderStatus();
if (!(key instanceof String)) {
throw new IllegalArgumentException("Unexpected key type of " + key.getClass().getName());
}
if (!(argument instanceof CacheLoaderArgument)) {
throw new IllegalArgumentException("Unexpected key type of " + argument.getClass().getName());
}
final String[] parts = ((String) key).split(CacheControllerDispatcher.CACHE_KEY_SEPARATOR);
final String rawKey = parts[0];
final Matcher matcher = PATTERN.matcher(rawKey);
if (!matcher.matches()) {
throw new IllegalArgumentException("Unexpected key " + rawKey);
}
final String pluginName = matcher.group(1);
final String tenantRecordId = parts[1];
final CacheLoaderArgument cacheLoaderArgument = (CacheLoaderArgument) argument;
final LoaderCallback callback = (LoaderCallback) cacheLoaderArgument.getArgs()[0];
final InternalTenantContext internalTenantContext = new InternalTenantContext(Long.valueOf(tenantRecordId));
final String stateMachineConfigXML = tenantApi.getPluginPaymentStateMachineConfig(pluginName, internalTenantContext);
if (stateMachineConfigXML == null) {
return null;
}
try {
log.info("Loading config state machine cache for pluginName='{}', tenantRecordId='{}'", pluginName, internalTenantContext.getTenantRecordId());
return callback.loadStateMachineConfig(stateMachineConfigXML);
} catch (final PaymentApiException e) {
throw new IllegalStateException(String.format("Failed to de-serialize state machine config for tenantRecordId='%s'", internalTenantContext.getTenantRecordId()), e);
}
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class GuicyKillbillTestSuite method refreshCallContext.
public static void refreshCallContext(final UUID accountId, final Clock clock, final InternalCallContextFactory internalCallContextFactory, final TenantContext callContext, final MutableInternalCallContext internalCallContext) {
final InternalTenantContext tmp = internalCallContextFactory.createInternalTenantContext(accountId, callContext);
internalCallContext.setAccountRecordId(tmp.getAccountRecordId());
internalCallContext.setFixedOffsetTimeZone(tmp.getFixedOffsetTimeZone());
internalCallContext.setReferenceTime(tmp.getReferenceTime());
internalCallContext.setCreatedDate(clock.getUTCNow());
internalCallContext.setUpdatedDate(clock.getUTCNow());
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class TestDefaultCatalogCache method testExistingTenantCatalog.
//
// Verify CatalogCache returns per tenant catalog:
// 1. We first mock TenantInternalApi to return a different catalog than the default one
// 2. We then mock TenantInternalApi to throw RuntimeException which means catalog was cached and there was no additional call
// to the TenantInternalApi api (otherwise test would fail with RuntimeException)
//
@Test(groups = "fast")
public void testExistingTenantCatalog() throws CatalogApiException, URISyntaxException, IOException {
final InternalCallContext differentMultiTenantContext = Mockito.mock(InternalCallContext.class);
Mockito.when(differentMultiTenantContext.getTenantRecordId()).thenReturn(55667788L);
final AtomicBoolean shouldThrow = new AtomicBoolean(false);
final Long multiTenantRecordId = multiTenantContext.getTenantRecordId();
final Long otherMultiTenantRecordId = otherMultiTenantContext.getTenantRecordId();
final InputStream tenantInputCatalog = UriAccessor.accessUri(new URI(Resources.getResource("org/killbill/billing/catalog/SpyCarAdvanced.xml").toExternalForm()));
final String tenantCatalogXML = CharStreams.toString(new InputStreamReader(tenantInputCatalog, "UTF-8"));
final InputStream otherTenantInputCatalog = UriAccessor.accessUri(new URI(Resources.getResource("org/killbill/billing/catalog/SpyCarBasic.xml").toExternalForm()));
final String otherTenantCatalogXML = CharStreams.toString(new InputStreamReader(otherTenantInputCatalog, "UTF-8"));
Mockito.when(tenantInternalApi.getTenantCatalogs(Mockito.any(InternalTenantContext.class))).thenAnswer(new Answer<List<String>>() {
@Override
public List<String> answer(final InvocationOnMock invocation) throws Throwable {
if (shouldThrow.get()) {
throw new RuntimeException();
}
final InternalTenantContext internalContext = (InternalTenantContext) invocation.getArguments()[0];
if (multiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return ImmutableList.<String>of(tenantCatalogXML);
} else if (otherMultiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return ImmutableList.<String>of(otherTenantCatalogXML);
} else {
return ImmutableList.<String>of();
}
}
});
// Verify the lookup for a non-cached tenant. No system config is set yet but DefaultCatalogCache returns a default empty one
VersionedCatalog differentResult = catalogCache.getCatalog(true, true, false, differentMultiTenantContext);
Assert.assertNotNull(differentResult);
Assert.assertEquals(differentResult.getCatalogName(), "EmptyCatalog");
// Make sure the cache loader isn't invoked, see https://github.com/killbill/killbill/issues/300
shouldThrow.set(true);
differentResult = catalogCache.getCatalog(true, true, false, differentMultiTenantContext);
Assert.assertNotNull(differentResult);
Assert.assertEquals(differentResult.getCatalogName(), "EmptyCatalog");
shouldThrow.set(false);
// Set a default config
catalogCache.loadDefaultCatalog(Resources.getResource("org/killbill/billing/catalog/SpyCarBasic.xml").toExternalForm());
// Verify the lookup for this tenant
final VersionedCatalog result = catalogCache.getCatalog(true, true, false, multiTenantContext);
Assert.assertNotNull(result);
final StaticCatalog catalogVersion = result.getVersions().get(result.getVersions().size() - 1);
final Collection<Product> products = catalogVersion.getProducts();
Assert.assertEquals(products.size(), 6);
// Verify the lookup for another tenant
final VersionedCatalog otherResult = catalogCache.getCatalog(true, true, false, otherMultiTenantContext);
Assert.assertNotNull(otherResult);
final StaticCatalog othercatalogVersion = otherResult.getVersions().get(result.getVersions().size() - 1);
final Collection<Product> otherProducts = othercatalogVersion.getProducts();
Assert.assertEquals(otherProducts.size(), 3);
shouldThrow.set(true);
// Verify the lookup for this tenant
final VersionedCatalog result2 = catalogCache.getCatalog(true, true, false, multiTenantContext);
Assert.assertEquals(result2, result);
// Verify the lookup with another context for the same tenant
final InternalCallContext sameMultiTenantContext = Mockito.mock(InternalCallContext.class);
Mockito.when(sameMultiTenantContext.getAccountRecordId()).thenReturn(9102L);
Mockito.when(sameMultiTenantContext.getTenantRecordId()).thenReturn(multiTenantRecordId);
Assert.assertEquals(catalogCache.getCatalog(true, true, false, sameMultiTenantContext), result);
// Verify the lookup with the other tenant
Assert.assertEquals(catalogCache.getCatalog(true, true, false, otherMultiTenantContext), otherResult);
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class DefaultImmutableAccountInternalApi method createImmutableAccountCacheLoaderArgument.
private CacheLoaderArgument createImmutableAccountCacheLoaderArgument(final InternalTenantContext context) {
final LoaderCallback loaderCallback = new LoaderCallback() {
@Override
public ImmutableAccountData loadAccount(final Long recordId, final InternalTenantContext context) {
final Account account = getAccountByRecordIdInternal(recordId, context);
return account != null ? new DefaultImmutableAccountData(account) : null;
}
};
final Object[] args = { loaderCallback };
return new CacheLoaderArgument(null, args, context);
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class DefaultEntitlementApi method getAllEntitlementsForAccountId.
@Override
public List<Entitlement> getAllEntitlementsForAccountId(final UUID accountId, final TenantContext tenantContext) throws EntitlementApiException {
final EntitlementApi entitlementApi = this;
final InternalTenantContext context = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
final AccountEventsStreams accountEventsStreams = eventsStreamBuilder.buildForAccount(context);
final List<EventsStream> eventsStreams = ImmutableList.<EventsStream>copyOf(Iterables.<EventsStream>concat(accountEventsStreams.getEventsStreams().values()));
return Lists.<EventsStream, Entitlement>transform(eventsStreams, new Function<EventsStream, Entitlement>() {
@Override
public Entitlement apply(final EventsStream eventsStream) {
return new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionBaseInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, context, internalCallContextFactory);
}
});
}
Aggregations