36 changed files with 1341 additions and 20 deletions
@ -0,0 +1,147 @@ |
|||||
|
/** |
||||
|
* 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.controller; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.core.io.ByteArrayResource; |
||||
|
import org.springframework.http.HttpHeaders; |
||||
|
import org.springframework.http.ResponseEntity; |
||||
|
import org.springframework.security.access.prepost.PreAuthorize; |
||||
|
import org.springframework.web.bind.annotation.PathVariable; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
||||
|
import org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.security.permission.Operation; |
||||
|
|
||||
|
import java.nio.ByteBuffer; |
||||
|
|
||||
|
@Slf4j |
||||
|
@RestController |
||||
|
@TbCoreComponent |
||||
|
@RequestMapping("/api") |
||||
|
public class FirmwareController extends BaseController { |
||||
|
|
||||
|
public static final String FIRMWARE_ID = "firmwareId"; |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority( 'TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/firmware/{firmwareId}/download", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public ResponseEntity<org.springframework.core.io.Resource> downloadFirmware(@PathVariable(FIRMWARE_ID) String strFirmwareId) throws ThingsboardException { |
||||
|
checkParameter(FIRMWARE_ID, strFirmwareId); |
||||
|
try { |
||||
|
FirmwareId firmwareId = new FirmwareId(toUUID(strFirmwareId)); |
||||
|
Firmware firmware = checkFirmwareId(firmwareId, Operation.READ); |
||||
|
|
||||
|
ByteArrayResource resource = new ByteArrayResource(firmware.getData().array()); |
||||
|
return ResponseEntity.ok() |
||||
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + firmware.getFileName()) |
||||
|
.header("x-filename", firmware.getFileName()) |
||||
|
.contentLength(resource.contentLength()) |
||||
|
.contentType(parseMediaType(firmware.getContentType())) |
||||
|
.body(resource); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/firmware/info/{firmwareId}", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public FirmwareInfo getFirmwareInfoById(@PathVariable(FIRMWARE_ID) String strFirmwareId) throws ThingsboardException { |
||||
|
checkParameter(FIRMWARE_ID, strFirmwareId); |
||||
|
try { |
||||
|
FirmwareId firmwareId = new FirmwareId(toUUID(strFirmwareId)); |
||||
|
return checkFirmwareInfoId(firmwareId, Operation.READ); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/firmware/{firmwareId}", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public Firmware getFirmwareById(@PathVariable(FIRMWARE_ID) String strFirmwareId) throws ThingsboardException { |
||||
|
checkParameter(FIRMWARE_ID, strFirmwareId); |
||||
|
try { |
||||
|
FirmwareId firmwareId = new FirmwareId(toUUID(strFirmwareId)); |
||||
|
return checkFirmwareId(firmwareId, Operation.READ); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/firmware", method = RequestMethod.POST) |
||||
|
@ResponseBody |
||||
|
public Firmware saveFirmware(@RequestParam("title") String title, |
||||
|
@RequestBody MultipartFile firmwareFile) throws ThingsboardException { |
||||
|
checkParameter("title", title); |
||||
|
try { |
||||
|
checkNotNull(firmwareFile); |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(getTenantId()); |
||||
|
firmware.setTitle(title); |
||||
|
firmware.setFileName(firmwareFile.getOriginalFilename()); |
||||
|
firmware.setContentType(firmwareFile.getContentType()); |
||||
|
firmware.setData(ByteBuffer.wrap(firmwareFile.getBytes())); |
||||
|
return firmwareService.saveFirmware(firmware); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/firmware", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public PageData<FirmwareInfo> getFirmwares(@RequestParam int pageSize, |
||||
|
@RequestParam int page, |
||||
|
@RequestParam(required = false) String textSearch, |
||||
|
@RequestParam(required = false) String sortProperty, |
||||
|
@RequestParam(required = false) String sortOrder) throws ThingsboardException { |
||||
|
try { |
||||
|
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); |
||||
|
return checkNotNull(firmwareService.findTenantFirmwaresByTenantId(getTenantId(), pageLink)); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/firmware/{firmwareId}", method = RequestMethod.DELETE) |
||||
|
@ResponseBody |
||||
|
public void deleteResource(@PathVariable("firmwareId") String strFirmwareId) throws ThingsboardException { |
||||
|
checkParameter(FIRMWARE_ID, strFirmwareId); |
||||
|
try { |
||||
|
FirmwareId firmwareId = new FirmwareId(toUUID(strFirmwareId)); |
||||
|
checkFirmwareInfoId(firmwareId, Operation.DELETE); |
||||
|
firmwareService.deleteFirmware(getTenantId(), firmwareId); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
/** |
||||
|
* 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.dao.firmware; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
|
||||
|
|
||||
|
public interface FirmwareService { |
||||
|
|
||||
|
Firmware saveFirmware(Firmware firmware); |
||||
|
|
||||
|
Firmware findFirmwareById(TenantId tenantId, FirmwareId firmwareId); |
||||
|
|
||||
|
FirmwareInfo findFirmwareInfoById(TenantId tenantId, FirmwareId firmwareId); |
||||
|
|
||||
|
PageData<FirmwareInfo> findTenantFirmwaresByTenantId(TenantId tenantId, PageLink pageLink); |
||||
|
|
||||
|
void deleteFirmware(TenantId tenantId, FirmwareId firmwareId); |
||||
|
|
||||
|
void deleteFirmwaresByTenantId(TenantId tenantId); |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* 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 org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
|
||||
|
import java.nio.ByteBuffer; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class Firmware extends FirmwareInfo { |
||||
|
|
||||
|
private static final long serialVersionUID = 3091601761339422546L; |
||||
|
|
||||
|
private transient ByteBuffer data; |
||||
|
|
||||
|
public Firmware() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public Firmware(FirmwareId id) { |
||||
|
super(id); |
||||
|
} |
||||
|
|
||||
|
public Firmware(Firmware firmware) { |
||||
|
super(firmware); |
||||
|
this.data = firmware.getData(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
/** |
||||
|
* 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.extern.slf4j.Slf4j; |
||||
|
import org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class FirmwareInfo extends SearchTextBased<FirmwareId> implements HasTenantId { |
||||
|
|
||||
|
private static final long serialVersionUID = 3168391583570815419L; |
||||
|
|
||||
|
private TenantId tenantId; |
||||
|
private String title; |
||||
|
private String fileName; |
||||
|
private String contentType; |
||||
|
|
||||
|
public FirmwareInfo() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public FirmwareInfo(FirmwareId id) { |
||||
|
super(id); |
||||
|
} |
||||
|
|
||||
|
public FirmwareInfo(FirmwareInfo firmwareInfo) { |
||||
|
super(firmwareInfo); |
||||
|
this.tenantId = firmwareInfo.getTenantId(); |
||||
|
this.title = firmwareInfo.getTitle(); |
||||
|
this.fileName = firmwareInfo.getFileName(); |
||||
|
this.contentType = firmwareInfo.getContentType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getSearchText() { |
||||
|
return title; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* 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.id; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public class FirmwareId extends UUIDBased implements EntityId { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
@JsonCreator |
||||
|
public FirmwareId(@JsonProperty("id") UUID id) { |
||||
|
super(id); |
||||
|
} |
||||
|
|
||||
|
public static FirmwareId fromString(String firmwareId) { |
||||
|
return new FirmwareId(UUID.fromString(firmwareId)); |
||||
|
} |
||||
|
|
||||
|
@JsonIgnore |
||||
|
@Override |
||||
|
public EntityType getEntityType() { |
||||
|
return EntityType.FIRMWARE; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,162 @@ |
|||||
|
/** |
||||
|
* 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.dao.firmware; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.hibernate.exception.ConstraintViolationException; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.Tenant; |
||||
|
import org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
import org.thingsboard.server.dao.exception.DataValidationException; |
||||
|
import org.thingsboard.server.dao.service.DataValidator; |
||||
|
import org.thingsboard.server.dao.service.PaginatedRemover; |
||||
|
import org.thingsboard.server.dao.tenant.TenantDao; |
||||
|
|
||||
|
import java.nio.ByteBuffer; |
||||
|
import java.util.Optional; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.service.Validator.validateId; |
||||
|
import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class BaseFirmwareService implements FirmwareService { |
||||
|
public static final String INCORRECT_FIRMWARE_ID = "Incorrect firmwareId "; |
||||
|
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
||||
|
|
||||
|
private final TenantDao tenantDao; |
||||
|
private final FirmwareDao firmwareDao; |
||||
|
private final FirmwareInfoDao firmwareInfoDao; |
||||
|
|
||||
|
public BaseFirmwareService(TenantDao tenantDao, FirmwareDao firmwareDao, FirmwareInfoDao firmwareInfoDao) { |
||||
|
this.tenantDao = tenantDao; |
||||
|
this.firmwareDao = firmwareDao; |
||||
|
this.firmwareInfoDao = firmwareInfoDao; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Firmware saveFirmware(Firmware firmware) { |
||||
|
log.trace("Executing saveFirmware [{}]", firmware); |
||||
|
firmwareValidator.validate(firmware, Firmware::getTenantId); |
||||
|
return firmwareDao.save(firmware.getTenantId(), firmware); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Firmware findFirmwareById(TenantId tenantId, FirmwareId firmwareId) { |
||||
|
log.trace("Executing findFirmwareById [{}]", firmwareId); |
||||
|
validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId); |
||||
|
return firmwareDao.findById(tenantId, firmwareId.getId()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public FirmwareInfo findFirmwareInfoById(TenantId tenantId, FirmwareId firmwareId) { |
||||
|
log.trace("Executing findFirmwareInfoById [{}]", firmwareId); |
||||
|
validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId); |
||||
|
return firmwareInfoDao.findById(tenantId, firmwareId.getId()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public PageData<FirmwareInfo> findTenantFirmwaresByTenantId(TenantId tenantId, PageLink pageLink) { |
||||
|
log.trace("Executing findTenantFirmwaresByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); |
||||
|
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
||||
|
validatePageLink(pageLink); |
||||
|
return firmwareInfoDao.findFirmwareInfoByTenantId(tenantId, pageLink); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteFirmware(TenantId tenantId, FirmwareId firmwareId) { |
||||
|
log.trace("Executing deleteFirmware [{}]", firmwareId); |
||||
|
validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId); |
||||
|
try { |
||||
|
firmwareDao.removeById(tenantId, firmwareId.getId()); |
||||
|
} catch (Exception t) { |
||||
|
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
||||
|
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_firmware_device")) { |
||||
|
throw new DataValidationException("The firmware referenced by the devices cannot be deleted!"); |
||||
|
} else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_firmware_device_profile")) { |
||||
|
throw new DataValidationException("The firmware referenced by the device profile cannot be deleted!"); |
||||
|
} else { |
||||
|
throw t; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteFirmwaresByTenantId(TenantId tenantId) { |
||||
|
log.trace("Executing deleteFirmwaresByTenantId, tenantId [{}]", tenantId); |
||||
|
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
||||
|
tenantFirmwareRemover.removeEntities(tenantId, tenantId); |
||||
|
} |
||||
|
|
||||
|
private DataValidator<Firmware> firmwareValidator = new DataValidator<>() { |
||||
|
|
||||
|
@Override |
||||
|
protected void validateDataImpl(TenantId tenantId, Firmware firmware) { |
||||
|
if (firmware.getTenantId() == null) { |
||||
|
throw new DataValidationException("Firmware should be assigned to tenant!"); |
||||
|
} else { |
||||
|
Tenant tenant = tenantDao.findById(firmware.getTenantId(), firmware.getTenantId().getId()); |
||||
|
if (tenant == null) { |
||||
|
throw new DataValidationException("Firmware is referencing to non-existent tenant!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isEmpty(firmware.getTitle())) { |
||||
|
throw new DataValidationException("Firmware title should be specified!"); |
||||
|
} |
||||
|
if (StringUtils.isEmpty(firmware.getFileName())) { |
||||
|
throw new DataValidationException("Firmware file name should be specified!"); |
||||
|
} |
||||
|
if (StringUtils.isEmpty(firmware.getContentType())) { |
||||
|
throw new DataValidationException("Firmware content type should be specified!"); |
||||
|
} |
||||
|
ByteBuffer data = firmware.getData(); |
||||
|
if (data == null || !data.hasArray() || data.array().length == 0) { |
||||
|
throw new DataValidationException("Firmware data should be specified!"); |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
private PaginatedRemover<TenantId, FirmwareInfo> tenantFirmwareRemover = |
||||
|
new PaginatedRemover<>() { |
||||
|
|
||||
|
@Override |
||||
|
protected PageData<FirmwareInfo> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) { |
||||
|
return firmwareInfoDao.findFirmwareInfoByTenantId(id, pageLink); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void removeEntity(TenantId tenantId, FirmwareInfo entity) { |
||||
|
deleteFirmware(tenantId, entity.getId()); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) { |
||||
|
if (t instanceof ConstraintViolationException) { |
||||
|
return Optional.of((ConstraintViolationException) t); |
||||
|
} else if (t.getCause() instanceof ConstraintViolationException) { |
||||
|
return Optional.of((ConstraintViolationException) (t.getCause())); |
||||
|
} else { |
||||
|
return Optional.empty(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
/** |
||||
|
* 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.dao.firmware; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.dao.Dao; |
||||
|
|
||||
|
public interface FirmwareDao extends Dao<Firmware> { |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
/** |
||||
|
* 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.dao.firmware; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
import org.thingsboard.server.dao.Dao; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public interface FirmwareInfoDao extends Dao<FirmwareInfo> { |
||||
|
|
||||
|
PageData<FirmwareInfo> findFirmwareInfoByTenantId(TenantId tenantId, PageLink pageLink); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,99 @@ |
|||||
|
/** |
||||
|
* 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.dao.model.sql; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.dao.model.BaseSqlEntity; |
||||
|
import org.thingsboard.server.dao.model.SearchTextEntity; |
||||
|
|
||||
|
import javax.persistence.Column; |
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.Table; |
||||
|
import java.nio.ByteBuffer; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CONTENT_TYPE_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_DATA_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_FILE_NAME_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TABLE_NAME; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TENANT_ID_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TITLE_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@Entity |
||||
|
@Table(name = FIRMWARE_TABLE_NAME) |
||||
|
public class FirmwareEntity extends BaseSqlEntity<Firmware> implements SearchTextEntity<Firmware> { |
||||
|
|
||||
|
@Column(name = FIRMWARE_TENANT_ID_COLUMN) |
||||
|
private UUID tenantId; |
||||
|
|
||||
|
@Column(name = FIRMWARE_TITLE_COLUMN) |
||||
|
private String title; |
||||
|
|
||||
|
@Column(name = FIRMWARE_FILE_NAME_COLUMN) |
||||
|
private String fileName; |
||||
|
|
||||
|
@Column(name = FIRMWARE_CONTENT_TYPE_COLUMN) |
||||
|
private String contentType; |
||||
|
|
||||
|
@Column(name = FIRMWARE_DATA_COLUMN, columnDefinition = "BINARY") |
||||
|
private byte[] data; |
||||
|
|
||||
|
@Column(name = SEARCH_TEXT_PROPERTY) |
||||
|
private String searchText; |
||||
|
|
||||
|
public FirmwareEntity() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public FirmwareEntity(Firmware firmware) { |
||||
|
this.createdTime = firmware.getCreatedTime(); |
||||
|
this.setUuid(firmware.getUuidId()); |
||||
|
this.tenantId = firmware.getTenantId().getId(); |
||||
|
this.title = firmware.getTitle(); |
||||
|
this.fileName = firmware.getFileName(); |
||||
|
this.contentType = firmware.getContentType(); |
||||
|
this.data = firmware.getData().array(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getSearchTextSource() { |
||||
|
return title; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void setSearchText(String searchText) { |
||||
|
this.searchText = searchText; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Firmware toData() { |
||||
|
Firmware firmware = new Firmware(new FirmwareId(id)); |
||||
|
firmware.setCreatedTime(createdTime); |
||||
|
firmware.setTenantId(new TenantId(tenantId)); |
||||
|
firmware.setTitle(title); |
||||
|
firmware.setFileName(fileName); |
||||
|
firmware.setContentType(contentType); |
||||
|
firmware.setData(ByteBuffer.wrap(data)); |
||||
|
return firmware; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,92 @@ |
|||||
|
/** |
||||
|
* 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.dao.model.sql; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.id.FirmwareId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.dao.model.BaseSqlEntity; |
||||
|
import org.thingsboard.server.dao.model.SearchTextEntity; |
||||
|
|
||||
|
import javax.persistence.Column; |
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.Table; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CONTENT_TYPE_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_FILE_NAME_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TABLE_NAME; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TENANT_ID_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TITLE_COLUMN; |
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.SEARCH_TEXT_PROPERTY; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@Entity |
||||
|
@Table(name = FIRMWARE_TABLE_NAME) |
||||
|
public class FirmwareInfoEntity extends BaseSqlEntity<FirmwareInfo> implements SearchTextEntity<FirmwareInfo> { |
||||
|
|
||||
|
@Column(name = FIRMWARE_TENANT_ID_COLUMN) |
||||
|
private UUID tenantId; |
||||
|
|
||||
|
@Column(name = FIRMWARE_TITLE_COLUMN) |
||||
|
private String title; |
||||
|
|
||||
|
@Column(name = FIRMWARE_FILE_NAME_COLUMN) |
||||
|
private String fileName; |
||||
|
|
||||
|
@Column(name = FIRMWARE_CONTENT_TYPE_COLUMN) |
||||
|
private String contentType; |
||||
|
|
||||
|
@Column(name = SEARCH_TEXT_PROPERTY) |
||||
|
private String searchText; |
||||
|
|
||||
|
public FirmwareInfoEntity() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public FirmwareInfoEntity(FirmwareInfo firmware) { |
||||
|
this.createdTime = firmware.getCreatedTime(); |
||||
|
this.setUuid(firmware.getUuidId()); |
||||
|
this.tenantId = firmware.getTenantId().getId(); |
||||
|
this.title = firmware.getTitle(); |
||||
|
this.fileName = firmware.getFileName(); |
||||
|
this.contentType = firmware.getContentType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getSearchTextSource() { |
||||
|
return title; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void setSearchText(String searchText) { |
||||
|
this.searchText = searchText; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public FirmwareInfo toData() { |
||||
|
FirmwareInfo firmware = new FirmwareInfo(new FirmwareId(id)); |
||||
|
firmware.setCreatedTime(createdTime); |
||||
|
firmware.setTenantId(new TenantId(tenantId)); |
||||
|
firmware.setTitle(title); |
||||
|
firmware.setFileName(fileName); |
||||
|
firmware.setContentType(contentType); |
||||
|
return firmware; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
/** |
||||
|
* 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.dao.sql.firmware; |
||||
|
|
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.springframework.data.repository.query.Param; |
||||
|
import org.thingsboard.server.dao.model.sql.FirmwareInfoEntity; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public interface FirmwareInfoRepository extends CrudRepository<FirmwareInfoEntity, UUID> { |
||||
|
@Query("SELECT fi FROM FirmwareInfoEntity fi WHERE " + |
||||
|
"fi.tenantId = :tenantId " + |
||||
|
"AND LOWER(fi.searchText) LIKE LOWER(CONCAT(:searchText, '%'))") |
||||
|
Page<FirmwareInfoEntity> findAllByTenantId(@Param("tenantId") UUID tenantId, |
||||
|
@Param("searchText") String searchText, |
||||
|
Pageable pageable); |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
/** |
||||
|
* 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.dao.sql.firmware; |
||||
|
|
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.thingsboard.server.dao.model.sql.FirmwareEntity; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public interface FirmwareRepository extends CrudRepository<FirmwareEntity, UUID> { |
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
/** |
||||
|
* 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.dao.sql.firmware; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.dao.firmware.FirmwareDao; |
||||
|
import org.thingsboard.server.dao.model.sql.FirmwareEntity; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Component |
||||
|
public class JpaFirmwareDao extends JpaAbstractSearchTextDao<FirmwareEntity, Firmware> implements FirmwareDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private FirmwareRepository firmwareRepository; |
||||
|
|
||||
|
@Override |
||||
|
protected Class<FirmwareEntity> getEntityClass() { |
||||
|
return FirmwareEntity.class; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected CrudRepository<FirmwareEntity, UUID> getCrudRepository() { |
||||
|
return firmwareRepository; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
/** |
||||
|
* 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.dao.sql.firmware; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
import org.thingsboard.server.dao.DaoUtil; |
||||
|
import org.thingsboard.server.dao.firmware.FirmwareInfoDao; |
||||
|
import org.thingsboard.server.dao.model.sql.FirmwareInfoEntity; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; |
||||
|
|
||||
|
import java.util.Objects; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Component |
||||
|
public class JpaFirmwareInfoDao extends JpaAbstractSearchTextDao<FirmwareInfoEntity, FirmwareInfo> implements FirmwareInfoDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private FirmwareInfoRepository firmwareInfoRepository; |
||||
|
|
||||
|
@Override |
||||
|
protected Class<FirmwareInfoEntity> getEntityClass() { |
||||
|
return FirmwareInfoEntity.class; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected CrudRepository<FirmwareInfoEntity, UUID> getCrudRepository() { |
||||
|
return firmwareInfoRepository; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public PageData<FirmwareInfo> findFirmwareInfoByTenantId(TenantId tenantId, PageLink pageLink) { |
||||
|
return DaoUtil.toPageData(firmwareInfoRepository |
||||
|
.findAllByTenantId( |
||||
|
tenantId.getId(), |
||||
|
Objects.toString(pageLink.getTextSearch(), ""), |
||||
|
DaoUtil.toPageable(pageLink))); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,271 @@ |
|||||
|
/** |
||||
|
* 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.dao.service; |
||||
|
|
||||
|
import com.datastax.oss.driver.api.core.uuid.Uuids; |
||||
|
import org.junit.After; |
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Before; |
||||
|
import org.junit.Test; |
||||
|
import org.thingsboard.server.common.data.Device; |
||||
|
import org.thingsboard.server.common.data.DeviceProfile; |
||||
|
import org.thingsboard.server.common.data.Firmware; |
||||
|
import org.thingsboard.server.common.data.FirmwareInfo; |
||||
|
import org.thingsboard.server.common.data.Tenant; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.page.PageLink; |
||||
|
import org.thingsboard.server.dao.exception.DataValidationException; |
||||
|
|
||||
|
import java.nio.ByteBuffer; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public abstract class BaseFirmwareServiceTest extends AbstractServiceTest { |
||||
|
|
||||
|
public static final String TITLE = "My firmware"; |
||||
|
private static final String FILE_NAME = "filename.txt"; |
||||
|
private static final String CONTENT_TYPE = "text/plain"; |
||||
|
private static final ByteBuffer DATA = ByteBuffer.wrap(new byte[]{0}); |
||||
|
|
||||
|
private IdComparator<FirmwareInfo> idComparator = new IdComparator<>(); |
||||
|
|
||||
|
private TenantId tenantId; |
||||
|
|
||||
|
@Before |
||||
|
public void before() { |
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setTitle("My tenant"); |
||||
|
Tenant savedTenant = tenantService.saveTenant(tenant); |
||||
|
Assert.assertNotNull(savedTenant); |
||||
|
tenantId = savedTenant.getId(); |
||||
|
} |
||||
|
|
||||
|
@After |
||||
|
public void after() { |
||||
|
tenantService.deleteTenant(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testSaveFirmware() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
Firmware savedFirmware = firmwareService.saveFirmware(firmware); |
||||
|
|
||||
|
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()); |
||||
|
Assert.assertEquals(firmware.getData(), savedFirmware.getData()); |
||||
|
|
||||
|
savedFirmware.setTitle("My new firmware"); |
||||
|
firmwareService.saveFirmware(savedFirmware); |
||||
|
|
||||
|
Firmware foundFirmware = firmwareService.findFirmwareById(tenantId, savedFirmware.getId()); |
||||
|
Assert.assertEquals(foundFirmware.getTitle(), savedFirmware.getTitle()); |
||||
|
|
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveFirmwareWithEmptyTenant() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
firmwareService.saveFirmware(firmware); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveFirmwareWithEmptyTitle() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
firmwareService.saveFirmware(firmware); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveFirmwareWithEmptyFileName() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
firmwareService.saveFirmware(firmware); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveFirmwareWithEmptyContentType() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setData(DATA); |
||||
|
firmwareService.saveFirmware(firmware); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveFirmwareWithEmptyData() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmwareService.saveFirmware(firmware); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveFirmwareWithInvalidTenant() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(new TenantId(Uuids.timeBased())); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
firmwareService.saveFirmware(firmware); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testDeleteFirmwareWithReferenceByDevice() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
Firmware savedFirmware = firmwareService.saveFirmware(firmware); |
||||
|
|
||||
|
Device device = new Device(); |
||||
|
device.setTenantId(tenantId); |
||||
|
device.setName("My device"); |
||||
|
device.setType("default"); |
||||
|
device.setFirmwareId(savedFirmware.getId()); |
||||
|
Device savedDevice = deviceService.saveDevice(device); |
||||
|
|
||||
|
try { |
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
} finally { |
||||
|
deviceService.deleteDevice(tenantId, savedDevice.getId()); |
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testDeleteFirmwareWithReferenceByDeviceProfile() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
Firmware savedFirmware = firmwareService.saveFirmware(firmware); |
||||
|
|
||||
|
DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile"); |
||||
|
deviceProfile.setFirmwareId(savedFirmware.getId()); |
||||
|
DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile); |
||||
|
|
||||
|
try { |
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
} finally { |
||||
|
deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId()); |
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindFirmwareById() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
Firmware savedFirmware = firmwareService.saveFirmware(firmware); |
||||
|
|
||||
|
Firmware foundFirmware = firmwareService.findFirmwareById(tenantId, savedFirmware.getId()); |
||||
|
Assert.assertNotNull(foundFirmware); |
||||
|
Assert.assertEquals(savedFirmware, foundFirmware); |
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDeleteFirmware() { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
Firmware savedFirmware = firmwareService.saveFirmware(firmware); |
||||
|
|
||||
|
Firmware foundFirmware = firmwareService.findFirmwareById(tenantId, savedFirmware.getId()); |
||||
|
Assert.assertNotNull(foundFirmware); |
||||
|
firmwareService.deleteFirmware(tenantId, savedFirmware.getId()); |
||||
|
foundFirmware = firmwareService.findFirmwareById(tenantId, savedFirmware.getId()); |
||||
|
Assert.assertNull(foundFirmware); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindTenantFirmwaresByTenantId() { |
||||
|
List<FirmwareInfo> firmwares = new ArrayList<>(); |
||||
|
for (int i = 0; i < 165; i++) { |
||||
|
Firmware firmware = new Firmware(); |
||||
|
firmware.setTenantId(tenantId); |
||||
|
firmware.setTitle(TITLE); |
||||
|
firmware.setFileName(FILE_NAME); |
||||
|
firmware.setContentType(CONTENT_TYPE); |
||||
|
firmware.setData(DATA); |
||||
|
firmwares.add(new FirmwareInfo(firmwareService.saveFirmware(firmware))); |
||||
|
} |
||||
|
|
||||
|
List<FirmwareInfo> loadedFirmwares = new ArrayList<>(); |
||||
|
PageLink pageLink = new PageLink(16); |
||||
|
PageData<FirmwareInfo> pageData; |
||||
|
do { |
||||
|
pageData = firmwareService.findTenantFirmwaresByTenantId(tenantId, pageLink); |
||||
|
loadedFirmwares.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageLink.nextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(firmwares, idComparator); |
||||
|
Collections.sort(loadedFirmwares, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(firmwares, loadedFirmwares); |
||||
|
|
||||
|
firmwareService.deleteFirmwaresByTenantId(tenantId); |
||||
|
|
||||
|
pageLink = new PageLink(31); |
||||
|
pageData = firmwareService.findTenantFirmwaresByTenantId(tenantId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertTrue(pageData.getData().isEmpty()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
/** |
||||
|
* 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.dao.service.sql; |
||||
|
|
||||
|
import org.thingsboard.server.dao.service.BaseFirmwareServiceTest; |
||||
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
||||
|
|
||||
|
@DaoSqlTest |
||||
|
public class FirmwareServiceSqlTest extends BaseFirmwareServiceTest { |
||||
|
} |
||||
Loading…
Reference in new issue