Commit f1d89ace5d62409a2c433bfa47721df724e8ee84

Authored by Andrii Shvaika
1 parent 936171b8

Removed invalid dependency

... ... @@ -110,7 +110,6 @@ import org.thingsboard.server.dao.model.ModelConstants;
110 110 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
111 111 import org.thingsboard.server.dao.oauth2.OAuth2Service;
112 112 import org.thingsboard.server.dao.relation.RelationService;
113   -import org.thingsboard.server.dao.resource.TbResourceService;
114 113 import org.thingsboard.server.dao.rule.RuleChainService;
115 114 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
116 115 import org.thingsboard.server.dao.tenant.TenantProfileService;
... ... @@ -130,6 +129,7 @@ import org.thingsboard.server.service.edge.rpc.init.SyncEdgeService;
130 129 import org.thingsboard.server.service.lwm2m.LwM2MModelsRepository;
131 130 import org.thingsboard.server.service.profile.TbDeviceProfileCache;
132 131 import org.thingsboard.server.service.queue.TbClusterService;
  132 +import org.thingsboard.server.service.resource.TbResourceService;
133 133 import org.thingsboard.server.service.security.model.SecurityUser;
134 134 import org.thingsboard.server.service.security.permission.AccessControlService;
135 135 import org.thingsboard.server.service.security.permission.Operation;
... ...
... ... @@ -36,8 +36,8 @@ import org.thingsboard.server.common.data.lwm2m.LwM2mObject;
36 36 import org.thingsboard.server.common.data.page.PageData;
37 37 import org.thingsboard.server.common.data.page.PageLink;
38 38 import org.thingsboard.server.common.data.security.Authority;
39   -import org.thingsboard.server.dao.resource.TbResourceService;
40 39 import org.thingsboard.server.queue.util.TbCoreComponent;
  40 +import org.thingsboard.server.service.resource.TbResourceService;
