Search in sources :

Example 81 with InternalTenantContext

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);
}
Also used : PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext)

Example 82 with InternalTenantContext

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OverdueConfig(org.killbill.billing.overdue.api.OverdueConfig) Test(org.testng.annotations.Test)

Example 83 with InternalTenantContext

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;
}
Also used : TenantKVModelDao(org.killbill.billing.tenant.dao.TenantKVModelDao) HashMap(java.util.HashMap) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 84 with InternalTenantContext

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());
        }
    }));
}
Also used : Function(com.google.common.base.Function) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) DryRunTenantContext(org.killbill.billing.usage.DryRunTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) DryRunTenantContext(org.killbill.billing.usage.DryRunTenantContext) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao)

Example 85 with InternalTenantContext

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);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException)

Aggregations

InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)92 UUID (java.util.UUID)15 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)13 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)11 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)10 ArrayList (java.util.ArrayList)9 ObjectType (org.killbill.billing.ObjectType)9 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)9 TenantContext (org.killbill.billing.util.callcontext.TenantContext)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 DateTime (org.joda.time.DateTime)7 LocalDate (org.joda.time.LocalDate)7 Predicate (com.google.common.base.Predicate)6 AccountApiException (org.killbill.billing.account.api.AccountApiException)6 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)6 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)6