Search in sources :

Example 1 with EntitySubtype

use of org.thingsboard.server.common.data.EntitySubtype in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testFindDeviceTypesByTenantId.

@Test
public void testFindDeviceTypesByTenantId() throws Exception {
    List<Device> devices = new ArrayList<>();
    try {
        for (int i = 0; i < 3; i++) {
            Device device = new Device();
            device.setTenantId(tenantId);
            device.setName("My device B" + i);
            device.setType("typeB");
            devices.add(deviceService.saveDevice(device));
        }
        for (int i = 0; i < 7; i++) {
            Device device = new Device();
            device.setTenantId(tenantId);
            device.setName("My device C" + i);
            device.setType("typeC");
            devices.add(deviceService.saveDevice(device));
        }
        for (int i = 0; i < 9; i++) {
            Device device = new Device();
            device.setTenantId(tenantId);
            device.setName("My device A" + i);
            device.setType("typeA");
            devices.add(deviceService.saveDevice(device));
        }
        List<EntitySubtype> deviceTypes = deviceService.findDeviceTypesByTenantId(tenantId).get();
        Assert.assertNotNull(deviceTypes);
        Assert.assertEquals(3, deviceTypes.size());
        Assert.assertEquals("typeA", deviceTypes.get(0).getType());
        Assert.assertEquals("typeB", deviceTypes.get(1).getType());
        Assert.assertEquals("typeC", deviceTypes.get(2).getType());
    } finally {
        devices.forEach((device) -> {
            deviceService.deleteDevice(device.getId());
        });
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Test(org.junit.Test)

Example 2 with EntitySubtype

use of org.thingsboard.server.common.data.EntitySubtype in project thingsboard by thingsboard.

the class CassandraDeviceDao method findTenantDeviceTypesAsync.

@Override
public ListenableFuture<List<EntitySubtype>> findTenantDeviceTypesAsync(UUID tenantId) {
    Select select = select().from(ENTITY_SUBTYPE_COLUMN_FAMILY_NAME);
    Select.Where query = select.where();
    query.and(eq(ENTITY_SUBTYPE_TENANT_ID_PROPERTY, tenantId));
    query.and(eq(ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY, EntityType.DEVICE));
    query.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
    ResultSetFuture resultSetFuture = executeAsyncRead(query);
    return Futures.transform(resultSetFuture, new Function<ResultSet, List<EntitySubtype>>() {

        @Nullable
        @Override
        public List<EntitySubtype> apply(@Nullable ResultSet resultSet) {
            Result<EntitySubtypeEntity> result = cluster.getMapper(EntitySubtypeEntity.class).map(resultSet);
            if (result != null) {
                List<EntitySubtype> entitySubtypes = new ArrayList<>();
                result.all().forEach((entitySubtypeEntity) -> entitySubtypes.add(entitySubtypeEntity.toEntitySubtype()));
                return entitySubtypes;
            } else {
                return Collections.emptyList();
            }
        }
    });
}
Also used : java.util(java.util) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) Function(com.google.common.base.Function) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) Result(com.datastax.driver.mapping.Result) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DeviceEntity(org.thingsboard.server.dao.model.nosql.DeviceEntity) Device(org.thingsboard.server.common.data.Device) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) ResultSet(com.datastax.driver.core.ResultSet) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) EntitySubtypeEntity(org.thingsboard.server.dao.model.EntitySubtypeEntity) Select(com.datastax.driver.core.querybuilder.Select) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Statement(com.datastax.driver.core.Statement) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Nullable(javax.annotation.Nullable) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Nullable(javax.annotation.Nullable) Result(com.datastax.driver.mapping.Result)

Example 3 with EntitySubtype

use of org.thingsboard.server.common.data.EntitySubtype in project thingsboard by thingsboard.

the class EntitySubtypeEntity method toEntitySubtype.

public EntitySubtype toEntitySubtype() {
    EntitySubtype entitySubtype = new EntitySubtype();
    entitySubtype.setTenantId(new TenantId(tenantId));
    entitySubtype.setEntityType(entityType);
    entitySubtype.setType(type);
    return entitySubtype;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype)

Example 4 with EntitySubtype

use of org.thingsboard.server.common.data.EntitySubtype in project thingsboard by thingsboard.

the class BaseAssetControllerTest method testFindAssetTypesByTenantId.

