Browse Source

improvements

pull/4681/head
YevhenBondarenko 5 years ago
parent
commit
10dda013cb
  1. 32
      application/src/main/java/org/thingsboard/server/service/ota/DefaultOtaPackageStateService.java
  2. 33
      application/src/test/java/org/thingsboard/server/controller/BaseOtaPackageControllerTest.java
  3. 2
      dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java
  4. 8
      dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java

32
application/src/main/java/org/thingsboard/server/service/ota/DefaultOtaPackageStateService.java

@ -285,18 +285,34 @@ public class DefaultOtaPackageStateService implements OtaPackageStateService {
List<AttributeKvEntry> attributes = new ArrayList<>(); List<AttributeKvEntry> attributes = new ArrayList<>();
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, TITLE), otaPackage.getTitle()))); attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, TITLE), otaPackage.getTitle())));
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, VERSION), otaPackage.getVersion()))); attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, VERSION), otaPackage.getVersion())));
if (StringUtils.isEmpty(otaPackage.getUrl())) { if (otaPackage.hasUrl()) {
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, URL), otaPackage.getUrl())));
List<String> attrToRemove = new ArrayList<>();
if (otaPackage.getDataSize() == null) {
attrToRemove.add(getAttributeKey(otaPackageType, SIZE));
} else {
attributes.add(new BaseAttributeKvEntry(ts, new LongDataEntry(getAttributeKey(otaPackageType, SIZE), otaPackage.getDataSize())));
}
if (otaPackage.getChecksumAlgorithm() != null) {
attrToRemove.add(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM));
} else {
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM), otaPackage.getChecksumAlgorithm().name())));
}
if (StringUtils.isEmpty(otaPackage.getChecksum())) {
attrToRemove.add(getAttributeKey(otaPackageType, CHECKSUM));
} else {
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM), otaPackage.getChecksum())));
}
remove(device, otaPackageType, attrToRemove);
} else {
attributes.add(new BaseAttributeKvEntry(ts, new LongDataEntry(getAttributeKey(otaPackageType, SIZE), otaPackage.getDataSize()))); attributes.add(new BaseAttributeKvEntry(ts, new LongDataEntry(getAttributeKey(otaPackageType, SIZE), otaPackage.getDataSize())));
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM), otaPackage.getChecksumAlgorithm().name()))); attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM), otaPackage.getChecksumAlgorithm().name())));
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM), otaPackage.getChecksum()))); attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, CHECKSUM), otaPackage.getChecksum())));
remove(device, otaPackageType, Collections.singletonList(getAttributeKey(otaPackageType, URL))); remove(device, otaPackageType, Collections.singletonList(getAttributeKey(otaPackageType, URL)));
} else {
List<String> attrToRemove = new ArrayList<>();
attrToRemove.add(getAttributeKey(otaPackageType, SIZE));
attrToRemove.add(getAttributeKey(otaPackageType, CHECKSUM_ALGORITHM));
attrToRemove.add(getAttributeKey(otaPackageType, CHECKSUM));
remove(device, otaPackageType, attrToRemove);
attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, URL), otaPackage.getUrl())));
} }
telemetryService.saveAndNotify(tenantId, deviceId, DataConstants.SHARED_SCOPE, attributes, new FutureCallback<>() { telemetryService.saveAndNotify(tenantId, deviceId, DataConstants.SHARED_SCOPE, attributes, new FutureCallback<>() {

33
application/src/test/java/org/thingsboard/server/controller/BaseOtaPackageControllerTest.java

@ -50,7 +50,7 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes
private static final String FILE_NAME = "filename.txt"; private static final String FILE_NAME = "filename.txt";
private static final String VERSION = "v1.0"; private static final String VERSION = "v1.0";
private static final String CONTENT_TYPE = "text/plain"; private static final String CONTENT_TYPE = "text/plain";
private static final String CHECKSUM_ALGORITHM = "sha256"; private static final String CHECKSUM_ALGORITHM = "SHA256";
private static final String CHECKSUM = "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a"; private static final String CHECKSUM = "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a";
private static final ByteBuffer DATA = ByteBuffer.wrap(new byte[]{1}); private static final ByteBuffer DATA = ByteBuffer.wrap(new byte[]{1});
@ -257,7 +257,7 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes
@Test @Test
public void testFindTenantFirmwaresByHasData() throws Exception { public void testFindTenantFirmwaresByHasData() throws Exception {
List<OtaPackageInfo> otaPackagesWithData = new ArrayList<>(); List<OtaPackageInfo> otaPackagesWithData = new ArrayList<>();
List<OtaPackageInfo> otaPackagesWithoutData = new ArrayList<>(); List<OtaPackageInfo> allOtaPackages = new ArrayList<>();
for (int i = 0; i < 165; i++) { for (int i = 0; i < 165; i++) {
OtaPackageInfo firmwareInfo = new OtaPackageInfo(); OtaPackageInfo firmwareInfo = new OtaPackageInfo();
@ -272,44 +272,45 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes
MockMultipartFile testData = new MockMultipartFile("file", FILE_NAME, CONTENT_TYPE, DATA.array()); MockMultipartFile testData = new MockMultipartFile("file", FILE_NAME, CONTENT_TYPE, DATA.array());
OtaPackage savedFirmware = savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, CHECKSUM_ALGORITHM); OtaPackage savedFirmware = savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, CHECKSUM_ALGORITHM);
otaPackagesWithData.add(new OtaPackageInfo(savedFirmware)); savedFirmwareInfo = new OtaPackageInfo(savedFirmware);
} else { otaPackagesWithData.add(savedFirmwareInfo);
otaPackagesWithoutData.add(savedFirmwareInfo);
} }
allOtaPackages.add(savedFirmwareInfo);
} }
List<OtaPackageInfo> loadedFirmwaresWithData = new ArrayList<>(); List<OtaPackageInfo> loadedOtaPackagesWithData = new ArrayList<>();
PageLink pageLink = new PageLink(24); PageLink pageLink = new PageLink(24);
PageData<OtaPackageInfo> pageData; PageData<OtaPackageInfo> pageData;
do { do {
pageData = doGetTypedWithPageLink("/api/otaPackages/" + deviceProfileId.toString() + "/FIRMWARE/true?", pageData = doGetTypedWithPageLink("/api/otaPackages/" + deviceProfileId.toString() + "/FIRMWARE?",
new TypeReference<>() { new TypeReference<>() {
}, pageLink); }, pageLink);
loadedFirmwaresWithData.addAll(pageData.getData()); loadedOtaPackagesWithData.addAll(pageData.getData());
if (pageData.hasNext()) { if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink(); pageLink = pageLink.nextPageLink();
} }
} while (pageData.hasNext()); } while (pageData.hasNext());
List<OtaPackageInfo> loadedFirmwaresWithoutData = new ArrayList<>(); List<OtaPackageInfo> allLoadedOtaPackages = new ArrayList<>();
pageLink = new PageLink(24); pageLink = new PageLink(24);
do { do {
pageData = doGetTypedWithPageLink("/api/otaPackages/" + deviceProfileId.toString() + "/FIRMWARE/false?", pageData = doGetTypedWithPageLink("/api/otaPackages?",
new TypeReference<>() { new TypeReference<>() {
}, pageLink); }, pageLink);
loadedFirmwaresWithoutData.addAll(pageData.getData()); allLoadedOtaPackages.addAll(pageData.getData());
if (pageData.hasNext()) { if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink(); pageLink = pageLink.nextPageLink();
} }
} while (pageData.hasNext()); } while (pageData.hasNext());
Collections.sort(otaPackagesWithData, idComparator); Collections.sort(otaPackagesWithData, idComparator);
Collections.sort(otaPackagesWithoutData, idComparator); Collections.sort(allOtaPackages, idComparator);
Collections.sort(loadedFirmwaresWithData, idComparator); Collections.sort(loadedOtaPackagesWithData, idComparator);
Collections.sort(loadedFirmwaresWithoutData, idComparator); Collections.sort(allLoadedOtaPackages, idComparator);
Assert.assertEquals(otaPackagesWithData, loadedFirmwaresWithData); Assert.assertEquals(otaPackagesWithData, loadedOtaPackagesWithData);
Assert.assertEquals(otaPackagesWithoutData, loadedFirmwaresWithoutData); Assert.assertEquals(allOtaPackages, allLoadedOtaPackages);
} }

