Search in sources :

Example 6 with TextPageLink

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

the class BaseDeviceServiceTest method testFindDevicesByTenantIdAndType.

@Test
public void testFindDevicesByTenantIdAndType() {
    String title1 = "Device title 1";
    String type1 = "typeA";
    List<Device> devicesType1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type1);
        devicesType1.add(deviceService.saveDevice(device));
    }
    String title2 = "Device title 2";
    String type2 = "typeB";
    List<Device> devicesType2 = new ArrayList<>();
    for (int i = 0; i < 175; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type2);
        devicesType2.add(deviceService.saveDevice(device));
    }
    List<Device> loadedDevicesType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type1, pageLink);
        loadedDevicesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType1, idComparator);
    Collections.sort(loadedDevicesType1, idComparator);
    Assert.assertEquals(devicesType1, loadedDevicesType1);
    List<Device> loadedDevicesType2 = new ArrayList<>();
    pageLink = new TextPageLink(4);
    do {
        pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type2, pageLink);
        loadedDevicesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType2, idComparator);
    Collections.sort(loadedDevicesType2, idComparator);
    Assert.assertEquals(devicesType2, loadedDevicesType2);
    for (Device device : loadedDevicesType1) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesType2) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type2, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with TextPageLink

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

the class BaseTenantServiceTest method testFindTenantsByTitle.

@Test
public void testFindTenantsByTitle() {
    String title1 = "Tenant title 1";
    List<Tenant> tenantsTitle1 = new ArrayList<>();
    for (int i = 0; i < 134; i++) {
        Tenant tenant = new Tenant();
        String suffix = RandomStringUtils.randomAlphanumeric((int) (Math.random() * 15));
        String title = title1 + suffix;
        title = i % 2 == 0 ? title.toLowerCase() : title.toUpperCase();
        tenant.setTitle(title);
        tenantsTitle1.add(tenantService.saveTenant(tenant));
    }
    String title2 = "Tenant title 2";
    List<Tenant> tenantsTitle2 = new ArrayList<>();
    for (int i = 0; i < 127; i++) {
        Tenant tenant = new Tenant();
        String suffix = RandomStringUtils.randomAlphanumeric((int) (Math.random() * 15));
        String title = title2 + suffix;
        title = i % 2 == 0 ? title.toLowerCase() : title.toUpperCase();
        tenant.setTitle(title);
        tenantsTitle2.add(tenantService.saveTenant(tenant));
    }
    List<Tenant> loadedTenantsTitle1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15, title1);
    TextPageData<Tenant> pageData = null;
    do {
        pageData = tenantService.findTenants(pageLink);
        loadedTenantsTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantsTitle1, idComparator);
    Collections.sort(loadedTenantsTitle1, idComparator);
    Assert.assertEquals(tenantsTitle1, loadedTenantsTitle1);
    List<Tenant> loadedTenantsTitle2 = new ArrayList<>();
    pageLink = new TextPageLink(4, title2);
    do {
        pageData = tenantService.findTenants(pageLink);
        loadedTenantsTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantsTitle2, idComparator);
    Collections.sort(loadedTenantsTitle2, idComparator);
    Assert.assertEquals(tenantsTitle2, loadedTenantsTitle2);
    for (Tenant tenant : loadedTenantsTitle1) {
        tenantService.deleteTenant(tenant.getId());
    }
    pageLink = new TextPageLink(4, title1);
    pageData = tenantService.findTenants(pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Tenant tenant : loadedTenantsTitle2) {
        tenantService.deleteTenant(tenant.getId());
    }
    pageLink = new TextPageLink(4, title2);
    pageData = tenantService.findTenants(pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with TextPageLink

use of org.thingsboard.server.common.data.page.TextPageLink 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)

Example 9 with TextPageLink

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

the class BaseRuleService method findSystemRules.

@Override
public List<RuleMetaData> findSystemRules() {
    log.trace("Executing findSystemRules");
    List<RuleMetaData> rules = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(300);
    TextPageData<RuleMetaData> pageData = null;
    do {
        pageData = findSystemRules(pageLink);
        rules.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    return rules;
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 10 with TextPageLink

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

the class BaseAssetControllerTest method testFindCustomerAssetsByType.

@Test
public void testFindCustomerAssetsByType() throws Exception {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer = doPost("/api/customer", customer, Customer.class);
    CustomerId customerId = customer.getId();
    String title1 = "Asset title 1";
    String type1 = "typeC";
    List<Asset> assetsType1 = new ArrayList<>();
    for (int i = 0; i < 125; 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);
        asset = doPost("/api/asset", asset, Asset.class);
        assetsType1.add(doPost("/api/customer/" + customerId.getId().toString() + "/asset/" + asset.getId().getId().toString(), Asset.class));
    }
    String title2 = "Asset title 2";
    String type2 = "typeD";
    List<Asset> assetsType2 = new ArrayList<>();
    for (int i = 0; i < 143; 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);
        asset = doPost("/api/asset", asset, Asset.class);
        assetsType2.add(doPost("/api/customer/" + customerId.getId().toString() + "/asset/" + asset.getId().getId().toString(), Asset.class));
    }
    List<Asset> loadedAssetsType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Asset> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/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/customer/" + customerId.getId().toString() + "/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/customer/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/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/customer/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new TextPageLink(4);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/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) 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) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

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