Search in sources :

Example 1 with AssetInfo

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);
    }
}
Also used : AssetInfo(org.thingsboard.server.common.data.asset.AssetInfo) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) MessagingException(javax.mail.MessagingException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with AssetInfo

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);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetInfo(org.thingsboard.server.common.data.asset.AssetInfo) Test(org.junit.Test)

Example 3 with AssetInfo

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());
}
Also used : ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Asset(org.thingsboard.server.common.data.asset.Asset) AssetInfo(org.thingsboard.server.common.data.asset.AssetInfo) Test(org.junit.Test)

Aggregations

AssetInfo (org.thingsboard.server.common.data.asset.AssetInfo)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Asset (org.thingsboard.server.common.data.asset.Asset)2 PageLink (org.thingsboard.server.common.data.page.PageLink)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 MessagingException (javax.mail.MessagingException)1 Customer (org.thingsboard.server.common.data.Customer)1 Tenant (org.thingsboard.server.common.data.Tenant)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 CustomerId (org.thingsboard.server.common.data.id.CustomerId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)1