@Test
public void testFindAssetTypesByTenantId() throws Exception {
    List<Asset> assets = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        Asset asset = new Asset();
        asset.setName("My asset B" + i);
        asset.setType("typeB");
        assets.add(doPost("/api/asset", asset, Asset.class));
    }
    for (int i = 0; i < 7; i++) {
        Asset asset = new Asset();
        asset.setName("My asset C" + i);
        asset.setType("typeC");
        assets.add(doPost("/api/asset", asset, Asset.class));
    }
    for (int i = 0; i < 9; i++) {
        Asset asset = new Asset();
        asset.setName("My asset A" + i);
        asset.setType("typeA");
        assets.add(doPost("/api/asset", asset, Asset.class));
    }
    List<EntitySubtype> assetTypes = doGetTyped("/api/asset/types", new TypeReference<List<EntitySubtype>>() {
    });
    Assert.assertNotNull(assetTypes);
    Assert.assertEquals(3, assetTypes.size());
    Assert.assertEquals("typeA", assetTypes.get(0).getType());
    Assert.assertEquals("typeB", assetTypes.get(1).getType());
    Assert.assertEquals("typeC", assetTypes.get(2).getType());
}
Also used : ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 5 with EntitySubtype

use of org.thingsboard.server.common.data.EntitySubtype in project thingsboard by thingsboard.

the class JpaAssetDaoTest method testFindTenantAssetTypesAsync.

@Test
public void testFindTenantAssetTypesAsync() throws ExecutionException, InterruptedException {
    UUID assetId1 = UUIDs.timeBased();
    UUID assetId2 = UUIDs.timeBased();
    UUID tenantId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    UUID customerId1 = UUIDs.timeBased();
    UUID customerId2 = UUIDs.timeBased();
    saveAsset(UUIDs.timeBased(), tenantId1, customerId1, "TEST_ASSET_1", "TYPE_1");
    saveAsset(UUIDs.timeBased(), tenantId1, customerId1, "TEST_ASSET_2", "TYPE_1");
    saveAsset(UUIDs.timeBased(), tenantId1, customerId1, "TEST_ASSET_3", "TYPE_2");
    saveAsset(UUIDs.timeBased(), tenantId1, customerId1, "TEST_ASSET_4", "TYPE_3");
    saveAsset(UUIDs.timeBased(), tenantId1, customerId1, "TEST_ASSET_5", "TYPE_3");
    saveAsset(UUIDs.timeBased(), tenantId1, customerId1, "TEST_ASSET_6", "TYPE_3");
    saveAsset(UUIDs.timeBased(), tenantId2, customerId2, "TEST_ASSET_7", "TYPE_4");
    saveAsset(UUIDs.timeBased(), tenantId2, customerId2, "TEST_ASSET_8", "TYPE_1");
    saveAsset(UUIDs.timeBased(), tenantId2, customerId2, "TEST_ASSET_9", "TYPE_1");
    List<EntitySubtype> tenant1Types = assetDao.findTenantAssetTypesAsync(tenantId1).get();
    assertNotNull(tenant1Types);
    List<EntitySubtype> tenant2Types = assetDao.findTenantAssetTypesAsync(tenantId2).get();
    assertNotNull(tenant2Types);
    assertEquals(3, tenant1Types.size());
    assertTrue(tenant1Types.stream().anyMatch(t -> t.getType().equals("TYPE_1")));
    assertTrue(tenant1Types.stream().anyMatch(t -> t.getType().equals("TYPE_2")));
    assertTrue(tenant1Types.stream().anyMatch(t -> t.getType().equals("TYPE_3")));
    assertFalse(tenant1Types.stream().anyMatch(t -> t.getType().equals("TYPE_4")));
    assertEquals(2, tenant2Types.size());
    assertTrue(tenant2Types.stream().anyMatch(t -> t.getType().equals("TYPE_1")));
    assertTrue(tenant2Types.stream().anyMatch(t -> t.getType().equals("TYPE_4")));
    assertFalse(tenant2Types.stream().anyMatch(t -> t.getType().equals("TYPE_2")));
    assertFalse(tenant2Types.stream().anyMatch(t -> t.getType().equals("TYPE_3")));
}
Also used : UUIDs(com.datastax.driver.core.utils.UUIDs) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AssetId(org.thingsboard.server.common.data.id.AssetId) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) UUID(java.util.UUID) TenantId(org.thingsboard.server.common.data.id.TenantId) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) AssetDao(org.thingsboard.server.dao.asset.AssetDao) Optional(java.util.Optional) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Assert(org.junit.Assert) TestCase.assertFalse(junit.framework.TestCase.assertFalse) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Aggregations

EntitySubtype (org.thingsboard.server.common.data.EntitySubtype)9 Asset (org.thingsboard.server.common.data.asset.Asset)5 Statement (com.datastax.driver.core.Statement)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 EntitySubtypeEntity (org.thingsboard.server.dao.model.EntitySubtypeEntity)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 Device (org.thingsboard.server.common.data.Device)3 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)3 ResultSet (com.datastax.driver.core.ResultSet)2 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)2 QueryBuilder (com.datastax.driver.core.querybuilder.QueryBuilder)2 Select (com.datastax.driver.core.querybuilder.Select)2 Result (com.datastax.driver.mapping.Result)2 Function (com.google.common.base.Function)2 Futures (com.google.common.util.concurrent.Futures)2 java.util (java.util)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 Slf4j (lombok.extern.slf4j.Slf4j)2