Browse Source

added corresponding tests

pull/11459/head
YevhenBondarenko 2 years ago
parent
commit
7f2d78cb04
  1. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceService.java
  2. 4
      common/data/src/main/java/org/thingsboard/server/common/data/HasOtaPackage.java
  3. 2
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java
  4. 125
      dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java

2
common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceService.java

@ -77,7 +77,7 @@ public interface DeviceService extends EntityDaoService {
PageData<Device> findDevicesByTenantIdAndTypeAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType type, PageLink pageLink);
Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType otaPackageType);
long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType otaPackageType);
ListenableFuture<List<Device>> findDevicesByTenantIdAndIdsAsync(TenantId tenantId, List<DeviceId> deviceIds);

4
common/data/src/main/java/org/thingsboard/server/common/data/HasOtaPackage.java

@ -22,4 +22,8 @@ public interface HasOtaPackage {
OtaPackageId getFirmwareId();
OtaPackageId getSoftwareId();
void setFirmwareId(OtaPackageId otaPackageId);
void setSoftwareId(OtaPackageId otaPackageId);
}

2
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -407,7 +407,7 @@ public class DeviceServiceImpl extends CachedVersionedEntityService<DeviceCacheK
}
@Override
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType type) {
public long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType type) {
log.trace("Executing countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage, tenantId [{}], deviceProfileId [{}], type [{}]", tenantId, deviceProfileId, type);
validateId(tenantId, id -> INCORRECT_TENANT_ID + id);
validateId(deviceProfileId, id -> INCORRECT_DEVICE_PROFILE_ID + id);

125
dao/src/test/java/org/thingsboard/server/dao/service/DeviceServiceTest.java

@ -33,13 +33,18 @@ import org.thingsboard.server.common.data.DeviceInfo;
import org.thingsboard.server.common.data.DeviceInfoFilter;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.EntitySubtype;
import org.thingsboard.server.common.data.HasOtaPackage;
import org.thingsboard.server.common.data.OtaPackage;
import org.thingsboard.server.common.data.OtaPackageInfo;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.TenantProfile;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.OtaPackageId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm;
import org.thingsboard.server.common.data.ota.OtaPackageType;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.security.DeviceCredentials;
@ -63,6 +68,7 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE;
import static org.thingsboard.server.common.data.ota.OtaPackageType.SOFTWARE;
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
@DaoSqlTest
@ -202,6 +208,125 @@ public class DeviceServiceTest extends AbstractServiceTest {
deleteDevice(anotherTenantId, anotherDevice);
}
@Test
public void testCountDevicesWithoutFirmware() {
testCountDevicesWithoutOta(FIRMWARE);
}
@Test
public void testCountDevicesWithoutSoftware() {
testCountDevicesWithoutOta(SOFTWARE);
}
public void testCountDevicesWithoutOta(OtaPackageType type) {
var defaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(tenantId);
var deviceProfileId = defaultDeviceProfile.getId();
Assert.assertEquals(0, deviceService.countByTenantId(tenantId));
Assert.assertEquals(0, deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(tenantId, deviceProfileId, type));
int maxDevices = 8;
List<Device> devices = new ArrayList<>(maxDevices);
for (int i = 1; i <= maxDevices; i++) {
devices.add(this.saveDevice(tenantId, "My device " + i));
Assert.assertEquals(i, deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(tenantId, deviceProfileId, type));
}
Assert.assertEquals(maxDevices, deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(tenantId, deviceProfileId, type));
var otaPackageId = createOta(deviceProfileId, type);
int devicesWithOta = maxDevices / 2;
for (int i = 0; i < devicesWithOta; i++) {
var device = devices.get(i);
setOtaPackageId(device, type, otaPackageId);
deviceService.saveDevice(device);
}
Assert.assertEquals(maxDevices - devicesWithOta, deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(tenantId, deviceProfileId, type));
devices.forEach(device -> deleteDevice(tenantId, device));
}
@Test
public void testFindDevicesWithoutFirmware() {
testFindDevicesWithoutOta(FIRMWARE);
}
@Test
public void testFindDevicesWithoutSoftware() {
testFindDevicesWithoutOta(SOFTWARE);
}
public void testFindDevicesWithoutOta(OtaPackageType type) {
var defaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(tenantId);
var deviceProfileId = defaultDeviceProfile.getId();
PageLink pageLink = new PageLink(100);
Assert.assertEquals(0, deviceService.countByTenantId(tenantId));
Assert.assertEquals(0, deviceService.findDevicesByTenantIdAndTypeAndEmptyOtaPackage(tenantId, deviceProfileId, type, pageLink).getData().size());
int maxDevices = 8;
List<Device> devices = new ArrayList<>(maxDevices);
for (int i = 1; i <= maxDevices; i++) {
devices.add(this.saveDevice(tenantId, "My device " + i));
}
var foundDevices = deviceService.findDevicesByTenantIdAndTypeAndEmptyOtaPackage(tenantId, deviceProfileId, type, pageLink).getData();
Assert.assertEquals(maxDevices, foundDevices.size());
devices.sort(idComparator);
foundDevices.sort(idComparator);
Assert.assertEquals(devices, foundDevices);
var otaPackageId = createOta(deviceProfileId, type);
int devicesWithOta = maxDevices / 2;
for (int i = 0; i < devicesWithOta; i++) {
var device = devices.get(i);
setOtaPackageId(device, type, otaPackageId);
deviceService.saveDevice(device);
}
foundDevices = deviceService.findDevicesByTenantIdAndTypeAndEmptyOtaPackage(tenantId, deviceProfileId, type, pageLink).getData();
Assert.assertEquals(maxDevices - devicesWithOta, foundDevices.size());
foundDevices.sort(idComparator);
for (int i = 0; i < foundDevices.size(); i++) {
Assert.assertEquals(devices.get(i + devicesWithOta), foundDevices.get(i));
}
devices.forEach(device -> deleteDevice(tenantId, device));
}
private <T extends HasOtaPackage> void setOtaPackageId(T obj, OtaPackageType type, OtaPackageId otaPackageId) {
switch (type) {
case FIRMWARE -> obj.setFirmwareId(otaPackageId);
case SOFTWARE -> obj.setSoftwareId(otaPackageId);
}
}
private OtaPackageId createOta(DeviceProfileId deviceProfileId, OtaPackageType type) {
OtaPackageInfo ota = new OtaPackageInfo();
ota.setTenantId(tenantId);
ota.setDeviceProfileId(deviceProfileId);
ota.setType(type);
ota.setTitle("Test_" + type);
ota.setVersion("v1.0");
ota.setUrl("http://ota.test.org");
ota.setDataSize(0L);
OtaPackageInfo savedOta = otaPackageService.saveOtaPackageInfo(ota, true);
Assert.assertNotNull(savedOta);
return savedOta.getId();
}
void deleteDevice(TenantId tenantId, Device device) {
deviceService.deleteDevice(tenantId, device.getId());
}

Loading…
Cancel
Save