41 41 import org.thingsboard.server.service.security.permission.Operation;
42 42 import org.thingsboard.server.service.security.permission.Resource;
43 43
... ... @@ -52,12 +52,6 @@ public class TbResourceController extends BaseController {
52 52
53 53 public static final String RESOURCE_ID = "resourceId";
54 54
55   - private final TbResourceService resourceService;
56   -
57   - public TbResourceController(TbResourceService resourceService) {
58   - this.resourceService = resourceService;
59   - }
60   -
61 55 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
62 56 @RequestMapping(value = "/resource/{resourceId}/download", method = RequestMethod.GET)
63 57 @ResponseBody
... ...
... ... @@ -35,7 +35,7 @@ import org.thingsboard.server.common.data.widget.WidgetsBundle;
35 35 import org.thingsboard.server.dao.dashboard.DashboardService;
36 36 import org.thingsboard.server.dao.exception.DataValidationException;
37 37 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
38   -import org.thingsboard.server.dao.resource.TbResourceService;
  38 +import org.thingsboard.server.dao.resource.ResourceService;
39 39 import org.thingsboard.server.dao.rule.RuleChainService;
40 40 import org.thingsboard.server.dao.widget.WidgetTypeService;
41 41 import org.thingsboard.server.dao.widget.WidgetsBundleService;
... ... @@ -97,7 +97,7 @@ public class InstallScripts {
97 97 private OAuth2ConfigTemplateService oAuth2TemplateService;
98 98
99 99 @Autowired
100   - private TbResourceService resourceService;
  100 + private ResourceService resourceService;
101 101
102 102 private Path getTenantRuleChainsDir() {
103 103 return Paths.get(getDataDir(), JSON_DIR, TENANT_DIR, RULE_CHAINS_DIR);
... ...
application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java renamed from dao/src/main/java/org/thingsboard/server/dao/resource/BaseTbResourceService.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.dao.resource;
  16 +package org.thingsboard.server.service.resource;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.apache.commons.lang3.StringUtils;
... ... @@ -21,12 +21,12 @@ import org.eclipse.leshan.core.model.DDFFileParser;
21 21 import org.eclipse.leshan.core.model.DefaultDDFFileValidator;
22 22 import org.eclipse.leshan.core.model.InvalidDDFFileException;
23 23 import org.eclipse.leshan.core.model.ObjectModel;
24   -import org.hibernate.exception.ConstraintViolationException;
25 24 import org.springframework.stereotype.Service;
26 25 import org.thingsboard.server.common.data.ResourceType;
27 26 import org.thingsboard.server.common.data.TbResource;
28 27 import org.thingsboard.server.common.data.TbResourceInfo;
29   -import org.thingsboard.server.common.data.Tenant;
  28 +import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
  29 +import org.thingsboard.server.common.data.exception.ThingsboardException;
30 30 import org.thingsboard.server.common.data.id.TbResourceId;
31 31 import org.thingsboard.server.common.data.id.TenantId;
32 32 import org.thingsboard.server.common.data.lwm2m.LwM2mInstance;
... ... @@ -35,11 +35,7 @@ import org.thingsboard.server.common.data.lwm2m.LwM2mResourceObserve;
35 35 import org.thingsboard.server.common.data.page.PageData;
36 36 import org.thingsboard.server.common.data.page.PageLink;
37 37 import org.thingsboard.server.dao.exception.DataValidationException;
38   -import org.thingsboard.server.dao.model.ModelConstants;
39   -import org.thingsboard.server.dao.service.DataValidator;
40   -import org.thingsboard.server.dao.service.PaginatedRemover;
41   -import org.thingsboard.server.dao.service.Validator;
42   -import org.thingsboard.server.dao.tenant.TenantDao;
  38 +import org.thingsboard.server.dao.resource.ResourceService;
43 39
44 40 import java.io.ByteArrayInputStream;
45 41 import java.io.IOException;
... ... @@ -47,7 +43,6 @@ import java.util.ArrayList;
47 43 import java.util.Base64;
48 44 import java.util.Comparator;
49 45 import java.util.List;
50   -import java.util.Optional;
51 46 import java.util.stream.Collectors;
52 47
53 48 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY;
... ... @@ -55,139 +50,120 @@ import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPA
55 50 import static org.thingsboard.server.dao.device.DeviceServiceImpl.INCORRECT_TENANT_ID;
56 51 import static org.thingsboard.server.dao.service.Validator.validateId;
57 52
58   -@Service
59 53 @Slf4j
60   -public class BaseTbResourceService implements TbResourceService {
  54 +@Service
  55 +public class DefaultTbResourceService implements TbResourceService {
61 56
62   - public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId ";
63   - private final TbResourceDao resourceDao;
64   - private final TbResourceInfoDao resourceInfoDao;
65   - private final TenantDao tenantDao;
  57 + private final ResourceService resourceService;
66 58 private final DDFFileParser ddfFileParser;
67 59
68   - public BaseTbResourceService(TbResourceDao resourceDao, TbResourceInfoDao resourceInfoDao, TenantDao tenantDao) {
69   - this.resourceDao = resourceDao;
70   - this.resourceInfoDao = resourceInfoDao;
71   - this.tenantDao = tenantDao;
  60 + public DefaultTbResourceService(ResourceService resourceService) {
  61 + this.resourceService = resourceService;
72 62 this.ddfFileParser = new DDFFileParser(new DefaultDDFFileValidator());
73 63 }
74 64
75 65 @Override
76   - public TbResource saveResource(TbResource resource) throws InvalidDDFFileException, IOException {
  66 + public TbResource saveResource(TbResource resource) throws ThingsboardException {
77 67 log.trace("Executing saveResource [{}]", resource);
78 68 if (StringUtils.isEmpty(resource.getData())) {
79 69 throw new DataValidationException("Resource data should be specified!");
80 70 }
81 71 if (ResourceType.LWM2M_MODEL.equals(resource.getResourceType())) {
82   - List<ObjectModel> objectModels =
83   - ddfFileParser.parseEx(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());
84   - if (!objectModels.isEmpty()) {
85   - ObjectModel objectModel = objectModels.get(0);
86   -
87   - String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion();
88   - String name = objectModel.name;
89   - resource.setResourceKey(resourceKey);
90   - if (resource.getId() == null) {
91   - resource.setTitle(name + " id=" + objectModel.id + " v" + objectModel.getVersion());
  72 + try {
  73 + List<ObjectModel> objectModels =
  74 + ddfFileParser.parseEx(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());
  75 + if (!objectModels.isEmpty()) {
  76 + ObjectModel objectModel = objectModels.get(0);
  77 +
  78 + String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.getVersion();
  79 + String name = objectModel.name;
  80 + resource.setResourceKey(resourceKey);
  81 + if (resource.getId() == null) {
  82 + resource.setTitle(name + " id=" + objectModel.id + " v" + objectModel.getVersion());
  83 + }
  84 + resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
  85 + } else {
  86 + throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
92 87 }
93   - resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
94   - } else {
  88 + } catch (InvalidDDFFileException | IOException e) {
  89 + throw new ThingsboardException(e, ThingsboardErrorCode.GENERAL);
  90 + }
  91 + if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource) == null) {
95 92 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
96 93 }
97 94 } else {
98 95 resource.setResourceKey(resource.getFileName());
99 96 }
100 97
101   - resourceValidator.validate(resource, TbResourceInfo::getTenantId);
102   -
103   - try {
104   - return resourceDao.save(resource.getTenantId(), resource);
105   - } catch (Exception t) {
106   - ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
107   - if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
108   - String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
109   - throw new DataValidationException("Resource with such " + field + " already exists!");
110   - } else {
111   - throw t;
112   - }
113   - }
114   -
  98 + return resourceService.saveResource(resource);
115 99 }
116 100
117 101 @Override
118   - public TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceKey) {
119   - log.trace("Executing getResource [{}] [{}] [{}]", tenantId, resourceType, resourceKey);
120   - return resourceDao.getResource(tenantId, resourceType, resourceKey);
  102 + public TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId) {
  103 + return resourceService.getResource(tenantId, resourceType, resourceId);
121 104 }
122 105
123 106 @Override
124 107 public TbResource findResourceById(TenantId tenantId, TbResourceId resourceId) {
125   - log.trace("Executing findResourceById [{}] [{}]", tenantId, resourceId);
126   - Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
127   - return resourceDao.findById(tenantId, resourceId.getId());
  108 + return resourceService.findResourceById(tenantId, resourceId);
128 109 }
129 110
130 111 @Override
131 112 public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) {
132   - log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId);
133   - Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
134   - return resourceInfoDao.findById(tenantId, resourceId.getId());
135   - }
136   -
137   - @Override
138   - public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
139   - log.trace("Executing deleteResource [{}] [{}]", tenantId, resourceId);
140   - Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
141   - resourceDao.removeById(tenantId, resourceId.getId());
  113 + return resourceService.findResourceInfoById(tenantId, resourceId);
142 114 }
143 115
144 116 @Override
145 117 public PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
146   - log.trace("Executing findAllTenantResourcesByTenantId [{}]", tenantId);
147   - validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
148   - return resourceInfoDao.findAllTenantResourcesByTenantId(tenantId.getId(), pageLink);
  118 + return resourceService.findAllTenantResourcesByTenantId(tenantId, pageLink);
149 119 }
150 120
151 121 @Override
152 122 public PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
153   - log.trace("Executing findTenantResourcesByTenantId [{}]", tenantId);
154   - validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
155   - return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink);
  123 + return resourceService.findTenantResourcesByTenantId(tenantId, pageLink);
