Search in sources :

Example 1 with DeviceInfo

use of org.thingsboard.server.common.data.DeviceInfo in project thingsboard by thingsboard.

the class BaseController method checkDeviceInfoId.

DeviceInfo checkDeviceInfoId(DeviceId deviceId, Operation operation) throws ThingsboardException {
    try {
        validateId(deviceId, "Incorrect deviceId " + deviceId);
        DeviceInfo device = deviceService.findDeviceInfoById(getCurrentUser().getTenantId(), deviceId);
        checkNotNull(device, "Device with id [" + deviceId + "] is not found");
        accessControlService.checkPermission(getCurrentUser(), Resource.DEVICE, operation, deviceId, device);
        return device;
    } catch (Exception e) {
        throw handleException(e, false);
    }
}
Also used : DeviceInfo(org.thingsboard.server.common.data.DeviceInfo) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) MessagingException(javax.mail.MessagingException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with DeviceInfo

use of org.thingsboard.server.common.data.DeviceInfo in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testFindDevicesByTenantIdAndCustomerId.

@Test
public void testFindDevicesByTenantIdAndCustomerId() {
    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<DeviceInfo> devices = new ArrayList<>();
    for (int i = 0; i < 278; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        device = deviceService.saveDevice(device);
        devices.add(new DeviceInfo(deviceService.assignDeviceToCustomer(tenantId, device.getId(), customerId), customer.getTitle(), customer.isPublic(), "default"));
    }
    List<DeviceInfo> loadedDevices = new ArrayList<>();
    PageLink pageLink = new PageLink(23);
    PageData<DeviceInfo> pageData = null;
    do {
        pageData = deviceService.findDeviceInfosByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        loadedDevices.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devices, idComparator);
    Collections.sort(loadedDevices, idComparator);
    Assert.assertEquals(devices, loadedDevices);
    deviceService.unassignCustomerDevices(tenantId, customerId);
    pageLink = new PageLink(33);
    pageData = deviceService.findDeviceInfosByTenantIdAndCustomerId(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) Customer(org.thingsboard.server.common.data.Customer) 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) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 3 with DeviceInfo

use of org.thingsboard.server.common.data.DeviceInfo in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testFindDeviceInfoByTenantId.

@Test
public void testFindDeviceInfoByTenantId() {
    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.findDeviceInfosByTenantId(tenantId, pageLinkWithLabel).getData();
    Assert.assertFalse(deviceInfosWithLabel.isEmpty());
    Assert.assertTrue(deviceInfosWithLabel.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getLabel().equals(savedDevice.getLabel())));
    PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
    List<DeviceInfo> deviceInfosWithCustomer = deviceService.findDeviceInfosByTenantId(tenantId, pageLinkWithCustomer).getData();
    Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
    Assert.assertTrue(deviceInfosWithCustomer.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getCustomerId().equals(savedCustomer.getId()) && d.getCustomerTitle().equals(savedCustomer.getTitle())));
    PageLink pageLinkWithType = new PageLink(100, 0, device.getType());
    List<DeviceInfo> deviceInfosWithType = deviceService.findDeviceInfosByTenantId(tenantId, pageLinkWithType).getData();
    Assert.assertFalse(deviceInfosWithType.isEmpty());
    Assert.assertTrue(deviceInfosWithType.stream().anyMatch(d -> d.getId().equals(savedDevice.getId()) && d.getTenantId().equals(tenantId) && d.getType().equals(device.getType())));
}
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 4 with DeviceInfo

use of org.thingsboard.server.common.data.DeviceInfo 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 5 with DeviceInfo

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

Aggregations

DeviceInfo (org.thingsboard.server.common.data.DeviceInfo)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Device (org.thingsboard.server.common.data.Device)5 PageLink (org.thingsboard.server.common.data.page.PageLink)5 Customer (org.thingsboard.server.common.data.Customer)4 Tenant (org.thingsboard.server.common.data.Tenant)4 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)4 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)3 ByteBuffer (java.nio.ByteBuffer)3 Collections (java.util.Collections)3 List (java.util.List)3 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)3 After (org.junit.After)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 Rule (org.junit.Rule)3 ExpectedException (org.junit.rules.ExpectedException)3