Search in sources :

Example 56 with PageLink

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

the class BaseDeviceServiceTest method testFindDevicesByTenantIdAndName.

@Test
public void testFindDevicesByTenantIdAndName() {
    String title1 = "Device title 1";
    List<DeviceInfo> devicesTitle1 = 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("default");
        devicesTitle1.add(new DeviceInfo(deviceService.saveDevice(device), null, false, "default"));
    }
    String title2 = "Device title 2";
    List<DeviceInfo> devicesTitle2 = 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("default");
        devicesTitle2.add(new DeviceInfo(deviceService.saveDevice(device), null, false, "default"));
    }
    List<DeviceInfo> loadedDevicesTitle1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15, 0, title1);
    PageData<DeviceInfo> pageData = null;
    do {
        pageData = deviceService.findDeviceInfosByTenantId(tenantId, pageLink);
        loadedDevicesTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle1, idComparator);
    Collections.sort(loadedDevicesTitle1, idComparator);
    Assert.assertEquals(devicesTitle1, loadedDevicesTitle1);
    List<DeviceInfo> loadedDevicesTitle2 = new ArrayList<>();
    pageLink = new PageLink(4, 0, title2);
    do {
        pageData = deviceService.findDeviceInfosByTenantId(tenantId, pageLink);
        loadedDevicesTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle2, idComparator);
    Collections.sort(loadedDevicesTitle2, idComparator);
    Assert.assertEquals(devicesTitle2, loadedDevicesTitle2);
    for (Device device : loadedDevicesTitle1) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4, 0, title1);
    pageData = deviceService.findDeviceInfosByTenantId(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesTitle2) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4, 0, title2);
    pageData = deviceService.findDeviceInfosByTenantId(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Test(org.junit.Test)

Example 57 with PageLink

use of org.thingsboard.server.common.data.page.PageLink 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<>();
    PageLink pageLink = new PageLink(15);
    PageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type1, pageLink);
        loadedDevicesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType1, idComparator);
    Collections.sort(loadedDevicesType1, idComparator);
    Assert.assertEquals(devicesType1, loadedDevicesType1);
    List<Device> loadedDevicesType2 = new ArrayList<>();
    pageLink = new PageLink(4);
    do {
        pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type2, pageLink);
        loadedDevicesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType2, idComparator);
    Collections.sort(loadedDevicesType2, idComparator);
    Assert.assertEquals(devicesType2, loadedDevicesType2);
    for (Device device : loadedDevicesType1) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesType2) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type2, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Test(org.junit.Test)

Example 58 with PageLink

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

the class BaseDeviceServiceTest method testFindDeviceInfoByTenantIdAndType.

@Test
public void testFindDeviceInfoByTenantIdAndType() {
    Customer customer = new Customer();
    customer.setTitle("Customer X");
    customer.setTenantId(tenantId);
    Customer savedCustomer = customerService.saveCustomer(customer);
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("default");
    device.setType("default");
    device.setLabel("label");
    device.setCustomerId(savedCustomer.getId());
    Device savedDevice = deviceService.saveDevice(device);
    PageLink pageLinkWithLabel = new PageLink(100, 0, "label");
    List<DeviceInfo> deviceInfosWithLabel = deviceService.findDeviceInfosByTenantIdAndType(tenantId, device.getType(), pageLinkWithLabel).getData();
    Assert.assertFalse(deviceInfosWithLabel.isEmpty());
    Assert.assertTrue(deviceInfosWithLabel.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getDeviceProfileName().equals(savedDevice.getType()) && d.getLabel().equals(savedDevice.getLabel())));
    PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
    List<DeviceInfo> deviceInfosWithCustomer = deviceService.findDeviceInfosByTenantIdAndType(tenantId, device.getType(), pageLinkWithCustomer).getData();
    Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
    Assert.assertTrue(deviceInfosWithCustomer.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getDeviceProfileName().equals(savedDevice.getType()) && d.getCustomerId().equals(savedCustomer.getId()) && d.getCustomerTitle().equals(savedCustomer.getTitle())));
}
Also used : DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) Device(org.thingsboard.server.common.data.Device) FIRMWARE(org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE) Customer(org.thingsboard.server.common.data.Customer) Tenant(org.thingsboard.server.common.data.Tenant) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TenantId(org.thingsboard.server.common.data.id.TenantId) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ChecksumAlgorithm(org.thingsboard.server.common.data.ota.ChecksumAlgorithm) TenantProfile(org.thingsboard.server.common.data.TenantProfile) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) List(java.util.List) Rule(org.junit.Rule) PageData(org.thingsboard.server.common.data.page.PageData) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Assert(org.junit.Assert) Collections(java.util.Collections) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) PageLink(org.thingsboard.server.common.data.page.PageLink) DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) Test(org.junit.Test)

Example 59 with PageLink

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

the class BaseDeviceServiceTest method testFindDeviceInfoByTenantIdAndDeviceProfileId.

