Search in sources :

Example 6 with OtaPackageInfo

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

the class BaseOtaPackageControllerTest method testFindTenantFirmwaresByHasData.

@Test
public void testFindTenantFirmwaresByHasData() throws Exception {
    List<OtaPackageInfo> otaPackagesWithData = new ArrayList<>();
    List<OtaPackageInfo> allOtaPackages = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest();
        firmwareInfo.setDeviceProfileId(deviceProfileId);
        firmwareInfo.setType(FIRMWARE);
        firmwareInfo.setTitle(TITLE);
        firmwareInfo.setVersion(VERSION + i);
        firmwareInfo.setUsesUrl(false);
        OtaPackageInfo savedFirmwareInfo = save(firmwareInfo);
        if (i > 100) {
            MockMultipartFile testData = new MockMultipartFile("file", FILE_NAME, CONTENT_TYPE, DATA.array());
            OtaPackageInfo savedFirmware = savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, CHECKSUM_ALGORITHM);
            savedFirmwareInfo = new OtaPackageInfo(savedFirmware);
            otaPackagesWithData.add(savedFirmwareInfo);
        }
        allOtaPackages.add(savedFirmwareInfo);
    }
    List<OtaPackageInfo> loadedOtaPackagesWithData = new ArrayList<>();
    PageLink pageLink = new PageLink(24);
    PageData<OtaPackageInfo> pageData;
    do {
        pageData = doGetTypedWithPageLink("/api/otaPackages/" + deviceProfileId.toString() + "/FIRMWARE?", new TypeReference<>() {
        }, pageLink);
        loadedOtaPackagesWithData.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    List<OtaPackageInfo> allLoadedOtaPackages = new ArrayList<>();
    pageLink = new PageLink(24);
    do {
        pageData = doGetTypedWithPageLink("/api/otaPackages?", new TypeReference<>() {
        }, pageLink);
        allLoadedOtaPackages.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(otaPackagesWithData, idComparator);
    Collections.sort(allOtaPackages, idComparator);
    Collections.sort(loadedOtaPackagesWithData, idComparator);
    Collections.sort(allLoadedOtaPackages, idComparator);
    Assert.assertEquals(otaPackagesWithData, loadedOtaPackagesWithData);
    Assert.assertEquals(allOtaPackages, allLoadedOtaPackages);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SaveOtaPackageInfoRequest(org.thingsboard.server.common.data.SaveOtaPackageInfoRequest) Test(org.junit.Test)

Example 7 with OtaPackageInfo

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

the class OtaPackageInfoEntity method toData.

@Override
public OtaPackageInfo toData() {
    OtaPackageInfo otaPackageInfo = new OtaPackageInfo(new OtaPackageId(id));
    otaPackageInfo.setCreatedTime(createdTime);
    otaPackageInfo.setTenantId(TenantId.fromUUID(tenantId));
    if (deviceProfileId != null) {
        otaPackageInfo.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
    }
    otaPackageInfo.setType(type);
    otaPackageInfo.setTitle(title);
    otaPackageInfo.setVersion(version);
    otaPackageInfo.setTag(tag);
    otaPackageInfo.setUrl(url);
    otaPackageInfo.setFileName(fileName);
    otaPackageInfo.setContentType(contentType);
    otaPackageInfo.setChecksumAlgorithm(checksumAlgorithm);
    otaPackageInfo.setChecksum(checksum);
    otaPackageInfo.setDataSize(dataSize);
    otaPackageInfo.setAdditionalInfo(additionalInfo);
    otaPackageInfo.setHasData(hasData);
    return otaPackageInfo;
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo)

Example 8 with OtaPackageInfo

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

the class BaseOtaPackageService method saveOtaPackageInfo.

@Override
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();
        if (otaPackageId != null) {
            Cache cache = cacheManager.getCache(OTA_PACKAGE_CACHE);
            cache.evict(toOtaPackageInfoKey(otaPackageId));
            otaPackageDataCache.evict(otaPackageId.toString());
        }
        return otaPackageInfoDao.save(otaPackageInfo.getTenantId(), otaPackageInfo);
    } catch (Exception t) {
        ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
        if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("ota_package_tenant_title_version_unq_key")) {
            throw new DataValidationException("OtaPackage with such title and version already exists!");
        } else {
            throw t;
        }
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Cache(org.springframework.cache.Cache) OtaPackageDataCache(org.thingsboard.server.cache.ota.OtaPackageDataCache)

Example 9 with OtaPackageInfo

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

the class BaseOtaPackageServiceTest method testFindTenantFirmwaresByTenantIdAndHasData.

@Test
public void testFindTenantFirmwaresByTenantIdAndHasData() {
    List<OtaPackageInfo> firmwares = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        firmwares.add(new OtaPackageInfo(otaPackageService.saveOtaPackage(createFirmware(tenantId, VERSION + i))));
    }
    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.findTenantOtaPackagesByTenantIdAndDeviceProfileIdAndTypeAndHasData(tenantId, deviceProfileId, FIRMWARE, pageLink);
        loadedFirmwares.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    loadedFirmwares = new ArrayList<>();
    pageLink = new PageLink(16);
    do {
        pageData = otaPackageService.findTenantOtaPackagesByTenantIdAndDeviceProfileIdAndTypeAndHasData(tenantId, deviceProfileId, FIRMWARE, 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 10 with OtaPackageInfo

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

the class BaseOtaPackageServiceTest method testFindFirmwareInfoById.

@Test
public void testFindFirmwareInfoById() {
    OtaPackageInfo firmware = new OtaPackageInfo();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(deviceProfileId);
    firmware.setType(FIRMWARE);
    firmware.setTitle(TITLE);
    firmware.setVersion(VERSION);
    OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware, false);
    OtaPackageInfo foundFirmware = otaPackageService.findOtaPackageInfoById(tenantId, savedFirmware.getId());
    Assert.assertNotNull(foundFirmware);
    Assert.assertEquals(savedFirmware, foundFirmware);
    otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
}
Also used : OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

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