|
|
|
@ -23,7 +23,6 @@ import org.junit.After; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.testcontainers.shaded.org.apache.commons.lang.RandomStringUtils; |
|
|
|
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
@ -43,6 +42,8 @@ import java.util.List; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeoutException; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
@ -52,6 +53,13 @@ import static org.junit.Assert.assertNotNull; |
|
|
|
*/ |
|
|
|
public class JpaDeviceDaoTest extends AbstractJpaDaoTest { |
|
|
|
|
|
|
|
public static final int COUNT_DEVICES = 40; |
|
|
|
public static final String PREFIX_FOR_DEVICE_NAME = "SEARCH_TEXT_"; |
|
|
|
List<UUID> deviceIds; |
|
|
|
UUID tenantId1; |
|
|
|
UUID tenantId2; |
|
|
|
UUID customerId1; |
|
|
|
UUID customerId2; |
|
|
|
@Autowired |
|
|
|
private DeviceDao deviceDao; |
|
|
|
|
|
|
|
@ -64,8 +72,19 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { |
|
|
|
|
|
|
|
@Before |
|
|
|
public void setUp() { |
|
|
|
createDeviceProfile(); |
|
|
|
|
|
|
|
tenantId1 = Uuids.timeBased(); |
|
|
|
customerId1 = Uuids.timeBased(); |
|
|
|
tenantId2 = Uuids.timeBased(); |
|
|
|
customerId2 = Uuids.timeBased(); |
|
|
|
|
|
|
|
deviceIds = createDevices(tenantId1, tenantId2, customerId1, customerId2, COUNT_DEVICES); |
|
|
|
} |
|
|
|
|
|
|
|
private void createDeviceProfile() { |
|
|
|
DeviceProfile deviceProfile = new DeviceProfile(); |
|
|
|
deviceProfile.setName("TEST" + RandomStringUtils.random(7)); |
|
|
|
deviceProfile.setName("TEST"); |
|
|
|
deviceProfile.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
deviceProfile.setType(DeviceProfileType.DEFAULT); |
|
|
|
deviceProfile.setTransportType(DeviceTransportType.DEFAULT); |
|
|
|
@ -75,6 +94,8 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { |
|
|
|
|
|
|
|
@After |
|
|
|
public void tearDown() throws Exception { |
|
|
|
deviceDao.removeAllByIds(deviceIds); |
|
|
|
deviceProfileDao.removeById(TenantId.SYS_TENANT_ID, savedDeviceProfile.getUuidId()); |
|
|
|
if (executor != null) { |
|
|
|
executor.shutdownNow(); |
|
|
|
} |
|
|
|
@ -82,13 +103,7 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFindDevicesByTenantId() { |
|
|
|
UUID tenantId1 = Uuids.timeBased(); |
|
|
|
UUID tenantId2 = Uuids.timeBased(); |
|
|
|
UUID customerId1 = Uuids.timeBased(); |
|
|
|
UUID customerId2 = Uuids.timeBased(); |
|
|
|
createDevices(tenantId1, tenantId2, customerId1, customerId2, 40); |
|
|
|
|
|
|
|
PageLink pageLink = new PageLink(15, 0, "SEARCH_TEXT"); |
|
|
|
PageLink pageLink = new PageLink(15, 0, PREFIX_FOR_DEVICE_NAME); |
|
|
|
PageData<Device> devices1 = deviceDao.findDevicesByTenantId(tenantId1, pageLink); |
|
|
|
assertEquals(15, devices1.getData().size()); |
|
|
|
|
|
|
|
@ -99,11 +114,12 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFindAsync() throws ExecutionException, InterruptedException { |
|
|
|
public void testFindAsync() throws ExecutionException, InterruptedException, TimeoutException { |
|
|
|
UUID tenantId = Uuids.timeBased(); |
|
|
|
UUID customerId = Uuids.timeBased(); |
|
|
|
Device device = getDevice(tenantId, customerId); |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId), device); |
|
|
|
// send to method getDevice() number = 40, because make random name is bad and name "SEARCH_TEXT_40" don't used
|
|
|
|
Device device = getDevice(tenantId, customerId, 40); |
|
|
|
deviceIds.add(deviceDao.save(TenantId.fromUUID(tenantId), device).getUuidId()); |
|
|
|
|
|
|
|
UUID uuid = device.getId().getId(); |
|
|
|
Device entity = deviceDao.findById(TenantId.fromUUID(tenantId), uuid); |
|
|
|
@ -112,73 +128,43 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest { |
|
|
|
|
|
|
|
executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope"))); |
|
|
|
ListenableFuture<Device> future = executor.submit(() -> deviceDao.findById(TenantId.fromUUID(tenantId), uuid)); |
|
|
|
Device asyncDevice = future.get(); |
|
|
|
Device asyncDevice = future.get(30, TimeUnit.SECONDS); |
|
|
|
assertNotNull("Async device expected to be not null", asyncDevice); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFindDevicesByTenantIdAndIdsAsync() throws ExecutionException, InterruptedException { |
|
|
|
UUID tenantId1 = Uuids.timeBased(); |
|
|
|
UUID customerId1 = Uuids.timeBased(); |
|
|
|
UUID tenantId2 = Uuids.timeBased(); |
|
|
|
UUID customerId2 = Uuids.timeBased(); |
|
|
|
|
|
|
|
List<UUID> deviceIds = new ArrayList<>(); |
|
|
|
|
|
|
|
for(int i = 0; i < 5; i++) { |
|
|
|
UUID deviceId1 = Uuids.timeBased(); |
|
|
|
UUID deviceId2 = Uuids.timeBased(); |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId1), getDevice(tenantId1, customerId1, deviceId1)); |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId2), getDevice(tenantId2, customerId2, deviceId2)); |
|
|
|
deviceIds.add(deviceId1); |
|
|
|
deviceIds.add(deviceId2); |
|
|
|
} |
|
|
|
|
|
|
|
public void testFindDevicesByTenantIdAndIdsAsync() throws ExecutionException, InterruptedException, TimeoutException { |
|
|
|
ListenableFuture<List<Device>> devicesFuture = deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId1, deviceIds); |
|
|
|
List<Device> devices = devicesFuture.get(); |
|
|
|
assertEquals(5, devices.size()); |
|
|
|
List<Device> devices = devicesFuture.get(30, TimeUnit.SECONDS); |
|
|
|
assertEquals(20, devices.size()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFindDevicesByTenantIdAndCustomerIdAndIdsAsync() throws ExecutionException, InterruptedException { |
|
|
|
UUID tenantId1 = Uuids.timeBased(); |
|
|
|
UUID customerId1 = Uuids.timeBased(); |
|
|
|
UUID tenantId2 = Uuids.timeBased(); |
|
|
|
UUID customerId2 = Uuids.timeBased(); |
|
|
|
|
|
|
|
List<UUID> deviceIds = new ArrayList<>(); |
|
|
|
|
|
|
|
for(int i = 0; i < 20; i++) { |
|
|
|
UUID deviceId1 = Uuids.timeBased(); |
|
|
|
UUID deviceId2 = Uuids.timeBased(); |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId1), getDevice(tenantId1, customerId1, deviceId1)); |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId2), getDevice(tenantId2, customerId2, deviceId2)); |
|
|
|
deviceIds.add(deviceId1); |
|
|
|
deviceIds.add(deviceId2); |
|
|
|
} |
|
|
|
|
|
|
|
public void testFindDevicesByTenantIdAndCustomerIdAndIdsAsync() throws ExecutionException, InterruptedException, TimeoutException { |
|
|
|
ListenableFuture<List<Device>> devicesFuture = deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId1, customerId1, deviceIds); |
|
|
|
List<Device> devices = devicesFuture.get(); |
|
|
|
List<Device> devices = devicesFuture.get(30, TimeUnit.SECONDS); |
|
|
|
assertEquals(20, devices.size()); |
|
|
|
} |
|
|
|
|
|
|
|
private void createDevices(UUID tenantId1, UUID tenantId2, UUID customerId1, UUID customerId2, int count) { |
|
|
|
private List<UUID> createDevices(UUID tenantId1, UUID tenantId2, UUID customerId1, UUID customerId2, int count) { |
|
|
|
List<UUID> savedDevicesUUID = new ArrayList<>(); |
|
|
|
for (int i = 0; i < count / 2; i++) { |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId1), getDevice(tenantId1, customerId1)); |
|
|
|
deviceDao.save(TenantId.fromUUID(tenantId2), getDevice(tenantId2, customerId2)); |
|
|
|
savedDevicesUUID.add(deviceDao.save(TenantId.fromUUID(tenantId1), getDevice(tenantId1, customerId1, i)).getUuidId()); |
|
|
|
savedDevicesUUID.add(deviceDao.save(TenantId.fromUUID(tenantId2), getDevice(tenantId2, customerId2, i + count / 2)).getUuidId()); |
|
|
|
} |
|
|
|
return savedDevicesUUID; |
|
|
|
} |
|
|
|
|
|
|
|
private Device getDevice(UUID tenantId, UUID customerID) { |
|
|
|
return getDevice(tenantId, customerID, Uuids.timeBased()); |
|
|
|
private Device getDevice(UUID tenantId, UUID customerID, int number) { |
|
|
|
return getDevice(tenantId, customerID, Uuids.timeBased(), number); |
|
|
|
} |
|
|
|
|
|
|
|
private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId) { |
|
|
|
private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId, int number) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setId(new DeviceId(deviceId)); |
|
|
|
device.setTenantId(TenantId.fromUUID(tenantId)); |
|
|
|
device.setCustomerId(new CustomerId(customerID)); |
|
|
|
device.setName("SEARCH_TEXT" + RandomStringUtils.random(7)); |
|
|
|
device.setName(PREFIX_FOR_DEVICE_NAME + number); |
|
|
|
device.setDeviceProfileId(savedDeviceProfile.getId()); |
|
|
|
return device; |
|
|
|
} |
|
|
|
|