add corresponding tests for caching in TenantService
This commit is contained in:
parent
2a93edbac4
commit
cc84069c68
@ -18,6 +18,12 @@ package org.thingsboard.server.dao.service;
|
|||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||||
|
import org.springframework.cache.Cache;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
|
import org.thingsboard.server.common.data.CacheConstants;
|
||||||
import org.thingsboard.server.common.data.Tenant;
|
import org.thingsboard.server.common.data.Tenant;
|
||||||
import org.thingsboard.server.common.data.TenantInfo;
|
import org.thingsboard.server.common.data.TenantInfo;
|
||||||
import org.thingsboard.server.common.data.TenantProfile;
|
import org.thingsboard.server.common.data.TenantProfile;
|
||||||
@ -27,16 +33,26 @@ import org.thingsboard.server.common.data.page.PageLink;
|
|||||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
|
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
|
||||||
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
|
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
|
||||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||||
|
import org.thingsboard.server.dao.tenant.TenantDao;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
|
||||||
public abstract class BaseTenantServiceTest extends AbstractServiceTest {
|
public abstract class BaseTenantServiceTest extends AbstractServiceTest {
|
||||||
|
|
||||||
private IdComparator<Tenant> idComparator = new IdComparator<>();
|
private IdComparator<Tenant> idComparator = new IdComparator<>();
|
||||||
|
|
||||||
|
@SpyBean
|
||||||
|
protected TenantDao tenantDao;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CacheManager cacheManager;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSaveTenant() {
|
public void testSaveTenant() {
|
||||||
Tenant tenant = new Tenant();
|
Tenant tenant = new Tenant();
|
||||||
@ -275,4 +291,69 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
|
|||||||
tenant.setTenantProfileId(isolatedTenantProfile.getId());
|
tenant.setTenantProfileId(isolatedTenantProfile.getId());
|
||||||
tenantService.saveTenant(tenant);
|
tenantService.saveTenant(tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGettingTenantAddItToCache() {
|
||||||
|
Tenant tenant = new Tenant();
|
||||||
|
tenant.setTitle("My tenant");
|
||||||
|
Tenant savedTenant = tenantService.saveTenant(tenant);
|
||||||
|
|
||||||
|
Mockito.reset(tenantDao);
|
||||||
|
Objects.requireNonNull(cacheManager.getCache(CacheConstants.TENANTS_CACHE), "Tenant cache manager is null").evict(savedTenant.getId());
|
||||||
|
|
||||||
|
Mockito.verify(tenantDao, Mockito.times(0)).findById(any(), any());
|
||||||
|
tenantService.findTenantById(savedTenant.getId());
|
||||||
|
Mockito.verify(tenantDao, Mockito.times(1)).findById(eq(savedTenant.getId()), eq(savedTenant.getId().getId()));
|
||||||
|
|
||||||
|
Cache.ValueWrapper cachedTenant =
|
||||||
|
Objects.requireNonNull(cacheManager.getCache(CacheConstants.TENANTS_CACHE), "Cache manager is null!").get(savedTenant.getId());
|
||||||
|
Assert.assertNotNull("Getting an existing Tenant doesn't add it to the cache!", cachedTenant);
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
tenantService.findTenantById(savedTenant.getId());
|
||||||
|
}
|
||||||
|
Mockito.verify(tenantDao, Mockito.times(1)).findById(eq(savedTenant.getId()), eq(savedTenant.getId().getId()));
|
||||||
|
|
||||||
|
tenantService.deleteTenant(savedTenant.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdatingExistingTenantEvictCache() {
|
||||||
|
Tenant tenant = new Tenant();
|
||||||
|
tenant.setTitle("My tenant");
|
||||||
|
Tenant savedTenant = tenantService.saveTenant(tenant);
|
||||||
|
|
||||||
|
Cache.ValueWrapper cachedTenant =
|
||||||
|
Objects.requireNonNull(cacheManager.getCache(CacheConstants.TENANTS_CACHE), "Cache manager is null!").get(savedTenant.getId());
|
||||||
|
Assert.assertNotNull("Saving a Tenant doesn't add it to the cache!", cachedTenant);
|
||||||
|
|
||||||
|
savedTenant.setTitle("My new tenant");
|
||||||
|
savedTenant = tenantService.saveTenant(savedTenant);
|
||||||
|
|
||||||
|
Mockito.reset(tenantDao);
|
||||||
|
|
||||||
|
cachedTenant = Objects.requireNonNull(cacheManager.getCache(CacheConstants.TENANTS_CACHE), "Cache manager is null!").get(savedTenant.getId());
|
||||||
|
Assert.assertNull("Updating a Tenant doesn't evict the cache!", cachedTenant);
|
||||||
|
|
||||||
|
Mockito.verify(tenantDao, Mockito.times(0)).findById(any(), any());
|
||||||
|
tenantService.findTenantById(savedTenant.getId());
|
||||||
|
Mockito.verify(tenantDao, Mockito.times(1)).findById(eq(savedTenant.getId()), eq(savedTenant.getId().getId()));
|
||||||
|
|
||||||
|
tenantService.deleteTenant(savedTenant.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemovingTenantEvictCache() {
|
||||||
|
Tenant tenant = new Tenant();
|
||||||
|
tenant.setTitle("My tenant");
|
||||||
|
Tenant savedTenant = tenantService.saveTenant(tenant);
|
||||||
|
|
||||||
|
Cache.ValueWrapper cachedTenant =
|
||||||
|
Objects.requireNonNull(cacheManager.getCache(CacheConstants.TENANTS_CACHE), "Cache manager is null!").get(savedTenant.getId());
|
||||||
|
Assert.assertNotNull("Saving a Tenant doesn't add it to the cache!", cachedTenant);
|
||||||
|
|
||||||
|
tenantService.deleteTenant(savedTenant.getId());
|
||||||
|
cachedTenant = Objects.requireNonNull(cacheManager.getCache(CacheConstants.TENANTS_CACHE), "Cache manager is null!").get(savedTenant.getId());
|
||||||
|
Assert.assertNull("Removing a Tenant doesn't evict the cache!", cachedTenant);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,6 +34,9 @@ caffeine.specs.claimDevices.maxSize=100000
|
|||||||
caffeine.specs.tenantProfiles.timeToLiveInMinutes=1440
|
caffeine.specs.tenantProfiles.timeToLiveInMinutes=1440
|
||||||
caffeine.specs.tenantProfiles.maxSize=100000
|
caffeine.specs.tenantProfiles.maxSize=100000
|
||||||
|
|
||||||
|
caffeine.specs.tenants.timeToLiveInMinutes=1440
|
||||||
|
caffeine.specs.tenants.maxSize=100000
|
||||||
|
|
||||||
caffeine.specs.deviceProfiles.timeToLiveInMinutes=1440
|
caffeine.specs.deviceProfiles.timeToLiveInMinutes=1440
|
||||||
caffeine.specs.deviceProfiles.maxSize=100000
|
caffeine.specs.deviceProfiles.maxSize=100000
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user