Search in sources :

Example 21 with OtaPackageInfo

use of org.thingsboard.server.common.data.OtaPackageInfo in project thingsboard by thingsboard.

the class BaseOtaPackageServiceTest method testSaveFirmwareInfoWithExistingTitleAndVersion.

@Test
public void testSaveFirmwareInfoWithExistingTitleAndVersion() {
    OtaPackageInfo firmwareInfo = new OtaPackageInfo();
    firmwareInfo.setTenantId(tenantId);
    firmwareInfo.setDeviceProfileId(deviceProfileId);
    firmwareInfo.setType(FIRMWARE);
    firmwareInfo.setTitle(TITLE);
    firmwareInfo.setVersion(VERSION);
    otaPackageService.saveOtaPackageInfo(firmwareInfo, false);
    OtaPackageInfo newFirmwareInfo = new OtaPackageInfo();
    newFirmwareInfo.setTenantId(tenantId);
    newFirmwareInfo.setDeviceProfileId(deviceProfileId);
    newFirmwareInfo.setType(FIRMWARE);
    newFirmwareInfo.setTitle(TITLE);
    newFirmwareInfo.setVersion(VERSION);
    thrown.expect(DataValidationException.class);
    thrown.expectMessage("OtaPackage with such title and version already exists!");
    otaPackageService.saveOtaPackageInfo(newFirmwareInfo, false);
}
Also used : OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

Example 22 with OtaPackageInfo

use of org.thingsboard.server.common.data.OtaPackageInfo in project thingsboard by thingsboard.

the class BaseOtaPackageServiceTest method testSaveOtaPackageUrlCantBeUpdated.

@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);
}
Also used : OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

Example 23 with OtaPackageInfo

use of org.thingsboard.server.common.data.OtaPackageInfo in project thingsboard by thingsboard.

the class BaseOtaPackageServiceTest method testSaveOtaPackageCantViolateSizeOfTitle.

@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(ValidationException.class);
    thrown.expectMessage("length of title must be equal or less than 255");
    otaPackageService.saveOtaPackageInfo(firmwareInfo, true);
}
Also used : OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

Example 24 with OtaPackageInfo

use of org.thingsboard.server.common.data.OtaPackageInfo in project thingsboard by thingsboard.

the class BaseOtaPackageServiceTest method testFindTenantFirmwaresByTenantId.

