Search in sources :

Example 66 with TextPageLink

use of org.thingsboard.server.common.data.page.TextPageLink in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testFindAssetsByTenantIdAndName.

@Test
public void testFindAssetsByTenantIdAndName() {
    String title1 = "Asset title 1";
    List<Asset> 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(assetService.saveAsset(asset));
    }
    String title2 = "Asset title 2";
    List<Asset> 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(assetService.saveAsset(asset));
    }
    List<Asset> loadedAssetsTitle1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15, title1);
    TextPageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantId(tenantId, pageLink);
        loadedAssetsTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsTitle1, idComparator);
    Collections.sort(loadedAssetsTitle1, idComparator);
    Assert.assertEquals(assetsTitle1, loadedAssetsTitle1);
    List<Asset> loadedAssetsTitle2 = new ArrayList<>();
    pageLink = new TextPageLink(4, title2);
    do {
        pageData = assetService.findAssetsByTenantId(tenantId, pageLink);
        loadedAssetsTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assetsTitle2, idComparator);
    Collections.sort(loadedAssetsTitle2, idComparator);
    Assert.assertEquals(assetsTitle2, loadedAssetsTitle2);
    for (Asset asset : loadedAssetsTitle1) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4, title1);
    pageData = assetService.findAssetsByTenantId(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Asset asset : loadedAssetsTitle2) {
        assetService.deleteAsset(asset.getId());
    }
    pageLink = new TextPageLink(4, title2);
    pageData = assetService.findAssetsByTenantId(tenantId, pageLink);
    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) Test(org.junit.Test)

Example 67 with TextPageLink

use of org.thingsboard.server.common.data.page.TextPageLink in project thingsboard by thingsboard.

the class BaseAssetServiceTest method testFindAssetsByTenantIdAndType.

@Test
public void testFindAssetsByTenantIdAndType() {
    String title1 = "Asset title 1";
    String type1 = "typeA";
    List<Asset> assetsType1 = 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(type1);
        assetsType1.add(assetService.saveAsset(asset));
    }
    String title2 = "Asset title 2";
    String type2 = "typeB";
    List<Asset> assetsType2 = 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(type2);
        assetsType2.add(assetService.saveAsset(asset));
    }
    List<Asset> loadedAssetsType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantIdAndType(tenantId, 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.findAssetsByTenantIdAndType(tenantId, 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.findAssetsByTenantIdAndType(tenantId, 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.findAssetsByTenantIdAndType(tenantId, type2, pageLink);
    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) Test(org.junit.Test)

Example 68 with TextPageLink

use of org.thingsboard.server.common.data.page.TextPageLink 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<Asset> 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(assetService.assignAssetToCustomer(asset.getId(), customerId));
    }
    List<Asset> loadedAssets = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(23);
    TextPageData<Asset> pageData = null;
    do {
        pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedAssets.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(assets, idComparator);
    Collections.sort(loadedAssets, idComparator);
    Assert.assertEquals(assets, loadedAssets);
    assetService.unassignCustomerAssets(tenantId, customerId);
    pageLink = new TextPageLink(33);
    pageData = assetService.findAssetsByTenantIdAndCustomerId(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) 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)

Example 69 with TextPageLink

use of org.thingsboard.server.common.data.page.TextPageLink in project thingsboard by thingsboard.

the class JpaBasePluginDaoTest method testFindByTenantIdAndPageLink.

@Test
@DatabaseSetup("classpath:dbunit/empty_dataset.xml")
public void testFindByTenantIdAndPageLink() {
    UUID tenantId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    createPluginsTwoTenants(tenantId1, tenantId2, "plugin_");
    List<PluginMetaData> rules1 = pluginDao.findByTenantIdAndPageLink(new TenantId(tenantId1), new TextPageLink(20, "plugin_"));
    assertEquals(20, rules1.size());
    List<PluginMetaData> rules2 = pluginDao.findByTenantIdAndPageLink(new TenantId(tenantId1), new TextPageLink(20, "plugin_", rules1.get(19).getId().getId(), null));
    assertEquals(10, rules2.size());
    List<PluginMetaData> rules3 = pluginDao.findByTenantIdAndPageLink(new TenantId(tenantId1), new TextPageLink(20, "plugin_", rules2.get(9).getId().getId(), null));
    assertEquals(0, rules3.size());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 70 with TextPageLink

use of org.thingsboard.server.common.data.page.TextPageLink in project thingsboard by thingsboard.

the class JpaBasePluginDaoTest method testFindAllTenantRulesByTenantId.

@Test
@DatabaseSetup(value = "classpath:dbunit/empty_dataset.xml")
public void testFindAllTenantRulesByTenantId() {
    UUID tenantId1 = UUIDs.timeBased();
    UUID tenantId2 = UUIDs.timeBased();
    createTenantsAndSystemPlugins(tenantId1, tenantId2, "name_");
    List<PluginMetaData> rules1 = pluginDao.findAllTenantPluginsByTenantId(tenantId1, new TextPageLink(40, "name_"));
    assertEquals(40, rules1.size());
    List<PluginMetaData> rules2 = pluginDao.findAllTenantPluginsByTenantId(tenantId1, new TextPageLink(40, "name_", rules1.get(19).getId().getId(), null));
    assertEquals(20, rules2.size());
    List<PluginMetaData> rules3 = pluginDao.findAllTenantPluginsByTenantId(tenantId1, new TextPageLink(40, "name_", rules2.get(19).getId().getId(), null));
    assertEquals(0, rules3.size());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Aggregations

TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)93 Test (org.junit.Test)75 ArrayList (java.util.ArrayList)58 TenantId (org.thingsboard.server.common.data.id.TenantId)37 TypeReference (com.fasterxml.jackson.core.type.TypeReference)27 Tenant (org.thingsboard.server.common.data.Tenant)24 CustomerId (org.thingsboard.server.common.data.id.CustomerId)19 Customer (org.thingsboard.server.common.data.Customer)18 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)18 UUID (java.util.UUID)15 Asset (org.thingsboard.server.common.data.asset.Asset)14 Matchers.containsString (org.hamcrest.Matchers.containsString)13 User (org.thingsboard.server.common.data.User)13 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)11 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)11 WidgetsBundle (org.thingsboard.server.common.data.widget.WidgetsBundle)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)10 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)10 Device (org.thingsboard.server.common.data.Device)7