Merge pull request #5859 from smatvienko-tb/dao-is-exists

dao: existsById, existsByIdAsync added
This commit is contained in:
Igor Kulikov 2022-01-12 10:56:47 +02:00 committed by GitHub
commit 1f0d6ad92a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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();
}
}