diff --git a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/AdminResource.java b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/AdminResource.java
index acee5e2..fc21dcf 100644
--- a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/AdminResource.java
+++ b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/AdminResource.java
@@ -32,6 +32,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
+import org.killbill.billing.ObjectType;
import org.killbill.billing.account.api.AccountUserApi;
import org.killbill.billing.jaxrs.json.AdminPaymentJson;
import org.killbill.billing.jaxrs.util.Context;
@@ -48,9 +49,11 @@ import org.killbill.billing.tenant.api.TenantApiException;
import org.killbill.billing.tenant.api.TenantUserApi;
import org.killbill.billing.util.api.AuditUserApi;
import org.killbill.billing.util.api.CustomFieldUserApi;
+import org.killbill.billing.util.api.RecordIdApi;
import org.killbill.billing.util.api.TagUserApi;
import org.killbill.billing.util.cache.Cachable.CacheType;
import org.killbill.billing.util.callcontext.CallContext;
+import org.killbill.billing.util.callcontext.TenantContext;
import org.killbill.clock.Clock;
import com.google.common.base.Predicate;
@@ -74,12 +77,14 @@ public class AdminResource extends JaxRsResourceBase {
private final AdminPaymentApi adminPaymentApi;
private final TenantUserApi tenantApi;
private final CacheManager cacheManager;
+ private final RecordIdApi recordIdApi;
@Inject
- public AdminResource(final JaxrsUriBuilder uriBuilder, final TagUserApi tagUserApi, final CustomFieldUserApi customFieldUserApi, final AuditUserApi auditUserApi, final AccountUserApi accountUserApi, final PaymentApi paymentApi, final AdminPaymentApi adminPaymentApi, final CacheManager cacheManager, final TenantUserApi tenantApi, final Clock clock, final Context context) {
+ public AdminResource(final JaxrsUriBuilder uriBuilder, final TagUserApi tagUserApi, final CustomFieldUserApi customFieldUserApi, final AuditUserApi auditUserApi, final AccountUserApi accountUserApi, final PaymentApi paymentApi, final AdminPaymentApi adminPaymentApi, final CacheManager cacheManager, final TenantUserApi tenantApi, final RecordIdApi recordIdApi, final Clock clock, final Context context) {
super(uriBuilder, tagUserApi, customFieldUserApi, auditUserApi, accountUserApi, paymentApi, null, clock, context);
this.adminPaymentApi = adminPaymentApi;
this.tenantApi = tenantApi;
+ this.recordIdApi = recordIdApi;
this.cacheManager = cacheManager;
}
@@ -171,36 +176,43 @@ public class AdminResource extends JaxRsResourceBase {
public Response invalidatesCacheByTenant(@QueryParam("tenantApiKey") final String tenantApiKey,
@javax.ws.rs.core.Context final HttpServletRequest request) throws TenantApiException {
- // getting Tenant information from the given Api Key
- Tenant tenant = tenantApi.getTenantByApiKey(tenantApiKey);
+ // creating Tenant Context from Request
+ TenantContext tenantContext = context.createContext(request);
+
+ Tenant currentTenant = tenantApi.getTenantById(tenantContext.getTenantId());
+
+ // getting Tenant Record Id
+ Long tenantRecordId = recordIdApi.getRecordId(tenantContext.getTenantId(), ObjectType.TENANT, tenantContext);
// clear tenant-record-id cache by tenantId
final Ehcache tenantRecordIdCache = cacheManager.getEhcache(CacheType.TENANT_RECORD_ID.getCacheName());
- tenantRecordIdCache.remove(tenant.getId().toString());
+ tenantRecordIdCache.remove(currentTenant.getId().toString());
- // clear tenant-payment-state-machine-config cache by tenantId
+ // clear tenant-payment-state-machine-config cache by tenantRecordId
final Ehcache tenantPaymentStateMachineConfigCache = cacheManager.getEhcache(CacheType.TENANT_PAYMENT_STATE_MACHINE_CONFIG.getCacheName());
- tenantPaymentStateMachineConfigCache.remove(tenant.getId().toString());
+ String tenantPaymentStateMachineConfigCacheKey = "PLUGIN_PAYMENT_STATE_MACHINE_noop::" + tenantRecordId.toString();
+ tenantPaymentStateMachineConfigCache.remove(tenantPaymentStateMachineConfigCacheKey);
- // clear tenant cache by tenantId
+ // clear tenant cache by tenantApiKey
final Ehcache tenantCache = cacheManager.getEhcache(CacheType.TENANT.getCacheName());
- tenantCache.remove(tenant.getId().toString());
+ tenantCache.remove(currentTenant.getApiKey());
- // clear tenant-kv cache by tenantId
+ // clear tenant-kv cache by tenantRecordId
final Ehcache tenantKvCache = cacheManager.getEhcache(CacheType.TENANT_KV.getCacheName());
- tenantKvCache.remove(tenant.getId().toString());
+ String tenantKvCacheKey = "PUSH_NOTIFICATION_CB::" + tenantRecordId.toString();
+ tenantKvCache.remove(tenantKvCacheKey);
- // clear tenant-config cache by tenantId
+ // clear tenant-config cache by tenantRecordId
final Ehcache tenantConfigCache = cacheManager.getEhcache(CacheType.TENANT_CONFIG.getCacheName());
- tenantConfigCache.remove(tenant.getId().toString());
+ tenantConfigCache.remove(tenantRecordId);
- // clear tenant-overdue-config cache by tenantId
+ // clear tenant-overdue-config cache by tenantRecordId
final Ehcache tenantOverdueConfigCache = cacheManager.getEhcache(CacheType.TENANT_OVERDUE_CONFIG.getCacheName());
- tenantOverdueConfigCache.remove(tenant.getId().toString());
+ tenantOverdueConfigCache.remove(tenantRecordId);
- // clear tenant-catalog cache by tenantId
+ // clear tenant-catalog cache by tenantRecordId
final Ehcache tenantCatalogCache = cacheManager.getEhcache(CacheType.TENANT_CATALOG.getCacheName());
- tenantCatalogCache.remove(tenant.getId().toString());
+ tenantCatalogCache.remove(tenantRecordId);
return Response.status(Status.OK).build();
}
diff --git a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestCache.java b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestCache.java
index f227081..6bf8368 100644
--- a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestCache.java
+++ b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestCache.java
@@ -17,7 +17,7 @@
package org.killbill.billing.jaxrs;
import org.killbill.billing.client.model.Account;
-import org.killbill.billing.client.model.Tenant;
+import org.killbill.billing.tenant.api.DefaultTenant;
import org.killbill.billing.util.cache.Cachable.CacheType;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -82,15 +82,9 @@ public class TestCache extends TestJaxrsBase {
Assert.assertNull(accountBcdCache.get(input.getAccountId()));
}
- @Test(groups = "slow", description = "Can Invalidate (clear) all Tenant Caches by tenantId")
+ @Test(groups = "slow", description = "Can Invalidate (clear) all Tenant Caches for current Tenant")
public void testInvalidateCacheByTenant() throws Exception {
- // create a new tenant so it can be stored in the cache
- final Tenant tenant = new Tenant();
- tenant.setApiKey("testApiKey");
- tenant.setApiSecret("testApiSecret");
- final Tenant input = killBillClient.createTenant(tenant, false, requestOptions);
-
createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();
// get all caches per tenant level
@@ -102,32 +96,36 @@ public class TestCache extends TestJaxrsBase {
final Ehcache tenantOverdueConfigCache = cacheManager.getEhcache(CacheType.TENANT_OVERDUE_CONFIG.getCacheName());
final Ehcache tenantCatalogCache = cacheManager.getEhcache(CacheType.TENANT_CATALOG.getCacheName());
- // verify that they are not null and empty
+ DefaultTenant currentTenant = (DefaultTenant) tenantCache.get(tenantCache.getKeys().get(0)).getObjectValue();
+
+ // verify that they are not null and have the expected tenant information
Assert.assertNotNull(tenantRecordIdCache);
- Assert.assertFalse(tenantRecordIdCache.getKeys().isEmpty());
+ Assert.assertNotNull(tenantRecordIdCache.get(currentTenant.getId().toString()));
Assert.assertNotNull(tenantPaymentStateMachineConfigCache);
- Assert.assertFalse(tenantPaymentStateMachineConfigCache.getKeys().isEmpty());
+ String tenantPaymentStateMachineConfigCacheKey = "PLUGIN_PAYMENT_STATE_MACHINE_noop::1";
+ Assert.assertNotNull(tenantPaymentStateMachineConfigCache.get(tenantPaymentStateMachineConfigCacheKey));
Assert.assertNotNull(tenantCache);
- Assert.assertFalse(tenantCache.getKeys().isEmpty());
+ Assert.assertNotNull(tenantCache.get(DEFAULT_API_KEY));
Assert.assertNotNull(tenantKvCache);
- Assert.assertFalse(tenantKvCache.getKeys().isEmpty());
+ String tenantKvCacheKey = "PUSH_NOTIFICATION_CB::1";
+ Assert.assertNotNull(tenantKvCache.get(tenantKvCacheKey));
Assert.assertNotNull(tenantConfigCache);
- Assert.assertFalse(tenantConfigCache.getKeys().isEmpty());
+ Assert.assertNotNull(tenantConfigCache.get(1L));
Assert.assertNotNull(tenantOverdueConfigCache);
- Assert.assertFalse(tenantOverdueConfigCache.getKeys().isEmpty());
+ Assert.assertNotNull(tenantOverdueConfigCache.get(1L));
Assert.assertNotNull(tenantCatalogCache);
- Assert.assertFalse(tenantCatalogCache.getKeys().isEmpty());
+ Assert.assertNotNull(tenantCatalogCache.get(1L));
// invalidate caches per tenant level by tenantId
- killBillClient.invalidateCacheByTenant(input.getApiKey(), requestOptions);
-
- // TODO: verify that now the caches are empty
-// Assert.assertTrue(tenantRecordIdCache.getKeys().isEmpty());
-// Assert.assertTrue(tenantPaymentStateMachineConfigCache.getKeys().isEmpty());
-// Assert.assertTrue(tenantCache.getKeys().isEmpty());
-// Assert.assertTrue(tenantKvCache.getKeys().isEmpty());
-// Assert.assertTrue(tenantConfigCache.getKeys().isEmpty());
-// Assert.assertTrue(tenantOverdueConfigCache.getKeys().isEmpty());
-// Assert.assertTrue(tenantCatalogCache.getKeys().isEmpty());
+ killBillClient.invalidateCacheByTenant(requestOptions);
+
+ // verify that now the caches don't have the previous values
+ Assert.assertNull(tenantRecordIdCache.get(currentTenant.getId().toString()));
+ Assert.assertNull(tenantPaymentStateMachineConfigCache.get(tenantPaymentStateMachineConfigCacheKey));
+ Assert.assertNull(tenantCache.get(DEFAULT_API_KEY));
+ Assert.assertNull(tenantKvCache.get(tenantKvCacheKey));
+ Assert.assertNull(tenantConfigCache.get(1L));
+ Assert.assertNull(tenantOverdueConfigCache.get(1L));
+ Assert.assertNull(tenantCatalogCache.get(1L));
}
}