Browse Source
Merge pull request #5859 from smatvienko-tb/dao-is-exists
dao: existsById, existsByIdAsync added
pull/5869/head
Igor Kulikov
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
33 additions and
1 deletions
-
dao/src/main/java/org/thingsboard/server/dao/Dao.java
-
dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java
-
dao/src/test/java/org/thingsboard/server/dao/sql/tenant/JpaTenantDaoTest.java
|
|
|
@ -30,6 +30,10 @@ public interface Dao<T> { |
|
|
|
|
|
|
|
ListenableFuture<T> findByIdAsync(TenantId tenantId, UUID id); |
|
|
|
|
|
|
|
boolean existsById(TenantId tenantId, UUID id); |
|
|
|
|
|
|
|
ListenableFuture<Boolean> existsByIdAsync(TenantId tenantId, UUID id); |
|
|
|
|
|
|
|
T save(TenantId tenantId, T t); |
|
|
|
|
|
|
|
boolean removeById(TenantId tenantId, UUID id); |
|
|
|
|
|
|
|
@ -80,6 +80,18 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D> |
|
|
|
return service.submit(() -> DaoUtil.getData(getCrudRepository().findById(key))); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean existsById(TenantId tenantId, UUID key) { |
|
|
|
log.debug("Exists by key {}", key); |
|
|
|
return getCrudRepository().existsById(key); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<Boolean> existsByIdAsync(TenantId tenantId, UUID key) { |
|
|
|
log.debug("Exists by key async {}", key); |
|
|
|
return service.submit(() -> getCrudRepository().existsById(key)); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public boolean removeById(TenantId tenantId, UUID id) { |
|
|
|
|
|
|
|
@ -27,8 +27,9 @@ import org.thingsboard.server.dao.AbstractJpaDaoTest; |
|
|
|
import org.thingsboard.server.dao.service.AbstractServiceTest; |
|
|
|
import org.thingsboard.server.dao.tenant.TenantDao; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -75,4 +76,19 @@ public class JpaTenantDaoTest extends AbstractJpaDaoTest { |
|
|
|
tenantDao.save(AbstractServiceTest.SYSTEM_TENANT_ID, tenant); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
|
|
|
public void testIsExistsTenantById() { |
|
|
|
final UUID uuid = Uuids.timeBased(); |
|
|
|
final TenantId tenantId = new TenantId(uuid); |
|
|
|
assertThat(tenantDao.existsById(tenantId, uuid)).as("Is tenant exists before save").isFalse(); |
|
|
|
|
|
|
|
final Tenant tenant = new Tenant(); |
|
|
|
tenant.setId(tenantId); |
|
|
|
tenant.setTitle("Tenant " + uuid); |
|
|
|
tenantDao.save(AbstractServiceTest.SYSTEM_TENANT_ID, tenant); |
|
|
|
|
|
|
|
assertThat(tenantDao.existsById(tenantId, uuid)).as("Is tenant exists after save").isTrue(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|