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);
}
}
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);
}
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())));
}
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());
}
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())));
}
Aggregations