Browse Source

refactoring

pull/14206/head
dashevchenko 11 months ago
parent
commit
e8e8ca13ec
  1. 2
      dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java
  2. 2
      dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java
  3. 2
      dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java
  4. 2
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java
  5. 2
      dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java
  6. 3
      dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java
  7. 2
      dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java
  8. 2
      dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java
  9. 17
      dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java
  10. 16
      dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java

2
dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java

@ -148,7 +148,7 @@ public class BaseAssetService extends AbstractCachedEntityService<AssetCacheKey,
@Override @Override
public Asset saveAsset(Asset asset, boolean doValidate) { public Asset saveAsset(Asset asset, boolean doValidate) {
return saveLimitedEntity(asset, () -> doSaveAsset(asset, doValidate)); return saveEntity(asset, () -> doSaveAsset(asset, doValidate));
} }
private Asset doSaveAsset(Asset asset, boolean doValidate) { private Asset doSaveAsset(Asset asset, boolean doValidate) {

2
dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java

@ -139,7 +139,7 @@ public class CustomerServiceImpl extends AbstractCachedEntityService<CustomerCac
@Override @Override
@Transactional @Transactional
public Customer saveCustomer(Customer customer) { public Customer saveCustomer(Customer customer) {
return saveLimitedEntity(customer, () -> saveCustomer(customer, true)); return saveEntity(customer, () -> saveCustomer(customer, true));
} }
private Customer saveCustomer(Customer customer, boolean doValidate) { private Customer saveCustomer(Customer customer, boolean doValidate) {

2
dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java

@ -157,7 +157,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
@Override @Override
public Dashboard saveDashboard(Dashboard dashboard, boolean doValidate) { public Dashboard saveDashboard(Dashboard dashboard, boolean doValidate) {
return saveLimitedEntity(dashboard, () -> doSaveDashboard(dashboard, doValidate)); return saveEntity(dashboard, () -> doSaveDashboard(dashboard, doValidate));
} }
private Dashboard doSaveDashboard(Dashboard dashboard, boolean doValidate) { private Dashboard doSaveDashboard(Dashboard dashboard, boolean doValidate) {

2
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -210,7 +210,7 @@ public class DeviceServiceImpl extends CachedVersionedEntityService<DeviceCacheK
} }
private Device saveDeviceWithoutCredentials(Device device, boolean doValidate) { private Device saveDeviceWithoutCredentials(Device device, boolean doValidate) {
return saveLimitedEntity(device, () -> doSaveDeviceWithoutCredentials(device, doValidate)); return saveEntity(device, () -> doSaveDeviceWithoutCredentials(device, doValidate));
} }
private Device doSaveDeviceWithoutCredentials(Device device, boolean doValidate) { private Device doSaveDeviceWithoutCredentials(Device device, boolean doValidate) {

2
dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java

@ -201,7 +201,7 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
@Override @Override
public Edge saveEdge(Edge edge) { public Edge saveEdge(Edge edge) {
return saveLimitedEntity(edge, () -> doSaveEdge(edge)); return saveEntity(edge, () -> doSaveEdge(edge));
} }
private Edge doSaveEdge(Edge edge) { private Edge doSaveEdge(Edge edge) {

3
dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java

@ -94,8 +94,7 @@ public abstract class AbstractEntityService {
@Value("${debug.settings.default_duration:15}") @Value("${debug.settings.default_duration:15}")
private int defaultDebugDurationMinutes; private int defaultDebugDurationMinutes;
protected <E extends HasId & HasTenantId> E saveLimitedEntity(E entity, Supplier<E> saveFunction) { protected <E extends HasId & HasTenantId> E saveEntity(E entity, Supplier<E> saveFunction) {
log.debug("Creating limited entity: {}", entity);
if (entity.getId() == null) { if (entity.getId() == null) {
ReentrantLock lock = entityCreationLocks.computeIfAbsent(entity.getTenantId(), id -> new ReentrantLock()); ReentrantLock lock = entityCreationLocks.computeIfAbsent(entity.getTenantId(), id -> new ReentrantLock());
lock.lock(); lock.lock();

2
dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java

@ -125,7 +125,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
@Override @Override
@Transactional @Transactional
public RuleChain saveRuleChain(RuleChain ruleChain, boolean publishSaveEvent, boolean doValidate) { public RuleChain saveRuleChain(RuleChain ruleChain, boolean publishSaveEvent, boolean doValidate) {
return saveLimitedEntity(ruleChain, () -> doSaveRuleChain(ruleChain, publishSaveEvent, true)); return saveEntity(ruleChain, () -> doSaveRuleChain(ruleChain, publishSaveEvent, doValidate));
} }
private RuleChain doSaveRuleChain(RuleChain ruleChain, boolean publishSaveEvent, boolean doValidate) { private RuleChain doSaveRuleChain(RuleChain ruleChain, boolean publishSaveEvent, boolean doValidate) {

2
dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java

@ -159,7 +159,7 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
@Override @Override
@Transactional @Transactional
public User saveUser(TenantId tenantId, User user) { public User saveUser(TenantId tenantId, User user) {
return saveLimitedEntity(user, () -> doSaveUser(tenantId, user)); return saveEntity(user, () -> doSaveUser(tenantId, user));
} }
private User doSaveUser(TenantId tenantId, User user) { private User doSaveUser(TenantId tenantId, User user) {

17
dao/src/test/java/org/thingsboard/server/dao/service/AssetServiceTest.java

@ -18,9 +18,9 @@ package org.thingsboard.server.dao.service;
import com.datastax.oss.driver.api.core.uuid.Uuids; import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.MoreExecutors;
import org.junit.After; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -93,17 +93,18 @@ public class AssetServiceTest extends AbstractServiceTest {
@Autowired @Autowired
private PlatformTransactionManager platformTransactionManager; private PlatformTransactionManager platformTransactionManager;
private static ListeningExecutorService executor;
private IdComparator<Asset> idComparator = new IdComparator<>(); private IdComparator<Asset> idComparator = new IdComparator<>();
ListeningExecutorService executor;
private TenantId anotherTenantId; private TenantId anotherTenantId;
@Before @BeforeClass
public void before() { public static void before() {
executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope"))); executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10, ThingsBoardThreadFactory.forName("AssetServiceTestScope")));
} }
@After @AfterClass
public void after() { public static void after() {
executor.shutdownNow(); executor.shutdownNow();
} }

16
dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java

@ -19,8 +19,10 @@ import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.MoreExecutors;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -111,12 +113,21 @@ public class DeviceServiceTest extends AbstractServiceTest {
private IdComparator<Device> idComparator = new IdComparator<>(); private IdComparator<Device> idComparator = new IdComparator<>();
private TenantId anotherTenantId; private TenantId anotherTenantId;
private ListeningExecutorService executor; private static ListeningExecutorService executor;
@BeforeClass
public static void beforeClass() {
executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10, ThingsBoardThreadFactory.forName("DeviceServiceTestScope")));
}
@AfterClass
public static void afterClass() {
executor.shutdownNow();
}
@Before @Before
public void before() { public void before() {
anotherTenantId = createTenant().getId(); anotherTenantId = createTenant().getId();
executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope")));
} }
@After @After
@ -126,7 +137,6 @@ public class DeviceServiceTest extends AbstractServiceTest {
tenantProfileService.deleteTenantProfiles(tenantId); tenantProfileService.deleteTenantProfiles(tenantId);
tenantProfileService.deleteTenantProfiles(anotherTenantId); tenantProfileService.deleteTenantProfiles(anotherTenantId);
executor.shutdownNow();
} }
@Test @Test

Loading…
Cancel
Save