2
dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java

@ -253,7 +253,7 @@ public class BaseOtaPackageService implements OtaPackageService {
protected void validateDataImpl(TenantId tenantId, OtaPackage otaPackage) { protected void validateDataImpl(TenantId tenantId, OtaPackage otaPackage) {
validateImpl(otaPackage); validateImpl(otaPackage);
if (StringUtils.isEmpty(otaPackage.getUrl())) { if (!otaPackage.hasUrl()) {
if (StringUtils.isEmpty(otaPackage.getFileName())) { if (StringUtils.isEmpty(otaPackage.getFileName())) {
throw new DataValidationException("OtaPackage file name should be specified!"); throw new DataValidationException("OtaPackage file name should be specified!");
} }

8
dao/src/test/java/org/thingsboard/server/dao/SqlDaoServiceTestSuite.java

@ -24,10 +24,10 @@ import java.util.Arrays;
@RunWith(ClasspathSuite.class) @RunWith(ClasspathSuite.class)
@ClassnameFilters({ @ClassnameFilters({
"org.thingsboard.server.dao.service.sql.OtaPackageServiceSqlTest", "org.thingsboard.server.dao.service.sql.*SqlTest",
// "org.thingsboard.server.dao.service.attributes.sql.*SqlTest", "org.thingsboard.server.dao.service.attributes.sql.*SqlTest",
// "org.thingsboard.server.dao.service.event.sql.*SqlTest", "org.thingsboard.server.dao.service.event.sql.*SqlTest",
// "org.thingsboard.server.dao.service.timeseries.sql.*SqlTest" "org.thingsboard.server.dao.service.timeseries.sql.*SqlTest"
}) })
public class SqlDaoServiceTestSuite { public class SqlDaoServiceTestSuite {

Loading…
Cancel
Save