Commit 2e6a98d8e02f54ec233ebd9c94d0d8f4e45def8b
1 parent
7841d117
Tenant/Device REST API controllers
Showing
20 changed files
with
1037 additions
and
34 deletions
... | ... | @@ -33,12 +33,14 @@ import org.thingsboard.server.common.data.DashboardInfo; |
33 | 33 | import org.thingsboard.server.common.data.DataConstants; |
34 | 34 | import org.thingsboard.server.common.data.Device; |
35 | 35 | import org.thingsboard.server.common.data.DeviceInfo; |
36 | +import org.thingsboard.server.common.data.DeviceProfile; | |
36 | 37 | import org.thingsboard.server.common.data.EntityType; |
37 | 38 | import org.thingsboard.server.common.data.EntityView; |
38 | 39 | import org.thingsboard.server.common.data.EntityViewInfo; |
39 | 40 | import org.thingsboard.server.common.data.HasName; |
40 | 41 | import org.thingsboard.server.common.data.HasTenantId; |
41 | 42 | import org.thingsboard.server.common.data.Tenant; |
43 | +import org.thingsboard.server.common.data.TenantProfile; | |
42 | 44 | import org.thingsboard.server.common.data.User; |
43 | 45 | import org.thingsboard.server.common.data.alarm.Alarm; |
44 | 46 | import org.thingsboard.server.common.data.alarm.AlarmInfo; |
... | ... | @@ -52,12 +54,14 @@ import org.thingsboard.server.common.data.id.AssetId; |
52 | 54 | import org.thingsboard.server.common.data.id.CustomerId; |
53 | 55 | import org.thingsboard.server.common.data.id.DashboardId; |
54 | 56 | import org.thingsboard.server.common.data.id.DeviceId; |
57 | +import org.thingsboard.server.common.data.id.DeviceProfileId; | |
55 | 58 | import org.thingsboard.server.common.data.id.EntityId; |
56 | 59 | import org.thingsboard.server.common.data.id.EntityIdFactory; |
57 | 60 | import org.thingsboard.server.common.data.id.EntityViewId; |
58 | 61 | import org.thingsboard.server.common.data.id.RuleChainId; |
59 | 62 | import org.thingsboard.server.common.data.id.RuleNodeId; |
60 | 63 | import org.thingsboard.server.common.data.id.TenantId; |
64 | +import org.thingsboard.server.common.data.id.TenantProfileId; | |
61 | 65 | import org.thingsboard.server.common.data.id.UserId; |
62 | 66 | import org.thingsboard.server.common.data.id.WidgetTypeId; |
63 | 67 | import org.thingsboard.server.common.data.id.WidgetsBundleId; |
... | ... | @@ -82,6 +86,7 @@ import org.thingsboard.server.dao.customer.CustomerService; |
82 | 86 | import org.thingsboard.server.dao.dashboard.DashboardService; |
83 | 87 | import org.thingsboard.server.dao.device.ClaimDevicesService; |
84 | 88 | import org.thingsboard.server.dao.device.DeviceCredentialsService; |
89 | +import org.thingsboard.server.dao.device.DeviceProfileService; | |
85 | 90 | import org.thingsboard.server.dao.device.DeviceService; |
86 | 91 | import org.thingsboard.server.dao.entityview.EntityViewService; |
87 | 92 | import org.thingsboard.server.dao.exception.DataValidationException; |
... | ... | @@ -89,6 +94,7 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException; |
89 | 94 | import org.thingsboard.server.dao.model.ModelConstants; |
90 | 95 | import org.thingsboard.server.dao.relation.RelationService; |
91 | 96 | import org.thingsboard.server.dao.rule.RuleChainService; |
97 | +import org.thingsboard.server.dao.tenant.TenantProfileService; | |
92 | 98 | import org.thingsboard.server.dao.tenant.TenantService; |
93 | 99 | import org.thingsboard.server.dao.user.UserService; |
94 | 100 | import org.thingsboard.server.dao.widget.WidgetTypeService; |
... | ... | @@ -135,6 +141,9 @@ public abstract class BaseController { |
135 | 141 | protected TenantService tenantService; |
136 | 142 | |
137 | 143 | @Autowired |
144 | + protected TenantProfileService tenantProfileService; | |
145 | + | |
146 | + @Autowired | |
138 | 147 | protected CustomerService customerService; |
139 | 148 | |
140 | 149 | @Autowired |
... | ... | @@ -144,6 +153,9 @@ public abstract class BaseController { |
144 | 153 | protected DeviceService deviceService; |
145 | 154 | |
146 | 155 | @Autowired |
156 | + protected DeviceProfileService deviceProfileService; | |
157 | + | |
158 | + @Autowired | |
147 | 159 | protected AssetService assetService; |
148 | 160 | |
149 | 161 | @Autowired |
... | ... | @@ -312,6 +324,18 @@ public abstract class BaseController { |
312 | 324 | } |
313 | 325 | } |
314 | 326 | |
327 | + TenantProfile checkTenantProfileId(TenantProfileId tenantProfileId, Operation operation) throws ThingsboardException { | |
328 | + try { | |
329 | + validateId(tenantProfileId, "Incorrect tenantProfileId " + tenantProfileId); | |
330 | + TenantProfile tenantProfile = tenantProfileService.findTenantProfileById(getTenantId(), tenantProfileId); | |
331 | + checkNotNull(tenantProfile); | |
332 | + accessControlService.checkPermission(getCurrentUser(), Resource.TENANT_PROFILE, operation); | |
333 | + return tenantProfile; | |
334 | + } catch (Exception e) { | |
335 | + throw handleException(e, false); | |
336 | + } | |
337 | + } | |
338 | + | |
315 | 339 | protected TenantId getTenantId() throws ThingsboardException { |
316 | 340 | return getCurrentUser().getTenantId(); |
317 | 341 | } |
... | ... | @@ -360,12 +384,18 @@ public abstract class BaseController { |
360 | 384 | case DEVICE: |
361 | 385 | checkDeviceId(new DeviceId(entityId.getId()), operation); |
362 | 386 | return; |
387 | + case DEVICE_PROFILE: | |
388 | + checkDeviceProfileId(new DeviceProfileId(entityId.getId()), operation); | |
389 | + return; | |
363 | 390 | case CUSTOMER: |
364 | 391 | checkCustomerId(new CustomerId(entityId.getId()), operation); |
365 | 392 | return; |
366 | 393 | case TENANT: |
367 | 394 | checkTenantId(new TenantId(entityId.getId()), operation); |
368 | 395 | return; |
396 | + case TENANT_PROFILE: | |
397 | + checkTenantProfileId(new TenantProfileId(entityId.getId()), operation); | |
398 | + return; | |
369 | 399 | case RULE_CHAIN: |
370 | 400 | checkRuleChain(new RuleChainId(entityId.getId()), operation); |
371 | 401 | return; |
... | ... | @@ -422,6 +452,18 @@ public abstract class BaseController { |
422 | 452 | } |
423 | 453 | } |
424 | 454 | |
455 | + DeviceProfile checkDeviceProfileId(DeviceProfileId deviceProfileId, Operation operation) throws ThingsboardException { | |
456 | + try { | |
457 | + validateId(deviceProfileId, "Incorrect deviceProfileId " + deviceProfileId); | |
458 | + DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(getCurrentUser().getTenantId(), deviceProfileId); | |
459 | + checkNotNull(deviceProfile); | |
460 | + accessControlService.checkPermission(getCurrentUser(), Resource.DEVICE_PROFILE, operation, deviceProfileId, deviceProfile); | |
461 | + return deviceProfile; | |
462 | + } catch (Exception e) { | |
463 | + throw handleException(e, false); | |
464 | + } | |
465 | + } | |
466 | + | |
425 | 467 | protected EntityView checkEntityViewId(EntityViewId entityViewId, Operation operation) throws ThingsboardException { |
426 | 468 | try { |
427 | 469 | validateId(entityViewId, "Incorrect entityViewId " + entityViewId); | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.controller; | |
17 | + | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | +import org.springframework.http.HttpStatus; | |
20 | +import org.springframework.security.access.prepost.PreAuthorize; | |
21 | +import org.springframework.web.bind.annotation.PathVariable; | |
22 | +import org.springframework.web.bind.annotation.RequestBody; | |
23 | +import org.springframework.web.bind.annotation.RequestMapping; | |
24 | +import org.springframework.web.bind.annotation.RequestMethod; | |
25 | +import org.springframework.web.bind.annotation.RequestParam; | |
26 | +import org.springframework.web.bind.annotation.ResponseBody; | |
27 | +import org.springframework.web.bind.annotation.ResponseStatus; | |
28 | +import org.springframework.web.bind.annotation.RestController; | |
29 | +import org.thingsboard.server.common.data.DeviceProfile; | |
30 | +import org.thingsboard.server.common.data.EntityInfo; | |
31 | +import org.thingsboard.server.common.data.EntityType; | |
32 | +import org.thingsboard.server.common.data.audit.ActionType; | |
33 | +import org.thingsboard.server.common.data.exception.ThingsboardException; | |
34 | +import org.thingsboard.server.common.data.id.DeviceProfileId; | |
35 | +import org.thingsboard.server.common.data.page.PageData; | |
36 | +import org.thingsboard.server.common.data.page.PageLink; | |
37 | +import org.thingsboard.server.queue.util.TbCoreComponent; | |
38 | +import org.thingsboard.server.service.security.permission.Operation; | |
39 | +import org.thingsboard.server.service.security.permission.Resource; | |
40 | + | |
41 | +@RestController | |
42 | +@TbCoreComponent | |
43 | +@RequestMapping("/api") | |
44 | +@Slf4j | |
45 | +public class DeviceProfileController extends BaseController { | |
46 | + | |
47 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | |
48 | + @RequestMapping(value = "/deviceProfile/{deviceProfileId}", method = RequestMethod.GET) | |
49 | + @ResponseBody | |
50 | + public DeviceProfile getDeviceProfileById(@PathVariable("deviceProfileId") String strDeviceProfileId) throws ThingsboardException { | |
51 | + checkParameter("deviceProfileId", strDeviceProfileId); | |
52 | + try { | |
53 | + DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); | |
54 | + return checkDeviceProfileId(deviceProfileId, Operation.READ); | |
55 | + } catch (Exception e) { | |
56 | + throw handleException(e); | |
57 | + } | |
58 | + } | |
59 | + | |
60 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") | |
61 | + @RequestMapping(value = "/deviceProfileInfo/{deviceProfileId}", method = RequestMethod.GET) | |
62 | + @ResponseBody | |
63 | + public EntityInfo getDeviceProfileInfoById(@PathVariable("deviceProfileId") String strDeviceProfileId) throws ThingsboardException { | |
64 | + checkParameter("deviceProfileId", strDeviceProfileId); | |
65 | + try { | |
66 | + DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); | |
67 | + return checkNotNull(deviceProfileService.findDeviceProfileInfoById(getTenantId(), deviceProfileId)); | |
68 | + } catch (Exception e) { | |
69 | + throw handleException(e); | |
70 | + } | |
71 | + } | |
72 | + | |
73 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") | |
74 | + @RequestMapping(value = "/deviceProfileInfo/default", method = RequestMethod.GET) | |
75 | + @ResponseBody | |
76 | + public EntityInfo getDefaultDeviceProfileInfo() throws ThingsboardException { | |
77 | + try { | |
78 | + return checkNotNull(deviceProfileService.findDefaultDeviceProfileInfo(getTenantId())); | |
79 | + } catch (Exception e) { | |
80 | + throw handleException(e); | |
81 | + } | |
82 | + } | |
83 | + | |
84 | + @PreAuthorize("hasAuthority('TENANT_ADMIN')") | |
85 | + @RequestMapping(value = "/deviceProfile", method = RequestMethod.POST) | |
86 | + @ResponseBody | |
87 | + public DeviceProfile saveDeviceProfile(@RequestBody DeviceProfile deviceProfile) throws ThingsboardException { | |
88 | + try { | |
89 | + deviceProfile.setTenantId(getTenantId()); | |
90 | + | |
91 | + checkEntity(deviceProfile.getId(), deviceProfile, Resource.DEVICE_PROFILE); | |
92 | + | |
93 | + DeviceProfile savedDeviceProfile = checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile)); | |
94 | + | |
95 | + logEntityAction(savedDeviceProfile.getId(), savedDeviceProfile, | |
96 | + null, | |
97 | + savedDeviceProfile.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); | |
98 | + | |
99 | + return savedDeviceProfile; | |
100 | + } catch (Exception e) { | |
101 | + logEntityAction(emptyId(EntityType.DEVICE_PROFILE), deviceProfile, | |
102 | + null, deviceProfile.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e); | |
103 | + throw handleException(e); | |
104 | + } | |
105 | + } | |
106 | + | |
107 | + @PreAuthorize("hasAuthority('TENANT_ADMIN')") | |
108 | + @RequestMapping(value = "/deviceProfile/{deviceProfileId}", method = RequestMethod.DELETE) | |
109 | + @ResponseStatus(value = HttpStatus.OK) | |
110 | + public void deleteDeviceProfile(@PathVariable("deviceProfileId") String strDeviceProfileId) throws ThingsboardException { | |
111 | + checkParameter("deviceProfileId", strDeviceProfileId); | |
112 | + try { | |
113 | + DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); | |
114 | + DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.DELETE); | |
115 | + deviceProfileService.deleteDeviceProfile(getTenantId(), deviceProfileId); | |
116 | + | |
117 | + logEntityAction(deviceProfileId, deviceProfile, | |
118 | + null, | |
119 | + ActionType.DELETED, null, strDeviceProfileId); | |
120 | + | |
121 | + } catch (Exception e) { | |
122 | + logEntityAction(emptyId(EntityType.DEVICE_PROFILE), | |
123 | + null, | |
124 | + null, | |
125 | + ActionType.DELETED, e, strDeviceProfileId); | |
126 | + throw handleException(e); | |
127 | + } | |
128 | + } | |
129 | + | |
130 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | |
131 | + @RequestMapping(value = "/deviceProfile/{deviceProfileId}/default", method = RequestMethod.POST) | |
132 | + @ResponseBody | |
133 | + public DeviceProfile setDefaultDeviceProfile(@PathVariable("deviceProfileId") String strDeviceProfileId) throws ThingsboardException { | |
134 | + checkParameter("deviceProfileId", strDeviceProfileId); | |
135 | + try { | |
136 | + DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId)); | |
137 | + DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.WRITE); | |
138 | + DeviceProfile previousDefaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(getTenantId()); | |
139 | + if (deviceProfileService.setDefaultDeviceProfile(getTenantId(), deviceProfileId)) { | |
140 | + if (previousDefaultDeviceProfile != null) { | |
141 | + previousDefaultDeviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), previousDefaultDeviceProfile.getId()); | |
142 | + | |
143 | + logEntityAction(previousDefaultDeviceProfile.getId(), previousDefaultDeviceProfile, | |
144 | + null, ActionType.UPDATED, null); | |
145 | + } | |
146 | + deviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfileId); | |
147 | + | |
148 | + logEntityAction(deviceProfile.getId(), deviceProfile, | |
149 | + null, ActionType.UPDATED, null); | |
150 | + } | |
151 | + return deviceProfile; | |
152 | + } catch (Exception e) { | |
153 | + logEntityAction(emptyId(EntityType.DEVICE_PROFILE), | |
154 | + null, | |
155 | + null, | |
156 | + ActionType.UPDATED, e, strDeviceProfileId); | |
157 | + throw handleException(e); | |
158 | + } | |
159 | + } | |
160 | + | |
161 | + @PreAuthorize("hasAuthority('TENANT_ADMIN')") | |
162 | + @RequestMapping(value = "/deviceProfiles", params = {"pageSize", "page"}, method = RequestMethod.GET) | |
163 | + @ResponseBody | |
164 | + public PageData<DeviceProfile> getDeviceProfiles(@RequestParam int pageSize, | |
165 | + @RequestParam int page, | |
166 | + @RequestParam(required = false) String textSearch, | |
167 | + @RequestParam(required = false) String sortProperty, | |
168 | + @RequestParam(required = false) String sortOrder) throws ThingsboardException { | |
169 | + try { | |
170 | + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); | |
171 | + return checkNotNull(deviceProfileService.findDeviceProfiles(getTenantId(), pageLink)); | |
172 | + } catch (Exception e) { | |
173 | + throw handleException(e); | |
174 | + } | |
175 | + } | |
176 | + | |
177 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") | |
178 | + @RequestMapping(value = "/deviceProfileInfos", params = {"pageSize", "page"}, method = RequestMethod.GET) | |
179 | + @ResponseBody | |
180 | + public PageData<EntityInfo> getDeviceProfileInfos(@RequestParam int pageSize, | |
181 | + @RequestParam int page, | |
182 | + @RequestParam(required = false) String textSearch, | |
183 | + @RequestParam(required = false) String sortProperty, | |
184 | + @RequestParam(required = false) String sortOrder) throws ThingsboardException { | |
185 | + try { | |
186 | + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); | |
187 | + return checkNotNull(deviceProfileService.findDeviceProfileInfos(getTenantId(), pageLink)); | |
188 | + } catch (Exception e) { | |
189 | + throw handleException(e); | |
190 | + } | |
191 | + } | |
192 | +} | ... | ... |
... | ... | @@ -58,8 +58,7 @@ public class TenantController extends BaseController { |
58 | 58 | checkParameter("tenantId", strTenantId); |
59 | 59 | try { |
60 | 60 | TenantId tenantId = new TenantId(toUUID(strTenantId)); |
61 | - checkTenantId(tenantId, Operation.READ); | |
62 | - return checkNotNull(tenantService.findTenantById(tenantId)); | |
61 | + return checkTenantId(tenantId, Operation.READ); | |
63 | 62 | } catch (Exception e) { |
64 | 63 | throw handleException(e); |
65 | 64 | } | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.controller; | |
17 | + | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | +import org.springframework.http.HttpStatus; | |
20 | +import org.springframework.security.access.prepost.PreAuthorize; | |
21 | +import org.springframework.web.bind.annotation.PathVariable; | |
22 | +import org.springframework.web.bind.annotation.RequestBody; | |
23 | +import org.springframework.web.bind.annotation.RequestMapping; | |
24 | +import org.springframework.web.bind.annotation.RequestMethod; | |
25 | +import org.springframework.web.bind.annotation.RequestParam; | |
26 | +import org.springframework.web.bind.annotation.ResponseBody; | |
27 | +import org.springframework.web.bind.annotation.ResponseStatus; | |
28 | +import org.springframework.web.bind.annotation.RestController; | |
29 | +import org.thingsboard.server.common.data.EntityInfo; | |
30 | +import org.thingsboard.server.common.data.TenantProfile; | |
31 | +import org.thingsboard.server.common.data.exception.ThingsboardException; | |
32 | +import org.thingsboard.server.common.data.id.TenantProfileId; | |
33 | +import org.thingsboard.server.common.data.page.PageData; | |
34 | +import org.thingsboard.server.common.data.page.PageLink; | |
35 | +import org.thingsboard.server.queue.util.TbCoreComponent; | |
36 | +import org.thingsboard.server.service.security.permission.Operation; | |
37 | +import org.thingsboard.server.service.security.permission.Resource; | |
38 | + | |
39 | +@RestController | |
40 | +@TbCoreComponent | |
41 | +@RequestMapping("/api") | |
42 | +@Slf4j | |
43 | +public class TenantProfileController extends BaseController { | |
44 | + | |
45 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
46 | + @RequestMapping(value = "/tenantProfile/{tenantProfileId}", method = RequestMethod.GET) | |
47 | + @ResponseBody | |
48 | + public TenantProfile getTenantProfileById(@PathVariable("tenantProfileId") String strTenantProfileId) throws ThingsboardException { | |
49 | + checkParameter("tenantProfileId", strTenantProfileId); | |
50 | + try { | |
51 | + TenantProfileId tenantProfileId = new TenantProfileId(toUUID(strTenantProfileId)); | |
52 | + return checkTenantProfileId(tenantProfileId, Operation.READ); | |
53 | + } catch (Exception e) { | |
54 | + throw handleException(e); | |
55 | + } | |
56 | + } | |
57 | + | |
58 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
59 | + @RequestMapping(value = "/tenantProfileInfo/{tenantProfileId}", method = RequestMethod.GET) | |
60 | + @ResponseBody | |
61 | + public EntityInfo getTenantProfileInfoById(@PathVariable("tenantProfileId") String strTenantProfileId) throws ThingsboardException { | |
62 | + checkParameter("tenantProfileId", strTenantProfileId); | |
63 | + try { | |
64 | + TenantProfileId tenantProfileId = new TenantProfileId(toUUID(strTenantProfileId)); | |
65 | + return checkNotNull(tenantProfileService.findTenantProfileInfoById(getTenantId(), tenantProfileId)); | |
66 | + } catch (Exception e) { | |
67 | + throw handleException(e); | |
68 | + } | |
69 | + } | |
70 | + | |
71 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
72 | + @RequestMapping(value = "/tenantProfileInfo/default", method = RequestMethod.GET) | |
73 | + @ResponseBody | |
74 | + public EntityInfo getDefaultTenantProfileInfo() throws ThingsboardException { | |
75 | + try { | |
76 | + return checkNotNull(tenantProfileService.findDefaultTenantProfileInfo(getTenantId())); | |
77 | + } catch (Exception e) { | |
78 | + throw handleException(e); | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + @PreAuthorize("hasAuthority('SYS_ADMIN')") | |
83 | + @RequestMapping(value = "/tenantProfile", method = RequestMethod.POST) | |
84 | + @ResponseBody | |
85 | + public TenantProfile saveTenantProfile(@RequestBody TenantProfile tenantProfile) throws ThingsboardException { | |
86 | + try { | |
87 | + boolean newTenantProfile = tenantProfile.getId() == null; | |
88 | + if (newTenantProfile) { | |
89 | + accessControlService | |
90 | + .checkPermission(getCurrentUser(), Resource.TENANT_PROFILE, Operation.CREATE); | |
91 | + } else { | |
92 | + checkEntityId(tenantProfile.getId(), Operation.WRITE); | |
93 | + } | |
94 | + | |
95 | + tenantProfile = checkNotNull(tenantProfileService.saveTenantProfile(getTenantId(), tenantProfile)); | |
96 | + return tenantProfile; | |
97 | + } catch (Exception e) { | |
98 | + throw handleException(e); | |
99 | + } | |
100 | + } | |
101 | + | |
102 | + @PreAuthorize("hasAuthority('SYS_ADMIN')") | |
103 | + @RequestMapping(value = "/tenantProfile/{tenantProfileId}", method = RequestMethod.DELETE) | |
104 | + @ResponseStatus(value = HttpStatus.OK) | |
105 | + public void deleteTenantProfile(@PathVariable("tenantProfileId") String strTenantProfileId) throws ThingsboardException { | |
106 | + checkParameter("tenantProfileId", strTenantProfileId); | |
107 | + try { | |
108 | + TenantProfileId tenantProfileId = new TenantProfileId(toUUID(strTenantProfileId)); | |
109 | + checkTenantProfileId(tenantProfileId, Operation.DELETE); | |
110 | + tenantProfileService.deleteTenantProfile(getTenantId(), tenantProfileId); | |
111 | + } catch (Exception e) { | |
112 | + throw handleException(e); | |
113 | + } | |
114 | + } | |
115 | + | |
116 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
117 | + @RequestMapping(value = "/tenantProfile/{tenantProfileId}/default", method = RequestMethod.POST) | |
118 | + @ResponseBody | |
119 | + public TenantProfile setDefaultTenantProfile(@PathVariable("tenantProfileId") String strTenantProfileId) throws ThingsboardException { | |
120 | + checkParameter("tenantProfileId", strTenantProfileId); | |
121 | + try { | |
122 | + TenantProfileId tenantProfileId = new TenantProfileId(toUUID(strTenantProfileId)); | |
123 | + TenantProfile tenantProfile = checkTenantProfileId(tenantProfileId, Operation.WRITE); | |
124 | + tenantProfileService.setDefaultTenantProfile(getTenantId(), tenantProfileId); | |
125 | + return tenantProfile; | |
126 | + } catch (Exception e) { | |
127 | + throw handleException(e); | |
128 | + } | |
129 | + } | |
130 | + | |
131 | + @PreAuthorize("hasAuthority('SYS_ADMIN')") | |
132 | + @RequestMapping(value = "/tenantProfiles", params = {"pageSize", "page"}, method = RequestMethod.GET) | |
133 | + @ResponseBody | |
134 | + public PageData<TenantProfile> getTenantProfiles(@RequestParam int pageSize, | |
135 | + @RequestParam int page, | |
136 | + @RequestParam(required = false) String textSearch, | |
137 | + @RequestParam(required = false) String sortProperty, | |
138 | + @RequestParam(required = false) String sortOrder) throws ThingsboardException { | |
139 | + try { | |
140 | + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); | |
141 | + return checkNotNull(tenantProfileService.findTenantProfiles(getTenantId(), pageLink)); | |
142 | + } catch (Exception e) { | |
143 | + throw handleException(e); | |
144 | + } | |
145 | + } | |
146 | + | |
147 | + @PreAuthorize("hasAuthority('SYS_ADMIN')") | |
148 | + @RequestMapping(value = "/tenantProfileInfos", params = {"pageSize", "page"}, method = RequestMethod.GET) | |
149 | + @ResponseBody | |
150 | + public PageData<EntityInfo> getTenantProfileInfos(@RequestParam int pageSize, | |
151 | + @RequestParam int page, | |
152 | + @RequestParam(required = false) String textSearch, | |
153 | + @RequestParam(required = false) String sortProperty, | |
154 | + @RequestParam(required = false) String sortOrder) throws ThingsboardException { | |
155 | + try { | |
156 | + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); | |
157 | + return checkNotNull(tenantProfileService.findTenantProfileInfos(getTenantId(), pageLink)); | |
158 | + } catch (Exception e) { | |
159 | + throw handleException(e); | |
160 | + } | |
161 | + } | |
162 | +} | ... | ... |
... | ... | @@ -27,6 +27,7 @@ import org.springframework.web.context.request.async.DeferredResult; |
27 | 27 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
28 | 28 | import org.thingsboard.server.common.data.Customer; |
29 | 29 | import org.thingsboard.server.common.data.Device; |
30 | +import org.thingsboard.server.common.data.DeviceProfile; | |
30 | 31 | import org.thingsboard.server.common.data.EntityView; |
31 | 32 | import org.thingsboard.server.common.data.Tenant; |
32 | 33 | import org.thingsboard.server.common.data.User; |
... | ... | @@ -35,6 +36,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardException; |
35 | 36 | import org.thingsboard.server.common.data.id.AssetId; |
36 | 37 | import org.thingsboard.server.common.data.id.CustomerId; |
37 | 38 | import org.thingsboard.server.common.data.id.DeviceId; |
39 | +import org.thingsboard.server.common.data.id.DeviceProfileId; | |
38 | 40 | import org.thingsboard.server.common.data.id.EntityId; |
39 | 41 | import org.thingsboard.server.common.data.id.EntityIdFactory; |
40 | 42 | import org.thingsboard.server.common.data.id.EntityViewId; |
... | ... | @@ -48,6 +50,7 @@ import org.thingsboard.server.controller.HttpValidationCallback; |
48 | 50 | import org.thingsboard.server.dao.alarm.AlarmService; |
49 | 51 | import org.thingsboard.server.dao.asset.AssetService; |
50 | 52 | import org.thingsboard.server.dao.customer.CustomerService; |
53 | +import org.thingsboard.server.dao.device.DeviceProfileService; | |
51 | 54 | import org.thingsboard.server.dao.device.DeviceService; |
52 | 55 | import org.thingsboard.server.dao.entityview.EntityViewService; |
53 | 56 | import org.thingsboard.server.dao.rule.RuleChainService; |
... | ... | @@ -72,6 +75,7 @@ import java.util.function.BiConsumer; |
72 | 75 | @Component |
73 | 76 | public class AccessValidator { |
74 | 77 | |
78 | + public static final String ONLY_SYSTEM_ADMINISTRATOR_IS_ALLOWED_TO_PERFORM_THIS_OPERATION = "Only system administrator is allowed to perform this operation!"; | |
75 | 79 | public static final String CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION = "Customer user is not allowed to perform this operation!"; |
76 | 80 | public static final String SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION = "System administrator is not allowed to perform this operation!"; |
77 | 81 | public static final String DEVICE_WITH_REQUESTED_ID_NOT_FOUND = "Device with requested id wasn't found!"; |
... | ... | @@ -90,6 +94,9 @@ public class AccessValidator { |
90 | 94 | protected DeviceService deviceService; |
91 | 95 | |
92 | 96 | @Autowired |
97 | + protected DeviceProfileService deviceProfileService; | |
98 | + | |
99 | + @Autowired | |
93 | 100 | protected AssetService assetService; |
94 | 101 | |
95 | 102 | @Autowired |
... | ... | @@ -162,6 +169,9 @@ public class AccessValidator { |
162 | 169 | case DEVICE: |
163 | 170 | validateDevice(currentUser, operation, entityId, callback); |
164 | 171 | return; |
172 | + case DEVICE_PROFILE: | |
173 | + validateDeviceProfile(currentUser, operation, entityId, callback); | |
174 | + return; | |
165 | 175 | case ASSET: |
166 | 176 | validateAsset(currentUser, operation, entityId, callback); |
167 | 177 | return; |
... | ... | @@ -174,6 +184,9 @@ public class AccessValidator { |
174 | 184 | case TENANT: |
175 | 185 | validateTenant(currentUser, operation, entityId, callback); |
176 | 186 | return; |
187 | + case TENANT_PROFILE: | |
188 | + validateTenantProfile(currentUser, operation, entityId, callback); | |
189 | + return; | |
177 | 190 | case USER: |
178 | 191 | validateUser(currentUser, operation, entityId, callback); |
179 | 192 | return; |
... | ... | @@ -206,6 +219,24 @@ public class AccessValidator { |
206 | 219 | } |
207 | 220 | } |
208 | 221 | |
222 | + private void validateDeviceProfile(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) { | |
223 | + if (currentUser.isSystemAdmin()) { | |
224 | + callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); | |
225 | + } else { | |
226 | + DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(currentUser.getTenantId(), new DeviceProfileId(entityId.getId())); | |
227 | + if (deviceProfile == null) { | |
228 | + callback.onSuccess(ValidationResult.entityNotFound("Device profile with requested id wasn't found!")); | |
229 | + } else { | |
230 | + try { | |
231 | + accessControlService.checkPermission(currentUser, Resource.DEVICE_PROFILE, operation, entityId, deviceProfile); | |
232 | + } catch (ThingsboardException e) { | |
233 | + callback.onSuccess(ValidationResult.accessDenied(e.getMessage())); | |
234 | + } | |
235 | + callback.onSuccess(ValidationResult.ok(deviceProfile)); | |
236 | + } | |
237 | + } | |
238 | + } | |
239 | + | |
209 | 240 | private void validateAsset(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) { |
210 | 241 | if (currentUser.isSystemAdmin()) { |
211 | 242 | callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); |
... | ... | @@ -313,6 +344,14 @@ public class AccessValidator { |
313 | 344 | } |
314 | 345 | } |
315 | 346 | |
347 | + private void validateTenantProfile(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) { | |
348 | + if (currentUser.isSystemAdmin()) { | |
349 | + callback.onSuccess(ValidationResult.ok(null)); | |
350 | + } else { | |
351 | + callback.onSuccess(ValidationResult.accessDenied(ONLY_SYSTEM_ADMINISTRATOR_IS_ALLOWED_TO_PERFORM_THIS_OPERATION)); | |
352 | + } | |
353 | + } | |
354 | + | |
316 | 355 | private void validateUser(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) { |
317 | 356 | ListenableFuture<User> userFuture = userService.findUserByIdAsync(currentUser.getTenantId(), new UserId(entityId.getId())); |
318 | 357 | Futures.addCallback(userFuture, getCallback(callback, user -> { | ... | ... |
... | ... | @@ -31,7 +31,9 @@ public enum Resource { |
31 | 31 | RULE_CHAIN(EntityType.RULE_CHAIN), |
32 | 32 | USER(EntityType.USER), |
33 | 33 | WIDGETS_BUNDLE(EntityType.WIDGETS_BUNDLE), |
34 | - WIDGET_TYPE(EntityType.WIDGET_TYPE); | |
34 | + WIDGET_TYPE(EntityType.WIDGET_TYPE), | |
35 | + TENANT_PROFILE(EntityType.TENANT_PROFILE), | |
36 | + DEVICE_PROFILE(EntityType.DEVICE_PROFILE); | |
35 | 37 | |
36 | 38 | private final EntityType entityType; |
37 | 39 | ... | ... |
... | ... | @@ -39,6 +39,7 @@ public class SysAdminPermissions extends AbstractPermissions { |
39 | 39 | put(Resource.USER, userPermissionChecker); |
40 | 40 | put(Resource.WIDGETS_BUNDLE, systemEntityPermissionChecker); |
41 | 41 | put(Resource.WIDGET_TYPE, systemEntityPermissionChecker); |
42 | + put(Resource.TENANT_PROFILE, PermissionChecker.allowAllPermissionChecker); | |
42 | 43 | } |
43 | 44 | |
44 | 45 | private static final PermissionChecker systemEntityPermissionChecker = new PermissionChecker() { | ... | ... |
... | ... | @@ -42,6 +42,7 @@ public class TenantAdminPermissions extends AbstractPermissions { |
42 | 42 | put(Resource.USER, userPermissionChecker); |
43 | 43 | put(Resource.WIDGETS_BUNDLE, widgetsPermissionChecker); |
44 | 44 | put(Resource.WIDGET_TYPE, widgetsPermissionChecker); |
45 | + put(Resource.DEVICE_PROFILE, tenantEntityPermissionChecker); | |
45 | 46 | } |
46 | 47 | |
47 | 48 | public static final PermissionChecker tenantEntityPermissionChecker = new PermissionChecker() { | ... | ... |
... | ... | @@ -489,6 +489,7 @@ audit-log: |
489 | 489 | "rule_chain": "${AUDIT_LOG_MASK_RULE_CHAIN:W}" |
490 | 490 | "alarm": "${AUDIT_LOG_MASK_ALARM:W}" |
491 | 491 | "entity_view": "${AUDIT_LOG_MASK_ENTITY_VIEW:W}" |
492 | + "device_profile": "${AUDIT_LOG_MASK_DEVICE_PROFILE:W}" | |
492 | 493 | sink: |
493 | 494 | # Type of external sink. possible options: none, elasticsearch |
494 | 495 | type: "${AUDIT_LOG_SINK_TYPE:none}" | ... | ... |
... | ... | @@ -61,6 +61,7 @@ import org.thingsboard.server.common.data.BaseData; |
61 | 61 | import org.thingsboard.server.common.data.Customer; |
62 | 62 | import org.thingsboard.server.common.data.Tenant; |
63 | 63 | import org.thingsboard.server.common.data.User; |
64 | +import org.thingsboard.server.common.data.id.HasId; | |
64 | 65 | import org.thingsboard.server.common.data.id.TenantId; |
65 | 66 | import org.thingsboard.server.common.data.id.UUIDBased; |
66 | 67 | import org.thingsboard.server.common.data.page.PageLink; |
... | ... | @@ -486,7 +487,7 @@ public abstract class AbstractWebTest { |
486 | 487 | return mapper.readerFor(type).readValue(content); |
487 | 488 | } |
488 | 489 | |
489 | - public class IdComparator<D extends BaseData<? extends UUIDBased>> implements Comparator<D> { | |
490 | + public class IdComparator<D extends HasId> implements Comparator<D> { | |
490 | 491 | @Override |
491 | 492 | public int compare(D o1, D o2) { |
492 | 493 | return o1.getId().getId().compareTo(o2.getId().getId()); | ... | ... |
application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.controller; | |
17 | + | |
18 | +import com.datastax.oss.driver.api.core.uuid.Uuids; | |
19 | +import com.fasterxml.jackson.core.type.TypeReference; | |
20 | +import org.junit.After; | |
21 | +import org.junit.Assert; | |
22 | +import org.junit.Before; | |
23 | +import org.junit.Test; | |
24 | +import org.thingsboard.server.common.data.DeviceProfile; | |
25 | +import org.thingsboard.server.common.data.EntityInfo; | |
26 | +import org.thingsboard.server.common.data.Tenant; | |
27 | +import org.thingsboard.server.common.data.User; | |
28 | +import org.thingsboard.server.common.data.id.RuleChainId; | |
29 | +import org.thingsboard.server.common.data.page.PageData; | |
30 | +import org.thingsboard.server.common.data.page.PageLink; | |
31 | +import org.thingsboard.server.common.data.security.Authority; | |
32 | +import org.thingsboard.server.dao.util.mapping.JacksonUtil; | |
33 | + | |
34 | +import java.util.ArrayList; | |
35 | +import java.util.Collections; | |
36 | +import java.util.List; | |
37 | +import java.util.stream.Collectors; | |
38 | + | |
39 | +import static org.hamcrest.Matchers.containsString; | |
40 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
41 | + | |
42 | +public abstract class BaseDeviceProfileControllerTest extends AbstractControllerTest { | |
43 | + | |
44 | + private IdComparator<DeviceProfile> idComparator = new IdComparator<>(); | |
45 | + private IdComparator<EntityInfo> deviceProfileInfoIdComparator = new IdComparator<>(); | |
46 | + | |
47 | + private Tenant savedTenant; | |
48 | + private User tenantAdmin; | |
49 | + | |
50 | + @Before | |
51 | + public void beforeTest() throws Exception { | |
52 | + loginSysAdmin(); | |
53 | + | |
54 | + Tenant tenant = new Tenant(); | |
55 | + tenant.setTitle("My tenant"); | |
56 | + savedTenant = doPost("/api/tenant", tenant, Tenant.class); | |
57 | + Assert.assertNotNull(savedTenant); | |
58 | + | |
59 | + tenantAdmin = new User(); | |
60 | + tenantAdmin.setAuthority(Authority.TENANT_ADMIN); | |
61 | + tenantAdmin.setTenantId(savedTenant.getId()); | |
62 | + tenantAdmin.setEmail("tenant2@thingsboard.org"); | |
63 | + tenantAdmin.setFirstName("Joe"); | |
64 | + tenantAdmin.setLastName("Downs"); | |
65 | + | |
66 | + tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1"); | |
67 | + } | |
68 | + | |
69 | + @After | |
70 | + public void afterTest() throws Exception { | |
71 | + loginSysAdmin(); | |
72 | + | |
73 | + doDelete("/api/tenant/" + savedTenant.getId().getId().toString()) | |
74 | + .andExpect(status().isOk()); | |
75 | + } | |
76 | + | |
77 | + @Test | |
78 | + public void testSaveDeviceProfile() throws Exception { | |
79 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); | |
80 | + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); | |
81 | + Assert.assertNotNull(savedDeviceProfile); | |
82 | + Assert.assertNotNull(savedDeviceProfile.getId()); | |
83 | + Assert.assertTrue(savedDeviceProfile.getCreatedTime() > 0); | |
84 | + Assert.assertEquals(deviceProfile.getName(), savedDeviceProfile.getName()); | |
85 | + Assert.assertEquals(deviceProfile.getDescription(), savedDeviceProfile.getDescription()); | |
86 | + Assert.assertEquals(deviceProfile.getProfileData(), savedDeviceProfile.getProfileData()); | |
87 | + Assert.assertEquals(deviceProfile.isDefault(), savedDeviceProfile.isDefault()); | |
88 | + Assert.assertEquals(deviceProfile.getDefaultRuleChainId(), savedDeviceProfile.getDefaultRuleChainId()); | |
89 | + savedDeviceProfile.setName("New device profile"); | |
90 | + doPost("/api/deviceProfile", savedDeviceProfile, DeviceProfile.class); | |
91 | + DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/"+savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); | |
92 | + Assert.assertEquals(savedDeviceProfile.getName(), foundDeviceProfile.getName()); | |
93 | + } | |
94 | + | |
95 | + @Test | |
96 | + public void testFindDeviceProfileById() throws Exception { | |
97 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); | |
98 | + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); | |
99 | + DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/"+savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); | |
100 | + Assert.assertNotNull(foundDeviceProfile); | |
101 | + Assert.assertEquals(savedDeviceProfile, foundDeviceProfile); | |
102 | + } | |
103 | + | |
104 | + @Test | |
105 | + public void testFindDeviceProfileInfoById() throws Exception { | |
106 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); | |
107 | + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); | |
108 | + EntityInfo foundDeviceProfileInfo = doGet("/api/deviceProfileInfo/"+savedDeviceProfile.getId().getId().toString(), EntityInfo.class); | |
109 | + Assert.assertNotNull(foundDeviceProfileInfo); | |
110 | + Assert.assertEquals(savedDeviceProfile.getId(), foundDeviceProfileInfo.getId()); | |
111 | + Assert.assertEquals(savedDeviceProfile.getName(), foundDeviceProfileInfo.getName()); | |
112 | + } | |
113 | + | |
114 | + @Test | |
115 | + public void testFindDefaultDeviceProfileInfo() throws Exception { | |
116 | + EntityInfo foundDefaultDeviceProfileInfo = doGet("/api/deviceProfileInfo/default", EntityInfo.class); | |
117 | + Assert.assertNotNull(foundDefaultDeviceProfileInfo); | |
118 | + Assert.assertNotNull(foundDefaultDeviceProfileInfo.getId()); | |
119 | + Assert.assertNotNull(foundDefaultDeviceProfileInfo.getName()); | |
120 | + Assert.assertEquals("Default", foundDefaultDeviceProfileInfo.getName()); | |
121 | + } | |
122 | + | |
123 | + @Test | |
124 | + public void testSetDefaultDeviceProfile() throws Exception { | |
125 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile 1"); | |
126 | + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); | |
127 | + DeviceProfile defaultDeviceProfile = doPost("/api/deviceProfile/"+savedDeviceProfile.getId().getId().toString()+"/default", null, DeviceProfile.class); | |
128 | + Assert.assertNotNull(defaultDeviceProfile); | |
129 | + EntityInfo foundDefaultDeviceProfile = doGet("/api/deviceProfileInfo/default", EntityInfo.class); | |
130 | + Assert.assertNotNull(foundDefaultDeviceProfile); | |
131 | + Assert.assertEquals(savedDeviceProfile.getName(), foundDefaultDeviceProfile.getName()); | |
132 | + Assert.assertEquals(savedDeviceProfile.getId(), foundDefaultDeviceProfile.getId()); | |
133 | + } | |
134 | + | |
135 | + @Test | |
136 | + public void testSaveDeviceProfileWithEmptyName() throws Exception { | |
137 | + DeviceProfile deviceProfile = new DeviceProfile(); | |
138 | + doPost("/api/deviceProfile", deviceProfile).andExpect(status().isBadRequest()) | |
139 | + .andExpect(statusReason(containsString("Device profile name should be specified"))); | |
140 | + } | |
141 | + | |
142 | + @Test | |
143 | + public void testSaveDeviceProfileWithSameName() throws Exception { | |
144 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); | |
145 | + doPost("/api/deviceProfile", deviceProfile).andExpect(status().isOk()); | |
146 | + DeviceProfile deviceProfile2 = this.createDeviceProfile("Device Profile"); | |
147 | + doPost("/api/deviceProfile", deviceProfile2).andExpect(status().isBadRequest()) | |
148 | + .andExpect(statusReason(containsString("Device profile with such name already exists"))); | |
149 | + } | |
150 | + | |
151 | + @Test | |
152 | + public void testDeleteDeviceProfile() throws Exception { | |
153 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); | |
154 | + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); | |
155 | + | |
156 | + doDelete("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString()) | |
157 | + .andExpect(status().isOk()); | |
158 | + | |
159 | + doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString()) | |
160 | + .andExpect(status().isNotFound()); | |
161 | + } | |
162 | + | |
163 | + @Test | |
164 | + public void testFindDeviceProfiles() throws Exception { | |
165 | + List<DeviceProfile> deviceProfiles = new ArrayList<>(); | |
166 | + PageLink pageLink = new PageLink(17); | |
167 | + PageData<DeviceProfile> pageData = doGetTypedWithPageLink("/api/deviceProfiles?", | |
168 | + new TypeReference<PageData<DeviceProfile>>(){}, pageLink); | |
169 | + Assert.assertFalse(pageData.hasNext()); | |
170 | + Assert.assertEquals(1, pageData.getTotalElements()); | |
171 | + deviceProfiles.addAll(pageData.getData()); | |
172 | + | |
173 | + for (int i=0;i<28;i++) { | |
174 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"+i); | |
175 | + deviceProfiles.add(doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class)); | |
176 | + } | |
177 | + | |
178 | + List<DeviceProfile> loadedDeviceProfiles = new ArrayList<>(); | |
179 | + pageLink = new PageLink(17); | |
180 | + do { | |
181 | + pageData = doGetTypedWithPageLink("/api/deviceProfiles?", | |
182 | + new TypeReference<PageData<DeviceProfile>>(){}, pageLink); | |
183 | + loadedDeviceProfiles.addAll(pageData.getData()); | |
184 | + if (pageData.hasNext()) { | |
185 | + pageLink = pageLink.nextPageLink(); | |
186 | + } | |
187 | + } while (pageData.hasNext()); | |
188 | + | |
189 | + Collections.sort(deviceProfiles, idComparator); | |
190 | + Collections.sort(loadedDeviceProfiles, idComparator); | |
191 | + | |
192 | + Assert.assertEquals(deviceProfiles, loadedDeviceProfiles); | |
193 | + | |
194 | + for (DeviceProfile deviceProfile : loadedDeviceProfiles) { | |
195 | + if (!deviceProfile.isDefault()) { | |
196 | + doDelete("/api/deviceProfile/" + deviceProfile.getId().getId().toString()) | |
197 | + .andExpect(status().isOk()); | |
198 | + } | |
199 | + } | |
200 | + | |
201 | + pageLink = new PageLink(17); | |
202 | + pageData = doGetTypedWithPageLink("/api/deviceProfiles?", | |
203 | + new TypeReference<PageData<DeviceProfile>>(){}, pageLink); | |
204 | + Assert.assertFalse(pageData.hasNext()); | |
205 | + Assert.assertEquals(1, pageData.getTotalElements()); | |
206 | + } | |
207 | + | |
208 | + @Test | |
209 | + public void testFindDeviceProfileInfos() throws Exception { | |
210 | + List<DeviceProfile> deviceProfiles = new ArrayList<>(); | |
211 | + PageLink pageLink = new PageLink(17); | |
212 | + PageData<DeviceProfile> deviceProfilePageData = doGetTypedWithPageLink("/api/deviceProfiles?", | |
213 | + new TypeReference<PageData<DeviceProfile>>(){}, pageLink); | |
214 | + Assert.assertFalse(deviceProfilePageData.hasNext()); | |
215 | + Assert.assertEquals(1, deviceProfilePageData.getTotalElements()); | |
216 | + deviceProfiles.addAll(deviceProfilePageData.getData()); | |
217 | + | |
218 | + for (int i=0;i<28;i++) { | |
219 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"+i); | |
220 | + deviceProfiles.add(doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class)); | |
221 | + } | |
222 | + | |
223 | + List<EntityInfo> loadedDeviceProfileInfos = new ArrayList<>(); | |
224 | + pageLink = new PageLink(17); | |
225 | + PageData<EntityInfo> pageData; | |
226 | + do { | |
227 | + pageData = doGetTypedWithPageLink("/api/deviceProfileInfos?", | |
228 | + new TypeReference<PageData<EntityInfo>>(){}, pageLink); | |
229 | + loadedDeviceProfileInfos.addAll(pageData.getData()); | |
230 | + if (pageData.hasNext()) { | |
231 | + pageLink = pageLink.nextPageLink(); | |
232 | + } | |
233 | + } while (pageData.hasNext()); | |
234 | + | |
235 | + Collections.sort(deviceProfiles, idComparator); | |
236 | + Collections.sort(loadedDeviceProfileInfos, deviceProfileInfoIdComparator); | |
237 | + | |
238 | + List<EntityInfo> deviceProfileInfos = deviceProfiles.stream().map(deviceProfile -> new EntityInfo(deviceProfile.getId(), | |
239 | + deviceProfile.getName())).collect(Collectors.toList()); | |
240 | + | |
241 | + Assert.assertEquals(deviceProfileInfos, loadedDeviceProfileInfos); | |
242 | + | |
243 | + for (DeviceProfile deviceProfile : deviceProfiles) { | |
244 | + if (!deviceProfile.isDefault()) { | |
245 | + doDelete("/api/deviceProfile/" + deviceProfile.getId().getId().toString()) | |
246 | + .andExpect(status().isOk()); | |
247 | + } | |
248 | + } | |
249 | + | |
250 | + pageLink = new PageLink(17); | |
251 | + pageData = doGetTypedWithPageLink("/api/deviceProfileInfos?", | |
252 | + new TypeReference<PageData<EntityInfo>>(){}, pageLink); | |
253 | + Assert.assertFalse(pageData.hasNext()); | |
254 | + Assert.assertEquals(1, pageData.getTotalElements()); | |
255 | + } | |
256 | + | |
257 | + private DeviceProfile createDeviceProfile(String name) { | |
258 | + DeviceProfile deviceProfile = new DeviceProfile(); | |
259 | + deviceProfile.setName(name); | |
260 | + deviceProfile.setDescription(name + " Test"); | |
261 | + deviceProfile.setProfileData(JacksonUtil.OBJECT_MAPPER.createObjectNode()); | |
262 | + deviceProfile.setDefault(false); | |
263 | + deviceProfile.setDefaultRuleChainId(new RuleChainId(Uuids.timeBased())); | |
264 | + return deviceProfile; | |
265 | + } | |
266 | +} | ... | ... |
application/src/test/java/org/thingsboard/server/controller/BaseTenantProfileControllerTest.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.controller; | |
17 | + | |
18 | +import com.fasterxml.jackson.core.type.TypeReference; | |
19 | +import org.junit.After; | |
20 | +import org.junit.Assert; | |
21 | +import org.junit.Test; | |
22 | +import org.springframework.beans.factory.annotation.Autowired; | |
23 | +import org.thingsboard.server.common.data.Device; | |
24 | +import org.thingsboard.server.common.data.EntityInfo; | |
25 | +import org.thingsboard.server.common.data.Tenant; | |
26 | +import org.thingsboard.server.common.data.TenantProfile; | |
27 | +import org.thingsboard.server.common.data.id.TenantId; | |
28 | +import org.thingsboard.server.common.data.page.PageData; | |
29 | +import org.thingsboard.server.common.data.page.PageLink; | |
30 | +import org.thingsboard.server.dao.exception.DataValidationException; | |
31 | +import org.thingsboard.server.dao.tenant.TenantProfileService; | |
32 | +import org.thingsboard.server.dao.util.mapping.JacksonUtil; | |
33 | + | |
34 | +import java.util.ArrayList; | |
35 | +import java.util.Collections; | |
36 | +import java.util.List; | |
37 | +import java.util.stream.Collectors; | |
38 | + | |
39 | +import static org.hamcrest.Matchers.containsString; | |
40 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
41 | + | |
42 | +public abstract class BaseTenantProfileControllerTest extends AbstractControllerTest { | |
43 | + | |
44 | + private IdComparator<TenantProfile> idComparator = new IdComparator<>(); | |
45 | + private IdComparator<EntityInfo> tenantProfileInfoIdComparator = new IdComparator<>(); | |
46 | + | |
47 | + @Autowired | |
48 | + private TenantProfileService tenantProfileService; | |
49 | + | |
50 | + @After | |
51 | + public void after() { | |
52 | + tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
53 | + } | |
54 | + | |
55 | + @Test | |
56 | + public void testSaveTenantProfile() throws Exception { | |
57 | + loginSysAdmin(); | |
58 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); | |
59 | + TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class); | |
60 | + Assert.assertNotNull(savedTenantProfile); | |
61 | + Assert.assertNotNull(savedTenantProfile.getId()); | |
62 | + Assert.assertTrue(savedTenantProfile.getCreatedTime() > 0); | |
63 | + Assert.assertEquals(tenantProfile.getName(), savedTenantProfile.getName()); | |
64 | + Assert.assertEquals(tenantProfile.getDescription(), savedTenantProfile.getDescription()); | |
65 | + Assert.assertEquals(tenantProfile.getProfileData(), savedTenantProfile.getProfileData()); | |
66 | + Assert.assertEquals(tenantProfile.isDefault(), savedTenantProfile.isDefault()); | |
67 | + Assert.assertEquals(tenantProfile.isIsolatedTbCore(), savedTenantProfile.isIsolatedTbCore()); | |
68 | + Assert.assertEquals(tenantProfile.isIsolatedTbRuleEngine(), savedTenantProfile.isIsolatedTbRuleEngine()); | |
69 | + | |
70 | + savedTenantProfile.setName("New tenant profile"); | |
71 | + doPost("/api/tenantProfile", savedTenantProfile, TenantProfile.class); | |
72 | + TenantProfile foundTenantProfile = doGet("/api/tenantProfile/"+savedTenantProfile.getId().getId().toString(), TenantProfile.class); | |
73 | + Assert.assertEquals(foundTenantProfile.getName(), savedTenantProfile.getName()); | |
74 | + } | |
75 | + | |
76 | + @Test | |
77 | + public void testFindTenantProfileById() throws Exception { | |
78 | + loginSysAdmin(); | |
79 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); | |
80 | + TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class); | |
81 | + TenantProfile foundTenantProfile = doGet("/api/tenantProfile/"+savedTenantProfile.getId().getId().toString(), TenantProfile.class); | |
82 | + Assert.assertNotNull(foundTenantProfile); | |
83 | + Assert.assertEquals(savedTenantProfile, foundTenantProfile); | |
84 | + } | |
85 | + | |
86 | + @Test | |
87 | + public void testFindTenantProfileInfoById() throws Exception { | |
88 | + loginSysAdmin(); | |
89 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); | |
90 | + TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class); | |
91 | + EntityInfo foundTenantProfileInfo = doGet("/api/tenantProfileInfo/"+savedTenantProfile.getId().getId().toString(), EntityInfo.class); | |
92 | + Assert.assertNotNull(foundTenantProfileInfo); | |
93 | + Assert.assertEquals(savedTenantProfile.getId(), foundTenantProfileInfo.getId()); | |
94 | + Assert.assertEquals(savedTenantProfile.getName(), foundTenantProfileInfo.getName()); | |
95 | + } | |
96 | + | |
97 | + @Test | |
98 | + public void testFindDefaultTenantProfileInfo() throws Exception { | |
99 | + loginSysAdmin(); | |
100 | + EntityInfo foundDefaultTenantProfile = doGet("/api/tenantProfileInfo/default", EntityInfo.class); | |
101 | + Assert.assertNotNull(foundDefaultTenantProfile); | |
102 | + Assert.assertEquals("Default", foundDefaultTenantProfile.getName()); | |
103 | + } | |
104 | + | |
105 | + @Test | |
106 | + public void testSetDefaultTenantProfile() throws Exception { | |
107 | + loginSysAdmin(); | |
108 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile 1"); | |
109 | + TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class); | |
110 | + TenantProfile defaultTenantProfile = doPost("/api/tenantProfile/"+savedTenantProfile.getId().getId().toString()+"/default", null, TenantProfile.class); | |
111 | + Assert.assertNotNull(defaultTenantProfile); | |
112 | + EntityInfo foundDefaultTenantProfile = doGet("/api/tenantProfileInfo/default", EntityInfo.class); | |
113 | + Assert.assertNotNull(foundDefaultTenantProfile); | |
114 | + Assert.assertEquals(savedTenantProfile.getName(), foundDefaultTenantProfile.getName()); | |
115 | + Assert.assertEquals(savedTenantProfile.getId(), foundDefaultTenantProfile.getId()); | |
116 | + } | |
117 | + | |
118 | + @Test | |
119 | + public void testSaveTenantProfileWithEmptyName() throws Exception { | |
120 | + loginSysAdmin(); | |
121 | + TenantProfile tenantProfile = new TenantProfile(); | |
122 | + doPost("/api/tenantProfile", tenantProfile).andExpect(status().isBadRequest()) | |
123 | + .andExpect(statusReason(containsString("Tenant profile name should be specified"))); | |
124 | + } | |
125 | + | |
126 | + @Test | |
127 | + public void testSaveTenantProfileWithSameName() throws Exception { | |
128 | + loginSysAdmin(); | |
129 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); | |
130 | + doPost("/api/tenantProfile", tenantProfile).andExpect(status().isOk()); | |
131 | + TenantProfile tenantProfile2 = this.createTenantProfile("Tenant Profile"); | |
132 | + doPost("/api/tenantProfile", tenantProfile2).andExpect(status().isBadRequest()) | |
133 | + .andExpect(statusReason(containsString("Tenant profile with such name already exists"))); | |
134 | + } | |
135 | + | |
136 | + @Test | |
137 | + public void testDeleteTenantProfile() throws Exception { | |
138 | + loginSysAdmin(); | |
139 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"); | |
140 | + TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class); | |
141 | + | |
142 | + doDelete("/api/tenantProfile/" + savedTenantProfile.getId().getId().toString()) | |
143 | + .andExpect(status().isOk()); | |
144 | + | |
145 | + doGet("/api/tenantProfile/" + savedTenantProfile.getId().getId().toString()) | |
146 | + .andExpect(status().isNotFound()); | |
147 | + } | |
148 | + | |
149 | + @Test | |
150 | + public void testFindTenantProfiles() throws Exception { | |
151 | + loginSysAdmin(); | |
152 | + List<TenantProfile> tenantProfiles = new ArrayList<>(); | |
153 | + PageLink pageLink = new PageLink(17); | |
154 | + PageData<TenantProfile> pageData = doGetTypedWithPageLink("/api/tenantProfiles?", | |
155 | + new TypeReference<PageData<TenantProfile>>(){}, pageLink); | |
156 | + Assert.assertFalse(pageData.hasNext()); | |
157 | + Assert.assertEquals(1, pageData.getTotalElements()); | |
158 | + tenantProfiles.addAll(pageData.getData()); | |
159 | + | |
160 | + for (int i=0;i<28;i++) { | |
161 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"+i); | |
162 | + tenantProfiles.add(doPost("/api/tenantProfile", tenantProfile, TenantProfile.class)); | |
163 | + } | |
164 | + | |
165 | + List<TenantProfile> loadedTenantProfiles = new ArrayList<>(); | |
166 | + pageLink = new PageLink(17); | |
167 | + do { | |
168 | + pageData = doGetTypedWithPageLink("/api/tenantProfiles?", | |
169 | + new TypeReference<PageData<TenantProfile>>(){}, pageLink); | |
170 | + loadedTenantProfiles.addAll(pageData.getData()); | |
171 | + if (pageData.hasNext()) { | |
172 | + pageLink = pageLink.nextPageLink(); | |
173 | + } | |
174 | + } while (pageData.hasNext()); | |
175 | + | |
176 | + Collections.sort(tenantProfiles, idComparator); | |
177 | + Collections.sort(loadedTenantProfiles, idComparator); | |
178 | + | |
179 | + Assert.assertEquals(tenantProfiles, loadedTenantProfiles); | |
180 | + | |
181 | + for (TenantProfile tenantProfile : loadedTenantProfiles) { | |
182 | + if (!tenantProfile.isDefault()) { | |
183 | + doDelete("/api/tenantProfile/" + tenantProfile.getId().getId().toString()) | |
184 | + .andExpect(status().isOk()); | |
185 | + } | |
186 | + } | |
187 | + | |
188 | + pageLink = new PageLink(17); | |
189 | + pageData = doGetTypedWithPageLink("/api/tenantProfiles?", | |
190 | + new TypeReference<PageData<TenantProfile>>(){}, pageLink); | |
191 | + Assert.assertFalse(pageData.hasNext()); | |
192 | + Assert.assertEquals(1, pageData.getTotalElements()); | |
193 | + } | |
194 | + | |
195 | + @Test | |
196 | + public void testFindTenantProfileInfos() throws Exception { | |
197 | + loginSysAdmin(); | |
198 | + List<TenantProfile> tenantProfiles = new ArrayList<>(); | |
199 | + PageLink pageLink = new PageLink(17); | |
200 | + PageData<TenantProfile> tenantProfilePageData = doGetTypedWithPageLink("/api/tenantProfiles?", | |
201 | + new TypeReference<PageData<TenantProfile>>(){}, pageLink); | |
202 | + Assert.assertFalse(tenantProfilePageData.hasNext()); | |
203 | + Assert.assertEquals(1, tenantProfilePageData.getTotalElements()); | |
204 | + tenantProfiles.addAll(tenantProfilePageData.getData()); | |
205 | + | |
206 | + for (int i=0;i<28;i++) { | |
207 | + TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile"+i); | |
208 | + tenantProfiles.add(doPost("/api/tenantProfile", tenantProfile, TenantProfile.class)); | |
209 | + } | |
210 | + | |
211 | + List<EntityInfo> loadedTenantProfileInfos = new ArrayList<>(); | |
212 | + pageLink = new PageLink(17); | |
213 | + PageData<EntityInfo> pageData; | |
214 | + do { | |
215 | + pageData = doGetTypedWithPageLink("/api/tenantProfileInfos?", | |
216 | + new TypeReference<PageData<EntityInfo>>(){}, pageLink); | |
217 | + loadedTenantProfileInfos.addAll(pageData.getData()); | |
218 | + if (pageData.hasNext()) { | |
219 | + pageLink = pageLink.nextPageLink(); | |
220 | + } | |
221 | + } while (pageData.hasNext()); | |
222 | + | |
223 | + Collections.sort(tenantProfiles, idComparator); | |
224 | + Collections.sort(loadedTenantProfileInfos, tenantProfileInfoIdComparator); | |
225 | + | |
226 | + List<EntityInfo> tenantProfileInfos = tenantProfiles.stream().map(tenantProfile -> new EntityInfo(tenantProfile.getId(), | |
227 | + tenantProfile.getName())).collect(Collectors.toList()); | |
228 | + | |
229 | + Assert.assertEquals(tenantProfileInfos, loadedTenantProfileInfos); | |
230 | + | |
231 | + for (TenantProfile tenantProfile : tenantProfiles) { | |
232 | + if (!tenantProfile.isDefault()) { | |
233 | + doDelete("/api/tenantProfile/" + tenantProfile.getId().getId().toString()) | |
234 | + .andExpect(status().isOk()); | |
235 | + } | |
236 | + } | |
237 | + | |
238 | + pageLink = new PageLink(17); | |
239 | + pageData = doGetTypedWithPageLink("/api/tenantProfileInfos?", | |
240 | + new TypeReference<PageData<EntityInfo>>(){}, pageLink); | |
241 | + Assert.assertFalse(pageData.hasNext()); | |
242 | + Assert.assertEquals(1, pageData.getTotalElements()); | |
243 | + } | |
244 | + | |
245 | + private TenantProfile createTenantProfile(String name) { | |
246 | + TenantProfile tenantProfile = new TenantProfile(); | |
247 | + tenantProfile.setName(name); | |
248 | + tenantProfile.setDescription(name + " Test"); | |
249 | + tenantProfile.setProfileData(JacksonUtil.OBJECT_MAPPER.createObjectNode()); | |
250 | + tenantProfile.setDefault(false); | |
251 | + tenantProfile.setIsolatedTbCore(false); | |
252 | + tenantProfile.setIsolatedTbRuleEngine(false); | |
253 | + return tenantProfile; | |
254 | + } | |
255 | +} | ... | ... |
application/src/test/java/org/thingsboard/server/controller/sql/DeviceProfileControllerSqlTest.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.controller.sql; | |
17 | + | |
18 | +import org.thingsboard.server.controller.BaseDeviceProfileControllerTest; | |
19 | +import org.thingsboard.server.dao.service.DaoSqlTest; | |
20 | + | |
21 | +@DaoSqlTest | |
22 | +public class DeviceProfileControllerSqlTest extends BaseDeviceProfileControllerTest { | |
23 | +} | ... | ... |
application/src/test/java/org/thingsboard/server/controller/sql/TenantProfileControllerSqlTest.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.controller.sql; | |
17 | + | |
18 | +import org.thingsboard.server.controller.BaseTenantProfileControllerTest; | |
19 | +import org.thingsboard.server.dao.service.DaoSqlTest; | |
20 | + | |
21 | +@DaoSqlTest | |
22 | +public class TenantProfileControllerSqlTest extends BaseTenantProfileControllerTest { | |
23 | +} | ... | ... |
... | ... | @@ -28,7 +28,7 @@ import static org.thingsboard.server.common.data.SearchTextBasedWithAdditionalIn |
28 | 28 | |
29 | 29 | @Data |
30 | 30 | @EqualsAndHashCode(callSuper = true) |
31 | -public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements HasName { | |
31 | +public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements HasName, HasTenantId { | |
32 | 32 | |
33 | 33 | private TenantId tenantId; |
34 | 34 | private String name; | ... | ... |
... | ... | @@ -15,6 +15,8 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.common.data; |
17 | 17 | |
18 | +import com.fasterxml.jackson.annotation.JsonCreator; | |
19 | +import com.fasterxml.jackson.annotation.JsonProperty; | |
18 | 20 | import lombok.Data; |
19 | 21 | import org.thingsboard.server.common.data.id.EntityId; |
20 | 22 | import org.thingsboard.server.common.data.id.EntityIdFactory; |
... | ... | @@ -28,7 +30,8 @@ public class EntityInfo implements HasId<EntityId>, HasName { |
28 | 30 | private final EntityId id; |
29 | 31 | private final String name; |
30 | 32 | |
31 | - public EntityInfo(EntityId id, String name) { | |
33 | + @JsonCreator | |
34 | + public EntityInfo(@JsonProperty("id") EntityId id, @JsonProperty("name") String name) { | |
32 | 35 | this.id = id; |
33 | 36 | this.name = name; |
34 | 37 | } | ... | ... |
... | ... | @@ -60,7 +60,7 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
60 | 60 | @Autowired |
61 | 61 | private CacheManager cacheManager; |
62 | 62 | |
63 | - @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{#deviceProfileId}") | |
63 | + @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{#deviceProfileId.id}") | |
64 | 64 | @Override |
65 | 65 | public DeviceProfile findDeviceProfileById(TenantId tenantId, DeviceProfileId deviceProfileId) { |
66 | 66 | log.trace("Executing findDeviceProfileById [{}]", deviceProfileId); |
... | ... | @@ -68,7 +68,7 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
68 | 68 | return deviceProfileDao.findById(tenantId, deviceProfileId.getId()); |
69 | 69 | } |
70 | 70 | |
71 | - @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{'info', #deviceProfileId}") | |
71 | + @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{'info', #deviceProfileId.id}") | |
72 | 72 | @Override |
73 | 73 | public EntityInfo findDeviceProfileInfoById(TenantId tenantId, DeviceProfileId deviceProfileId) { |
74 | 74 | log.trace("Executing findDeviceProfileById [{}]", deviceProfileId); |
... | ... | @@ -92,11 +92,11 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
92 | 92 | } |
93 | 93 | } |
94 | 94 | Cache cache = cacheManager.getCache(DEVICE_PROFILE_CACHE); |
95 | - cache.evict(Collections.singletonList(savedDeviceProfile.getId())); | |
96 | - cache.evict(Arrays.asList("info", savedDeviceProfile.getId())); | |
95 | + cache.evict(Collections.singletonList(savedDeviceProfile.getId().getId())); | |
96 | + cache.evict(Arrays.asList("info", savedDeviceProfile.getId().getId())); | |
97 | 97 | if (savedDeviceProfile.isDefault()) { |
98 | - cache.evict(Arrays.asList("default", savedDeviceProfile.getTenantId())); | |
99 | - cache.evict(Arrays.asList("default", "info", savedDeviceProfile.getTenantId())); | |
98 | + cache.evict(Arrays.asList("default", savedDeviceProfile.getTenantId().getId())); | |
99 | + cache.evict(Arrays.asList("default", "info", savedDeviceProfile.getTenantId().getId())); | |
100 | 100 | } |
101 | 101 | return savedDeviceProfile; |
102 | 102 | } |
... | ... | @@ -149,7 +149,7 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
149 | 149 | return saveDeviceProfile(deviceProfile); |
150 | 150 | } |
151 | 151 | |
152 | - @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{'default', #tenantId}") | |
152 | + @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{'default', #tenantId.id}") | |
153 | 153 | @Override |
154 | 154 | public DeviceProfile findDefaultDeviceProfile(TenantId tenantId) { |
155 | 155 | log.trace("Executing findDefaultDeviceProfile tenantId [{}]", tenantId); |
... | ... | @@ -157,7 +157,7 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
157 | 157 | return deviceProfileDao.findDefaultDeviceProfile(tenantId); |
158 | 158 | } |
159 | 159 | |
160 | - @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{'default', 'info', #tenantId}") | |
160 | + @Cacheable(cacheNames = DEVICE_PROFILE_CACHE, key = "{'default', 'info', #tenantId.id}") | |
161 | 161 | @Override |
162 | 162 | public EntityInfo findDefaultDeviceProfileInfo(TenantId tenantId) { |
163 | 163 | log.trace("Executing findDefaultDeviceProfileInfo tenantId [{}]", tenantId); |
... | ... | @@ -182,13 +182,13 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
182 | 182 | previousDefaultDeviceProfile.setDefault(false); |
183 | 183 | deviceProfileDao.save(tenantId, previousDefaultDeviceProfile); |
184 | 184 | deviceProfileDao.save(tenantId, deviceProfile); |
185 | - cache.evict(Collections.singletonList(previousDefaultDeviceProfile.getId())); | |
186 | - cache.evict(Arrays.asList("info", previousDefaultDeviceProfile.getId())); | |
185 | + cache.evict(Collections.singletonList(previousDefaultDeviceProfile.getId().getId())); | |
186 | + cache.evict(Arrays.asList("info", previousDefaultDeviceProfile.getId().getId())); | |
187 | 187 | changed = true; |
188 | 188 | } |
189 | 189 | if (changed) { |
190 | - cache.evict(Collections.singletonList(deviceProfile.getId())); | |
191 | - cache.evict(Arrays.asList("info", deviceProfile.getId())); | |
190 | + cache.evict(Collections.singletonList(deviceProfile.getId().getId())); | |
191 | + cache.evict(Arrays.asList("info", deviceProfile.getId().getId())); | |
192 | 192 | cache.evict(Arrays.asList("default", tenantId)); |
193 | 193 | cache.evict(Arrays.asList("default", "info", tenantId)); |
194 | 194 | } | ... | ... |
... | ... | @@ -54,7 +54,7 @@ public class TenantProfileServiceImpl extends AbstractEntityService implements T |
54 | 54 | @Autowired |
55 | 55 | private CacheManager cacheManager; |
56 | 56 | |
57 | - @Cacheable(cacheNames = TENANT_PROFILE_CACHE, key = "{#tenantProfileId}") | |
57 | + @Cacheable(cacheNames = TENANT_PROFILE_CACHE, key = "{#tenantProfileId.id}") | |
58 | 58 | @Override |
59 | 59 | public TenantProfile findTenantProfileById(TenantId tenantId, TenantProfileId tenantProfileId) { |
60 | 60 | log.trace("Executing findTenantProfileById [{}]", tenantProfileId); |
... | ... | @@ -62,7 +62,7 @@ public class TenantProfileServiceImpl extends AbstractEntityService implements T |
62 | 62 | return tenantProfileDao.findById(tenantId, tenantProfileId.getId()); |
63 | 63 | } |
64 | 64 | |
65 | - @Cacheable(cacheNames = TENANT_PROFILE_CACHE, key = "{'info', #tenantProfileId}") | |
65 | + @Cacheable(cacheNames = TENANT_PROFILE_CACHE, key = "{'info', #tenantProfileId.id}") | |
66 | 66 | @Override |
67 | 67 | public EntityInfo findTenantProfileInfoById(TenantId tenantId, TenantProfileId tenantProfileId) { |
68 | 68 | log.trace("Executing findTenantProfileInfoById [{}]", tenantProfileId); |
... | ... | @@ -86,8 +86,8 @@ public class TenantProfileServiceImpl extends AbstractEntityService implements T |
86 | 86 | } |
87 | 87 | } |
88 | 88 | Cache cache = cacheManager.getCache(TENANT_PROFILE_CACHE); |
89 | - cache.evict(Collections.singletonList(savedTenantProfile.getId())); | |
90 | - cache.evict(Arrays.asList("info", savedTenantProfile.getId())); | |
89 | + cache.evict(Collections.singletonList(savedTenantProfile.getId().getId())); | |
90 | + cache.evict(Arrays.asList("info", savedTenantProfile.getId().getId())); | |
91 | 91 | if (savedTenantProfile.isDefault()) { |
92 | 92 | cache.evict(Collections.singletonList("default")); |
93 | 93 | cache.evict(Arrays.asList("default", "info")); |
... | ... | @@ -176,13 +176,13 @@ public class TenantProfileServiceImpl extends AbstractEntityService implements T |
176 | 176 | previousDefaultTenantProfile.setDefault(false); |
177 | 177 | tenantProfileDao.save(tenantId, previousDefaultTenantProfile); |
178 | 178 | tenantProfileDao.save(tenantId, tenantProfile); |
179 | - cache.evict(Collections.singletonList(previousDefaultTenantProfile.getId())); | |
180 | - cache.evict(Arrays.asList("info", previousDefaultTenantProfile.getId())); | |
179 | + cache.evict(Collections.singletonList(previousDefaultTenantProfile.getId().getId())); | |
180 | + cache.evict(Arrays.asList("info", previousDefaultTenantProfile.getId().getId())); | |
181 | 181 | changed = true; |
182 | 182 | } |
183 | 183 | if (changed) { |
184 | - cache.evict(Collections.singletonList(tenantProfile.getId())); | |
185 | - cache.evict(Arrays.asList("info", tenantProfile.getId())); | |
184 | + cache.evict(Collections.singletonList(tenantProfile.getId().getId())); | |
185 | + cache.evict(Arrays.asList("info", tenantProfile.getId().getId())); | |
186 | 186 | cache.evict(Collections.singletonList("default")); |
187 | 187 | cache.evict(Arrays.asList("default", "info")); |
188 | 188 | } | ... | ... |
... | ... | @@ -104,9 +104,9 @@ public class BaseDeviceProfileServiceTest extends AbstractServiceTest { |
104 | 104 | @Test |
105 | 105 | public void testFindDefaultDeviceProfileInfo() { |
106 | 106 | EntityInfo foundDefaultDeviceProfileInfo = deviceProfileService.findDefaultDeviceProfileInfo(tenantId); |
107 | + Assert.assertNotNull(foundDefaultDeviceProfileInfo); | |
107 | 108 | Assert.assertNotNull(foundDefaultDeviceProfileInfo.getId()); |
108 | 109 | Assert.assertNotNull(foundDefaultDeviceProfileInfo.getName()); |
109 | - Assert.assertNotNull(foundDefaultDeviceProfileInfo); | |
110 | 110 | } |
111 | 111 | |
112 | 112 | @Test | ... | ... |
... | ... | @@ -60,8 +60,6 @@ public class BaseTenantProfileServiceTest extends AbstractServiceTest { |
60 | 60 | tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, savedTenantProfile); |
61 | 61 | TenantProfile foundTenantProfile = tenantProfileService.findTenantProfileById(TenantId.SYS_TENANT_ID, savedTenantProfile.getId()); |
62 | 62 | Assert.assertEquals(foundTenantProfile.getName(), savedTenantProfile.getName()); |
63 | - | |
64 | - tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
65 | 63 | } |
66 | 64 | |
67 | 65 | @Test |
... | ... | @@ -71,7 +69,6 @@ public class BaseTenantProfileServiceTest extends AbstractServiceTest { |
71 | 69 | TenantProfile foundTenantProfile = tenantProfileService.findTenantProfileById(TenantId.SYS_TENANT_ID, savedTenantProfile.getId()); |
72 | 70 | Assert.assertNotNull(foundTenantProfile); |
73 | 71 | Assert.assertEquals(savedTenantProfile, foundTenantProfile); |
74 | - tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
75 | 72 | } |
76 | 73 | |
77 | 74 | @Test |
... | ... | @@ -82,7 +79,6 @@ public class BaseTenantProfileServiceTest extends AbstractServiceTest { |
82 | 79 | Assert.assertNotNull(foundTenantProfileInfo); |
83 | 80 | Assert.assertEquals(savedTenantProfile.getId(), foundTenantProfileInfo.getId()); |
84 | 81 | Assert.assertEquals(savedTenantProfile.getName(), foundTenantProfileInfo.getName()); |
85 | - tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
86 | 82 | } |
87 | 83 | |
88 | 84 | @Test |
... | ... | @@ -93,7 +89,6 @@ public class BaseTenantProfileServiceTest extends AbstractServiceTest { |
93 | 89 | TenantProfile foundDefaultTenantProfile = tenantProfileService.findDefaultTenantProfile(TenantId.SYS_TENANT_ID); |
94 | 90 | Assert.assertNotNull(foundDefaultTenantProfile); |
95 | 91 | Assert.assertEquals(savedTenantProfile, foundDefaultTenantProfile); |
96 | - tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
97 | 92 | } |
98 | 93 | |
99 | 94 | @Test |
... | ... | @@ -105,7 +100,6 @@ public class BaseTenantProfileServiceTest extends AbstractServiceTest { |
105 | 100 | Assert.assertNotNull(foundDefaultTenantProfileInfo); |
106 | 101 | Assert.assertEquals(savedTenantProfile.getId(), foundDefaultTenantProfileInfo.getId()); |
107 | 102 | Assert.assertEquals(savedTenantProfile.getName(), foundDefaultTenantProfileInfo.getName()); |
108 | - tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
109 | 103 | } |
110 | 104 | |
111 | 105 | @Test |
... | ... | @@ -126,7 +120,6 @@ public class BaseTenantProfileServiceTest extends AbstractServiceTest { |
126 | 120 | defaultTenantProfile = tenantProfileService.findDefaultTenantProfile(TenantId.SYS_TENANT_ID); |
127 | 121 | Assert.assertNotNull(defaultTenantProfile); |
128 | 122 | Assert.assertEquals(savedTenantProfile2.getId(), defaultTenantProfile.getId()); |
129 | - tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID); | |
130 | 123 | } |
131 | 124 | |
132 | 125 | @Test(expected = DataValidationException.class) | ... | ... |