156 124 }
157 125
158 126 @Override
159   - public List<LwM2mObject> findLwM2mObjectPage(TenantId tenantId, String sortProperty, String sortOrder, PageLink pageLink) {
  127 + public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder, String sortProperty, String[] objectIds) {
160 128 log.trace("Executing findByTenantId [{}]", tenantId);
161 129 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
162   - PageData<TbResource> resourcePageData = resourceDao.findResourcesByTenantIdAndResourceType(
163   - tenantId,
164   - ResourceType.LWM2M_MODEL, pageLink);
165   - return resourcePageData.getData().stream()
  130 + List<TbResource> resources = resourceService.findTenantResourcesByResourceTypeAndObjectIds(tenantId, ResourceType.LWM2M_MODEL,
  131 + objectIds);
  132 + return resources.stream()
166 133 .map(this::toLwM2mObject)
167 134 .sorted(getComparator(sortProperty, sortOrder))
168 135 .collect(Collectors.toList());
169 136 }
170 137
171 138 @Override
172   - public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder,
173   - String sortProperty,
174   - String[] objectIds) {
  139 + public List<LwM2mObject> findLwM2mObjectPage(TenantId tenantId, String sortProperty, String sortOrder, PageLink pageLink) {
175 140 log.trace("Executing findByTenantId [{}]", tenantId);
176 141 validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
177   - List<TbResource> resources = resourceDao.findResourcesByTenantIdAndResourceType(tenantId, ResourceType.LWM2M_MODEL,
178   - objectIds,
179   - null);
180   - return resources.stream()
  142 + PageData<TbResource> resourcePageData = resourceService.findTenantResourcesByResourceTypeAndPageLink(tenantId, ResourceType.LWM2M_MODEL, pageLink);
  143 + return resourcePageData.getData().stream()
181 144 .map(this::toLwM2mObject)
182 145 .sorted(getComparator(sortProperty, sortOrder))
183 146 .collect(Collectors.toList());
184 147 }
185 148
186 149 @Override
  150 + public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
  151 + resourceService.deleteResource(tenantId, resourceId);
  152 + }
  153 +
  154 + @Override
