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