diff --git a/application/src/main/java/org/thingsboard/server/controller/OtaPackageController.java b/application/src/main/java/org/thingsboard/server/controller/OtaPackageController.java index e28c5e37e1..153b5b5c93 100644 --- a/application/src/main/java/org/thingsboard/server/controller/OtaPackageController.java +++ b/application/src/main/java/org/thingsboard/server/controller/OtaPackageController.java @@ -32,12 +32,13 @@ import org.springframework.web.multipart.MultipartFile; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.OtaPackage; import org.thingsboard.server.common.data.OtaPackageInfo; +import org.thingsboard.server.common.data.SaveOtaPackageInfoRequest; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardException; -import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; -import org.thingsboard.server.common.data.ota.OtaPackageType; import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.OtaPackageId; +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.queue.util.TbCoreComponent; @@ -109,12 +110,12 @@ public class OtaPackageController extends BaseController { @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") @RequestMapping(value = "/otaPackage", method = RequestMethod.POST) @ResponseBody - public OtaPackageInfo saveOtaPackageInfo(@RequestBody OtaPackageInfo otaPackageInfo) throws ThingsboardException { + public OtaPackageInfo saveOtaPackageInfo(@RequestBody SaveOtaPackageInfoRequest otaPackageInfo) throws ThingsboardException { boolean created = otaPackageInfo.getId() == null; try { otaPackageInfo.setTenantId(getTenantId()); checkEntity(otaPackageInfo.getId(), otaPackageInfo, Resource.OTA_PACKAGE); - OtaPackageInfo savedOtaPackageInfo = otaPackageService.saveOtaPackageInfo(otaPackageInfo); + OtaPackageInfo savedOtaPackageInfo = otaPackageService.saveOtaPackageInfo(new OtaPackageInfo(otaPackageInfo), otaPackageInfo.isUsesUrl()); logEntityAction(savedOtaPackageInfo.getId(), savedOtaPackageInfo, null, created ? ActionType.ADDED : ActionType.UPDATED, null); return savedOtaPackageInfo; diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseOtaPackageControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseOtaPackageControllerTest.java index 6aa341cef6..7026548b25 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseOtaPackageControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseOtaPackageControllerTest.java @@ -27,6 +27,7 @@ import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.OtaPackage; import org.thingsboard.server.common.data.OtaPackageInfo; +import org.thingsboard.server.common.data.SaveOtaPackageInfoRequest; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.id.DeviceProfileId; @@ -92,11 +93,12 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes @Test public void testSaveFirmware() throws Exception { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -109,7 +111,7 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes savedFirmwareInfo.setAdditionalInfo(JacksonUtil.newObjectNode()); - save(savedFirmwareInfo); + save(new SaveOtaPackageInfoRequest(savedFirmwareInfo, false)); OtaPackageInfo foundFirmwareInfo = doGet("/api/otaPackage/info/" + savedFirmwareInfo.getId().getId().toString(), OtaPackageInfo.class); Assert.assertEquals(foundFirmwareInfo.getTitle(), savedFirmwareInfo.getTitle()); @@ -117,11 +119,12 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes @Test public void testSaveFirmwareData() throws Exception { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -134,7 +137,7 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes savedFirmwareInfo.setAdditionalInfo(JacksonUtil.newObjectNode()); - save(savedFirmwareInfo); + save(new SaveOtaPackageInfoRequest(savedFirmwareInfo, false)); OtaPackageInfo foundFirmwareInfo = doGet("/api/otaPackage/info/" + savedFirmwareInfo.getId().getId().toString(), OtaPackageInfo.class); Assert.assertEquals(foundFirmwareInfo.getTitle(), savedFirmwareInfo.getTitle()); @@ -151,26 +154,31 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes @Test public void testUpdateFirmwareFromDifferentTenant() throws Exception { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); loginDifferentTenant(); - doPost("/api/otaPackage", savedFirmwareInfo, OtaPackageInfo.class, status().isForbidden()); + doPost("/api/otaPackage", + new SaveOtaPackageInfoRequest(savedFirmwareInfo, false), + OtaPackageInfo.class, + status().isForbidden()); deleteDifferentTenant(); } @Test public void testFindFirmwareInfoById() throws Exception { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -181,11 +189,12 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes @Test public void testFindFirmwareById() throws Exception { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -201,11 +210,12 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes @Test public void testDeleteFirmware() throws Exception { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -220,11 +230,12 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes public void testFindTenantFirmwares() throws Exception { List otaPackages = new ArrayList<>(); for (int i = 0; i < 165; i++) { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION + i); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -263,11 +274,12 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes List allOtaPackages = new ArrayList<>(); for (int i = 0; i < 165; i++) { - OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest(); firmwareInfo.setDeviceProfileId(deviceProfileId); firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION + i); + firmwareInfo.setUsesUrl(false); OtaPackageInfo savedFirmwareInfo = save(firmwareInfo); @@ -316,8 +328,7 @@ public abstract class BaseOtaPackageControllerTest extends AbstractControllerTes Assert.assertEquals(allOtaPackages, allLoadedOtaPackages); } - - private OtaPackageInfo save(OtaPackageInfo firmwareInfo) throws Exception { + private OtaPackageInfo save(SaveOtaPackageInfoRequest firmwareInfo) throws Exception { return doPost("/api/otaPackage", firmwareInfo, OtaPackageInfo.class); } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/ota/OtaPackageService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/ota/OtaPackageService.java index fea29681c1..b14653b1d0 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/ota/OtaPackageService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/ota/OtaPackageService.java @@ -18,11 +18,11 @@ package org.thingsboard.server.dao.ota; import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.OtaPackage; import org.thingsboard.server.common.data.OtaPackageInfo; -import org.thingsboard.server.common.data.ota.ChecksumAlgorithm; -import org.thingsboard.server.common.data.ota.OtaPackageType; 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; @@ -30,7 +30,7 @@ import java.nio.ByteBuffer; public interface OtaPackageService { - OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo); + OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo, boolean isUrl); OtaPackage saveOtaPackage(OtaPackage otaPackage); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/SaveOtaPackageInfoRequest.java b/common/data/src/main/java/org/thingsboard/server/common/data/SaveOtaPackageInfoRequest.java new file mode 100644 index 0000000000..f9c363adf4 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/SaveOtaPackageInfoRequest.java @@ -0,0 +1,37 @@ +/** + * Copyright © 2016-2021 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@Data +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +public class SaveOtaPackageInfoRequest extends OtaPackageInfo{ + boolean usesUrl; + + public SaveOtaPackageInfoRequest(OtaPackageInfo otaPackageInfo, boolean usesUrl) { + super(otaPackageInfo); + this.usesUrl = usesUrl; + } + + public SaveOtaPackageInfoRequest(SaveOtaPackageInfoRequest saveOtaPackageInfoRequest) { + super(saveOtaPackageInfoRequest); + this.usesUrl = saveOtaPackageInfoRequest.isUsesUrl(); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java b/dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java index 881baea4f1..b9942ec6e6 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java @@ -77,8 +77,11 @@ public class BaseOtaPackageService implements OtaPackageService { private TbTenantProfileCache tenantProfileCache; @Override - public OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo) { + public OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo, boolean isUrl) { log.trace("Executing saveOtaPackageInfo [{}]", otaPackageInfo); + if(isUrl && (StringUtils.isEmpty(otaPackageInfo.getUrl()) || otaPackageInfo.getUrl().trim().length() == 0)) { + throw new DataValidationException("Ota package URL should be specified!"); + } otaPackageInfoValidator.validate(otaPackageInfo, OtaPackageInfo::getTenantId); try { OtaPackageId otaPackageId = otaPackageInfo.getId(); @@ -277,7 +280,9 @@ public class BaseOtaPackageService implements OtaPackageService { throw new DataValidationException("Wrong otaPackage file!"); } } else { - //TODO: validate url + if(otaPackage.getData() != null) { + throw new DataValidationException("File can't be saved if URL present!"); + } } } @@ -336,6 +341,10 @@ public class BaseOtaPackageService implements OtaPackageService { if (otaPackageOld.getDataSize() != null && !otaPackageOld.getDataSize().equals(otaPackage.getDataSize())) { throw new DataValidationException("Updating otaPackage data size is prohibited!"); } + + if(otaPackageOld.getUrl() != null && !otaPackageOld.getUrl().equals(otaPackage.getUrl())) { + throw new DataValidationException("Updating otaPackage URL is prohibited!"); + } } private void validateImpl(OtaPackageInfo otaPackageInfo) { @@ -366,6 +375,15 @@ public class BaseOtaPackageService implements OtaPackageService { if (StringUtils.isEmpty(otaPackageInfo.getVersion())) { throw new DataValidationException("OtaPackage version should be specified!"); } + + if(otaPackageInfo.getTitle().length() > 255) { + throw new DataValidationException("The length of title should be equal or shorter than 255"); + } + + if(otaPackageInfo.getVersion().length() > 255) { + throw new DataValidationException("The length of version should be equal or shorter than 255"); + } + } private PaginatedRemover tenantOtaPackageRemover = diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java index 35063eef75..1bf71370ea 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/BaseOtaPackageServiceTest.java @@ -16,6 +16,7 @@ package org.thingsboard.server.dao.service; import com.datastax.oss.driver.api.core.uuid.Uuids; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -163,7 +164,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setVersion(VERSION); firmware.setUrl(URL); firmware.setDataSize(0L); - OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware); + OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware, true); Assert.assertNotNull(savedFirmware); Assert.assertNotNull(savedFirmware.getId()); @@ -174,7 +175,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { Assert.assertEquals(firmware.getContentType(), savedFirmware.getContentType()); savedFirmware.setAdditionalInfo(JacksonUtil.newObjectNode()); - otaPackageService.saveOtaPackageInfo(savedFirmware); + otaPackageService.saveOtaPackageInfo(savedFirmware, true); OtaPackage foundFirmware = otaPackageService.findOtaPackageById(tenantId, savedFirmware.getId()); Assert.assertEquals(foundFirmware.getTitle(), savedFirmware.getTitle()); @@ -190,7 +191,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); - OtaPackageInfo savedFirmwareInfo = otaPackageService.saveOtaPackageInfo(firmwareInfo); + OtaPackageInfo savedFirmwareInfo = otaPackageService.saveOtaPackageInfo(firmwareInfo, false); Assert.assertNotNull(savedFirmwareInfo); Assert.assertNotNull(savedFirmwareInfo.getId()); @@ -216,7 +217,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { savedFirmwareInfo = otaPackageService.findOtaPackageInfoById(tenantId, savedFirmwareInfo.getId()); savedFirmwareInfo.setAdditionalInfo(JacksonUtil.newObjectNode()); - otaPackageService.saveOtaPackageInfo(savedFirmwareInfo); + otaPackageService.saveOtaPackageInfo(savedFirmwareInfo, false); OtaPackage foundFirmware = otaPackageService.findOtaPackageById(tenantId, firmware.getId()); firmware.setAdditionalInfo(JacksonUtil.newObjectNode()); @@ -399,7 +400,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareInfo.setType(FIRMWARE); firmwareInfo.setTitle(TITLE); firmwareInfo.setVersion(VERSION); - otaPackageService.saveOtaPackageInfo(firmwareInfo); + otaPackageService.saveOtaPackageInfo(firmwareInfo, false); OtaPackageInfo newFirmwareInfo = new OtaPackageInfo(); newFirmwareInfo.setTenantId(tenantId); @@ -410,7 +411,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { thrown.expect(DataValidationException.class); thrown.expectMessage("OtaPackage with such title and version already exists!"); - otaPackageService.saveOtaPackageInfo(newFirmwareInfo); + otaPackageService.saveOtaPackageInfo(newFirmwareInfo, false); } @Test @@ -506,7 +507,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setType(FIRMWARE); firmware.setTitle(TITLE); firmware.setVersion(VERSION); - OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware); + OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware, false); OtaPackageInfo foundFirmware = otaPackageService.findOtaPackageInfoById(tenantId, savedFirmware.getId()); Assert.assertNotNull(foundFirmware); @@ -543,7 +544,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareWithUrl.setUrl(URL); firmwareWithUrl.setDataSize(0L); - OtaPackageInfo savedFwWithUrl = otaPackageService.saveOtaPackageInfo(firmwareWithUrl); + OtaPackageInfo savedFwWithUrl = otaPackageService.saveOtaPackageInfo(firmwareWithUrl, true); savedFwWithUrl.setHasData(true); firmwares.add(savedFwWithUrl); @@ -588,7 +589,7 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmwareWithUrl.setUrl(URL); firmwareWithUrl.setDataSize(0L); - OtaPackageInfo savedFwWithUrl = otaPackageService.saveOtaPackageInfo(firmwareWithUrl); + OtaPackageInfo savedFwWithUrl = otaPackageService.saveOtaPackageInfo(firmwareWithUrl, true); savedFwWithUrl.setHasData(true); firmwares.add(savedFwWithUrl); @@ -627,6 +628,71 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { Assert.assertTrue(pageData.getData().isEmpty()); } + @Test + public void testSaveOtaPackageInfoWithBlankAndEmptyUrl() { + OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + firmwareInfo.setDeviceProfileId(deviceProfileId); + firmwareInfo.setType(FIRMWARE); + firmwareInfo.setTitle(TITLE); + firmwareInfo.setVersion(VERSION); + firmwareInfo.setUrl(" "); + thrown.expect(DataValidationException.class); + thrown.expectMessage("Ota package URL should be specified!"); + otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + firmwareInfo.setUrl(""); + otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + } + + @Test + public void testSaveOtaPackageUrlCantBeUpdated() { + OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + firmwareInfo.setDeviceProfileId(deviceProfileId); + firmwareInfo.setType(FIRMWARE); + firmwareInfo.setTitle(TITLE); + firmwareInfo.setVersion(VERSION); + firmwareInfo.setUrl(URL); + firmwareInfo.setTenantId(tenantId); + + OtaPackageInfo savedFirmwareInfo = otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + + thrown.expect(DataValidationException.class); + thrown.expectMessage("Updating otaPackage URL is prohibited!"); + + savedFirmwareInfo.setUrl("https://newurl.com"); + otaPackageService.saveOtaPackageInfo(savedFirmwareInfo, true); + } + + @Test + public void testSaveOtaPackageCantViolateSizeOfTitle() { + OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + firmwareInfo.setDeviceProfileId(deviceProfileId); + firmwareInfo.setType(FIRMWARE); + firmwareInfo.setTitle(RandomStringUtils.random(257)); + firmwareInfo.setVersion(VERSION); + firmwareInfo.setUrl(URL); + firmwareInfo.setTenantId(tenantId); + + thrown.expect(DataValidationException.class); + thrown.expectMessage("The length of title should be equal or shorter than 255"); + + otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + } + + @Test + public void testSaveOtaPackageCantViolateSizeOfVersion() { + OtaPackageInfo firmwareInfo = new OtaPackageInfo(); + firmwareInfo.setDeviceProfileId(deviceProfileId); + firmwareInfo.setType(FIRMWARE); + firmwareInfo.setUrl(URL); + firmwareInfo.setTenantId(tenantId); + firmwareInfo.setTitle(TITLE); + + firmwareInfo.setVersion(RandomStringUtils.random(257)); + thrown.expectMessage("The length of version should be equal or shorter than 255"); + + otaPackageService.saveOtaPackageInfo(firmwareInfo, true); + } + private OtaPackage createFirmware(TenantId tenantId, String version) { OtaPackage firmware = new OtaPackage(); firmware.setTenantId(tenantId); @@ -642,5 +708,4 @@ public abstract class BaseOtaPackageServiceTest extends AbstractServiceTest { firmware.setDataSize(DATA_SIZE); return otaPackageService.saveOtaPackage(firmware); } - } diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index 0c4b673d7f..d32552f63d 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -2977,8 +2977,10 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable { ).getBody(); } - public OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo) { - return restTemplate.postForEntity(baseURL + "/api/otaPackage", otaPackageInfo, OtaPackageInfo.class).getBody(); + public OtaPackageInfo saveOtaPackageInfo(OtaPackageInfo otaPackageInfo, boolean isUrl) { + Map params = new HashMap<>(); + params.put("isUrl", Boolean.toString(isUrl)); + return restTemplate.postForEntity(baseURL + "/api/otaPackage?isUrl={isUrl}", otaPackageInfo, OtaPackageInfo.class, params).getBody(); } public OtaPackageInfo saveOtaPackageData(OtaPackageId otaPackageId, String checkSum, ChecksumAlgorithm checksumAlgorithm, MultipartFile file) throws Exception { diff --git a/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.html b/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.html index 6802768303..be3a2ead31 100644 --- a/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.html +++ b/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.html @@ -93,17 +93,17 @@
ota-update.warning-after-save-no-edit
- - Upload binary file - Use external URL + + Upload binary file + Use external URL
-
+
@@ -143,12 +143,12 @@
-
+
ota-update.direct-url + [required]="entityForm.get('isURL').value"> ota-update.direct-url-required diff --git a/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.ts b/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.ts index 0c2d82e171..9fd51302e1 100644 --- a/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.ts +++ b/ui-ngx/src/app/modules/home/pages/ota-update/ota-update.component.ts @@ -56,11 +56,11 @@ export class OtaUpdateComponent extends EntityComponent implements O ngOnInit() { super.ngOnInit(); - this.entityForm.get('resource').valueChanges.pipe( + this.entityForm.get('isURL').valueChanges.pipe( filter(() => this.isAdd), takeUntil(this.destroy$) - ).subscribe((resource) => { - if (resource === 'file') { + ).subscribe((isURL) => { + if (isURL === false) { this.entityForm.get('url').clearValidators(); this.entityForm.get('file').setValidators(Validators.required); this.entityForm.get('url').updateValueAndValidity({emitEvent: false}); @@ -97,7 +97,7 @@ export class OtaUpdateComponent extends EntityComponent implements O checksumAlgorithm: [entity && entity.checksumAlgorithm ? entity.checksumAlgorithm : ChecksumAlgorithm.SHA256], checksum: [entity ? entity.checksum : '', Validators.maxLength(1020)], url: [entity ? entity.url : ''], - resource: ['file'], + isURL: [false], additionalInfo: this.fb.group( { description: [entity && entity.additionalInfo ? entity.additionalInfo.description : ''], @@ -127,7 +127,7 @@ export class OtaUpdateComponent extends EntityComponent implements O dataSize: entity.dataSize, contentType: entity.contentType, url: entity.url, - resource: isNotEmptyStr(entity.url) ? 'url' : 'file', + isURL: isNotEmptyStr(entity.url), additionalInfo: { description: entity.additionalInfo ? entity.additionalInfo.description : '' } @@ -172,12 +172,11 @@ export class OtaUpdateComponent extends EntityComponent implements O } prepareFormValue(formValue: any): any { - if (formValue.resource === 'url') { + if (formValue.isURL) { delete formValue.file; } else { delete formValue.url; } - delete formValue.resource; delete formValue.generateChecksum; return super.prepareFormValue(formValue); } diff --git a/ui-ngx/src/app/shared/models/ota-package.models.ts b/ui-ngx/src/app/shared/models/ota-package.models.ts index f8b99f317f..a27330235a 100644 --- a/ui-ngx/src/app/shared/models/ota-package.models.ts +++ b/ui-ngx/src/app/shared/models/ota-package.models.ts @@ -99,6 +99,7 @@ export interface OtaPackageInfo extends BaseData { contentType: string; dataSize?: number; additionalInfo?: any; + isURL?: boolean; } export interface OtaPackage extends OtaPackageInfo {