@Test
public void testFindDeviceInfoByTenantIdAndDeviceProfileId() {
    Customer customer = new Customer();
    customer.setTitle("Customer X");
    customer.setTenantId(tenantId);
    Customer savedCustomer = customerService.saveCustomer(customer);
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("default");
    device.setLabel("label");
    device.setCustomerId(savedCustomer.getId());
    Device savedDevice = deviceService.saveDevice(device);
    PageLink pageLinkWithLabel = new PageLink(100, 0, "label");
    List<DeviceInfo> deviceInfosWithLabel = deviceService.findDeviceInfosByTenantIdAndDeviceProfileId(tenantId, savedDevice.getDeviceProfileId(), pageLinkWithLabel).getData();
    Assert.assertFalse(deviceInfosWithLabel.isEmpty());
    Assert.assertTrue(deviceInfosWithLabel.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getDeviceProfileId().equals(savedDevice.getDeviceProfileId()) && d.getLabel().equals(savedDevice.getLabel())));
    PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
    List<DeviceInfo> deviceInfosWithCustomer = deviceService.findDeviceInfosByTenantIdAndDeviceProfileId(tenantId, savedDevice.getDeviceProfileId(), pageLinkWithCustomer).getData();
    Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
    Assert.assertTrue(deviceInfosWithCustomer.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getDeviceProfileId().equals(savedDevice.getDeviceProfileId()) && d.getCustomerId().equals(savedCustomer.getId()) && d.getCustomerTitle().equals(savedCustomer.getTitle())));
}
Also used : DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) Device(org.thingsboard.server.common.data.Device) FIRMWARE(org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE) Customer(org.thingsboard.server.common.data.Customer) Tenant(org.thingsboard.server.common.data.Tenant) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TenantId(org.thingsboard.server.common.data.id.TenantId) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ChecksumAlgorithm(org.thingsboard.server.common.data.ota.ChecksumAlgorithm) TenantProfile(org.thingsboard.server.common.data.TenantProfile) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) List(java.util.List) Rule(org.junit.Rule) PageData(org.thingsboard.server.common.data.page.PageData) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Assert(org.junit.Assert) Collections(java.util.Collections) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) PageLink(org.thingsboard.server.common.data.page.PageLink) DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) Test(org.junit.Test)

Example 60 with PageLink

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

the class BaseDeviceServiceTest method testFindDevicesByTenantIdCustomerIdAndName.

@Test
public void testFindDevicesByTenantIdCustomerIdAndName() {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String title1 = "Device title 1";
    List<Device> devicesTitle1 = new ArrayList<>();
    for (int i = 0; i < 175; 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("default");
        device = deviceService.saveDevice(device);
        devicesTitle1.add(deviceService.assignDeviceToCustomer(tenantId, device.getId(), customerId));
    }
    String title2 = "Device title 2";
    List<Device> devicesTitle2 = new ArrayList<>();
    for (int i = 0; i < 143; 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("default");
        device = deviceService.saveDevice(device);
        devicesTitle2.add(deviceService.assignDeviceToCustomer(tenantId, device.getId(), customerId));
    }
    List<Device> loadedDevicesTitle1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15, 0, title1);
    PageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevicesTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle1, idComparator);
    Collections.sort(loadedDevicesTitle1, idComparator);
    Assert.assertEquals(devicesTitle1, loadedDevicesTitle1);
    List<Device> loadedDevicesTitle2 = new ArrayList<>();
    pageLink = new PageLink(4, 0, title2);
    do {
        pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevicesTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesTitle2, idComparator);
    Collections.sort(loadedDevicesTitle2, idComparator);
    Assert.assertEquals(devicesTitle2, loadedDevicesTitle2);
    for (Device device : loadedDevicesTitle1) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4, 0, title1);
    pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesTitle2) {
        deviceService.deleteDevice(tenantId, device.getId());
    }
    pageLink = new PageLink(4, 0, title2);
    pageData = deviceService.findDevicesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    customerService.deleteCustomer(tenantId, customerId);
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Aggregations

PageLink (org.thingsboard.server.common.data.page.PageLink)187 Test (org.junit.Test)117 ArrayList (java.util.ArrayList)106 TenantId (org.thingsboard.server.common.data.id.TenantId)67 TypeReference (com.fasterxml.jackson.core.type.TypeReference)54 CustomerId (org.thingsboard.server.common.data.id.CustomerId)47 Tenant (org.thingsboard.server.common.data.Tenant)45 Customer (org.thingsboard.server.common.data.Customer)43 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)34 ApiOperation (io.swagger.annotations.ApiOperation)33 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)33 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)33 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)33 Edge (org.thingsboard.server.common.data.edge.Edge)32 Device (org.thingsboard.server.common.data.Device)28 PageData (org.thingsboard.server.common.data.page.PageData)28 Matchers.containsString (org.hamcrest.Matchers.containsString)22 UUID (java.util.UUID)20 Asset (org.thingsboard.server.common.data.asset.Asset)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20