Search in sources :

Example 11 with Asset

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());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 12 with Asset

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

Example 13 with Asset

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());
}
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 14 with Asset

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

Example 15 with Asset

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

Aggregations

Asset (org.thingsboard.server.common.data.asset.Asset)49 Test (org.junit.Test)35 ArrayList (java.util.ArrayList)16 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)15 Customer (org.thingsboard.server.common.data.Customer)13 CustomerId (org.thingsboard.server.common.data.id.CustomerId)12 AssetId (org.thingsboard.server.common.data.id.AssetId)8 TenantId (org.thingsboard.server.common.data.id.TenantId)7 TypeReference (com.fasterxml.jackson.core.type.TypeReference)6 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)6 UUID (java.util.UUID)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 Tenant (org.thingsboard.server.common.data.Tenant)5 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 EntitySubtype (org.thingsboard.server.common.data.EntitySubtype)4 List (java.util.List)3 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)2 Statement (com.datastax.driver.core.Statement)1