Commit f264aa43302dd70f9dfd39cd8475939889a117de

Authored by YevhenBondarenko
Committed by Andrew Shvayka
1 parent 29f404ac

fixed firmware tests and validation

Showing 25 changed files with 456 additions and 89 deletions
... ... @@ -35,6 +35,8 @@ import org.thingsboard.server.common.data.Firmware;
35 35 import org.thingsboard.server.common.data.FirmwareInfo;
36 36 import org.thingsboard.server.common.data.audit.ActionType;
37 37 import org.thingsboard.server.common.data.exception.ThingsboardException;
  38 +import org.thingsboard.server.common.data.firmware.FirmwareType;
  39 +import org.thingsboard.server.common.data.id.DeviceProfileId;
38 40 import org.thingsboard.server.common.data.id.FirmwareId;
39 41 import org.thingsboard.server.common.data.page.PageData;
40 42 import org.thingsboard.server.common.data.page.PageLink;
... ... @@ -133,6 +135,8 @@ public class FirmwareController extends BaseController {
133 135 Firmware firmware = new Firmware(firmwareId);
134 136 firmware.setCreatedTime(info.getCreatedTime());
135 137 firmware.setTenantId(getTenantId());
  138 + firmware.setDeviceProfileId(info.getDeviceProfileId());
  139 + firmware.setType(info.getType());
136 140 firmware.setTitle(info.getTitle());
137 141 firmware.setVersion(info.getVersion());
138 142 firmware.setAdditionalInfo(info.getAdditionalInfo());
... ... @@ -175,17 +179,22 @@ public class FirmwareController extends BaseController {
175 179 }
176 180
177 181 @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
178   - @RequestMapping(value = "/firmwares/{hasData}", method = RequestMethod.GET)
  182 + @RequestMapping(value = "/firmwares/{deviceProfileId}/{type}/{hasData}", method = RequestMethod.GET)
179 183 @ResponseBody
180   - public PageData<FirmwareInfo> getFirmwares(@PathVariable("hasData") boolean hasData,
  184 + public PageData<FirmwareInfo> getFirmwares(@PathVariable("deviceProfileId") String strDeviceProfileId,
  185 + @PathVariable("type") String strType,
  186 + @PathVariable("hasData") boolean hasData,
181 187 @RequestParam int pageSize,
182 188 @RequestParam int page,
183 189 @RequestParam(required = false) String textSearch,
184 190 @RequestParam(required = false) String sortProperty,
185 191 @RequestParam(required = false) String sortOrder) throws ThingsboardException {
  192 + checkParameter("deviceProfileId", strDeviceProfileId);
  193 + checkParameter("type", strType);
186 194 try {
187 195 PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
188   - return checkNotNull(firmwareService.findTenantFirmwaresByTenantIdAndHasData(getTenantId(), hasData, pageLink));
  196 + return checkNotNull(firmwareService.findTenantFirmwaresByTenantIdAndDeviceProfileIdAndTypeAndHasData(getTenantId(),
  197 + new DeviceProfileId(toUUID(strDeviceProfileId)), FirmwareType.valueOf(strType), hasData, pageLink));
189 198 } catch (Exception e) {
190 199 throw handleException(e);
191 200 }
... ...
... ... @@ -16,7 +16,6 @@
16 16 package org.thingsboard.server.service.firmware;
17 17
18 18 import com.google.common.util.concurrent.FutureCallback;
19   -import lombok.RequiredArgsConstructor;
20 19 import lombok.extern.slf4j.Slf4j;
21 20 import org.springframework.stereotype.Service;
22 21 import org.thingsboard.rule.engine.api.RuleEngineTelemetryService;
... ... @@ -45,6 +44,7 @@ import org.thingsboard.server.dao.firmware.FirmwareService;
45 44 import org.thingsboard.server.gen.transport.TransportProtos.ToFirmwareStateServiceMsg;
46 45 import org.thingsboard.server.queue.TbQueueProducer;
47 46 import org.thingsboard.server.queue.common.TbProtoQueueMsg;
  47 +import org.thingsboard.server.queue.provider.TbCoreQueueFactory;
48 48 import org.thingsboard.server.queue.util.TbCoreComponent;
49 49 import org.thingsboard.server.service.queue.TbClusterService;
50 50
... ... @@ -71,7 +71,6 @@ import static org.thingsboard.server.common.data.firmware.FirmwareKeyUtil.getTel
71 71 @Slf4j
72 72 @Service
73 73 @TbCoreComponent
74   -@RequiredArgsConstructor
75 74 public class DefaultFirmwareStateService implements FirmwareStateService {
76 75
77 76 private final TbClusterService tbClusterService;
... ... @@ -81,6 +80,19 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
81 80 private final RuleEngineTelemetryService telemetryService;
82 81 private final TbQueueProducer<TbProtoQueueMsg<ToFirmwareStateServiceMsg>> fwStateMsgProducer;
83 82
  83 + public DefaultFirmwareStateService(TbClusterService tbClusterService, FirmwareService firmwareService,
  84 + DeviceService deviceService,
  85 + DeviceProfileService deviceProfileService,
  86 + RuleEngineTelemetryService telemetryService,
  87 + TbCoreQueueFactory coreQueueFactory) {
  88 + this.tbClusterService = tbClusterService;
  89 + this.firmwareService = firmwareService;
  90 + this.deviceService = deviceService;
  91 + this.deviceProfileService = deviceProfileService;
  92 + this.telemetryService = telemetryService;
  93 + this.fwStateMsgProducer = coreQueueFactory.createToFirmwareStateServiceMsgProducer();
  94 + }
  95 +
84 96 @Override
85 97 public void update(Device device, Device oldDevice) {
86 98 FirmwareId newFirmwareId = device.getFirmwareId();
... ...
... ... @@ -480,6 +480,7 @@ public class DefaultTransportApiService implements TransportApiService {
480 480 builder.setResponseStatus(TransportProtos.ResponseStatus.SUCCESS);
481 481 builder.setFirmwareIdMSB(firmwareId.getId().getMostSignificantBits());
482 482 builder.setFirmwareIdLSB(firmwareId.getId().getLeastSignificantBits());
  483 + builder.setType(firmwareInfo.getType().name());
483 484 builder.setTitle(firmwareInfo.getTitle());
484 485 builder.setVersion(firmwareInfo.getVersion());
485 486 builder.setFileName(firmwareInfo.getFileName());
... ...
... ... @@ -24,10 +24,13 @@ import org.springframework.mock.web.MockMultipartFile;
24 24 import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
25 25 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
26 26 import org.thingsboard.common.util.JacksonUtil;
  27 +import org.thingsboard.server.common.data.DeviceProfile;
27 28 import org.thingsboard.server.common.data.Firmware;
28 29 import org.thingsboard.server.common.data.FirmwareInfo;
29 30 import org.thingsboard.server.common.data.Tenant;
30 31 import org.thingsboard.server.common.data.User;
  32 +import org.thingsboard.server.common.data.firmware.FirmwareType;
  33 +import org.thingsboard.server.common.data.id.DeviceProfileId;
31 34 import org.thingsboard.server.common.data.page.PageData;
32 35 import org.thingsboard.server.common.data.page.PageLink;
33 36 import org.thingsboard.server.common.data.security.Authority;
... ... @@ -38,6 +41,7 @@ import java.util.Collections;
38 41 import java.util.List;
39 42
40 43 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  44 +import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
41 45
42 46 public abstract class BaseFirmwareControllerTest extends AbstractControllerTest {
43 47
... ... @@ -53,6 +57,7 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
53 57
54 58 private Tenant savedTenant;
55 59 private User tenantAdmin;
  60 + private DeviceProfileId deviceProfileId;
56 61
57 62 @Before
58 63 public void beforeTest() throws Exception {
... ... @@ -71,6 +76,11 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
71 76 tenantAdmin.setLastName("Downs");
72 77
73 78 tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
  79 +
  80 + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", null);
  81 + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class);
  82 + Assert.assertNotNull(savedDeviceProfile);
  83 + deviceProfileId = savedDeviceProfile.getId();
74 84 }
75 85
76 86 @After
... ... @@ -84,6 +94,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
84 94 @Test
85 95 public void testSaveFirmware() throws Exception {
86 96 FirmwareInfo firmwareInfo = new FirmwareInfo();
  97 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  98 + firmwareInfo.setType(FIRMWARE);
87 99 firmwareInfo.setTitle(TITLE);
88 100 firmwareInfo.setVersion(VERSION);
89 101
... ... @@ -107,6 +119,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
107 119 @Test
108 120 public void testSaveFirmwareData() throws Exception {
109 121 FirmwareInfo firmwareInfo = new FirmwareInfo();
  122 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  123 + firmwareInfo.setType(FIRMWARE);
110 124 firmwareInfo.setTitle(TITLE);
111 125 firmwareInfo.setVersion(VERSION);
112 126
... ... @@ -137,6 +151,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
137 151 @Test
138 152 public void testUpdateFirmwareFromDifferentTenant() throws Exception {
139 153 FirmwareInfo firmwareInfo = new FirmwareInfo();
  154 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  155 + firmwareInfo.setType(FIRMWARE);
140 156 firmwareInfo.setTitle(TITLE);
141 157 firmwareInfo.setVersion(VERSION);
142 158
... ... @@ -150,6 +166,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
150 166 @Test
151 167 public void testFindFirmwareInfoById() throws Exception {
152 168 FirmwareInfo firmwareInfo = new FirmwareInfo();
  169 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  170 + firmwareInfo.setType(FIRMWARE);
153 171 firmwareInfo.setTitle(TITLE);
154 172 firmwareInfo.setVersion(VERSION);
155 173
... ... @@ -163,6 +181,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
163 181 @Test
164 182 public void testFindFirmwareById() throws Exception {
165 183 FirmwareInfo firmwareInfo = new FirmwareInfo();
  184 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  185 + firmwareInfo.setType(FIRMWARE);
166 186 firmwareInfo.setTitle(TITLE);
167 187 firmwareInfo.setVersion(VERSION);
168 188
... ... @@ -180,6 +200,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
180 200 @Test
181 201 public void testDeleteFirmware() throws Exception {
182 202 FirmwareInfo firmwareInfo = new FirmwareInfo();
  203 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  204 + firmwareInfo.setType(FIRMWARE);
183 205 firmwareInfo.setTitle(TITLE);
184 206 firmwareInfo.setVersion(VERSION);
185 207
... ... @@ -197,6 +219,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
197 219 List<FirmwareInfo> firmwares = new ArrayList<>();
198 220 for (int i = 0; i < 165; i++) {
199 221 FirmwareInfo firmwareInfo = new FirmwareInfo();
  222 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  223 + firmwareInfo.setType(FIRMWARE);
200 224 firmwareInfo.setTitle(TITLE);
201 225 firmwareInfo.setVersion(VERSION + i);
202 226
... ... @@ -238,6 +262,8 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
238 262
239 263 for (int i = 0; i < 165; i++) {
240 264 FirmwareInfo firmwareInfo = new FirmwareInfo();
  265 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  266 + firmwareInfo.setType(FIRMWARE);
241 267 firmwareInfo.setTitle(TITLE);
242 268 firmwareInfo.setVersion(VERSION + i);
243 269
... ... @@ -257,7 +283,7 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
257 283 PageLink pageLink = new PageLink(24);
258 284 PageData<FirmwareInfo> pageData;
259 285 do {
260   - pageData = doGetTypedWithPageLink("/api/firmwares/true?",
  286 + pageData = doGetTypedWithPageLink("/api/firmwares/" + deviceProfileId.toString() + "/FIRMWARE/true?",
261 287 new TypeReference<>() {
262 288 }, pageLink);
263 289 loadedFirmwaresWithData.addAll(pageData.getData());
... ... @@ -269,7 +295,7 @@ public abstract class BaseFirmwareControllerTest extends AbstractControllerTest
269 295 List<FirmwareInfo> loadedFirmwaresWithoutData = new ArrayList<>();
270 296 pageLink = new PageLink(24);
271 297 do {
272   - pageData = doGetTypedWithPageLink("/api/firmwares/false?",
  298 + pageData = doGetTypedWithPageLink("/api/firmwares/" + deviceProfileId.toString() + "/FIRMWARE/false?",
273 299 new TypeReference<>() {
274 300 }, pageLink);
275 301 loadedFirmwaresWithoutData.addAll(pageData.getData());
... ...
... ... @@ -29,7 +29,7 @@ import java.util.Arrays;
29 29 // "org.thingsboard.server.controller.sql.WebsocketApiSqlTest",
30 30 // "org.thingsboard.server.controller.sql.EntityQueryControllerSqlTest",
31 31 // "org.thingsboard.server.controller.sql.TbResourceControllerSqlTest",
32   - "org.thingsboard.server.controller.sql.*Test",
  32 + "org.thingsboard.server.controller.sql.FirmwareControllerSqlTest",
33 33 })
34 34 public class ControllerSqlTestSuite {
35 35
... ...
... ... @@ -18,6 +18,8 @@ package org.thingsboard.server.dao.firmware;
18 18 import com.google.common.util.concurrent.ListenableFuture;
19 19 import org.thingsboard.server.common.data.Firmware;
20 20 import org.thingsboard.server.common.data.FirmwareInfo;
  21 +import org.thingsboard.server.common.data.firmware.FirmwareType;
  22 +import org.thingsboard.server.common.data.id.DeviceProfileId;
21 23 import org.thingsboard.server.common.data.id.FirmwareId;
22 24 import org.thingsboard.server.common.data.id.TenantId;
23 25 import org.thingsboard.server.common.data.page.PageData;
... ... @@ -37,7 +39,7 @@ public interface FirmwareService {
37 39
38 40 PageData<FirmwareInfo> findTenantFirmwaresByTenantId(TenantId tenantId, PageLink pageLink);
39 41
40   - PageData<FirmwareInfo> findTenantFirmwaresByTenantIdAndHasData(TenantId tenantId, boolean hasData, PageLink pageLink);
  42 + PageData<FirmwareInfo> findTenantFirmwaresByTenantIdAndDeviceProfileIdAndTypeAndHasData(TenantId tenantId, DeviceProfileId deviceProfileId, FirmwareType firmwareType, boolean hasData, PageLink pageLink);
41 43
42 44 void deleteFirmware(TenantId tenantId, FirmwareId firmwareId);
43 45
... ...
... ... @@ -50,6 +50,7 @@ public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implemen
50 50 private byte[] deviceDataBytes;
51 51
52 52 private FirmwareId firmwareId;
  53 + private FirmwareId softwareId;
53 54
54 55 public Device() {
55 56 super();
... ... @@ -69,6 +70,7 @@ public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implemen
69 70 this.deviceProfileId = device.getDeviceProfileId();
70 71 this.setDeviceData(device.getDeviceData());
71 72 this.firmwareId = device.getFirmwareId();
  73 + this.softwareId = device.getSoftwareId();
72 74 }
73 75
74 76 public Device updateDevice(Device device) {
... ... @@ -79,6 +81,8 @@ public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implemen
79 81 this.label = device.getLabel();
80 82 this.deviceProfileId = device.getDeviceProfileId();
81 83 this.setDeviceData(device.getDeviceData());
  84 + this.setFirmwareId(device.getFirmwareId());
  85 + this.setSoftwareId(device.getSoftwareId());
82 86 return this;
83 87 }
84 88
... ... @@ -171,6 +175,14 @@ public class Device extends SearchTextBasedWithAdditionalInfo<DeviceId> implemen
171 175 this.firmwareId = firmwareId;
172 176 }
173 177
  178 + public FirmwareId getSoftwareId() {
  179 + return softwareId;
  180 + }
  181 +
  182 + public void setSoftwareId(FirmwareId softwareId) {
  183 + this.softwareId = softwareId;
  184 + }
  185 +
174 186 @Override
175 187 public String toString() {
176 188 StringBuilder builder = new StringBuilder();
... ...
... ... @@ -59,6 +59,8 @@ public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements H
59 59
60 60 private FirmwareId firmwareId;
61 61
  62 + private FirmwareId softwareId;
  63 +
62 64 public DeviceProfile() {
63 65 super();
64 66 }
... ... @@ -77,6 +79,8 @@ public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements H
77 79 this.defaultQueueName = deviceProfile.getDefaultQueueName();
78 80 this.setProfileData(deviceProfile.getProfileData());
79 81 this.provisionDeviceKey = deviceProfile.getProvisionDeviceKey();
  82 + this.firmwareId = deviceProfile.getFirmwareId();
  83 + this.softwareId = deviceProfile.getSoftwareId();
80 84 }
81 85
82 86 @Override
... ...
... ... @@ -20,6 +20,7 @@ import lombok.Data;
20 20 import lombok.EqualsAndHashCode;
21 21 import lombok.extern.slf4j.Slf4j;
22 22 import org.thingsboard.server.common.data.firmware.FirmwareType;
  23 +import org.thingsboard.server.common.data.id.DeviceProfileId;
23 24 import org.thingsboard.server.common.data.id.FirmwareId;
24 25 import org.thingsboard.server.common.data.id.TenantId;
25 26
... ... @@ -31,6 +32,7 @@ public class FirmwareInfo extends SearchTextBasedWithAdditionalInfo<FirmwareId>
31 32 private static final long serialVersionUID = 3168391583570815419L;
32 33
33 34 private TenantId tenantId;
  35 + private DeviceProfileId deviceProfileId;
34 36 private FirmwareType type;
35 37 private String title;
36 38 private String version;
... ... @@ -53,6 +55,7 @@ public class FirmwareInfo extends SearchTextBasedWithAdditionalInfo<FirmwareId>
53 55 public FirmwareInfo(FirmwareInfo firmwareInfo) {
54 56 super(firmwareInfo);
55 57 this.tenantId = firmwareInfo.getTenantId();
  58 + this.deviceProfileId = firmwareInfo.getDeviceProfileId();
56 59 this.type = firmwareInfo.getType();
57 60 this.title = firmwareInfo.getTitle();
58 61 this.version = firmwareInfo.getVersion();
... ...
... ... @@ -409,6 +409,16 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D
409 409 throw new DataValidationException("Can't assign firmware with empty data!");
410 410 }
411 411 }
  412 +
  413 + if (deviceProfile.getSoftwareId() != null) {
  414 + Firmware software = firmwareService.findFirmwareById(tenantId, deviceProfile.getSoftwareId());
  415 + if (software == null) {
  416 + throw new DataValidationException("Can't assign non-existent software!");
  417 + }
  418 + if (software.getData() == null) {
  419 + throw new DataValidationException("Can't assign software with empty data!");
  420 + }
  421 + }
412 422 }
413 423
414 424 @Override
... ...
... ... @@ -686,6 +686,22 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
686 686 if (firmware.getData() == null) {
687 687 throw new DataValidationException("Can't assign firmware with empty data!");
688 688 }
  689 + if (!firmware.getDeviceProfileId().equals(device.getDeviceProfileId())) {
  690 + throw new DataValidationException("Can't assign firmware with different deviceProfile!");
  691 + }
  692 + }
  693 +
  694 + if (device.getSoftwareId() != null) {
  695 + Firmware software = firmwareService.findFirmwareById(tenantId, device.getSoftwareId());
  696 + if (software == null) {
  697 + throw new DataValidationException("Can't assign non-existent software!");
  698 + }
  699 + if (software.getData() == null) {
  700 + throw new DataValidationException("Can't assign software with empty data!");
  701 + }
  702 + if (!software.getDeviceProfileId().equals(device.getDeviceProfileId())) {
  703 + throw new DataValidationException("Can't assign firmware with different deviceProfile!");
  704 + }
689 705 }
690 706 }
691 707 };
... ...
... ... @@ -27,13 +27,17 @@ import org.springframework.cache.CacheManager;
27 27 import org.springframework.cache.annotation.Cacheable;
28 28 import org.springframework.stereotype.Service;
29 29 import org.thingsboard.server.cache.firmware.FirmwareDataCache;
  30 +import org.thingsboard.server.common.data.DeviceProfile;
30 31 import org.thingsboard.server.common.data.Firmware;
31 32 import org.thingsboard.server.common.data.FirmwareInfo;
32 33 import org.thingsboard.server.common.data.Tenant;
  34 +import org.thingsboard.server.common.data.firmware.FirmwareType;
  35 +import org.thingsboard.server.common.data.id.DeviceProfileId;
33 36 import org.thingsboard.server.common.data.id.FirmwareId;
34 37 import org.thingsboard.server.common.data.id.TenantId;
35 38 import org.thingsboard.server.common.data.page.PageData;
36 39 import org.thingsboard.server.common.data.page.PageLink;
  40 +import org.thingsboard.server.dao.device.DeviceProfileDao;
37 41 import org.thingsboard.server.dao.exception.DataValidationException;
38 42 import org.thingsboard.server.dao.service.DataValidator;
39 43 import org.thingsboard.server.dao.service.PaginatedRemover;
... ... @@ -56,6 +60,7 @@ public class BaseFirmwareService implements FirmwareService {
56 60 public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
57 61
58 62 private final TenantDao tenantDao;
  63 + private final DeviceProfileDao deviceProfileDao;
59 64 private final FirmwareDao firmwareDao;
60 65 private final FirmwareInfoDao firmwareInfoDao;
61 66 private final CacheManager cacheManager;
... ... @@ -124,7 +129,8 @@ public class BaseFirmwareService implements FirmwareService {
124 129 public ListenableFuture<FirmwareInfo> findFirmwareInfoByIdAsync(TenantId tenantId, FirmwareId firmwareId) {
125 130 log.trace("Executing findFirmwareInfoByIdAsync [{}]", firmwareId);
126 131 validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId);
127   - return firmwareInfoDao.findByIdAsync(tenantId, firmwareId.getId()); }
  132 + return firmwareInfoDao.findByIdAsync(tenantId, firmwareId.getId());
  133 + }
128 134
129 135 @Override
130 136 public PageData<FirmwareInfo> findTenantFirmwaresByTenantId(TenantId tenantId, PageLink pageLink) {
... ... @@ -135,11 +141,11 @@ public class BaseFirmwareService implements FirmwareService {
135 141 }
136 142
137 143 @Override
138   - public PageData<FirmwareInfo> findTenantFirmwaresByTenantIdAndHasData(TenantId tenantId, boolean hasData, PageLink pageLink) {
  144 + public PageData<FirmwareInfo> findTenantFirmwaresByTenantIdAndDeviceProfileIdAndTypeAndHasData(TenantId tenantId, DeviceProfileId deviceProfileId, FirmwareType firmwareType, boolean hasData, PageLink pageLink) {
139 145 log.trace("Executing findTenantFirmwaresByTenantIdAndHasData, tenantId [{}], hasData [{}] pageLink [{}]", tenantId, hasData, pageLink);
140 146 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
141 147 validatePageLink(pageLink);
142   - return firmwareInfoDao.findFirmwareInfoByTenantIdAndHasData(tenantId, hasData, pageLink);
  148 + return firmwareInfoDao.findFirmwareInfoByTenantIdAndDeviceProfileIdAndTypeAndHasData(tenantId, deviceProfileId, firmwareType, hasData, pageLink);
143 149 }
144 150
145 151 @Override
... ... @@ -157,6 +163,10 @@ public class BaseFirmwareService implements FirmwareService {
157 163 throw new DataValidationException("The firmware referenced by the devices cannot be deleted!");
158 164 } else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_firmware_device_profile")) {
159 165 throw new DataValidationException("The firmware referenced by the device profile cannot be deleted!");
  166 + } else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_software_device")) {
  167 + throw new DataValidationException("The software referenced by the devices cannot be deleted!");
  168 + } else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_software_device_profile")) {
  169 + throw new DataValidationException("The software referenced by the device profile cannot be deleted!");
160 170 } else {
161 171 throw t;
162 172 }
... ... @@ -173,23 +183,8 @@ public class BaseFirmwareService implements FirmwareService {
173 183 private DataValidator<FirmwareInfo> firmwareInfoValidator = new DataValidator<>() {
174 184
175 185 @Override
176   - protected void validateDataImpl(TenantId tenantId, FirmwareInfo firmware) {
177   - if (firmware.getTenantId() == null) {
178   - throw new DataValidationException("Firmware should be assigned to tenant!");
179   - } else {
180   - Tenant tenant = tenantDao.findById(firmware.getTenantId(), firmware.getTenantId().getId());
181   - if (tenant == null) {
182   - throw new DataValidationException("Firmware is referencing to non-existent tenant!");
183   - }
184   - }
185   -
186   - if (StringUtils.isEmpty(firmware.getTitle())) {
187   - throw new DataValidationException("Firmware title should be specified!");
188   - }
189   -
190   - if (StringUtils.isEmpty(firmware.getVersion())) {
191   - throw new DataValidationException("Firmware version should be specified!");
192   - }
  186 + protected void validateDataImpl(TenantId tenantId, FirmwareInfo firmwareInfo) {
  187 + validateImpl(firmwareInfo);
193 188 }
194 189
195 190 @Override
... ... @@ -204,22 +199,7 @@ public class BaseFirmwareService implements FirmwareService {
204 199
205 200 @Override
206 201 protected void validateDataImpl(TenantId tenantId, Firmware firmware) {
207   - if (firmware.getTenantId() == null) {
208   - throw new DataValidationException("Firmware should be assigned to tenant!");
209   - } else {
210   - Tenant tenant = tenantDao.findById(firmware.getTenantId(), firmware.getTenantId().getId());
211   - if (tenant == null) {
212   - throw new DataValidationException("Firmware is referencing to non-existent tenant!");
213   - }
214   - }
215   -
216   - if (StringUtils.isEmpty(firmware.getTitle())) {
217   - throw new DataValidationException("Firmware title should be specified!");
218   - }
219   -
220   - if (StringUtils.isEmpty(firmware.getVersion())) {
221   - throw new DataValidationException("Firmware version should be specified!");
222   - }
  202 + validateImpl(firmware);
223 203
224 204 if (StringUtils.isEmpty(firmware.getFileName())) {
225 205 throw new DataValidationException("Firmware file name should be specified!");
... ... @@ -276,6 +256,14 @@ public class BaseFirmwareService implements FirmwareService {
276 256 };
277 257
278 258 private static void validateUpdate(FirmwareInfo firmware, FirmwareInfo firmwareOld) {
  259 + if (!firmwareOld.getDeviceProfileId().equals(firmware.getDeviceProfileId())) {
  260 + throw new DataValidationException("Updating firmware deviceProfile is prohibited!");
  261 + }
  262 +
  263 + if (!firmwareOld.getType().equals(firmware.getType())) {
  264 + throw new DataValidationException("Updating type is prohibited!");
  265 + }
  266 +
279 267 if (!firmwareOld.getTitle().equals(firmware.getTitle())) {
280 268 throw new DataValidationException("Updating firmware title is prohibited!");
281 269 }
... ... @@ -305,6 +293,38 @@ public class BaseFirmwareService implements FirmwareService {
305 293 }
306 294 }
307 295
  296 + private void validateImpl(FirmwareInfo firmwareInfo) {
  297 + if (firmwareInfo.getTenantId() == null) {
  298 + throw new DataValidationException("Firmware should be assigned to tenant!");
  299 + } else {
  300 + Tenant tenant = tenantDao.findById(firmwareInfo.getTenantId(), firmwareInfo.getTenantId().getId());
  301 + if (tenant == null) {
  302 + throw new DataValidationException("Firmware is referencing to non-existent tenant!");
  303 + }
  304 + }
  305 +
  306 + if (firmwareInfo.getDeviceProfileId() == null) {
  307 + throw new DataValidationException("Firmware should be assigned to deviceProfile!");
  308 + } else {
  309 + DeviceProfile deviceProfile = deviceProfileDao.findById(firmwareInfo.getTenantId(), firmwareInfo.getDeviceProfileId().getId());
  310 + if (deviceProfile == null) {
  311 + throw new DataValidationException("Firmware is referencing to non-existent device profile!");
  312 + }
  313 + }
  314 +
  315 + if (firmwareInfo.getType() == null) {
  316 + throw new DataValidationException("Type should be specified!");
  317 + }
  318 +
  319 + if (StringUtils.isEmpty(firmwareInfo.getTitle())) {
  320 + throw new DataValidationException("Firmware title should be specified!");
  321 + }
  322 +
  323 + if (StringUtils.isEmpty(firmwareInfo.getVersion())) {
  324 + throw new DataValidationException("Firmware version should be specified!");
  325 + }
  326 + }
  327 +
308 328 private PaginatedRemover<TenantId, FirmwareInfo> tenantFirmwareRemover =
309 329 new PaginatedRemover<>() {
310 330
... ...
... ... @@ -16,6 +16,8 @@
16 16 package org.thingsboard.server.dao.firmware;
17 17
18 18 import org.thingsboard.server.common.data.FirmwareInfo;
  19 +import org.thingsboard.server.common.data.firmware.FirmwareType;
  20 +import org.thingsboard.server.common.data.id.DeviceProfileId;
19 21 import org.thingsboard.server.common.data.id.TenantId;
20 22 import org.thingsboard.server.common.data.page.PageData;
21 23 import org.thingsboard.server.common.data.page.PageLink;
... ... @@ -27,6 +29,6 @@ public interface FirmwareInfoDao extends Dao<FirmwareInfo> {
27 29
28 30 PageData<FirmwareInfo> findFirmwareInfoByTenantId(TenantId tenantId, PageLink pageLink);
29 31
30   - PageData<FirmwareInfo> findFirmwareInfoByTenantIdAndHasData(TenantId tenantId, boolean hasData, PageLink pageLink);
  32 + PageData<FirmwareInfo> findFirmwareInfoByTenantIdAndDeviceProfileIdAndTypeAndHasData(TenantId tenantId, DeviceProfileId deviceProfileId, FirmwareType firmwareType, boolean hasData, PageLink pageLink);
31 33
32 34 }
... ...
... ... @@ -154,6 +154,7 @@ public class ModelConstants {
154 154 public static final String DEVICE_DEVICE_PROFILE_ID_PROPERTY = "device_profile_id";
155 155 public static final String DEVICE_DEVICE_DATA_PROPERTY = "device_data";
156 156 public static final String DEVICE_FIRMWARE_ID_PROPERTY = "firmware_id";
  157 + public static final String DEVICE_SOFTWARE_ID_PROPERTY = "software_id";
157 158
158 159 public static final String DEVICE_BY_TENANT_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME = "device_by_tenant_and_search_text";
159 160 public static final String DEVICE_BY_TENANT_BY_TYPE_AND_SEARCH_TEXT_COLUMN_FAMILY_NAME = "device_by_tenant_by_type_and_search_text";
... ... @@ -178,6 +179,7 @@ public class ModelConstants {
178 179 public static final String DEVICE_PROFILE_DEFAULT_QUEUE_NAME_PROPERTY = "default_queue_name";
179 180 public static final String DEVICE_PROFILE_PROVISION_DEVICE_KEY = "provision_device_key";
180 181 public static final String DEVICE_PROFILE_FIRMWARE_ID_PROPERTY = "firmware_id";
  182 + public static final String DEVICE_PROFILE_SOFTWARE_ID_PROPERTY = "software_id";
181 183
182 184 /**
183 185 * Cassandra entityView constants.
... ... @@ -476,6 +478,7 @@ public class ModelConstants {
476 478 */
477 479 public static final String FIRMWARE_TABLE_NAME = "firmware";
478 480 public static final String FIRMWARE_TENANT_ID_COLUMN = TENANT_ID_COLUMN;
  481 + public static final String FIRMWARE_DEVICE_PROFILE_ID_COLUMN = "device_profile_id";
479 482 public static final String FIRMWARE_TYPE_COLUMN = "type";
480 483 public static final String FIRMWARE_TITLE_COLUMN = TITLE_PROPERTY;
481 484 public static final String FIRMWARE_VERSION_COLUMN = "version";
... ...
... ... @@ -22,6 +22,7 @@ import lombok.EqualsAndHashCode;
22 22 import org.hibernate.annotations.Type;
23 23 import org.hibernate.annotations.TypeDef;
24 24 import org.hibernate.annotations.TypeDefs;
  25 +import org.thingsboard.common.util.JacksonUtil;
25 26 import org.thingsboard.server.common.data.Device;
26 27 import org.thingsboard.server.common.data.device.data.DeviceData;
27 28 import org.thingsboard.server.common.data.id.CustomerId;
... ... @@ -32,7 +33,6 @@ import org.thingsboard.server.common.data.id.TenantId;
32 33 import org.thingsboard.server.dao.model.BaseSqlEntity;
33 34 import org.thingsboard.server.dao.model.ModelConstants;
34 35 import org.thingsboard.server.dao.model.SearchTextEntity;
35   -import org.thingsboard.common.util.JacksonUtil;
36 36 import org.thingsboard.server.dao.util.mapping.JsonBinaryType;
37 37 import org.thingsboard.server.dao.util.mapping.JsonStringType;
38 38
... ... @@ -43,8 +43,8 @@ import java.util.UUID;
43 43 @Data
44 44 @EqualsAndHashCode(callSuper = true)
45 45 @TypeDefs({
46   - @TypeDef(name = "json", typeClass = JsonStringType.class),
47   - @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
  46 + @TypeDef(name = "json", typeClass = JsonStringType.class),
  47 + @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
48 48 })
49 49 @MappedSuperclass
50 50 public abstract class AbstractDeviceEntity<T extends Device> extends BaseSqlEntity<T> implements SearchTextEntity<T> {
... ... @@ -77,6 +77,9 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseSqlEnti
77 77 @Column(name = ModelConstants.DEVICE_FIRMWARE_ID_PROPERTY, columnDefinition = "uuid")
78 78 private UUID firmwareId;
79 79
  80 + @Column(name = ModelConstants.DEVICE_SOFTWARE_ID_PROPERTY, columnDefinition = "uuid")
  81 + private UUID softwareId;
  82 +
80 83 @Type(type = "jsonb")
81 84 @Column(name = ModelConstants.DEVICE_DEVICE_DATA_PROPERTY, columnDefinition = "jsonb")
82 85 private JsonNode deviceData;
... ... @@ -102,6 +105,9 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseSqlEnti
102 105 if (device.getFirmwareId() != null) {
103 106 this.firmwareId = device.getFirmwareId().getId();
104 107 }
  108 + if (device.getSoftwareId() != null) {
  109 + this.firmwareId = device.getSoftwareId().getId();
  110 + }
105 111 this.deviceData = JacksonUtil.convertValue(device.getDeviceData(), ObjectNode.class);
106 112 this.name = device.getName();
107 113 this.type = device.getType();
... ... @@ -122,6 +128,7 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseSqlEnti
122 128 this.searchText = deviceEntity.getSearchText();
123 129 this.additionalInfo = deviceEntity.getAdditionalInfo();
124 130 this.firmwareId = deviceEntity.getFirmwareId();
  131 + this.softwareId = deviceEntity.getSoftwareId();
125 132 }
126 133
127 134 @Override
... ... @@ -149,6 +156,9 @@ public abstract class AbstractDeviceEntity<T extends Device> extends BaseSqlEnti
149 156 if (firmwareId != null) {
150 157 device.setFirmwareId(new FirmwareId(firmwareId));
151 158 }
  159 + if (softwareId != null) {
  160 + device.setSoftwareId(new FirmwareId(softwareId));
  161 + }
152 162 device.setDeviceData(JacksonUtil.convertValue(deviceData, DeviceData.class));
153 163 device.setName(name);
154 164 device.setType(type);
... ...
... ... @@ -21,9 +21,10 @@ import lombok.Data;
21 21 import lombok.EqualsAndHashCode;
22 22 import org.hibernate.annotations.Type;
23 23 import org.hibernate.annotations.TypeDef;
  24 +import org.thingsboard.common.util.JacksonUtil;
24 25 import org.thingsboard.server.common.data.DeviceProfile;
25   -import org.thingsboard.server.common.data.DeviceProfileType;
26 26 import org.thingsboard.server.common.data.DeviceProfileProvisionType;
  27 +import org.thingsboard.server.common.data.DeviceProfileType;
27 28 import org.thingsboard.server.common.data.DeviceTransportType;
28 29 import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
29 30 import org.thingsboard.server.common.data.id.DeviceProfileId;
... ... @@ -33,7 +34,6 @@ import org.thingsboard.server.common.data.id.TenantId;
33 34 import org.thingsboard.server.dao.model.BaseSqlEntity;
34 35 import org.thingsboard.server.dao.model.ModelConstants;
35 36 import org.thingsboard.server.dao.model.SearchTextEntity;
36   -import org.thingsboard.common.util.JacksonUtil;
37 37 import org.thingsboard.server.dao.util.mapping.JsonBinaryType;
38 38
39 39 import javax.persistence.Column;
... ... @@ -87,12 +87,15 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
87 87 @Column(name = ModelConstants.DEVICE_PROFILE_PROFILE_DATA_PROPERTY, columnDefinition = "jsonb")
88 88 private JsonNode profileData;
89 89
90   - @Column(name=ModelConstants.DEVICE_PROFILE_PROVISION_DEVICE_KEY)
  90 + @Column(name = ModelConstants.DEVICE_PROFILE_PROVISION_DEVICE_KEY)
91 91 private String provisionDeviceKey;
92 92
93   - @Column(name=ModelConstants.DEVICE_PROFILE_FIRMWARE_ID_PROPERTY)
  93 + @Column(name = ModelConstants.DEVICE_PROFILE_FIRMWARE_ID_PROPERTY)
94 94 private UUID firmwareId;
95 95
  96 + @Column(name = ModelConstants.DEVICE_PROFILE_SOFTWARE_ID_PROPERTY)
  97 + private UUID softwareId;
  98 +
96 99 public DeviceProfileEntity() {
97 100 super();
98 101 }
... ... @@ -120,6 +123,9 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
120 123 if (deviceProfile.getFirmwareId() != null) {
121 124 this.firmwareId = deviceProfile.getFirmwareId().getId();
122 125 }
  126 + if (deviceProfile.getSoftwareId() != null) {
  127 + this.firmwareId = deviceProfile.getSoftwareId().getId();
  128 + }
123 129 }
124 130
125 131 @Override
... ... @@ -160,6 +166,10 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
160 166 deviceProfile.setFirmwareId(new FirmwareId(firmwareId));
161 167 }
162 168
  169 + if (softwareId != null) {
  170 + deviceProfile.setSoftwareId(new FirmwareId(softwareId));
  171 + }
  172 +
163 173 return deviceProfile;
164 174 }
165 175 }
... ...
... ... @@ -22,6 +22,7 @@ import org.hibernate.annotations.Type;
22 22 import org.hibernate.annotations.TypeDef;
23 23 import org.thingsboard.server.common.data.Firmware;
24 24 import org.thingsboard.server.common.data.firmware.FirmwareType;
  25 +import org.thingsboard.server.common.data.id.DeviceProfileId;
25 26 import org.thingsboard.server.common.data.id.FirmwareId;
26 27 import org.thingsboard.server.common.data.id.TenantId;
27 28 import org.thingsboard.server.dao.model.BaseSqlEntity;
... ... @@ -43,6 +44,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CHECKSUM_
43 44 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CONTENT_TYPE_COLUMN;
44 45 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_DATA_COLUMN;
45 46 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_DATA_SIZE_COLUMN;
  47 +import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_DEVICE_PROFILE_ID_COLUMN;
46 48 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_FILE_NAME_COLUMN;
47 49 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TABLE_NAME;
48 50 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TENANT_ID_COLUMN;
... ... @@ -61,6 +63,9 @@ public class FirmwareEntity extends BaseSqlEntity<Firmware> implements SearchTex
61 63 @Column(name = FIRMWARE_TENANT_ID_COLUMN)
62 64 private UUID tenantId;
63 65
  66 + @Column(name = FIRMWARE_DEVICE_PROFILE_ID_COLUMN)
  67 + private UUID deviceProfileId;
  68 +
64 69 @Enumerated(EnumType.STRING)
65 70 @Column(name = FIRMWARE_TYPE_COLUMN)
66 71 private FirmwareType type;
... ... @@ -105,6 +110,7 @@ public class FirmwareEntity extends BaseSqlEntity<Firmware> implements SearchTex
105 110 this.createdTime = firmware.getCreatedTime();
106 111 this.setUuid(firmware.getUuidId());
107 112 this.tenantId = firmware.getTenantId().getId();
  113 + this.deviceProfileId = firmware.getDeviceProfileId().getId();
108 114 this.type = firmware.getType();
109 115 this.title = firmware.getTitle();
110 116 this.version = firmware.getVersion();
... ... @@ -132,6 +138,7 @@ public class FirmwareEntity extends BaseSqlEntity<Firmware> implements SearchTex
132 138 Firmware firmware = new Firmware(new FirmwareId(id));
133 139 firmware.setCreatedTime(createdTime);
134 140 firmware.setTenantId(new TenantId(tenantId));
  141 + firmware.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
135 142 firmware.setType(type);
136 143 firmware.setTitle(title);
137 144 firmware.setVersion(version);
... ...
... ... @@ -23,6 +23,7 @@ import org.hibernate.annotations.TypeDef;
23 23 import org.thingsboard.common.util.JacksonUtil;
24 24 import org.thingsboard.server.common.data.FirmwareInfo;
25 25 import org.thingsboard.server.common.data.firmware.FirmwareType;
  26 +import org.thingsboard.server.common.data.id.DeviceProfileId;
26 27 import org.thingsboard.server.common.data.id.FirmwareId;
27 28 import org.thingsboard.server.common.data.id.TenantId;
28 29 import org.thingsboard.server.dao.model.BaseSqlEntity;
... ... @@ -42,6 +43,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CHECKSUM_
42 43 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CHECKSUM_COLUMN;
43 44 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_CONTENT_TYPE_COLUMN;
44 45 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_DATA_SIZE_COLUMN;
  46 +import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_DEVICE_PROFILE_ID_COLUMN;
45 47 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_FILE_NAME_COLUMN;
46 48 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TABLE_NAME;
47 49 import static org.thingsboard.server.dao.model.ModelConstants.FIRMWARE_TENANT_ID_COLUMN;
... ... @@ -60,6 +62,9 @@ public class FirmwareInfoEntity extends BaseSqlEntity<FirmwareInfo> implements S
60 62 @Column(name = FIRMWARE_TENANT_ID_COLUMN)
61 63 private UUID tenantId;
62 64
  65 + @Column(name = FIRMWARE_DEVICE_PROFILE_ID_COLUMN)
  66 + private UUID deviceProfileId;
  67 +
63 68 @Enumerated(EnumType.STRING)
64 69 @Column(name = FIRMWARE_TYPE_COLUMN)
65 70 private FirmwareType type;
... ... @@ -103,6 +108,8 @@ public class FirmwareInfoEntity extends BaseSqlEntity<FirmwareInfo> implements S
103 108 this.createdTime = firmware.getCreatedTime();
104 109 this.setUuid(firmware.getUuidId());
105 110 this.tenantId = firmware.getTenantId().getId();
  111 + this.type = firmware.getType();
  112 + this.deviceProfileId = firmware.getDeviceProfileId().getId();
106 113 this.title = firmware.getTitle();
107 114 this.version = firmware.getVersion();
108 115 this.fileName = firmware.getFileName();
... ... @@ -113,13 +120,14 @@ public class FirmwareInfoEntity extends BaseSqlEntity<FirmwareInfo> implements S
113 120 this.additionalInfo = firmware.getAdditionalInfo();
114 121 }
115 122
116   - public FirmwareInfoEntity(UUID id, long createdTime, UUID tenantId, String type, String title, String version,
  123 + public FirmwareInfoEntity(UUID id, long createdTime, UUID tenantId, UUID deviceProfileId, FirmwareType type, String title, String version,
117 124 String fileName, String contentType, String checksumAlgorithm, String checksum, Long dataSize,
118 125 Object additionalInfo, boolean hasData) {
119 126 this.id = id;
120 127 this.createdTime = createdTime;
121 128 this.tenantId = tenantId;
122   - this.type = FirmwareType.valueOf(type);
  129 + this.deviceProfileId = deviceProfileId;
  130 + this.type = type;
123 131 this.title = title;
124 132 this.version = version;
125 133 this.fileName = fileName;
... ... @@ -146,6 +154,7 @@ public class FirmwareInfoEntity extends BaseSqlEntity<FirmwareInfo> implements S
146 154 FirmwareInfo firmware = new FirmwareInfo(new FirmwareId(id));
147 155 firmware.setCreatedTime(createdTime);
148 156 firmware.setTenantId(new TenantId(tenantId));
  157 + firmware.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
149 158 firmware.setType(type);
150 159 firmware.setTitle(title);
151 160 firmware.setVersion(version);
... ...
... ... @@ -20,27 +20,32 @@ import org.springframework.data.domain.Pageable;
20 20 import org.springframework.data.jpa.repository.Query;
21 21 import org.springframework.data.repository.CrudRepository;
22 22 import org.springframework.data.repository.query.Param;
  23 +import org.thingsboard.server.common.data.firmware.FirmwareType;
23 24 import org.thingsboard.server.dao.model.sql.FirmwareInfoEntity;
24 25
25 26 import java.util.UUID;
26 27
27 28 public interface FirmwareInfoRepository extends CrudRepository<FirmwareInfoEntity, UUID> {
28   - @Query("SELECT new FirmwareInfoEntity(f.id, f.createdTime, f.tenantId, f.type, f.title, f.version, f.fileName, f.contentType, f.checksumAlgorithm, f.checksum, f.dataSize, f.additionalInfo, f.data IS NOT NULL) FROM FirmwareEntity f WHERE " +
  29 + @Query("SELECT new FirmwareInfoEntity(f.id, f.createdTime, f.tenantId, f.deviceProfileId, f.type, f.title, f.version, f.fileName, f.contentType, f.checksumAlgorithm, f.checksum, f.dataSize, f.additionalInfo, f.data IS NOT NULL) FROM FirmwareEntity f WHERE " +
29 30 "f.tenantId = :tenantId " +
30 31 "AND LOWER(f.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
31 32 Page<FirmwareInfoEntity> findAllByTenantId(@Param("tenantId") UUID tenantId,
32 33 @Param("searchText") String searchText,
33 34 Pageable pageable);
34 35
35   - @Query("SELECT new FirmwareInfoEntity(f.id, f.createdTime, f.tenantId, f.type, f.title, f.version, f.fileName, f.contentType, f.checksumAlgorithm, f.checksum, f.dataSize, f.additionalInfo, f.data IS NOT NULL) FROM FirmwareEntity f WHERE " +
  36 + @Query("SELECT new FirmwareInfoEntity(f.id, f.createdTime, f.tenantId, f.deviceProfileId, f.type, f.title, f.version, f.fileName, f.contentType, f.checksumAlgorithm, f.checksum, f.dataSize, f.additionalInfo, f.data IS NOT NULL) FROM FirmwareEntity f WHERE " +
36 37 "f.tenantId = :tenantId " +
  38 + "AND f.deviceProfileId = :deviceProfileId " +
  39 + "AND f.type = :type " +
37 40 "AND ((f.data IS NOT NULL AND :hasData = true) OR (f.data IS NULL AND :hasData = false ))" +
38 41 "AND LOWER(f.searchText) LIKE LOWER(CONCAT(:searchText, '%'))")
39   - Page<FirmwareInfoEntity> findAllByTenantIdAndHasData(@Param("tenantId") UUID tenantId,
40   - @Param("hasData") boolean hasData,
41   - @Param("searchText") String searchText,
42   - Pageable pageable);
  42 + Page<FirmwareInfoEntity> findAllByTenantIdAndTypeAndDeviceProfileIdAndHasData(@Param("tenantId") UUID tenantId,
  43 + @Param("deviceProfileId") UUID deviceProfileId,
  44 + @Param("type") FirmwareType type,
  45 + @Param("hasData") boolean hasData,
  46 + @Param("searchText") String searchText,
  47 + Pageable pageable);
43 48
44   - @Query("SELECT new FirmwareInfoEntity(f.id, f.createdTime, f.tenantId, f.title, f.version, f.fileName, f.contentType, f.checksumAlgorithm, f.checksum, f.dataSize, f.additionalInfo, f.data IS NOT NULL) FROM FirmwareEntity f WHERE f.id = :id")
  49 + @Query("SELECT new FirmwareInfoEntity(f.id, f.createdTime, f.tenantId, f.deviceProfileId, f.type, f.title, f.version, f.fileName, f.contentType, f.checksumAlgorithm, f.checksum, f.dataSize, f.additionalInfo, f.data IS NOT NULL) FROM FirmwareEntity f WHERE f.id = :id")
45 50 FirmwareInfoEntity findFirmwareInfoById(@Param("id") UUID id);
46 51 }
... ...
... ... @@ -20,6 +20,8 @@ import org.springframework.beans.factory.annotation.Autowired;
20 20 import org.springframework.data.repository.CrudRepository;
21 21 import org.springframework.stereotype.Component;
22 22 import org.thingsboard.server.common.data.FirmwareInfo;
  23 +import org.thingsboard.server.common.data.firmware.FirmwareType;
  24 +import org.thingsboard.server.common.data.id.DeviceProfileId;
23 25 import org.thingsboard.server.common.data.id.TenantId;
24 26 import org.thingsboard.server.common.data.page.PageData;
25 27 import org.thingsboard.server.common.data.page.PageLink;
... ... @@ -73,10 +75,12 @@ public class JpaFirmwareInfoDao extends JpaAbstractSearchTextDao<FirmwareInfoEnt
73 75 }
74 76
75 77 @Override
76   - public PageData<FirmwareInfo> findFirmwareInfoByTenantIdAndHasData(TenantId tenantId, boolean hasData, PageLink pageLink) {
  78 + public PageData<FirmwareInfo> findFirmwareInfoByTenantIdAndDeviceProfileIdAndTypeAndHasData(TenantId tenantId, DeviceProfileId deviceProfileId, FirmwareType firmwareType, boolean hasData, PageLink pageLink) {
77 79 return DaoUtil.toPageData(firmwareInfoRepository
78   - .findAllByTenantIdAndHasData(
  80 + .findAllByTenantIdAndTypeAndDeviceProfileIdAndHasData(
79 81 tenantId.getId(),
  82 + deviceProfileId.getId(),
  83 + firmwareType,
80 84 hasData,
81 85 Objects.toString(pageLink.getTextSearch(), ""),
82 86 DaoUtil.toPageable(pageLink)));
... ...
... ... @@ -162,6 +162,7 @@ CREATE TABLE IF NOT EXISTS firmware (
162 162 id uuid NOT NULL CONSTRAINT firmware_pkey PRIMARY KEY,
163 163 created_time bigint NOT NULL,
164 164 tenant_id uuid NOT NULL,
  165 + device_profile_id uuid NOT NULL,
165 166 type varchar(32) NOT NULL,
166 167 title varchar(255) NOT NULL,
167 168 version varchar(255) NOT NULL,
... ... @@ -189,13 +190,15 @@ CREATE TABLE IF NOT EXISTS device_profile (
189 190 is_default boolean,
190 191 tenant_id uuid,
191 192 firmware_id uuid,
  193 + software_id uuid,
192 194 default_rule_chain_id uuid,
193 195 default_queue_name varchar(255),
194 196 provision_device_key varchar,
195 197 CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
196 198 CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
197 199 CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
198   - CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES firmware(id)
  200 + CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES firmware(id),
  201 + CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES firmware(id)
199 202 );
200 203
201 204 CREATE TABLE IF NOT EXISTS device (
... ... @@ -211,9 +214,11 @@ CREATE TABLE IF NOT EXISTS device (
211 214 search_text varchar(255),
212 215 tenant_id uuid,
213 216 firmware_id uuid,
  217 + software_id uuid,
214 218 CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
215 219 CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
216   - CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES firmware(id)
  220 + CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES firmware(id),
  221 + CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES firmware(id)
217 222 );
218 223
219 224 CREATE TABLE IF NOT EXISTS device_credentials (
... ...
... ... @@ -192,8 +192,8 @@ CREATE TABLE IF NOT EXISTS firmware (
192 192 data_size bigint,
193 193 additional_info varchar,
194 194 search_text varchar(255),
195   - CONSTRAINT firmware_tenant_title_version_unq_key UNIQUE (tenant_id, title, version),
196   - CONSTRAINT fk_firmware_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id) ON DELETE CASCADE
  195 + CONSTRAINT firmware_tenant_title_version_unq_key UNIQUE (tenant_id, title, version)
  196 +-- CONSTRAINT fk_device_profile_firmware FOREIGN KEY (device_profile_id) REFERENCES device_profile(id) ON DELETE CASCADE
197 197 );
198 198
199 199 CREATE TABLE IF NOT EXISTS device_profile (
... ... @@ -216,8 +216,8 @@ CREATE TABLE IF NOT EXISTS device_profile (
216 216 CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
217 217 CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
218 218 CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id),
219   - CONSTRAINT fk_device_profile_firmware FOREIGN KEY (firmware_id) REFERENCES firmware(id)
220   - CONSTRAINT fk_device_profile_software FOREIGN KEY (software_id) REFERENCES firmware(id)
  219 + CONSTRAINT fk_firmware_device_profile FOREIGN KEY (firmware_id) REFERENCES firmware(id),
  220 + CONSTRAINT fk_software_device_profile FOREIGN KEY (software_id) REFERENCES firmware(id)
221 221 );
222 222
223 223 -- We will use one-to-many relation in the first release and extend this feature in case of user requests
... ... @@ -245,7 +245,7 @@ CREATE TABLE IF NOT EXISTS device (
245 245 software_id uuid,
246 246 CONSTRAINT device_name_unq_key UNIQUE (tenant_id, name),
247 247 CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id),
248   - CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES firmware(id)
  248 + CONSTRAINT fk_firmware_device FOREIGN KEY (firmware_id) REFERENCES firmware(id),
249 249 CONSTRAINT fk_software_device FOREIGN KEY (software_id) REFERENCES firmware(id)
250 250 );
251 251
... ...
... ... @@ -30,6 +30,7 @@ import org.thingsboard.server.common.data.DeviceProfileInfo;
30 30 import org.thingsboard.server.common.data.DeviceTransportType;
31 31 import org.thingsboard.server.common.data.Firmware;
32 32 import org.thingsboard.server.common.data.Tenant;
  33 +import org.thingsboard.server.common.data.firmware.FirmwareType;
33 34 import org.thingsboard.server.common.data.id.TenantId;
34 35 import org.thingsboard.server.common.data.page.PageData;
35 36 import org.thingsboard.server.common.data.page.PageLink;
... ... @@ -43,6 +44,8 @@ import java.util.concurrent.ExecutionException;
43 44 import java.util.concurrent.Executors;
44 45 import java.util.stream.Collectors;
45 46
  47 +import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
  48 +
46 49 public class BaseDeviceProfileServiceTest extends AbstractServiceTest {
47 50
48 51 private IdComparator<DeviceProfile> idComparator = new IdComparator<>();
... ... @@ -97,6 +100,8 @@ public class BaseDeviceProfileServiceTest extends AbstractServiceTest {
97 100
98 101 Firmware firmware = new Firmware();
99 102 firmware.setTenantId(tenantId);
  103 + firmware.setDeviceProfileId(savedDeviceProfile.getId());
  104 + firmware.setType(FIRMWARE);
100 105 firmware.setTitle("my firmware");
101 106 firmware.setVersion("v1.0");
102 107 firmware.setFileName("test.txt");
... ...
... ... @@ -20,10 +20,13 @@ import org.apache.commons.lang3.RandomStringUtils;
20 20 import org.junit.After;
21 21 import org.junit.Assert;
22 22 import org.junit.Before;
  23 +import org.junit.Rule;
23 24 import org.junit.Test;
  25 +import org.junit.rules.ExpectedException;
24 26 import org.thingsboard.server.common.data.Customer;
25 27 import org.thingsboard.server.common.data.Device;
26 28 import org.thingsboard.server.common.data.DeviceInfo;
  29 +import org.thingsboard.server.common.data.DeviceProfile;
27 30 import org.thingsboard.server.common.data.EntitySubtype;
28 31 import org.thingsboard.server.common.data.Firmware;
29 32 import org.thingsboard.server.common.data.Tenant;
... ... @@ -42,6 +45,7 @@ import java.util.ArrayList;
42 45 import java.util.Collections;
43 46 import java.util.List;
44 47
  48 +import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
45 49 import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
46 50
47 51 public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
... ... @@ -66,6 +70,9 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
66 70 tenantProfileService.deleteTenantProfiles(anotherTenantId);
67 71 }
68 72
  73 + @Rule
  74 + public ExpectedException thrown = ExpectedException.none();
  75 +
69 76 @Test
70 77 public void testSaveDevicesWithoutMaxDeviceLimit() {
71 78 Device device = this.saveDevice(tenantId, "My device");
... ... @@ -183,6 +190,8 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
183 190
184 191 Firmware firmware = new Firmware();
185 192 firmware.setTenantId(tenantId);
  193 + firmware.setDeviceProfileId(device.getDeviceProfileId());
  194 + firmware.setType(FIRMWARE);
186 195 firmware.setTitle("my firmware");
187 196 firmware.setVersion("v1.0");
188 197 firmware.setFileName("test.txt");
... ... @@ -198,6 +207,40 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
198 207 Device foundDevice = deviceService.findDeviceById(tenantId, savedDevice.getId());
199 208 Assert.assertEquals(foundDevice.getName(), savedDevice.getName());
200 209 }
  210 +
  211 + @Test
  212 + public void testAssignFirmwareToDeviceWithDifferentDeviceProfile() {
  213 + Device device = new Device();
  214 + device.setTenantId(tenantId);
  215 + device.setName("My device");
  216 + device.setType("default");
  217 + Device savedDevice = deviceService.saveDevice(device);
  218 +
  219 + Assert.assertNotNull(savedDevice);
  220 +
  221 + DeviceProfile deviceProfile = createDeviceProfile(tenantId, "New device Profile");
  222 + DeviceProfile savedProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
  223 + Assert.assertNotNull(savedProfile);
  224 +
  225 + Firmware firmware = new Firmware();
  226 + firmware.setTenantId(tenantId);
  227 + firmware.setDeviceProfileId(savedProfile.getId());
  228 + firmware.setType(FIRMWARE);
  229 + firmware.setTitle("my firmware");
  230 + firmware.setVersion("v1.0");
  231 + firmware.setFileName("test.txt");
  232 + firmware.setContentType("text/plain");
  233 + firmware.setChecksumAlgorithm("sha256");
  234 + firmware.setChecksum("4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a");
  235 + firmware.setData(ByteBuffer.wrap(new byte[]{1}));
  236 + Firmware savedFirmware = firmwareService.saveFirmware(firmware);
  237 +
  238 + savedDevice.setFirmwareId(savedFirmware.getId());
  239 +
  240 + thrown.expect(DataValidationException.class);
  241 + thrown.expectMessage("Can't assign firmware with different deviceProfile!");
  242 + deviceService.saveDevice(savedDevice);
  243 + }
201 244
202 245 @Test(expected = DataValidationException.class)
203 246 public void testSaveDeviceWithEmptyName() {
... ...
... ... @@ -19,13 +19,16 @@ import com.datastax.oss.driver.api.core.uuid.Uuids;
19 19 import org.junit.After;
20 20 import org.junit.Assert;
21 21 import org.junit.Before;
  22 +import org.junit.Rule;
22 23 import org.junit.Test;
  24 +import org.junit.rules.ExpectedException;
23 25 import org.thingsboard.common.util.JacksonUtil;
24 26 import org.thingsboard.server.common.data.Device;
25 27 import org.thingsboard.server.common.data.DeviceProfile;
26 28 import org.thingsboard.server.common.data.Firmware;
27 29 import org.thingsboard.server.common.data.FirmwareInfo;
28 30 import org.thingsboard.server.common.data.Tenant;
  31 +import org.thingsboard.server.common.data.id.DeviceProfileId;
29 32 import org.thingsboard.server.common.data.id.TenantId;
30 33 import org.thingsboard.server.common.data.page.PageData;
31 34 import org.thingsboard.server.common.data.page.PageLink;
... ... @@ -36,6 +39,8 @@ import java.util.ArrayList;
36 39 import java.util.Collections;
37 40 import java.util.List;
38 41
  42 +import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
  43 +
39 44 public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
40 45
41 46 public static final String TITLE = "My firmware";
... ... @@ -50,6 +55,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
50 55
51 56 private TenantId tenantId;
52 57
  58 + private DeviceProfileId deviceProfileId;
  59 +
53 60 @Before
54 61 public void before() {
55 62 Tenant tenant = new Tenant();
... ... @@ -57,8 +64,16 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
57 64 Tenant savedTenant = tenantService.saveTenant(tenant);
58 65 Assert.assertNotNull(savedTenant);
59 66 tenantId = savedTenant.getId();
  67 +
  68 + DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
  69 + DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
  70 + Assert.assertNotNull(savedDeviceProfile);
  71 + deviceProfileId = savedDeviceProfile.getId();
60 72 }
61 73
  74 + @Rule
  75 + public ExpectedException thrown = ExpectedException.none();
  76 +
62 77 @After
63 78 public void after() {
64 79 tenantService.deleteTenant(tenantId);
... ... @@ -68,6 +83,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
68 83 public void testSaveFirmware() {
69 84 Firmware firmware = new Firmware();
70 85 firmware.setTenantId(tenantId);
  86 + firmware.setDeviceProfileId(deviceProfileId);
  87 + firmware.setType(FIRMWARE);
71 88 firmware.setTitle(TITLE);
72 89 firmware.setVersion(VERSION);
73 90 firmware.setFileName(FILE_NAME);
... ... @@ -99,6 +116,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
99 116 public void testSaveFirmwareInfoAndUpdateWithData() {
100 117 FirmwareInfo firmwareInfo = new FirmwareInfo();
101 118 firmwareInfo.setTenantId(tenantId);
  119 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  120 + firmwareInfo.setType(FIRMWARE);
102 121 firmwareInfo.setTitle(TITLE);
103 122 firmwareInfo.setVersion(VERSION);
104 123 FirmwareInfo savedFirmwareInfo = firmwareService.saveFirmwareInfo(firmwareInfo);
... ... @@ -112,6 +131,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
112 131 Firmware firmware = new Firmware(savedFirmwareInfo.getId());
113 132 firmware.setCreatedTime(firmwareInfo.getCreatedTime());
114 133 firmware.setTenantId(tenantId);
  134 + firmware.setDeviceProfileId(deviceProfileId);
  135 + firmware.setType(FIRMWARE);
115 136 firmware.setTitle(TITLE);
116 137 firmware.setVersion(VERSION);
117 138 firmware.setFileName(FILE_NAME);
... ... @@ -135,9 +156,11 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
135 156 firmwareService.deleteFirmware(tenantId, savedFirmwareInfo.getId());
136 157 }
137 158
138   - @Test(expected = DataValidationException.class)
  159 + @Test
139 160 public void testSaveFirmwareWithEmptyTenant() {
140 161 Firmware firmware = new Firmware();
  162 + firmware.setDeviceProfileId(deviceProfileId);
  163 + firmware.setType(FIRMWARE);
141 164 firmware.setTitle(TITLE);
142 165 firmware.setVersion(VERSION);
143 166 firmware.setFileName(FILE_NAME);
... ... @@ -145,65 +168,126 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
145 168 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
146 169 firmware.setChecksum(CHECKSUM);
147 170 firmware.setData(DATA);
  171 +
  172 + thrown.expect(DataValidationException.class);
  173 + thrown.expectMessage("Firmware should be assigned to tenant!");
148 174 firmwareService.saveFirmware(firmware);
149 175 }
150 176
151   - @Test(expected = DataValidationException.class)
  177 + @Test
  178 + public void testSaveFirmwareWithEmptyDeviceProfile() {
  179 + Firmware firmware = new Firmware();
  180 + firmware.setTenantId(tenantId);
  181 + firmware.setType(FIRMWARE);
  182 + firmware.setTitle(TITLE);
  183 + firmware.setVersion(VERSION);
  184 + firmware.setFileName(FILE_NAME);
  185 + firmware.setContentType(CONTENT_TYPE);
  186 + firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
  187 + firmware.setChecksum(CHECKSUM);
  188 + firmware.setData(DATA);
  189 +
  190 + thrown.expect(DataValidationException.class);
  191 + thrown.expectMessage("Firmware should be assigned to deviceProfile!");
  192 + firmwareService.saveFirmware(firmware);
  193 + }
  194 +
  195 + @Test
  196 + public void testSaveFirmwareWithEmptyType() {
  197 + Firmware firmware = new Firmware();
  198 + firmware.setTenantId(tenantId);
  199 + firmware.setDeviceProfileId(deviceProfileId);
  200 + firmware.setTitle(TITLE);
  201 + firmware.setVersion(VERSION);
  202 + firmware.setFileName(FILE_NAME);
  203 + firmware.setContentType(CONTENT_TYPE);
  204 + firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
  205 + firmware.setChecksum(CHECKSUM);
  206 + firmware.setData(DATA);
  207 +
  208 + thrown.expect(DataValidationException.class);
  209 + thrown.expectMessage("Type should be specified!");
  210 + firmwareService.saveFirmware(firmware);
  211 + }
  212 +
  213 + @Test
152 214 public void testSaveFirmwareWithEmptyTitle() {
153 215 Firmware firmware = new Firmware();
154 216 firmware.setTenantId(tenantId);
  217 + firmware.setDeviceProfileId(deviceProfileId);
  218 + firmware.setType(FIRMWARE);
155 219 firmware.setVersion(VERSION);
156 220 firmware.setFileName(FILE_NAME);
157 221 firmware.setContentType(CONTENT_TYPE);
158 222 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
159 223 firmware.setChecksum(CHECKSUM);
160 224 firmware.setData(DATA);
  225 +
  226 + thrown.expect(DataValidationException.class);
  227 + thrown.expectMessage("Firmware title should be specified!");
161 228 firmwareService.saveFirmware(firmware);
162 229 }
163 230
164   - @Test(expected = DataValidationException.class)
  231 + @Test
165 232 public void testSaveFirmwareWithEmptyFileName() {
166 233 Firmware firmware = new Firmware();
167 234 firmware.setTenantId(tenantId);
  235 + firmware.setDeviceProfileId(deviceProfileId);
  236 + firmware.setType(FIRMWARE);
168 237 firmware.setTitle(TITLE);
169 238 firmware.setVersion(VERSION);
170 239 firmware.setContentType(CONTENT_TYPE);
171 240 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
172 241 firmware.setChecksum(CHECKSUM);
173 242 firmware.setData(DATA);
  243 +
  244 + thrown.expect(DataValidationException.class);
  245 + thrown.expectMessage("Firmware file name should be specified!");
174 246 firmwareService.saveFirmware(firmware);
175 247 }
176 248
177   - @Test(expected = DataValidationException.class)
  249 + @Test
178 250 public void testSaveFirmwareWithEmptyContentType() {
179 251 Firmware firmware = new Firmware();
180 252 firmware.setTenantId(tenantId);
  253 + firmware.setDeviceProfileId(deviceProfileId);
  254 + firmware.setType(FIRMWARE);
181 255 firmware.setTitle(TITLE);
182 256 firmware.setVersion(VERSION);
183 257 firmware.setFileName(FILE_NAME);
184 258 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
185 259 firmware.setChecksum(CHECKSUM);
186 260 firmware.setData(DATA);
  261 +
  262 + thrown.expect(DataValidationException.class);
  263 + thrown.expectMessage("Firmware content type should be specified!");
187 264 firmwareService.saveFirmware(firmware);
188 265 }
189 266
190   - @Test(expected = DataValidationException.class)
  267 + @Test
191 268 public void testSaveFirmwareWithEmptyData() {
192 269 Firmware firmware = new Firmware();
193 270 firmware.setTenantId(tenantId);
  271 + firmware.setDeviceProfileId(deviceProfileId);
  272 + firmware.setType(FIRMWARE);
194 273 firmware.setTitle(TITLE);
195 274 firmware.setVersion(VERSION);
196 275 firmware.setFileName(FILE_NAME);
197 276 firmware.setContentType(CONTENT_TYPE);
198 277 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
199 278 firmware.setChecksum(CHECKSUM);
  279 +
  280 + thrown.expect(DataValidationException.class);
  281 + thrown.expectMessage("Firmware data should be specified!");
200 282 firmwareService.saveFirmware(firmware);
201 283 }
202 284
203   - @Test(expected = DataValidationException.class)
  285 + @Test
204 286 public void testSaveFirmwareWithInvalidTenant() {
205 287 Firmware firmware = new Firmware();
206 288 firmware.setTenantId(new TenantId(Uuids.timeBased()));
  289 + firmware.setDeviceProfileId(deviceProfileId);
  290 + firmware.setType(FIRMWARE);
207 291 firmware.setTitle(TITLE);
208 292 firmware.setVersion(VERSION);
209 293 firmware.setFileName(FILE_NAME);
... ... @@ -211,41 +295,77 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
211 295 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
212 296 firmware.setChecksum(CHECKSUM);
213 297 firmware.setData(DATA);
  298 +
  299 + thrown.expect(DataValidationException.class);
  300 + thrown.expectMessage("Firmware is referencing to non-existent tenant!");
214 301 firmwareService.saveFirmware(firmware);
215 302 }
216 303
217   - @Test(expected = DataValidationException.class)
  304 + @Test
  305 + public void testSaveFirmwareWithInvalidDeviceProfileId() {
  306 + Firmware firmware = new Firmware();
  307 + firmware.setTenantId(tenantId);
  308 + firmware.setDeviceProfileId(new DeviceProfileId(Uuids.timeBased()));
  309 + firmware.setType(FIRMWARE);
  310 + firmware.setTitle(TITLE);
  311 + firmware.setVersion(VERSION);
  312 + firmware.setFileName(FILE_NAME);
  313 + firmware.setContentType(CONTENT_TYPE);
  314 + firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
  315 + firmware.setChecksum(CHECKSUM);
  316 + firmware.setData(DATA);
  317 +
  318 + thrown.expect(DataValidationException.class);
  319 + thrown.expectMessage("Firmware is referencing to non-existent device profile!");
  320 + firmwareService.saveFirmware(firmware);
  321 + }
  322 +
  323 + @Test
218 324 public void testSaveFirmwareWithEmptyChecksum() {
219 325 Firmware firmware = new Firmware();
220   - firmware.setTenantId(new TenantId(Uuids.timeBased()));
  326 + firmware.setTenantId(tenantId);
  327 + firmware.setDeviceProfileId(deviceProfileId);
  328 + firmware.setType(FIRMWARE);
221 329 firmware.setTitle(TITLE);
222 330 firmware.setVersion(VERSION);
223 331 firmware.setFileName(FILE_NAME);
224 332 firmware.setContentType(CONTENT_TYPE);
225 333 firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
226 334 firmware.setData(DATA);
  335 +
  336 + thrown.expect(DataValidationException.class);
  337 + thrown.expectMessage("Firmware checksum should be specified!");
227 338 firmwareService.saveFirmware(firmware);
228 339 }
229 340
230   - @Test(expected = DataValidationException.class)
  341 + @Test
231 342 public void testSaveFirmwareInfoWithExistingTitleAndVersion() {
232 343 FirmwareInfo firmwareInfo = new FirmwareInfo();
233 344 firmwareInfo.setTenantId(tenantId);
  345 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  346 + firmwareInfo.setType(FIRMWARE);
234 347 firmwareInfo.setTitle(TITLE);
235 348 firmwareInfo.setVersion(VERSION);
236 349 firmwareService.saveFirmwareInfo(firmwareInfo);
237 350
238 351 FirmwareInfo newFirmwareInfo = new FirmwareInfo();
239 352 newFirmwareInfo.setTenantId(tenantId);
  353 + newFirmwareInfo.setDeviceProfileId(deviceProfileId);
  354 + newFirmwareInfo.setType(FIRMWARE);
240 355 newFirmwareInfo.setTitle(TITLE);
241 356 newFirmwareInfo.setVersion(VERSION);
  357 +
  358 + thrown.expect(DataValidationException.class);
  359 + thrown.expectMessage("Firmware with such title and version already exists!");
242 360 firmwareService.saveFirmwareInfo(newFirmwareInfo);
243 361 }
244 362
245   - @Test(expected = DataValidationException.class)
  363 + @Test
246 364 public void testSaveFirmwareWithExistingTitleAndVersion() {
247 365 Firmware firmware = new Firmware();
248 366 firmware.setTenantId(tenantId);
  367 + firmware.setDeviceProfileId(deviceProfileId);
  368 + firmware.setType(FIRMWARE);
249 369 firmware.setTitle(TITLE);
250 370 firmware.setVersion(VERSION);
251 371 firmware.setFileName(FILE_NAME);
... ... @@ -257,18 +377,27 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
257 377
258 378 Firmware newFirmware = new Firmware();
259 379 newFirmware.setTenantId(tenantId);
  380 + newFirmware.setDeviceProfileId(deviceProfileId);
  381 + newFirmware.setType(FIRMWARE);
260 382 newFirmware.setTitle(TITLE);
261 383 newFirmware.setVersion(VERSION);
262 384 newFirmware.setFileName(FILE_NAME);
263 385 newFirmware.setContentType(CONTENT_TYPE);
  386 + newFirmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
  387 + newFirmware.setChecksum(CHECKSUM);
264 388 newFirmware.setData(DATA);
  389 +
  390 + thrown.expect(DataValidationException.class);
  391 + thrown.expectMessage("Firmware with such title and version already exists!");
265 392 firmwareService.saveFirmware(newFirmware);
266 393 }
267 394
268   - @Test(expected = DataValidationException.class)
  395 + @Test
269 396 public void testDeleteFirmwareWithReferenceByDevice() {
270 397 Firmware firmware = new Firmware();
271 398 firmware.setTenantId(tenantId);
  399 + firmware.setDeviceProfileId(deviceProfileId);
  400 + firmware.setType(FIRMWARE);
272 401 firmware.setTitle(TITLE);
273 402 firmware.setVersion(VERSION);
274 403 firmware.setFileName(FILE_NAME);
... ... @@ -281,11 +410,13 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
281 410 Device device = new Device();
282 411 device.setTenantId(tenantId);
283 412 device.setName("My device");
284   - device.setType("default");
  413 + device.setDeviceProfileId(deviceProfileId);
285 414 device.setFirmwareId(savedFirmware.getId());
286 415 Device savedDevice = deviceService.saveDevice(device);
287 416
288 417 try {
  418 + thrown.expect(DataValidationException.class);
  419 + thrown.expectMessage("The firmware referenced by the devices cannot be deleted!");
289 420 firmwareService.deleteFirmware(tenantId, savedFirmware.getId());
290 421 } finally {
291 422 deviceService.deleteDevice(tenantId, savedDevice.getId());
... ... @@ -293,10 +424,15 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
293 424 }
294 425 }
295 426
296   - @Test(expected = DataValidationException.class)
  427 + @Test
297 428 public void testDeleteFirmwareWithReferenceByDeviceProfile() {
  429 + DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Test Device Profile");
  430 + DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
  431 +
298 432 Firmware firmware = new Firmware();
299 433 firmware.setTenantId(tenantId);
  434 + firmware.setDeviceProfileId(savedDeviceProfile.getId());
  435 + firmware.setType(FIRMWARE);
300 436 firmware.setTitle(TITLE);
301 437 firmware.setVersion(VERSION);
302 438 firmware.setFileName(FILE_NAME);
... ... @@ -306,11 +442,12 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
306 442 firmware.setData(DATA);
307 443 Firmware savedFirmware = firmwareService.saveFirmware(firmware);
308 444
309   - DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
310   - deviceProfile.setFirmwareId(savedFirmware.getId());
311   - DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
  445 + savedDeviceProfile.setFirmwareId(savedFirmware.getId());
  446 + deviceProfileService.saveDeviceProfile(savedDeviceProfile);
312 447
313 448 try {
  449 + thrown.expect(DataValidationException.class);
  450 + thrown.expectMessage("The firmware referenced by the device profile cannot be deleted!");
314 451 firmwareService.deleteFirmware(tenantId, savedFirmware.getId());
315 452 } finally {
316 453 deviceProfileService.deleteDeviceProfile(tenantId, savedDeviceProfile.getId());
... ... @@ -322,6 +459,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
322 459 public void testFindFirmwareById() {
323 460 Firmware firmware = new Firmware();
324 461 firmware.setTenantId(tenantId);
  462 + firmware.setDeviceProfileId(deviceProfileId);
  463 + firmware.setType(FIRMWARE);
325 464 firmware.setTitle(TITLE);
326 465 firmware.setVersion(VERSION);
327 466 firmware.setFileName(FILE_NAME);
... ... @@ -341,6 +480,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
341 480 public void testFindFirmwareInfoById() {
342 481 FirmwareInfo firmware = new FirmwareInfo();
343 482 firmware.setTenantId(tenantId);
  483 + firmware.setDeviceProfileId(deviceProfileId);
  484 + firmware.setType(FIRMWARE);
344 485 firmware.setTitle(TITLE);
345 486 firmware.setVersion(VERSION);
346 487 FirmwareInfo savedFirmware = firmwareService.saveFirmwareInfo(firmware);
... ... @@ -355,6 +496,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
355 496 public void testDeleteFirmware() {
356 497 Firmware firmware = new Firmware();
357 498 firmware.setTenantId(tenantId);
  499 + firmware.setDeviceProfileId(deviceProfileId);
  500 + firmware.setType(FIRMWARE);
358 501 firmware.setTitle(TITLE);
359 502 firmware.setVersion(VERSION);
360 503 firmware.setFileName(FILE_NAME);
... ... @@ -377,6 +520,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
377 520 for (int i = 0; i < 165; i++) {
378 521 Firmware firmware = new Firmware();
379 522 firmware.setTenantId(tenantId);
  523 + firmware.setDeviceProfileId(deviceProfileId);
  524 + firmware.setType(FIRMWARE);
380 525 firmware.setTitle(TITLE);
381 526 firmware.setVersion(VERSION + i);
382 527 firmware.setFileName(FILE_NAME);
... ... @@ -420,6 +565,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
420 565 for (int i = 0; i < 165; i++) {
421 566 FirmwareInfo firmwareInfo = new FirmwareInfo();
422 567 firmwareInfo.setTenantId(tenantId);
  568 + firmwareInfo.setDeviceProfileId(deviceProfileId);
  569 + firmwareInfo.setType(FIRMWARE);
423 570 firmwareInfo.setTitle(TITLE);
424 571 firmwareInfo.setVersion(VERSION + i);
425 572 firmwareInfo.setFileName(FILE_NAME);
... ... @@ -434,7 +581,7 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
434 581 PageLink pageLink = new PageLink(16);
435 582 PageData<FirmwareInfo> pageData;
436 583 do {
437   - pageData = firmwareService.findTenantFirmwaresByTenantIdAndHasData(tenantId, false, pageLink);
  584 + pageData = firmwareService.findTenantFirmwaresByTenantIdAndDeviceProfileIdAndTypeAndHasData(tenantId, deviceProfileId, FIRMWARE, false, pageLink);
438 585 loadedFirmwares.addAll(pageData.getData());
439 586 if (pageData.hasNext()) {
440 587 pageLink = pageLink.nextPageLink();
... ... @@ -450,6 +597,8 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
450 597 Firmware firmware = new Firmware(f.getId());
451 598 firmware.setCreatedTime(f.getCreatedTime());
452 599 firmware.setTenantId(f.getTenantId());
  600 + firmware.setDeviceProfileId(deviceProfileId);
  601 + firmware.setType(FIRMWARE);
453 602 firmware.setTitle(f.getTitle());
454 603 firmware.setVersion(f.getVersion());
455 604 firmware.setFileName(FILE_NAME);
... ... @@ -465,7 +614,7 @@ public abstract class BaseFirmwareServiceTest extends AbstractServiceTest {
465 614 loadedFirmwares = new ArrayList<>();
466 615 pageLink = new PageLink(16);
467 616 do {
468   - pageData = firmwareService.findTenantFirmwaresByTenantIdAndHasData(tenantId, true, pageLink);
  617 + pageData = firmwareService.findTenantFirmwaresByTenantIdAndDeviceProfileIdAndTypeAndHasData(tenantId, deviceProfileId, FIRMWARE, true, pageLink);
469 618 loadedFirmwares.addAll(pageData.getData());
470 619 if (pageData.hasNext()) {
471 620 pageLink = pageLink.nextPageLink();
... ...