use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class PaymentRefresher method toPayment.
private Payment toPayment(final PaymentModelDao paymentModelDao, @Nullable final Iterable<PaymentTransactionInfoPlugin> pluginTransactions, final boolean withAttempts, final boolean isApiPayment, final InternalTenantContext tenantContext) {
final InternalTenantContext tenantContextWithAccountRecordId = getInternalTenantContextWithAccountRecordId(paymentModelDao.getAccountId(), tenantContext);
final List<PaymentTransactionModelDao> transactionsForPayment = paymentDao.getTransactionsForPayment(paymentModelDao.getId(), tenantContextWithAccountRecordId);
return toPayment(paymentModelDao, transactionsForPayment, pluginTransactions, withAttempts, isApiPayment, tenantContextWithAccountRecordId);
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class TestDefaultOverdueConfigCache method testExistingTenantOverdue.
//
// Verify OverdueConfigCache returns per tenant overdue config:
// 1. We first mock TenantInternalApi to return a different overdue config than the default one
// 2. We then mock TenantInternalApi to throw RuntimeException which means overdue config was cached and there was no additional call
// to the TenantInternalApi api (otherwise test would fail with RuntimeException)
//
@Test(groups = "fast")
public void testExistingTenantOverdue() throws OverdueApiException, 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 tenantInputOverdueConfig = UriAccessor.accessUri(new URI(Resources.getResource("org/killbill/billing/overdue/OverdueConfig2.xml").toExternalForm()));
final String tenantOverdueConfigXML = CharStreams.toString(new InputStreamReader(tenantInputOverdueConfig, "UTF-8"));
final InputStream otherTenantInputOverdueConfig = UriAccessor.accessUri(new URI(Resources.getResource("org/killbill/billing/overdue/OverdueConfig.xml").toExternalForm()));
final String otherTenantOverdueConfigXML = CharStreams.toString(new InputStreamReader(otherTenantInputOverdueConfig, "UTF-8"));
Mockito.when(tenantInternalApi.getTenantOverdueConfig(Mockito.any(InternalTenantContext.class))).thenAnswer(new Answer<String>() {
@Override
public 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 tenantOverdueConfigXML;
} else if (otherMultiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return otherTenantOverdueConfigXML;
} else {
return null;
}
}
});
// Verify the lookup for a non-cached tenant. No system config is set yet but DefaultOverdueConfigCache returns a default no-op one
OverdueConfig differentResult = overdueConfigCache.getOverdueConfig(differentMultiTenantContext);
Assert.assertNotNull(differentResult);
Assert.assertEquals(differentResult.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(differentResult.getOverdueStatesAccount().getStates()[0].isClearState());
// Make sure the cache loader isn't invoked, see https://github.com/killbill/killbill/issues/298
shouldThrow.set(true);
differentResult = overdueConfigCache.getOverdueConfig(differentMultiTenantContext);
Assert.assertNotNull(differentResult);
Assert.assertEquals(differentResult.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(differentResult.getOverdueStatesAccount().getStates()[0].isClearState());
shouldThrow.set(false);
// Set a default config
overdueConfigCache.loadDefaultOverdueConfig(Resources.getResource("org/killbill/billing/overdue/OverdueConfig.xml").toExternalForm());
// Verify the lookup for this tenant
final OverdueConfig result = overdueConfigCache.getOverdueConfig(multiTenantContext);
Assert.assertNotNull(result);
Assert.assertEquals(result.getOverdueStatesAccount().getStates().length, 1);
Assert.assertFalse(result.getOverdueStatesAccount().getStates()[0].isClearState());
// Verify the lookup for another tenant
final OverdueConfig otherResult = overdueConfigCache.getOverdueConfig(otherMultiTenantContext);
Assert.assertNotNull(otherResult);
Assert.assertEquals(otherResult.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(otherResult.getOverdueStatesAccount().getStates()[0].isClearState());
shouldThrow.set(true);
// Verify the lookup for this tenant
final OverdueConfig result2 = overdueConfigCache.getOverdueConfig(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(overdueConfigCache.getOverdueConfig(sameMultiTenantContext), result);
// Verify the lookup with the other tenant
Assert.assertEquals(overdueConfigCache.getOverdueConfig(otherMultiTenantContext), otherResult);
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class DefaultTenantUserApi method searchTenantKeyValues.
@Override
public Map<String, List<String>> searchTenantKeyValues(String searchKey, TenantContext context) throws TenantApiException {
final InternalTenantContext internalContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context);
final List<TenantKVModelDao> daoResult = tenantDao.searchTenantKeyValues(searchKey, internalContext);
final Map<String, List<String>> result = new HashMap<String, List<String>>();
for (final TenantKVModelDao cur : daoResult) {
if (!result.containsKey(cur.getTenantKey())) {
result.put(cur.getTenantKey(), new ArrayList<String>());
}
result.get(cur.getTenantKey()).add(cur.getTenantValue());
}
return result;
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class DefaultInternalUserApi method getRawUsageForAccount.
@Override
public List<RawUsageRecord> getRawUsageForAccount(final LocalDate startDate, final LocalDate endDate, @Nullable final DryRunInfo dryRunInfo, final InternalTenantContext internalTenantContext) {
log.info("GetRawUsageForAccount startDate='{}', endDate='{}'", startDate, endDate);
final TenantContext tenantContext = internalCallContextFactory.createTenantContext(internalTenantContext);
final TenantContext tenantContextWithHint = dryRunInfo != null ? new DryRunTenantContext(dryRunInfo, tenantContext) : tenantContext;
final List<RawUsageRecord> resultFromPlugin = getAccountUsageFromPlugin(startDate, endDate, tenantContextWithHint);
if (resultFromPlugin != null) {
return resultFromPlugin;
}
final List<RolledUpUsageModelDao> usage = rolledUpUsageDao.getRawUsageForAccount(startDate, endDate, internalTenantContext);
return ImmutableList.copyOf(Iterables.transform(usage, new Function<RolledUpUsageModelDao, RawUsageRecord>() {
@Override
public RawUsageRecord apply(final RolledUpUsageModelDao input) {
return new DefaultRawUsage(input.getSubscriptionId(), input.getRecordDate(), input.getUnitType(), input.getAmount(), input.getTrackingId());
}
}));
}
use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.
the class DefaultOverdueApi method uploadOverdueConfig.
@Override
public void uploadOverdueConfig(final String overdueXML, final CallContext callContext) throws OverdueApiException {
try {
final InternalTenantContext internalTenantContext = createInternalTenantContext(callContext);
final String tenantKey = TenantKey.OVERDUE_CONFIG.toString();
if (!tenantApi.getTenantValuesForKey(tenantKey, callContext).isEmpty()) {
tenantApi.deleteTenantKey(tenantKey, callContext);
}
tenantApi.addTenantKeyValue(tenantKey, overdueXML, callContext);
overdueConfigCache.clearOverdueConfig(internalTenantContext);
} catch (final TenantApiException e) {
throw new OverdueApiException(e);
}
}
Aggregations