diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/device/DeviceRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/device/DeviceRepository.java index 2deae0bbe1..7eaf5eec00 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/device/DeviceRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/device/DeviceRepository.java @@ -19,7 +19,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.dao.model.sql.DeviceEntity; @@ -83,7 +82,10 @@ public interface DeviceRepository extends JpaRepository { "LEFT JOIN CustomerEntity c on c.id = d.customerId " + "LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " + "WHERE d.tenantId = :tenantId " + - "AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") + "AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(p.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))") Page findDeviceInfosByTenantId(@Param("tenantId") UUID tenantId, @Param("textSearch") String textSearch, Pageable pageable); @@ -132,7 +134,9 @@ public interface DeviceRepository extends JpaRepository { "LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " + "WHERE d.tenantId = :tenantId " + "AND d.type = :type " + - "AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") + "AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))") Page findDeviceInfosByTenantIdAndType(@Param("tenantId") UUID tenantId, @Param("type") String type, @Param("textSearch") String textSearch, @@ -144,7 +148,9 @@ public interface DeviceRepository extends JpaRepository { "LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " + "WHERE d.tenantId = :tenantId " + "AND d.deviceProfileId = :deviceProfileId " + - "AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") + "AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " + + "OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))") Page findDeviceInfosByTenantIdAndDeviceProfileId(@Param("tenantId") UUID tenantId, @Param("deviceProfileId") UUID deviceProfileId, @Param("textSearch") String textSearch, diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java index 48b8b427e0..1a9432419e 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseDeviceServiceTest.java @@ -245,7 +245,7 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { thrown.expectMessage("Can't assign firmware with different deviceProfile!"); deviceService.saveDevice(savedDevice); } - + @Test(expected = DataValidationException.class) public void testSaveDeviceWithEmptyName() { Device device = new Device(); @@ -324,24 +324,24 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { public void testFindDeviceTypesByTenantId() throws Exception { List devices = new ArrayList<>(); try { - for (int i=0;i<3;i++) { + for (int i = 0; i < 3; i++) { Device device = new Device(); device.setTenantId(tenantId); - device.setName("My device B"+i); + device.setName("My device B" + i); device.setType("typeB"); devices.add(deviceService.saveDevice(device)); } - for (int i=0;i<7;i++) { + for (int i = 0; i < 7; i++) { Device device = new Device(); device.setTenantId(tenantId); - device.setName("My device C"+i); + device.setName("My device C" + i); device.setType("typeC"); devices.add(deviceService.saveDevice(device)); } - for (int i=0;i<9;i++) { + for (int i = 0; i < 9; i++) { Device device = new Device(); device.setTenantId(tenantId); - device.setName("My device A"+i); + device.setName("My device A" + i); device.setType("typeA"); devices.add(deviceService.saveDevice(device)); } @@ -352,7 +352,9 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { Assert.assertEquals("typeB", deviceTypes.get(1).getType()); Assert.assertEquals("typeC", deviceTypes.get(2).getType()); } finally { - devices.forEach((device) -> { deviceService.deleteDevice(tenantId, device.getId()); }); + devices.forEach((device) -> { + deviceService.deleteDevice(tenantId, device.getId()); + }); } } @@ -381,10 +383,10 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { TenantId tenantId = tenant.getId(); List devices = new ArrayList<>(); - for (int i=0;i<178;i++) { + for (int i = 0; i < 178; i++) { Device device = new Device(); device.setTenantId(tenantId); - device.setName("Device"+i); + device.setName("Device" + i); device.setType("default"); devices.add(deviceService.saveDevice(device)); } @@ -419,11 +421,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { public void testFindDevicesByTenantIdAndName() { String title1 = "Device title 1"; List devicesTitle1 = new ArrayList<>(); - for (int i=0;i<143;i++) { + for (int i = 0; i < 143; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title1+suffix; + String name = title1 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType("default"); @@ -431,11 +433,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { } String title2 = "Device title 2"; List devicesTitle2 = new ArrayList<>(); - for (int i=0;i<175;i++) { + for (int i = 0; i < 175; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title2+suffix; + String name = title2 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType("default"); @@ -497,11 +499,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { String title1 = "Device title 1"; String type1 = "typeA"; List devicesType1 = new ArrayList<>(); - for (int i=0;i<143;i++) { + for (int i = 0; i < 143; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title1+suffix; + String name = title1 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType(type1); @@ -510,11 +512,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { String title2 = "Device title 2"; String type2 = "typeB"; List devicesType2 = new ArrayList<>(); - for (int i=0;i<175;i++) { + for (int i = 0; i < 175; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title2+suffix; + String name = title2 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType(type2); @@ -586,10 +588,10 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { CustomerId customerId = customer.getId(); List devices = new ArrayList<>(); - for (int i=0;i<278;i++) { + for (int i = 0; i < 278; i++) { Device device = new Device(); device.setTenantId(tenantId); - device.setName("Device"+i); + 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")); @@ -632,11 +634,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { String title1 = "Device title 1"; List devicesTitle1 = new ArrayList<>(); - for (int i=0;i<175;i++) { + for (int i = 0; i < 175; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title1+suffix; + String name = title1 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType("default"); @@ -645,11 +647,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { } String title2 = "Device title 2"; List devicesTitle2 = new ArrayList<>(); - for (int i=0;i<143;i++) { + for (int i = 0; i < 143; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title2+suffix; + String name = title2 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType("default"); @@ -720,11 +722,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { String title1 = "Device title 1"; String type1 = "typeC"; List devicesType1 = new ArrayList<>(); - for (int i=0;i<175;i++) { + for (int i = 0; i < 175; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title1+suffix; + String name = title1 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType(type1); @@ -734,11 +736,11 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { String title2 = "Device title 2"; String type2 = "typeD"; List devicesType2 = new ArrayList<>(); - for (int i=0;i<143;i++) { + for (int i = 0; i < 143; i++) { Device device = new Device(); device.setTenantId(tenantId); String suffix = RandomStringUtils.randomAlphanumeric(15); - String name = title2+suffix; + String name = title2 + suffix; name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); device.setName(name); device.setType(type2); @@ -818,4 +820,157 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest { Assert.assertNull("Can't find device by name in cache if it was renamed", renamedDevice); deviceService.deleteDevice(tenantId, savedDevice.getId()); } + + @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 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 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 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()) + ) + ); + } + + @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 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 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()) + ) + ); + } + + @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 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 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()) + ) + ); + } }