@ -16,17 +16,25 @@
package org.thingsboard.server.dao.service ;
import com.datastax.oss.driver.api.core.uuid.Uuids ;
import com.google.common.util.concurrent.ListeningExecutorService ;
import com.google.common.util.concurrent.MoreExecutors ;
import org.junit.AfterClass ;
import org.junit.Assert ;
import org.junit.BeforeClass ;
import org.junit.Test ;
import org.junit.jupiter.api.Assertions ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.transaction.PlatformTransactionManager ;
import org.springframework.transaction.TransactionStatus ;
import org.springframework.transaction.support.DefaultTransactionDefinition ;
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils ;
import org.testcontainers.shaded.org.awaitility.Awaitility ;
import org.thingsboard.common.util.ThingsBoardThreadFactory ;
import org.thingsboard.server.common.data.Customer ;
import org.thingsboard.server.common.data.EntitySubtype ;
import org.thingsboard.server.common.data.StringUtils ;
import org.thingsboard.server.common.data.Tenant ;
import org.thingsboard.server.common.data.TenantProfile ;
import org.thingsboard.server.common.data.asset.Asset ;
import org.thingsboard.server.common.data.asset.AssetInfo ;
import org.thingsboard.server.common.data.asset.AssetProfile ;
@ -44,6 +52,8 @@ import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink ;
import org.thingsboard.server.common.data.relation.EntityRelation ;
import org.thingsboard.server.common.data.relation.RelationTypeGroup ;
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration ;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData ;
import org.thingsboard.server.dao.asset.AssetDao ;
import org.thingsboard.server.dao.asset.AssetProfileService ;
import org.thingsboard.server.dao.asset.AssetService ;
@ -51,12 +61,16 @@ import org.thingsboard.server.dao.cf.CalculatedFieldService;
import org.thingsboard.server.dao.customer.CustomerService ;
import org.thingsboard.server.dao.exception.DataValidationException ;
import org.thingsboard.server.dao.relation.RelationService ;
import org.thingsboard.server.dao.tenant.TenantProfileService ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.Executors ;
import java.util.concurrent.TimeUnit ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatThrownBy ;
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID ;
@ -72,13 +86,28 @@ public class AssetServiceTest extends AbstractServiceTest {
@Autowired
RelationService relationService ;
@Autowired
TenantProfileService tenantProfileService ;
@Autowired
private AssetProfileService assetProfileService ;
@Autowired
private CalculatedFieldService calculatedFieldService ;
@Autowired
private PlatformTransactionManager platformTransactionManager ;
private static ListeningExecutorService executor ;
private IdComparator < Asset > idComparator = new IdComparator < > ( ) ;
private TenantId anotherTenantId ;
@BeforeClass
public static void before ( ) {
executor = MoreExecutors . listeningDecorator ( Executors . newFixedThreadPool ( 10 , ThingsBoardThreadFactory . forName ( "AssetServiceTestScope" ) ) ) ;
}
@AfterClass
public static void after ( ) {
executor . shutdownNow ( ) ;
}
@Test
public void testSaveAsset ( ) {
@ -105,6 +134,39 @@ public class AssetServiceTest extends AbstractServiceTest {
assetService . deleteAsset ( tenantId , savedAsset . getId ( ) ) ;
}
@Test
public void testAssetLimitOnTenantProfileLevel ( ) throws InterruptedException {
TenantProfile tenantProfile = new TenantProfile ( ) ;
tenantProfile . setName ( "Test profile" ) ;
tenantProfile . setDescription ( "Test" ) ;
TenantProfileData profileData = new TenantProfileData ( ) ;
profileData . setConfiguration ( DefaultTenantProfileConfiguration . builder ( ) . maxAssets ( 5l ) . build ( ) ) ;
tenantProfile . setProfileData ( profileData ) ;
tenantProfile . setDefault ( false ) ;
tenantProfile . setIsolatedTbRuleEngine ( false ) ;
tenantProfile = tenantProfileService . saveTenantProfile ( anotherTenantId , tenantProfile ) ;
anotherTenantId = createTenant ( tenantProfile . getId ( ) ) . getId ( ) ;
for ( int i = 0 ; i < 20 ; i + + ) {
executor . submit ( ( ) - > {
Asset asset = new Asset ( ) ;
asset . setTenantId ( anotherTenantId ) ;
asset . setName ( RandomStringUtils . randomAlphabetic ( 10 ) ) ;
asset . setType ( "default" ) ;
assetService . saveAsset ( asset ) ;
} ) ;
}
Awaitility . await ( ) . atMost ( 10 , TimeUnit . SECONDS ) . until ( ( ) - > {
long countByTenantId = assetService . countByTenantId ( anotherTenantId ) ;
return countByTenantId = = 5 ;
} ) ;
Thread . sleep ( 2000 ) ;
assertThat ( assetService . countByTenantId ( anotherTenantId ) ) . isEqualTo ( 5 ) ;
}
@Test
public void testShouldNotPutInCacheRolledbackAssetProfile ( ) {
AssetProfile assetProfile = new AssetProfile ( ) ;