187 155 public void deleteResourcesByTenantId(TenantId tenantId) {
188   - log.trace("Executing deleteResourcesByTenantId, tenantId [{}]", tenantId);
189   - validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
190   - tenantResourcesRemover.removeEntities(tenantId, tenantId);
  156 + resourceService.deleteResourcesByTenantId(tenantId);
  157 + }
  158 +
  159 + private Comparator<? super LwM2mObject> getComparator(String sortProperty, String sortOrder) {
  160 + Comparator<LwM2mObject> comparator;
  161 + if ("name".equals(sortProperty)) {
  162 + comparator = Comparator.comparing(LwM2mObject::getName);
  163 + } else {
  164 + comparator = Comparator.comparingLong(LwM2mObject::getId);
  165 + }
  166 + return "DESC".equals(sortOrder) ? comparator.reversed() : comparator;
191 167 }
192 168
193 169 private LwM2mObject toLwM2mObject(TbResource resource) {
... ... @@ -223,69 +199,4 @@ public class BaseTbResourceService implements TbResourceService {
223 199 return null;
224 200 }
225 201 }
226   -
227   - private Comparator<? super LwM2mObject> getComparator(String sortProperty, String sortOrder) {
228   - Comparator<LwM2mObject> comparator;
229   - if ("name".equals(sortProperty)) {
230   - comparator = Comparator.comparing(LwM2mObject::getName);
231   - } else {
232   - comparator = Comparator.comparingLong(LwM2mObject::getId);
233   - }
234   - return "DESC".equals(sortOrder) ? comparator.reversed() : comparator;
235   - }
236   -
237   - private DataValidator<TbResource> resourceValidator = new DataValidator<>() {
238   -
239   - @Override
240   - protected void validateDataImpl(TenantId tenantId, TbResource resource) {
241   - if (StringUtils.isEmpty(resource.getTitle())) {
242   - throw new DataValidationException("Resource title should be specified!");
243   - }
244   - if (resource.getResourceType() == null) {
245   - throw new DataValidationException("Resource type should be specified!");
246   - }
247   - if (StringUtils.isEmpty(resource.getFileName())) {
248   - throw new DataValidationException("Resource file name should be specified!");
249   - }
250   - if (StringUtils.isEmpty(resource.getResourceKey())) {
251   - throw new DataValidationException("Resource key should be specified!");
252   - }
253   - if (resource.getTenantId() == null) {
254   - resource.setTenantId(new TenantId(ModelConstants.NULL_UUID));
255   - }
256   - if (!resource.getTenantId().getId().equals(ModelConstants.NULL_UUID)) {
257   - Tenant tenant = tenantDao.findById(tenantId, resource.getTenantId().getId());
258   - if (tenant == null) {
259   - throw new DataValidationException("Resource is referencing to non-existent tenant!");
260   - }
261   - }
262   - if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource) == null) {
263   - throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
264   - }
265   - }
266   - };
267   -
268   - private PaginatedRemover<TenantId, TbResource> tenantResourcesRemover =
269   - new PaginatedRemover<>() {
270   -
271   - @Override
272   - protected PageData<TbResource> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) {
273   - return resourceDao.findAllByTenantId(id, pageLink);
274   - }
275   -
276   - @Override
277   - protected void removeEntity(TenantId tenantId, TbResource entity) {
278   - deleteResource(tenantId, new TbResourceId(entity.getUuidId()));
279   - }
280   - };
281   -
282   - protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {
283   - if (t instanceof ConstraintViolationException) {
284   - return Optional.of((ConstraintViolationException) t);
285   - } else if (t.getCause() instanceof ConstraintViolationException) {
286   - return Optional.of((ConstraintViolationException) (t.getCause()));
287   - } else {
288   - return Optional.empty();
289   - }
290   - }
291 202 }
... ...
application/src/main/java/org/thingsboard/server/service/resource/TbResourceService.java renamed from common/dao-api/src/main/java/org/thingsboard/server/dao/resource/TbResourceService.java
... ... @@ -13,24 +13,23 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.dao.resource;
  16 +package org.thingsboard.server.service.resource;
17 17
18   -import org.eclipse.leshan.core.model.InvalidDDFFileException;
  18 +import org.thingsboard.server.common.data.ResourceType;
19 19 import org.thingsboard.server.common.data.TbResource;
20 20 import org.thingsboard.server.common.data.TbResourceInfo;
21   -import org.thingsboard.server.common.data.ResourceType;
  21 +import org.thingsboard.server.common.data.exception.ThingsboardException;
22 22 import org.thingsboard.server.common.data.id.TbResourceId;
23 23 import org.thingsboard.server.common.data.id.TenantId;
24 24 import org.thingsboard.server.common.data.lwm2m.LwM2mObject;
25 25 import org.thingsboard.server.common.data.page.PageData;
26 26 import org.thingsboard.server.common.data.page.PageLink;
27 27
28   -import java.io.IOException;
29 28 import java.util.List;
30 29
31   -
32 30 public interface TbResourceService {
33   - TbResource saveResource(TbResource resource) throws InvalidDDFFileException, IOException;
  31 +
  32 + TbResource saveResource(TbResource resource) throws ThingsboardException;
34 33
35 34 TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId);
36 35
... ... @@ -55,4 +54,5 @@ public interface TbResourceService {
55 54 void deleteResource(TenantId tenantId, TbResourceId resourceId);
56 55
57 56 void deleteResourcesByTenantId(TenantId tenantId);
  57 +
58 58 }
... ...
... ... @@ -56,7 +56,7 @@ import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
56 56 import org.thingsboard.server.dao.device.provision.ProvisionRequest;
57 57 import org.thingsboard.server.dao.device.provision.ProvisionResponse;
58 58 import org.thingsboard.server.dao.relation.RelationService;
59   -import org.thingsboard.server.dao.resource.TbResourceService;
  59 +import org.thingsboard.server.dao.resource.ResourceService;
