Search in sources :

Example 16 with OtaPackageInfo

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

the class BaseOtaPackageService method saveOtaPackage.

@Override
public OtaPackage saveOtaPackage(OtaPackage otaPackage) {
    log.trace("Executing saveOtaPackage [{}]", otaPackage);
    otaPackageValidator.validate(otaPackage, OtaPackageInfo::getTenantId);
    try {
        OtaPackageId otaPackageId = otaPackage.getId();
        if (otaPackageId != null) {
            Cache cache = cacheManager.getCache(OTA_PACKAGE_CACHE);
            cache.evict(toOtaPackageInfoKey(otaPackageId));
            otaPackageDataCache.evict(otaPackageId.toString());
        }
        return otaPackageDao.save(otaPackage.getTenantId(), otaPackage);
    } 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 17 with OtaPackageInfo

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

the class OtaPackageInfoDataValidator method validateUpdate.

@Override
protected void validateUpdate(TenantId tenantId, OtaPackageInfo otaPackage) {
    OtaPackageInfo otaPackageOld = otaPackageInfoDao.findById(tenantId, otaPackage.getUuidId());
    validateUpdate(otaPackage, otaPackageOld);
}
Also used : OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo)

Example 18 with OtaPackageInfo

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

the class BaseOtaPackageServiceTest method testSaveOtaPackageCantViolateSizeOfVersion.

@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("length of version 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 19 with OtaPackageInfo

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

the class BaseOtaPackageServiceTest method testSaveOtaPackageInfoWithBlankAndEmptyUrl.

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

Example 20 with OtaPackageInfo

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

the class BaseOtaPackageServiceTest method testSaveFirmwareWithUrl.

@Test
public void testSaveFirmwareWithUrl() {
    OtaPackageInfo firmware = new OtaPackageInfo();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(deviceProfileId);
    firmware.setType(FIRMWARE);
    firmware.setTitle(TITLE);
    firmware.setVersion(VERSION);
    firmware.setUrl(URL);
    firmware.setDataSize(0L);
    OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware, true);
    Assert.assertNotNull(savedFirmware);
    Assert.assertNotNull(savedFirmware.getId());
    Assert.assertTrue(savedFirmware.getCreatedTime() > 0);
    Assert.assertEquals(firmware.getTenantId(), savedFirmware.getTenantId());
    Assert.assertEquals(firmware.getTitle(), savedFirmware.getTitle());
    Assert.assertEquals(firmware.getFileName(), savedFirmware.getFileName());
    Assert.assertEquals(firmware.getContentType(), savedFirmware.getContentType());
    savedFirmware.setAdditionalInfo(JacksonUtil.newObjectNode());
    otaPackageService.saveOtaPackageInfo(savedFirmware, true);
    OtaPackage foundFirmware = otaPackageService.findOtaPackageById(tenantId, savedFirmware.getId());
    Assert.assertEquals(foundFirmware.getTitle(), savedFirmware.getTitle());
    otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) 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