use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class BaseAssetControllerTest method testFindTenantAssetsByType.
@Test
public void testFindTenantAssetsByType() throws Exception {
String title1 = "Asset title 1";
String type1 = "typeA";
List<Asset> assetsType1 = new ArrayList<>();
for (int i = 0; i < 143; i++) {
Asset asset = new Asset();
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title1 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType(type1);
assetsType1.add(doPost("/api/asset", asset, Asset.class));
}
String title2 = "Asset title 2";
String type2 = "typeB";
List<Asset> assetsType2 = new ArrayList<>();
for (int i = 0; i < 75; i++) {
Asset asset = new Asset();
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title2 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType(type2);
assetsType2.add(doPost("/api/asset", asset, Asset.class));
}
List<Asset> loadedAssetsType1 = new ArrayList<>();
TextPageLink pageLink = new TextPageLink(15);
TextPageData<Asset> pageData = null;
do {
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type1);
loadedAssetsType1.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsType1, idComparator);
Collections.sort(loadedAssetsType1, idComparator);
Assert.assertEquals(assetsType1, loadedAssetsType1);
List<Asset> loadedAssetsType2 = new ArrayList<>();
pageLink = new TextPageLink(4);
do {
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type2);
loadedAssetsType2.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsType2, idComparator);
Collections.sort(loadedAssetsType2, idComparator);
Assert.assertEquals(assetsType2, loadedAssetsType2);
for (Asset asset : loadedAssetsType1) {
doDelete("/api/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new TextPageLink(4);
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type1);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
for (Asset asset : loadedAssetsType2) {
doDelete("/api/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new TextPageLink(4);
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type2);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class BaseAssetControllerTest method testSaveAsset.
@Test
public void testSaveAsset() throws Exception {
Asset asset = new Asset();
asset.setName("My asset");
asset.setType("default");
Asset savedAsset = doPost("/api/asset", asset, Asset.class);
Assert.assertNotNull(savedAsset);
Assert.assertNotNull(savedAsset.getId());
Assert.assertTrue(savedAsset.getCreatedTime() > 0);
Assert.assertEquals(savedTenant.getId(), savedAsset.getTenantId());
Assert.assertNotNull(savedAsset.getCustomerId());
Assert.assertEquals(NULL_UUID, savedAsset.getCustomerId().getId());
Assert.assertEquals(asset.getName(), savedAsset.getName());
savedAsset.setName("My new asset");
doPost("/api/asset", savedAsset, Asset.class);
Asset foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class);
Assert.assertEquals(foundAsset.getName(), savedAsset.getName());
}
use of org.thingsboard.server.common.data.asset.Asset 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());
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class JpaAssetDaoTest method testFindAssetsByTenantIdAndIdsAsync.
@Test
public void testFindAssetsByTenantIdAndIdsAsync() throws ExecutionException, InterruptedException {
UUID tenantId = UUIDs.timeBased();
UUID customerId = UUIDs.timeBased();
List<UUID> searchIds = new ArrayList<>();
for (int i = 0; i < 30; i++) {
UUID assetId = UUIDs.timeBased();
saveAsset(assetId, tenantId, customerId, "ASSET_" + i, "TYPE_1");
if (i % 3 == 0) {
searchIds.add(assetId);
}
}
ListenableFuture<List<Asset>> assetsFuture = assetDao.findAssetsByTenantIdAndIdsAsync(tenantId, searchIds);
List<Asset> assets = assetsFuture.get();
assertNotNull(assets);
assertEquals(10, assets.size());
}
use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.
the class JpaAssetDaoTest method testFindAssetsByTenantId.
@Test
public void testFindAssetsByTenantId() {
UUID tenantId1 = UUIDs.timeBased();
UUID tenantId2 = UUIDs.timeBased();
UUID customerId1 = UUIDs.timeBased();
UUID customerId2 = UUIDs.timeBased();
for (int i = 0; i < 60; i++) {
UUID assetId = UUIDs.timeBased();
UUID tenantId = i % 2 == 0 ? tenantId1 : tenantId2;
UUID customerId = i % 2 == 0 ? customerId1 : customerId2;
saveAsset(assetId, tenantId, customerId, "ASSET_" + i, "TYPE_1");
}
assertEquals(60, assetDao.find().size());
TextPageLink pageLink1 = new TextPageLink(20, "ASSET_");
List<Asset> assets1 = assetDao.findAssetsByTenantId(tenantId1, pageLink1);
assertEquals(20, assets1.size());
TextPageLink pageLink2 = new TextPageLink(20, "ASSET_", assets1.get(19).getId().getId(), null);
List<Asset> assets2 = assetDao.findAssetsByTenantId(tenantId1, pageLink2);
assertEquals(10, assets2.size());
TextPageLink pageLink3 = new TextPageLink(20, "ASSET_", assets2.get(9).getId().getId(), null);
List<Asset> assets3 = assetDao.findAssetsByTenantId(tenantId1, pageLink3);
assertEquals(0, assets3.size());
}
Aggregations