Search in sources :

Example 1 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testAssignAssetToNonExistentCustomer.

@Test(expected = DataValidationException.class)
public void testAssignAssetToNonExistentCustomer() {
    Asset asset = new Asset();
    asset.setName("My asset");
    asset.setType("default");
    asset.setTenantId(tenantId);
    asset = assetService.saveAsset(asset);
    try {
        assetService.assignAssetToCustomer(asset.getId(), new CustomerId(UUIDs.timeBased()));
    } finally {
        assetService.deleteAsset(asset.getId());
    }
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 2 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testSaveAsset.

@Test
public void testSaveAsset() {
    Asset asset = new Asset();
    asset.setTenantId(tenantId);
    asset.setName("My asset");
    asset.setType("default");
    Asset savedAsset = assetService.saveAsset(asset);
    Assert.assertNotNull(savedAsset);
    Assert.assertNotNull(savedAsset.getId());
    Assert.assertTrue(savedAsset.getCreatedTime() > 0);
    Assert.assertEquals(asset.getTenantId(), savedAsset.getTenantId());
    Assert.assertNotNull(savedAsset.getCustomerId());
    Assert.assertEquals(NULL_UUID, savedAsset.getCustomerId().getId());
    Assert.assertEquals(asset.getName(), savedAsset.getName());
    savedAsset.setName("My new asset");
    assetService.saveAsset(savedAsset);
    Asset foundAsset = assetService.findAssetById(savedAsset.getId());
    Assert.assertEquals(foundAsset.getName(), savedAsset.getName());
    assetService.deleteAsset(savedAsset.getId());
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) Test(org.junit.Test)

Example 3 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testSaveAssetWithEmptyName.

@Test(expected = DataValidationException.class)
public void testSaveAssetWithEmptyName() {
    Asset asset = new Asset();
    asset.setTenantId(tenantId);
    asset.setType("default");
    assetService.saveAsset(asset);
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) Test(org.junit.Test)

Example 4 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testSaveAssetWithEmptyTenant.

@Test(expected = DataValidationException.class)
public void testSaveAssetWithEmptyTenant() {
    Asset asset = new Asset();
    asset.setName("My asset");
    asset.setType("default");
    assetService.saveAsset(asset);
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) Test(org.junit.Test)

Example 5 with Asset

use of org.thingsboard.server.common.data.asset.Asset in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testFindAssetsByTenantIdCustomerIdAndType.

@Test
public void testFindAssetsByTenantIdCustomerIdAndType() {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String title1 = "Asset title 1";
    String type1 = "typeC";
    List<Asset> assetsType1 = new ArrayList<>();
    for (int i = 0; i < 175; 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(type1);
        asset = assetService.saveAsset(asset);
        assetsType1.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    String title2 = "Asset title 2";
    String type2 = "typeD";
    List<Asset> assetsType2 = new ArrayList<>();
    for (int i = 0; i < 143; 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(type2);
        asset = assetService.saveAsset(asset);
        assetsType2.add(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    List<Asset> loadedAssetsType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
        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 = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
        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) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Asset asset : loadedAssetsType2) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    customerService.deleteCustomer(customerId);
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) 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