@Test
public void testFindTenantFirmwaresByTenantId() {
    List<OtaPackageInfo> firmwares = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        OtaPackageInfo info = new OtaPackageInfo(createFirmware(tenantId, VERSION + i));
        info.setHasData(true);
        firmwares.add(info);
    }
    OtaPackageInfo firmwareWithUrl = new OtaPackageInfo();
    firmwareWithUrl.setTenantId(tenantId);
    firmwareWithUrl.setDeviceProfileId(deviceProfileId);
    firmwareWithUrl.setType(FIRMWARE);
    firmwareWithUrl.setTitle(TITLE);
    firmwareWithUrl.setVersion(VERSION);
    firmwareWithUrl.setUrl(URL);
    firmwareWithUrl.setDataSize(0L);
    OtaPackageInfo savedFwWithUrl = otaPackageService.saveOtaPackageInfo(firmwareWithUrl, true);
    savedFwWithUrl.setHasData(true);
    firmwares.add(savedFwWithUrl);
    List<OtaPackageInfo> loadedFirmwares = new ArrayList<>();
    PageLink pageLink = new PageLink(16);
    PageData<OtaPackageInfo> pageData;
    do {
        pageData = otaPackageService.findTenantOtaPackagesByTenantId(tenantId, pageLink);
        loadedFirmwares.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(firmwares, idComparator);
    Collections.sort(loadedFirmwares, idComparator);
    assertThat(firmwares).isEqualTo(loadedFirmwares);
    otaPackageService.deleteOtaPackagesByTenantId(tenantId);
    pageLink = new PageLink(31);
    pageData = otaPackageService.findTenantOtaPackagesByTenantId(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
}
Also used : ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

Example 25 with OtaPackageInfo

use of org.thingsboard.server.common.data.OtaPackageInfo in project thingsboard by thingsboard.

the class OtaPackageController method saveOtaPackageData.

@ApiOperation(value = "Save OTA Package data (saveOtaPackageData)", notes = "Update the OTA Package. Adds the date to the existing OTA Package Info" + TENANT_AUTHORITY_PARAGRAPH, produces = "application/json")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/otaPackage/{otaPackageId}", method = RequestMethod.POST)
@ResponseBody
public OtaPackageInfo saveOtaPackageData(@ApiParam(value = OTA_PACKAGE_ID_PARAM_DESCRIPTION) @PathVariable(OTA_PACKAGE_ID) String strOtaPackageId, @ApiParam(value = "OTA Package checksum. For example, '0xd87f7e0c'") @RequestParam(required = false) String checksum, @ApiParam(value = "OTA Package checksum algorithm.", allowableValues = OTA_PACKAGE_CHECKSUM_ALGORITHM_ALLOWABLE_VALUES) @RequestParam(CHECKSUM_ALGORITHM) String checksumAlgorithmStr, @ApiParam(value = "OTA Package data.") @RequestBody MultipartFile file) throws ThingsboardException {
    checkParameter(OTA_PACKAGE_ID, strOtaPackageId);
    checkParameter(CHECKSUM_ALGORITHM, checksumAlgorithmStr);
    try {
        OtaPackageId otaPackageId = new OtaPackageId(toUUID(strOtaPackageId));
        OtaPackageInfo info = checkOtaPackageInfoId(otaPackageId, Operation.READ);
        OtaPackage otaPackage = new OtaPackage(otaPackageId);
        otaPackage.setCreatedTime(info.getCreatedTime());
        otaPackage.setTenantId(getTenantId());
        otaPackage.setDeviceProfileId(info.getDeviceProfileId());
        otaPackage.setType(info.getType());
        otaPackage.setTitle(info.getTitle());
        otaPackage.setVersion(info.getVersion());
        otaPackage.setTag(info.getTag());
        otaPackage.setAdditionalInfo(info.getAdditionalInfo());
        ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.valueOf(checksumAlgorithmStr.toUpperCase());
        byte[] bytes = file.getBytes();
        if (StringUtils.isEmpty(checksum)) {
            checksum = otaPackageService.generateChecksum(checksumAlgorithm, ByteBuffer.wrap(bytes));
        }
        otaPackage.setChecksumAlgorithm(checksumAlgorithm);
        otaPackage.setChecksum(checksum);
        otaPackage.setFileName(file.getOriginalFilename());
        otaPackage.setContentType(file.getContentType());
        otaPackage.setData(ByteBuffer.wrap(bytes));
        otaPackage.setDataSize((long) bytes.length);
        OtaPackageInfo savedOtaPackage = otaPackageService.saveOtaPackage(otaPackage);
        logEntityAction(savedOtaPackage.getId(), savedOtaPackage, null, ActionType.UPDATED, null);
        return savedOtaPackage;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.OTA_PACKAGE), null, null, ActionType.UPDATED, e, strOtaPackageId);
        throw handleException(e);
    }
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) ChecksumAlgorithm(org.thingsboard.server.common.data.ota.ChecksumAlgorithm) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)31 Test (org.junit.Test)18 SaveOtaPackageInfoRequest (org.thingsboard.server.common.data.SaveOtaPackageInfoRequest)8 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)6 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)6 ArrayList (java.util.ArrayList)5 OtaPackage (org.thingsboard.server.common.data.OtaPackage)5 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)4 PageLink (org.thingsboard.server.common.data.page.PageLink)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 Cache (org.springframework.cache.Cache)2 OtaPackageDataCache (org.thingsboard.server.cache.ota.OtaPackageDataCache)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashMap (java.util.HashMap)1