use of org.thingsboard.server.common.data.asset.AssetInfo in project thingsboard by thingsboard.
the class BaseController method checkAssetInfoId.
AssetInfo checkAssetInfoId(AssetId assetId, Operation operation) throws ThingsboardException {
try {
validateId(assetId, "Incorrect assetId " + assetId);
AssetInfo asset = assetService.findAssetInfoById(getCurrentUser().getTenantId(), assetId);
checkNotNull(asset, "Asset with id [" + assetId + "] is not found");
accessControlService.checkPermission(getCurrentUser(), Resource.ASSET, operation, assetId, asset);
return asset;
} catch (Exception e) {
throw handleException(e, false);
}
}
use of org.thingsboard.server.common.data.asset.AssetInfo in project thingsboard by thingsboard.
the class BaseAssetServiceTest method testFindAssetsByTenantIdAndCustomerId.
@Test
public void testFindAssetsByTenantIdAndCustomerId() {
Tenant tenant = new Tenant();
tenant.setTitle("Test tenant");
tenant = tenantService.saveTenant(tenant);
TenantId tenantId = tenant.getId();
Customer customer = new Customer();
customer.setTitle("Test customer");
customer.setTenantId(tenantId);
customer = customerService.saveCustomer(customer);
CustomerId customerId = customer.getId();
List<AssetInfo> assets = new ArrayList<>();
for (int i = 0; i < 278; i++) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
asset.setName("Asset" + i);
asset.setType("default");
asset = assetService.saveAsset(asset);
assets.add(new AssetInfo(assetService.assignAssetToCustomer(tenantId, asset.getId(), customerId), customer.getTitle(), customer.isPublic()));
}
List<AssetInfo> loadedAssets = new ArrayList<>();
PageLink pageLink = new PageLink(23);
PageData<AssetInfo> pageData = null;
do {
pageData = assetService.findAssetInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink);
loadedAssets.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assets, idComparator);
Collections.sort(loadedAssets, idComparator);
Assert.assertEquals(assets, loadedAssets);
assetService.unassignCustomerAssets(tenantId, customerId);
pageLink = new PageLink(33);
pageData = assetService.findAssetInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertTrue(pageData.getData().isEmpty());
tenantService.deleteTenant(tenantId);
}
use of org.thingsboard.server.common.data.asset.AssetInfo in project thingsboard by thingsboard.
the class BaseAssetServiceTest method testFindAssetsByTenantIdAndName.
@Test
public void testFindAssetsByTenantIdAndName() {
String title1 = "Asset title 1";
List<AssetInfo> assetsTitle1 = new ArrayList<>();
for (int i = 0; i < 143; i++) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title1 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType("default");
assetsTitle1.add(new AssetInfo(assetService.saveAsset(asset), null, false));
}
String title2 = "Asset title 2";
List<AssetInfo> assetsTitle2 = new ArrayList<>();
for (int i = 0; i < 175; i++) {
Asset asset = new Asset();
asset.setTenantId(tenantId);
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title2 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType("default");
assetsTitle2.add(new AssetInfo(assetService.saveAsset(asset), null, false));
}
List<AssetInfo> loadedAssetsTitle1 = new ArrayList<>();
PageLink pageLink = new PageLink(15, 0, title1);
PageData<AssetInfo> pageData = null;
do {
pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink);
loadedAssetsTitle1.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsTitle1, idComparator);
Collections.sort(loadedAssetsTitle1, idComparator);
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1);
List<AssetInfo> loadedAssetsTitle2 = new ArrayList<>();
pageLink = new PageLink(4, 0, title2);
do {
pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink);
loadedAssetsTitle2.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsTitle2, idComparator);
Collections.sort(loadedAssetsTitle2, idComparator);
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2);
for (Asset asset : loadedAssetsTitle1) {
assetService.deleteAsset(tenantId, asset.getId());
}
pageLink = new PageLink(4, 0, title1);
pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
for (Asset asset : loadedAssetsTitle2) {
assetService.deleteAsset(tenantId, asset.getId());
}
pageLink = new PageLink(4, 0, title2);
pageData = assetService.findAssetInfosByTenantId(tenantId, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
}
Aggregations