Tests added for JpaDeviceDao and DeviceService

This commit is contained in:
Sergey Matvienko 2023-09-01 15:08:50 +02:00
parent 22c9aa3776
commit f621c10260
2 changed files with 22 additions and 4 deletions

View File

@ -283,6 +283,17 @@ public class DeviceServiceTest extends AbstractServiceTest {
}); });
} }
@Test
public void testSaveDeviceWithNameContains0x00_thenDataValidationException() {
Device device = new Device();
device.setType("default");
device.setTenantId(tenantId);
device.setName("F0929906\000\000\000\000\000\000\000\000\000");
Assertions.assertThrows(DataValidationException.class, () -> {
deviceService.saveDevice(device);
});
}
@Test @Test
public void testSaveDeviceWithInvalidTenant() { public void testSaveDeviceWithInvalidTenant() {
Device device = new Device(); Device device = new Device();

View File

@ -45,6 +45,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -101,6 +102,12 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest {
} }
} }
@Test
public void testSaveDeviceName0x00_thenSomeDatabaseException() {
Device device = getDevice(tenantId1, customerId1, "\u0000");
assertThatThrownBy(() -> deviceIds.add(deviceDao.save(TenantId.fromUUID(tenantId1), device).getUuidId()));
}
@Test @Test
public void testFindDevicesByTenantId() { public void testFindDevicesByTenantId() {
PageLink pageLink = new PageLink(15, 0, PREFIX_FOR_DEVICE_NAME); PageLink pageLink = new PageLink(15, 0, PREFIX_FOR_DEVICE_NAME);
@ -155,16 +162,16 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest {
return savedDevicesUUID; return savedDevicesUUID;
} }
private Device getDevice(UUID tenantId, UUID customerID, int number) { private Device getDevice(UUID tenantId, UUID customerID, Object nameSuffix) {
return getDevice(tenantId, customerID, Uuids.timeBased(), number); return getDevice(tenantId, customerID, Uuids.timeBased(), nameSuffix);
} }
private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId, int number) { private Device getDevice(UUID tenantId, UUID customerID, UUID deviceId, Object nameSuffix) {
Device device = new Device(); Device device = new Device();
device.setId(new DeviceId(deviceId)); device.setId(new DeviceId(deviceId));
device.setTenantId(TenantId.fromUUID(tenantId)); device.setTenantId(TenantId.fromUUID(tenantId));
device.setCustomerId(new CustomerId(customerID)); device.setCustomerId(new CustomerId(customerID));
device.setName(PREFIX_FOR_DEVICE_NAME + number); device.setName(PREFIX_FOR_DEVICE_NAME + nameSuffix);
device.setDeviceProfileId(savedDeviceProfile.getId()); device.setDeviceProfileId(savedDeviceProfile.getId());
return device; return device;
} }