Merge pull request #10741 from thingsboard/fix/attribute-query
Fix finding of attribute keys by entity id and scope
This commit is contained in:
commit
d0f9d01abd
@ -45,7 +45,7 @@ public interface AttributeKvRepository extends JpaRepository<AttributeKvEntity,
|
||||
@Query(value = "SELECT DISTINCT attribute_key FROM attribute_kv WHERE " +
|
||||
"entity_id in (SELECT id FROM device WHERE tenant_id = :tenantId and device_profile_id = :deviceProfileId limit 100) ORDER BY attribute_key", nativeQuery = true)
|
||||
List<Integer> findAllKeysByDeviceProfileId(@Param("tenantId") UUID tenantId,
|
||||
@Param("deviceProfileId") UUID deviceProfileId);
|
||||
@Param("deviceProfileId") UUID deviceProfileId);
|
||||
|
||||
@Query(value = "SELECT DISTINCT attribute_key FROM attribute_kv WHERE " +
|
||||
"entity_id in (SELECT id FROM device WHERE tenant_id = :tenantId limit 100) ORDER BY attribute_key", nativeQuery = true)
|
||||
@ -58,6 +58,6 @@ public interface AttributeKvRepository extends JpaRepository<AttributeKvEntity,
|
||||
@Query(value = "SELECT DISTINCT attribute_key FROM attribute_kv WHERE " +
|
||||
"entity_id in :entityIds AND attribute_type = :attributeType ORDER BY attribute_key", nativeQuery = true)
|
||||
List<Integer> findAllKeysByEntityIdsAndAttributeType(@Param("entityIds") List<UUID> entityIds,
|
||||
@Param("attributeType") String attributeType);
|
||||
@Param("attributeType") int attributeType);
|
||||
}
|
||||
|
||||
|
||||
@ -173,7 +173,7 @@ public class JpaAttributeDao extends JpaAbstractDaoListeningExecutorService impl
|
||||
@Override
|
||||
public List<String> findAllKeysByEntityIdsAndAttributeType(TenantId tenantId, List<EntityId> entityIds, String attributeType) {
|
||||
return attributeKvRepository
|
||||
.findAllKeysByEntityIdsAndAttributeType(entityIds.stream().map(EntityId::getId).collect(Collectors.toList()), attributeType)
|
||||
.findAllKeysByEntityIdsAndAttributeType(entityIds.stream().map(EntityId::getId).collect(Collectors.toList()), AttributeScope.valueOf(attributeType).getId())
|
||||
.stream().map(id -> keyDictionaryDao.getKey(id)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.ListeningExecutorService;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -47,6 +48,8 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Andrew Shvayka
|
||||
*/
|
||||
@ -221,6 +224,45 @@ public abstract class BaseAttributesServiceTest extends AbstractServiceTest {
|
||||
Assert.assertEquals(NEW_VALUE, value.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllKeysByEntityId() {
|
||||
var tenantId = new TenantId(UUID.randomUUID());
|
||||
var deviceId = new DeviceId(UUID.randomUUID());
|
||||
saveAttribute(tenantId, deviceId, AttributeScope.SERVER_SCOPE, "key1", "123");
|
||||
saveAttribute(tenantId, deviceId, AttributeScope.SERVER_SCOPE, "key2", "123");
|
||||
|
||||
Awaitility.await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> {
|
||||
List<String> keys = attributesService.findAllKeysByEntityIds(tenantId, List.of(deviceId));
|
||||
assertThat(keys).containsOnly("key1", "key2");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllKeysByEntityIdAndAttributeType() {
|
||||
var tenantId = new TenantId(UUID.randomUUID());
|
||||
var deviceId = new DeviceId(UUID.randomUUID());
|
||||
saveAttribute(tenantId, deviceId, AttributeScope.SERVER_SCOPE, "key1", "123");
|
||||
saveAttribute(tenantId, deviceId, AttributeScope.SERVER_SCOPE, "key2", "123");
|
||||
|
||||
Awaitility.await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> {
|
||||
List<String> keys = attributesService.findAllKeysByEntityIds(tenantId, List.of(deviceId), AttributeScope.SERVER_SCOPE.name());
|
||||
assertThat(keys).containsOnly("key1", "key2");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllByEntityIdAndAttributeType() {
|
||||
var tenantId = new TenantId(UUID.randomUUID());
|
||||
var deviceId = new DeviceId(UUID.randomUUID());
|
||||
saveAttribute(tenantId, deviceId, AttributeScope.SERVER_SCOPE, "key1", "123");
|
||||
saveAttribute(tenantId, deviceId, AttributeScope.SERVER_SCOPE, "key2", "123");
|
||||
|
||||
Awaitility.await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> {
|
||||
List<AttributeKvEntry> attributes = attributesService.findAll(tenantId, deviceId, AttributeScope.SERVER_SCOPE).get();
|
||||
assertThat(attributes).extracting(KvEntry::getKey).containsOnly("key1", "key2");
|
||||
});
|
||||
}
|
||||
|
||||
private void testConcurrentFetchAndUpdate(TenantId tenantId, DeviceId deviceId, ListeningExecutorService pool) throws Exception {
|
||||
var scope = AttributeScope.SERVER_SCOPE;
|
||||
var key = "TEST";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user