60 60 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
61 61 import org.thingsboard.server.gen.transport.TransportProtos;
62 62 import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto;
... ... @@ -108,7 +108,7 @@ public class DefaultTransportApiService implements TransportApiService {
108 108 private final TbClusterService tbClusterService;
109 109 private final DataDecodingEncodingService dataDecodingEncodingService;
110 110 private final DeviceProvisionService deviceProvisionService;
111   - private final TbResourceService resourceService;
  111 + private final ResourceService resourceService;
112 112
113 113 private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>();
114 114
... ... @@ -117,7 +117,7 @@ public class DefaultTransportApiService implements TransportApiService {
117 117 RelationService relationService, DeviceCredentialsService deviceCredentialsService,
118 118 DeviceStateService deviceStateService, DbCallbackExecutorService dbCallbackExecutorService,
119 119 TbClusterService tbClusterService, DataDecodingEncodingService dataDecodingEncodingService,
120   - DeviceProvisionService deviceProvisionService, TbResourceService resourceService) {
  120 + DeviceProvisionService deviceProvisionService, ResourceService resourceService) {
121 121 this.deviceProfileCache = deviceProfileCache;
122 122 this.tenantProfileCache = tenantProfileCache;
123 123 this.apiUsageStateService = apiUsageStateService;
... ...
application/src/test/java/org/thingsboard/server/service/ServiceSqlTestSuite.java renamed from dao/src/test/java/org/thingsboard/server/dao/service/sql/TbResourceServiceSqlTest.java
... ... @@ -13,11 +13,31 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.dao.service.sql;
  16 +package org.thingsboard.server.service;
17 17
18   -import org.thingsboard.server.dao.service.BaseTbResourceServiceTest;
19   -import org.thingsboard.server.dao.service.DaoSqlTest;
  18 +import org.junit.BeforeClass;
  19 +import org.junit.ClassRule;
  20 +import org.junit.extensions.cpsuite.ClasspathSuite;
  21 +import org.junit.runner.RunWith;
  22 +import org.thingsboard.server.dao.CustomSqlUnit;
  23 +import org.thingsboard.server.queue.memory.InMemoryStorage;
20 24
21   -@DaoSqlTest
22   -public class TbResourceServiceSqlTest extends BaseTbResourceServiceTest {
  25 +import java.util.Arrays;
  26 +
  27 +@RunWith(ClasspathSuite.class)
  28 +@ClasspathSuite.ClassnameFilters({
  29 + "org.thingsboard.server.service.resource.*Test",
  30 + })
  31 +public class ServiceSqlTestSuite {
  32 +
  33 + @ClassRule
  34 + public static CustomSqlUnit sqlUnit = new CustomSqlUnit(
  35 + Arrays.asList("sql/schema-types-hsql.sql", "sql/schema-ts-hsql.sql", "sql/schema-entities-hsql.sql", "sql/schema-entities-idx.sql", "sql/system-data.sql"),
  36 + "sql/hsql/drop-all-tables.sql",
  37 + "sql-test.properties");
  38 +
  39 + @BeforeClass
  40 + public static void cleanupInMemStorage(){
  41 + InMemoryStorage.getInstance().cleanup();
  42 + }
23 43 }
... ...
application/src/test/java/org/thingsboard/server/service/resource/BaseTbResourceServiceTest.java renamed from dao/src/test/java/org/thingsboard/server/dao/service/BaseTbResourceServiceTest.java
... ... @@ -13,28 +13,37 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.dao.service;
  16 +package org.thingsboard.server.service.resource;
17 17
18 18 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 22 import org.junit.Test;
  23 +import org.springframework.beans.factory.annotation.Autowired;
23 24 import org.thingsboard.server.common.data.ResourceType;
24 25 import org.thingsboard.server.common.data.TbResource;
25 26 import org.thingsboard.server.common.data.TbResourceInfo;
26 27 import org.thingsboard.server.common.data.Tenant;
  28 +import org.thingsboard.server.common.data.User;
27 29 import org.thingsboard.server.common.data.id.TenantId;
28 30 import org.thingsboard.server.common.data.page.PageData;
29 31 import org.thingsboard.server.common.data.page.PageLink;
  32 +import org.thingsboard.server.common.data.security.Authority;
  33 +import org.thingsboard.server.controller.AbstractControllerTest;
30 34 import org.thingsboard.server.dao.exception.DataValidationException;
  35 +import org.thingsboard.server.dao.service.AbstractServiceTest;
  36 +import org.thingsboard.server.dao.service.DaoSqlTest;
31 37
32 38 import java.util.ArrayList;
33 39 import java.util.Base64;
34 40 import java.util.Collections;
35 41 import java.util.List;
36 42
37   -public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
  43 +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  44 +
  45 +@DaoSqlTest
  46 +public class BaseTbResourceServiceTest extends AbstractControllerTest {
38 47
39 48 private static final String LWM2M_TEST_MODEL = "<LWM2M xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.openmobilealliance.org/tech/profiles/LWM2M-v1_1.xsd\">\n" +
40 49 "<Object ObjectType=\"MODefinition\">\n" +
... ... @@ -67,18 +76,38 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
67 76
68 77 private TenantId tenantId;
69 78
  79 + @Autowired
  80 + private TbResourceService resourceService;
  81 +
  82 + private Tenant savedTenant;
  83 + private User tenantAdmin;
  84 +
70 85 @Before
71   - public void before() {
  86 + public void beforeTest() throws Exception {
  87 + loginSysAdmin();
  88 +
72 89 Tenant tenant = new Tenant();
73 90 tenant.setTitle("My tenant");
74   - Tenant savedTenant = tenantService.saveTenant(tenant);
75   - Assert.assertNotNull(savedTenant);
  91 + savedTenant = doPost("/api/tenant", tenant, Tenant.class);
76 92 tenantId = savedTenant.getId();
  93 + Assert.assertNotNull(savedTenant);
  94 +
  95 + tenantAdmin = new User();
  96 + tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
  97 + tenantAdmin.setTenantId(savedTenant.getId());
  98 + tenantAdmin.setEmail("tenant2@thingsboard.org");
  99 + tenantAdmin.setFirstName("Joe");
  100 + tenantAdmin.setLastName("Downs");
  101 +
  102 + tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
77 103 }
78 104
79 105 @After
80   - public void after() {
81   - tenantService.deleteTenant(tenantId);
  106 + public void afterTest() throws Exception {
  107 + loginSysAdmin();
  108 +
  109 + doDelete("/api/tenant/" + savedTenant.getId().getId().toString())
  110 + .andExpect(status().isOk());
82 111 }
83 112
84 113 @Test
... ... @@ -239,9 +268,10 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
239 268
240 269 @Test
241 270 public void testFindTenantResourcesByTenantId() throws Exception {
  271 + loginSysAdmin();
242 272 Tenant tenant = new Tenant();
243 273 tenant.setTitle("Test tenant");
244   - tenant = tenantService.saveTenant(tenant);
  274 + tenant = doPost("/api/tenant", tenant, Tenant.class);
245 275
246 276 TenantId tenantId = tenant.getId();
247 277
... ... @@ -279,14 +309,17 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
279 309 Assert.assertFalse(pageData.hasNext());
280 310 Assert.assertTrue(pageData.getData().isEmpty());
281 311
282   - tenantService.deleteTenant(tenantId);
  312 + doDelete("/api/tenant/" + tenantId.getId().toString())
  313 + .andExpect(status().isOk());
283 314 }
284 315
285 316 @Test
286 317 public void testFindAllTenantResourcesByTenantId() throws Exception {
  318 + loginSysAdmin();
  319 +
287 320 Tenant tenant = new Tenant();
288 321 tenant.setTitle("Test tenant");
289   - tenant = tenantService.saveTenant(tenant);
  322 + tenant = doPost("/api/tenant", tenant, Tenant.class);
290 323
291 324 TenantId tenantId = tenant.getId();
292 325
... ... @@ -344,7 +377,8 @@ public abstract class BaseTbResourceServiceTest extends AbstractServiceTest {
344 377 Assert.assertFalse(pageData.hasNext());
345 378 Assert.assertTrue(pageData.getData().isEmpty());
346 379
347   - tenantService.deleteTenant(tenantId);
  380 + doDelete("/api/tenant/" + tenantId.getId().toString())
  381 + .andExpect(status().isOk());
348 382 }
349 383
350 384 }
... ...
  1 +/**
  2 + * Copyright © 2016-2021 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.dao.resource;
  17 +
  18 +import org.thingsboard.server.common.data.ResourceType;
  19 +import org.thingsboard.server.common.data.TbResource;
  20 +import org.thingsboard.server.common.data.TbResourceInfo;
  21 +import org.thingsboard.server.common.data.id.TbResourceId;
  22 +import org.thingsboard.server.common.data.id.TenantId;
  23 +import org.thingsboard.server.common.data.page.PageData;
  24 +import org.thingsboard.server.common.data.page.PageLink;
  25 +
  26 +import java.util.List;
  27 +
  28 +public interface ResourceService {
  29 + TbResource saveResource(TbResource resource);
  30 +
  31 + TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceId);
  32 +
  33 + TbResource findResourceById(TenantId tenantId, TbResourceId resourceId);
  34 +
  35 + TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId);
  36 +
  37 + PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
  38 +
  39 + PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
  40 +
  41 + List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType lwm2mModel, String[] objectIds);
  42 +
  43 + PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType lwm2mModel, PageLink pageLink);
  44 +
  45 + void deleteResource(TenantId tenantId, TbResourceId resourceId);
  46 +
  47 + void deleteResourcesByTenantId(TenantId tenantId);
  48 +
  49 +
  50 +}
... ...
... ... @@ -87,10 +87,6 @@
87 87 <groupId>org.thingsboard</groupId>
88 88 <artifactId>protobuf-dynamic</artifactId>
89 89 </dependency>
90   - <dependency>
91   - <groupId>org.eclipse.leshan</groupId>
92   - <artifactId>leshan-core</artifactId>
93   - </dependency>
94 90 </dependencies>
95 91
96 92 <build>
... ...
... ... @@ -227,10 +227,6 @@
227 227 <groupId>org.elasticsearch.client</groupId>
228 228 <artifactId>rest</artifactId>
229 229 </dependency>
230   - <dependency>
231   - <groupId>org.eclipse.leshan</groupId>
232   - <artifactId>leshan-core</artifactId>
233   - </dependency>
234 230 </dependencies>
235 231 <build>
236 232 <plugins>
... ...
  1 +/**
  2 + * Copyright © 2016-2021 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.dao.resource;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.apache.commons.lang3.StringUtils;
  20 +import org.hibernate.exception.ConstraintViolationException;
  21 +import org.springframework.stereotype.Service;
  22 +import org.thingsboard.server.common.data.ResourceType;
  23 +import org.thingsboard.server.common.data.TbResource;
  24 +import org.thingsboard.server.common.data.TbResourceInfo;
  25 +import org.thingsboard.server.common.data.Tenant;
  26 +import org.thingsboard.server.common.data.id.TbResourceId;
  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.model.ModelConstants;
  32 +import org.thingsboard.server.dao.service.DataValidator;
  33 +import org.thingsboard.server.dao.service.PaginatedRemover;
  34 +import org.thingsboard.server.dao.service.Validator;
  35 +import org.thingsboard.server.dao.tenant.TenantDao;
  36 +
  37 +import java.util.List;
  38 +import java.util.Optional;
  39 +
  40 +import static org.thingsboard.server.dao.device.DeviceServiceImpl.INCORRECT_TENANT_ID;
  41 +import static org.thingsboard.server.dao.service.Validator.validateId;
  42 +
  43 +@Service
  44 +@Slf4j
  45 +public class BaseResourceService implements ResourceService {
  46 +
  47 + public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId ";
  48 + private final TbResourceDao resourceDao;
  49 + private final TbResourceInfoDao resourceInfoDao;
  50 + private final TenantDao tenantDao;
  51 +
  52 +
  53 + public BaseResourceService(TbResourceDao resourceDao, TbResourceInfoDao resourceInfoDao, TenantDao tenantDao) {
  54 + this.resourceDao = resourceDao;
  55 + this.resourceInfoDao = resourceInfoDao;
  56 + this.tenantDao = tenantDao;
  57 + }
  58 +
  59 + @Override
  60 + public TbResource saveResource(TbResource resource) {
  61 + resourceValidator.validate(resource, TbResourceInfo::getTenantId);
  62 +
  63 + try {
  64 + return resourceDao.save(resource.getTenantId(), resource);
  65 + } catch (Exception t) {
  66 + ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
  67 + if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
  68 + String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
  69 + throw new DataValidationException("Resource with such " + field + " already exists!");
  70 + } else {
  71 + throw t;
  72 + }
  73 + }
  74 + }
  75 +
  76 + @Override
  77 + public TbResource getResource(TenantId tenantId, ResourceType resourceType, String resourceKey) {
  78 + log.trace("Executing getResource [{}] [{}] [{}]", tenantId, resourceType, resourceKey);
  79 + return resourceDao.getResource(tenantId, resourceType, resourceKey);
  80 + }
  81 +
  82 + @Override
  83 + public TbResource findResourceById(TenantId tenantId, TbResourceId resourceId) {
  84 + log.trace("Executing findResourceById [{}] [{}]", tenantId, resourceId);
  85 + Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
  86 + return resourceDao.findById(tenantId, resourceId.getId());
  87 + }
  88 +
  89 + @Override
  90 + public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) {
  91 + log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId);
  92 + Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
  93 + return resourceInfoDao.findById(tenantId, resourceId.getId());
  94 + }
  95 +
  96 + @Override
  97 + public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
  98 + log.trace("Executing deleteResource [{}] [{}]", tenantId, resourceId);
  99 + Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
  100 + resourceDao.removeById(tenantId, resourceId.getId());
  101 + }
  102 +
  103 + @Override
  104 + public PageData<TbResourceInfo> findAllTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
  105 + log.trace("Executing findAllTenantResourcesByTenantId [{}]", tenantId);
  106 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  107 + return resourceInfoDao.findAllTenantResourcesByTenantId(tenantId.getId(), pageLink);
  108 + }
  109 +
  110 + @Override
  111 + public PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink) {
  112 + log.trace("Executing findTenantResourcesByTenantId [{}]", tenantId);
  113 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  114 + return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink);
  115 + }
  116 +
  117 + @Override
  118 + public List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType resourceType, String[] objectIds) {
  119 + log.trace("Executing findTenantResourcesByResourceTypeAndObjectIds [{}][{}][{}]", tenantId, resourceType, objectIds);
  120 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  121 + return resourceDao.findResourcesByTenantIdAndResourceType(tenantId, resourceType, objectIds, null);
  122 + }
  123 +
  124 + @Override
  125 + public PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
  126 + log.trace("Executing findTenantResourcesByResourceTypeAndPageLink [{}][{}][{}]", tenantId, resourceType, pageLink);
  127 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  128 + return resourceDao.findResourcesByTenantIdAndResourceType(tenantId, resourceType, pageLink);
  129 + }
  130 +
  131 + @Override
  132 + public void deleteResourcesByTenantId(TenantId tenantId) {
  133 + log.trace("Executing deleteResourcesByTenantId, tenantId [{}]", tenantId);
  134 + validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
  135 + tenantResourcesRemover.removeEntities(tenantId, tenantId);
  136 + }
  137 +
  138 + private DataValidator<TbResource> resourceValidator = new DataValidator<>() {
  139 +
  140 + @Override
  141 + protected void validateDataImpl(TenantId tenantId, TbResource resource) {
  142 + if (StringUtils.isEmpty(resource.getTitle())) {
  143 + throw new DataValidationException("Resource title should be specified!");
  144 + }
  145 + if (resource.getResourceType() == null) {
  146 + throw new DataValidationException("Resource type should be specified!");
  147 + }
  148 + if (StringUtils.isEmpty(resource.getFileName())) {
  149 + throw new DataValidationException("Resource file name should be specified!");
  150 + }
  151 + if (StringUtils.isEmpty(resource.getResourceKey())) {
  152 + throw new DataValidationException("Resource key should be specified!");
  153 + }
  154 + if (resource.getTenantId() == null) {
  155 + resource.setTenantId(new TenantId(ModelConstants.NULL_UUID));
  156 + }
  157 + if (!resource.getTenantId().getId().equals(ModelConstants.NULL_UUID)) {
  158 + Tenant tenant = tenantDao.findById(tenantId, resource.getTenantId().getId());
  159 + if (tenant == null) {
  160 + throw new DataValidationException("Resource is referencing to non-existent tenant!");
  161 + }
  162 + }
  163 + }
  164 + };
  165 +
  166 + private PaginatedRemover<TenantId, TbResource> tenantResourcesRemover =
  167 + new PaginatedRemover<>() {
  168 +
  169 + @Override
  170 + protected PageData<TbResource> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) {
  171 + return resourceDao.findAllByTenantId(id, pageLink);
  172 + }
  173 +
  174 + @Override
  175 + protected void removeEntity(TenantId tenantId, TbResource entity) {
  176 + deleteResource(tenantId, new TbResourceId(entity.getUuidId()));
  177 + }
  178 + };
  179 +
  180 + protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) {
  181 + if (t instanceof ConstraintViolationException) {
  182 + return Optional.of((ConstraintViolationException) t);
  183 + } else if (t.getCause() instanceof ConstraintViolationException) {
  184 + return Optional.of((ConstraintViolationException) (t.getCause()));
  185 + } else {
  186 + return Optional.empty();
  187 + }
  188 + }
  189 +}
... ...
... ... @@ -35,7 +35,7 @@ import org.thingsboard.server.dao.device.DeviceService;
35 35 import org.thingsboard.server.dao.entity.AbstractEntityService;
36 36 import org.thingsboard.server.dao.entityview.EntityViewService;
37 37 import org.thingsboard.server.dao.exception.DataValidationException;
38   -import org.thingsboard.server.dao.resource.TbResourceService;
  38 +import org.thingsboard.server.dao.resource.ResourceService;
39 39 import org.thingsboard.server.dao.rule.RuleChainService;
40 40 import org.thingsboard.server.dao.service.DataValidator;
41 41 import org.thingsboard.server.dao.service.PaginatedRemover;
... ... @@ -90,7 +90,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
90 90 private RuleChainService ruleChainService;
91 91
92 92 @Autowired
93   - private TbResourceService resourceService;
  93 + private ResourceService resourceService;
94 94
95 95 @Override
96 96 public Tenant findTenantById(TenantId tenantId) {
... ...
... ... @@ -54,7 +54,7 @@ import org.thingsboard.server.dao.entity.EntityService;
54 54 import org.thingsboard.server.dao.entityview.EntityViewService;
55 55 import org.thingsboard.server.dao.event.EventService;
56 56 import org.thingsboard.server.dao.relation.RelationService;
57   -import org.thingsboard.server.dao.resource.TbResourceService;
  57 +import org.thingsboard.server.dao.resource.ResourceService;
58 58 import org.thingsboard.server.dao.rule.RuleChainService;
59 59 import org.thingsboard.server.dao.settings.AdminSettingsService;
60 60 import org.thingsboard.server.dao.tenant.TenantProfileService;
... ... @@ -152,9 +152,9 @@ public abstract class AbstractServiceTest {
152 152 protected DeviceProfileService deviceProfileService;
153 153
154 154 @Autowired
155   - protected TbResourceService resourceService;
  155 + protected ResourceService resourceService;
156 156
157   - class IdComparator<D extends HasId> implements Comparator<D> {
  157 + public class IdComparator<D extends HasId> implements Comparator<D> {
158 158 @Override
159 159 public int compare(D o1, D o2) {
160 160 return o1.getId().getId().compareTo(o2.getId().getId());
... ...