Commit 5ab6e886bcd1589961124787cf3233e5d8a0d6d1

Authored by Volodymyr Babak
1 parent 73d6eee4

JPA Dao misc fixes due to test failures

Showing 24 changed files with 97 additions and 374 deletions
  1 +/**
  2 + * Copyright © 2016-2017 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 + */
1 16 package org.thingsboard.server.dao.annotation;
2 17
3 18 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
... ...
  1 +/**
  2 + * Copyright © 2016-2017 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 + */
1 16 package org.thingsboard.server.dao.annotation;
2 17
3 18 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
... ...
... ... @@ -39,7 +39,6 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
39 39 import org.thingsboard.server.dao.customer.CustomerDao;
40 40 import org.thingsboard.server.dao.entity.AbstractEntityService;
41 41 import org.thingsboard.server.dao.exception.DataValidationException;
42   -import org.thingsboard.server.dao.model.*;
43 42 import org.thingsboard.server.dao.relation.EntitySearchDirection;
44 43 import org.thingsboard.server.dao.service.DataValidator;
45 44 import org.thingsboard.server.dao.service.PaginatedRemover;
... ... @@ -150,7 +149,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
150 149 public void deleteAssetsByTenantId(TenantId tenantId) {
151 150 log.trace("Executing deleteAssetsByTenantId, tenantId [{}]", tenantId);
152 151 validateId(tenantId, "Incorrect tenantId " + tenantId);
153   - tenantAssetsRemover.removeEntitites(tenantId);
  152 + tenantAssetsRemover.removeEntities(tenantId);
154 153 }
155 154
156 155 @Override
... ... @@ -188,7 +187,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
188 187 log.trace("Executing unassignCustomerAssets, tenantId [{}], customerId [{}]", tenantId, customerId);
189 188 validateId(tenantId, "Incorrect tenantId " + tenantId);
190 189 validateId(customerId, "Incorrect customerId " + customerId);
191   - new CustomerAssetsUnassigner(tenantId).removeEntitites(customerId);
  190 + new CustomerAssetsUnassigner(tenantId).removeEntities(customerId);
192 191 }
193 192
194 193 @Override
... ...
... ... @@ -135,7 +135,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
135 135 public void deleteCustomersByTenantId(TenantId tenantId) {
136 136 log.trace("Executing deleteCustomersByTenantId, tenantId [{}]", tenantId);
137 137 Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
138   - customersByTenantRemover.removeEntitites(tenantId);
  138 + customersByTenantRemover.removeEntities(tenantId);
139 139 }
140 140
141 141 private DataValidator<Customer> customerValidator =
... ...
... ... @@ -128,7 +128,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
128 128 public void deleteDashboardsByTenantId(TenantId tenantId) {
129 129 log.trace("Executing deleteDashboardsByTenantId, tenantId [{}]", tenantId);
130 130 Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
131   - tenantDashboardsRemover.removeEntitites(tenantId);
  131 + tenantDashboardsRemover.removeEntities(tenantId);
132 132 }
133 133
134 134 @Override
... ... @@ -146,7 +146,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
146 146 log.trace("Executing unassignCustomerDashboards, tenantId [{}], customerId [{}]", tenantId, customerId);
147 147 Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
148 148 Validator.validateId(customerId, "Incorrect customerId " + customerId);
149   - new CustomerDashboardsUnassigner(tenantId).removeEntitites(customerId);
  149 + new CustomerDashboardsUnassigner(tenantId).removeEntities(customerId);
150 150 }
151 151
152 152 private DataValidator<Dashboard> dashboardValidator =
... ...
... ... @@ -168,7 +168,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
168 168 public void deleteDevicesByTenantId(TenantId tenantId) {
169 169 log.trace("Executing deleteDevicesByTenantId, tenantId [{}]", tenantId);
170 170 validateId(tenantId, "Incorrect tenantId " + tenantId);
171   - tenantDevicesRemover.removeEntitites(tenantId);
  171 + tenantDevicesRemover.removeEntities(tenantId);
172 172 }
173 173
174 174 @Override
... ... @@ -207,7 +207,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
207 207 log.trace("Executing unassignCustomerDevices, tenantId [{}], customerId [{}]", tenantId, customerId);
208 208 validateId(tenantId, "Incorrect tenantId " + tenantId);
209 209 validateId(customerId, "Incorrect customerId " + customerId);
210   - new CustomerDevicesUnassigner(tenantId).removeEntitites(customerId);
  210 + new CustomerDevicesUnassigner(tenantId).removeEntities(customerId);
211 211 }
212 212
213 213 @Override
... ...
... ... @@ -83,15 +83,6 @@ public final class DashboardEntity implements SearchTextEntity<Dashboard> {
83 83 }
84 84
85 85 @Override
86   - public UUID getId() {
87   - return id;
88   - }
89   -
90   - @Override
91   - public void setId(UUID id) {
92   - }
93   -
94   - @Override
95 86 public String getSearchTextSource() {
96 87 return title;
97 88 }
... ... @@ -100,10 +91,6 @@ public final class DashboardEntity implements SearchTextEntity<Dashboard> {
100 91 public void setSearchText(String searchText) {
101 92 this.searchText = searchText;
102 93 }
103   -
104   - public String getSearchText() {
105   - return searchText;
106   - }
107 94
108 95 @Override
109 96 public Dashboard toData() {
... ...
... ... @@ -96,10 +96,6 @@ public final class DeviceEntity implements SearchTextEntity<Device> {
96 96 this.searchText = searchText;
97 97 }
98 98
99   - public String getSearchText() {
100   - return searchText;
101   - }
102   -
103 99 @Override
104 100 public Device toData() {
105 101 Device device = new Device(new DeviceId(id));
... ...
... ... @@ -99,7 +99,7 @@ public class UserEntity implements SearchTextEntity<User> {
99 99
100 100 @Override
101 101 public String getSearchTextSource() {
102   - return searchText;
  102 + return email;
103 103 }
104 104
105 105 @Override
... ...
... ... @@ -15,7 +15,6 @@
15 15 */
16 16 package org.thingsboard.server.dao.plugin;
17 17
18   -import com.google.common.util.concurrent.Futures;
19 18 import com.google.common.util.concurrent.ListenableFuture;
20 19 import lombok.extern.slf4j.Slf4j;
21 20 import org.apache.commons.lang3.StringUtils;
... ... @@ -223,7 +222,7 @@ public class BasePluginService extends AbstractEntityService implements PluginSe
223 222 @Override
224 223 public void deletePluginsByTenantId(TenantId tenantId) {
225 224 Validator.validateId(tenantId, "Incorrect tenant id for delete plugins request.");
226   - tenantPluginRemover.removeEntitites(tenantId);
  225 + tenantPluginRemover.removeEntities(tenantId);
227 226 }
228 227
229 228
... ...
... ... @@ -265,7 +265,7 @@ public class BaseRuleService extends AbstractEntityService implements RuleServic
265 265 @Override
266 266 public void deleteRulesByTenantId(TenantId tenantId) {
267 267 validateId(tenantId, "Incorrect tenant id for delete rules request.");
268   - tenantRulesRemover.removeEntitites(tenantId);
  268 + tenantRulesRemover.removeEntities(tenantId);
269 269 }
270 270
271 271 private DataValidator<RuleMetaData> ruleValidator =
... ...
... ... @@ -25,7 +25,7 @@ public abstract class PaginatedRemover<I, D extends IdBased<?>> {
25 25
26 26 private static final int DEFAULT_LIMIT = 100;
27 27
28   - public void removeEntitites(I id) {
  28 + public void removeEntities(I id) {
29 29 TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT);
30 30 boolean hasNext = true;
31 31 while (hasNext) {
... ...
... ... @@ -178,21 +178,21 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
178 178 public Predicate toPredicate(Root<RelationEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
179 179 List<Predicate> predicates = new ArrayList<>();
180 180 if (from != null) {
181   - Predicate fromIdPredicate = criteriaBuilder.equal(root.get(RELATION_FROM_ID_PROPERTY), from.getId());
  181 + Predicate fromIdPredicate = criteriaBuilder.equal(root.get("fromId"), from.getId());
182 182 predicates.add(fromIdPredicate);
183   - Predicate fromEntityTypePredicate = criteriaBuilder.equal(root.get(RELATION_FROM_TYPE_PROPERTY), from.getEntityType().name());
  183 + Predicate fromEntityTypePredicate = criteriaBuilder.equal(root.get("fromType"), from.getEntityType().name());
184 184 predicates.add(fromEntityTypePredicate);
185 185 }
186 186 if (relationType != null) {
187   - Predicate relationTypePredicate = criteriaBuilder.equal(root.get(RELATION_TYPE_PROPERTY), relationType);
  187 + Predicate relationTypePredicate = criteriaBuilder.equal(root.get("relationType"), relationType);
188 188 predicates.add(relationTypePredicate);
189 189 }
190 190 if (typeGroup != null) {
191   - Predicate typeGroupPredicate = criteriaBuilder.equal(root.get(RELATION_TYPE_GROUP_PROPERTY), typeGroup);
  191 + Predicate typeGroupPredicate = criteriaBuilder.equal(root.get("relationTypeGroup"), typeGroup);
192 192 predicates.add(typeGroupPredicate);
193 193 }
194 194 if (childType != null) {
195   - Predicate childTypePredicate = criteriaBuilder.equal(root.get(RELATION_TO_TYPE_PROPERTY), childType.name());
  195 + Predicate childTypePredicate = criteriaBuilder.equal(root.get("toType"), childType.name());
196 196 predicates.add(childTypePredicate);
197 197 }
198 198 return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
... ...
... ... @@ -31,7 +31,7 @@ import java.util.UUID;
31 31 @Component
32 32 @Slf4j
33 33 @SqlDao
34   -public class JpaAdminSettingsDao extends JpaAbstractDao<AdminSettingsEntity, AdminSettings> implements AdminSettingsDao{
  34 +public class JpaAdminSettingsDao extends JpaAbstractDao<AdminSettingsEntity, AdminSettings> implements AdminSettingsDao {
35 35
36 36 @Autowired
37 37 private AdminSettingsRepository adminSettingsRepository;
... ...
... ... @@ -20,10 +20,12 @@ import org.springframework.data.repository.CrudRepository;
20 20 import org.springframework.stereotype.Component;
21 21 import org.thingsboard.server.common.data.User;
22 22 import org.thingsboard.server.common.data.page.TextPageLink;
  23 +import org.thingsboard.server.common.data.security.Authority;
23 24 import org.thingsboard.server.dao.DaoUtil;
24 25 import org.thingsboard.server.dao.annotation.SqlDao;
25 26 import org.thingsboard.server.dao.model.sql.UserEntity;
26 27 import org.thingsboard.server.dao.sql.JpaAbstractDao;
  28 +import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;
27 29 import org.thingsboard.server.dao.user.UserDao;
28 30
29 31 import java.util.List;
... ... @@ -36,7 +38,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
36 38 */
37 39 @Component
38 40 @SqlDao
39   -public class JpaUserDao extends JpaAbstractDao<UserEntity, User> implements UserDao {
  41 +public class JpaUserDao extends JpaAbstractSearchTextDao<UserEntity, User> implements UserDao {
40 42
41 43 @Autowired
42 44 private UserRepository userRepository;
... ... @@ -60,20 +62,26 @@ public class JpaUserDao extends JpaAbstractDao<UserEntity, User> implements User
60 62 public List<User> findTenantAdmins(UUID tenantId, TextPageLink pageLink) {
61 63 return DaoUtil.convertDataList(
62 64 userRepository
63   - .findTenantAdmins(
64   - pageLink.getLimit(),
  65 + .findUsersByAuthority(
65 66 tenantId,
66   - pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset()));
  67 + NULL_UUID,
  68 + pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset(),
  69 + pageLink.getTextSearch(),
  70 + Authority.TENANT_ADMIN.name(),
  71 + pageLink.getLimit()));
67 72 }
68 73
69 74 @Override
70 75 public List<User> findCustomerUsers(UUID tenantId, UUID customerId, TextPageLink pageLink) {
71 76 return DaoUtil.convertDataList(
72 77 userRepository
73   - .findCustomerUsers(
74   - pageLink.getLimit(),
  78 + .findUsersByAuthority(
75 79 tenantId,
76 80 customerId,
77   - pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset()));
  81 + pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset(),
  82 + pageLink.getTextSearch(),
  83 + Authority.CUSTOMER_USER.name(),
  84 + pageLink.getLimit()));
  85 +
78 86 }
79 87 }
... ...
... ... @@ -33,18 +33,14 @@ public interface UserRepository extends CrudRepository<UserEntity, UUID> {
33 33 UserEntity findByEmail(String email);
34 34
35 35 @Query(nativeQuery = true, value = "SELECT * FROM TB_USER WHERE TENANT_ID = :tenantId " +
36   - "AND CUSTOMER_ID IS NULL AND AUTHORITY = 'TENANT_ADMIN' " +
  36 + "AND CUSTOMER_ID = :customerId AND AUTHORITY = :authority " +
  37 + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:searchText, '%'))" +
37 38 "AND ID > :idOffset ORDER BY ID LIMIT :limit")
38   - List<UserEntity> findTenantAdmins(@Param("limit") int limit,
39   - @Param("tenantId") UUID tenantId,
40   - @Param("idOffset") UUID idOffset);
41   -
42   - @Query(nativeQuery = true, value = "SELECT * FROM TB_USER WHERE TENANT_ID = :tenantId " +
43   - "AND CUSTOMER_ID = :customerId AND AUTHORITY = 'CUSTOMER_USER' " +
44   - "AND ID > :idOffset ORDER BY ID LIMIT :limit")
45   - List<UserEntity> findCustomerUsers(@Param("limit") int limit,
46   - @Param("tenantId") UUID tenantId,
47   - @Param("customerId") UUID customerId,
48   - @Param("idOffset") UUID idOffset);
  39 + List<UserEntity> findUsersByAuthority(@Param("tenantId") UUID tenantId,
  40 + @Param("customerId") UUID customerId,
  41 + @Param("idOffset") UUID idOffset,
  42 + @Param("searchText") String searchText,
  43 + @Param("authority") String authority,
  44 + @Param("limit") int limit);
49 45
50 46 }
\ No newline at end of file
... ...
... ... @@ -119,7 +119,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
119 119 @Override
120 120 public void deleteTenants() {
121 121 log.trace("Executing deleteTenants");
122   - tenantsRemover.removeEntitites(DEFAULT_TENANT_REGION);
  122 + tenantsRemover.removeEntities(DEFAULT_TENANT_REGION);
123 123 }
124 124
125 125 private DataValidator<Tenant> tenantValidator =
... ...
... ... @@ -15,14 +15,12 @@
15 15 */
16 16 package org.thingsboard.server.dao.user;
17 17
18   -import static org.thingsboard.server.dao.DaoUtil.getData;
19 18 import static org.thingsboard.server.dao.service.Validator.validateId;
20 19 import static org.thingsboard.server.dao.service.Validator.validatePageLink;
21 20 import static org.thingsboard.server.dao.service.Validator.validateString;
22 21
23 22 import java.util.List;
24 23
25   -import com.google.common.util.concurrent.Futures;
26 24 import com.google.common.util.concurrent.ListenableFuture;
27 25 import lombok.extern.slf4j.Slf4j;
28 26 import org.apache.commons.lang3.RandomStringUtils;
... ... @@ -48,10 +46,6 @@ import org.thingsboard.server.dao.service.DataValidator;
48 46 import org.thingsboard.server.dao.service.PaginatedRemover;
49 47 import org.thingsboard.server.dao.tenant.TenantDao;
50 48
51   -import java.util.List;
52   -
53   -import static org.thingsboard.server.dao.service.Validator.*;
54   -
55 49 @Service
56 50 @Slf4j
57 51 public class UserServiceImpl extends AbstractEntityService implements UserService {
... ... @@ -193,7 +187,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
193 187 public void deleteTenantAdmins(TenantId tenantId) {
194 188 log.trace("Executing deleteTenantAdmins, tenantId [{}]", tenantId);
195 189 validateId(tenantId, "Incorrect tenantId " + tenantId);
196   - tenantAdminsRemover.removeEntitites(tenantId);
  190 + tenantAdminsRemover.removeEntities(tenantId);
197 191 }
198 192
199 193 @Override
... ... @@ -211,7 +205,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
211 205 log.trace("Executing deleteCustomerUsers, customerId [{}]", customerId);
212 206 validateId(tenantId, "Incorrect tenantId " + tenantId);
213 207 validateId(customerId, "Incorrect customerId " + customerId);
214   - new CustomerUsersRemover(tenantId).removeEntitites(customerId);
  208 + new CustomerUsersRemover(tenantId).removeEntities(customerId);
215 209 }
216 210
217 211 private DataValidator<User> userValidator =
... ...
... ... @@ -145,7 +145,7 @@ public class WidgetsBundleServiceImpl implements WidgetsBundleService {
145 145 public void deleteWidgetsBundlesByTenantId(TenantId tenantId) {
146 146 log.trace("Executing deleteWidgetsBundlesByTenantId, tenantId [{}]", tenantId);
147 147 Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
148   - tenantWidgetsBundleRemover.removeEntitites(tenantId);
  148 + tenantWidgetsBundleRemover.removeEntities(tenantId);
149 149 }
150 150
151 151 private DataValidator<WidgetsBundle> widgetsBundleValidator =
... ...
... ... @@ -17,7 +17,7 @@
17 17
18 18 CREATE TABLE IF NOT EXISTS admin_settings (
19 19 id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
20   - json_value varchar,
  20 + json_value text,
21 21 key character varying(255)
22 22 );
23 23
... ... @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS alarm (
25 25 id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
26 26 ack_ts bigint,
27 27 clear_ts bigint,
28   - additional_info varchar,
  28 + additional_info text,
29 29 end_ts bigint,
30 30 originator_id uuid,
31 31 originator_type integer,
... ... @@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS alarm (
39 39
40 40 CREATE TABLE IF NOT EXISTS asset (
41 41 id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
42   - additional_info varchar,
  42 + additional_info text,
43 43 customer_id uuid,
44 44 name character varying(255),
45 45 search_text character varying(255),
... ... @@ -64,7 +64,7 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
64 64 id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
65 65 actions character varying(255),
66 66 clazz character varying(255),
67   - configuration_descriptor varchar,
  67 + configuration_descriptor text,
68 68 name character varying(255),
69 69 scope character varying(255),
70 70 search_text character varying(255),
... ... @@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
73 73
74 74 CREATE TABLE IF NOT EXISTS customer (
75 75 id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
76   - additional_info varchar,
  76 + additional_info text,
77 77 address character varying(255),
78 78 address2 character varying(255),
79 79 city character varying(255),
... ... @@ -89,7 +89,7 @@ CREATE TABLE IF NOT EXISTS customer (
89 89
90 90 CREATE TABLE IF NOT EXISTS dashboard (
91 91 id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
92   - configuration varchar,
  92 + configuration text,
93 93 customer_id uuid,
94 94 search_text character varying(255),
95 95 tenant_id uuid,
... ... @@ -98,7 +98,7 @@ CREATE TABLE IF NOT EXISTS dashboard (
98 98
99 99 CREATE TABLE IF NOT EXISTS device (
100 100 id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
101   - additional_info varchar,
  101 + additional_info text,
102 102 customer_id uuid,
103 103 type character varying(255),
104 104 name character varying(255),
... ... @@ -116,7 +116,7 @@ CREATE TABLE IF NOT EXISTS device_credentials (
116 116
117 117 CREATE TABLE IF NOT EXISTS event (
118 118 id uuid NOT NULL CONSTRAINT event_pkey PRIMARY KEY,
119   - body varchar,
  119 + body text,
120 120 entity_id uuid,
121 121 entity_type character varying(255),
122 122 event_type character varying(255),
... ... @@ -126,10 +126,10 @@ CREATE TABLE IF NOT EXISTS event (
126 126
127 127 CREATE TABLE IF NOT EXISTS plugin (
128 128 id uuid NOT NULL CONSTRAINT plugin_pkey PRIMARY KEY,
129   - additional_info varchar,
  129 + additional_info text,
130 130 api_token character varying(255),
131 131 plugin_class character varying(255),
132   - configuration varchar,
  132 + configuration text,
133 133 name character varying(255),
134 134 public_access boolean,
135 135 search_text character varying(255),
... ... @@ -144,18 +144,18 @@ CREATE TABLE IF NOT EXISTS relation (
144 144 to_type character varying(255),
145 145 relation_type_group character varying(255),
146 146 relation_type character varying(255),
147   - additional_info varchar,
  147 + additional_info text,
148 148 CONSTRAINT relation_unq_key UNIQUE (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
149 149 );
150 150
151 151 CREATE TABLE IF NOT EXISTS rule (
152 152 id uuid NOT NULL CONSTRAINT rule_pkey PRIMARY KEY,
153   - action varchar,
154   - additional_info varchar,
155   - filters varchar,
  153 + action text,
  154 + additional_info text,
  155 + filters text,
156 156 name character varying(255),
157 157 plugin_token character varying(255),
158   - processor varchar,
  158 + processor text,
159 159 search_text character varying(255),
160 160 state character varying(255),
161 161 tenant_id uuid,
... ... @@ -164,7 +164,7 @@ CREATE TABLE IF NOT EXISTS rule (
164 164
165 165 CREATE TABLE IF NOT EXISTS tb_user (
166 166 id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
167   - additional_info varchar,
  167 + additional_info text,
168 168 authority character varying(255),
169 169 customer_id uuid,
170 170 email character varying(255) UNIQUE,
... ... @@ -176,7 +176,7 @@ CREATE TABLE IF NOT EXISTS tb_user (
176 176
177 177 CREATE TABLE IF NOT EXISTS tenant (
178 178 id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
179   - additional_info varchar,
  179 + additional_info text,
180 180 address character varying(255),
181 181 address2 character varying(255),
182 182 city character varying(255),
... ... @@ -227,7 +227,7 @@ CREATE TABLE IF NOT EXISTS widget_type (
227 227 id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
228 228 alias character varying(255),
229 229 bundle_alias character varying(255),
230   - descriptor varchar,
  230 + descriptor text,
231 231 name character varying(255),
232 232 tenant_id uuid
233 233 );
... ...
... ... @@ -16,13 +16,11 @@
16 16 package org.thingsboard.server.dao.service;
17 17
18 18 import com.datastax.driver.core.utils.UUIDs;
19   -import com.google.common.util.concurrent.ListenableFuture;
20 19 import org.junit.After;
21 20 import org.junit.Assert;
22 21 import org.junit.Before;
23 22 import org.junit.Test;
24 23 import org.thingsboard.server.common.data.EntityType;
25   -import org.thingsboard.server.common.data.asset.Asset;
26 24 import org.thingsboard.server.common.data.id.AssetId;
27 25 import org.thingsboard.server.common.data.id.DeviceId;
28 26 import org.thingsboard.server.common.data.relation.EntityRelation;
... ...
1   -/**
2   - * Copyright © 2016-2017 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.service;
17   -
18   -import com.datastax.driver.core.utils.UUIDs;
19   -import com.google.common.util.concurrent.ListenableFuture;
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.EntityType;
25   -import org.thingsboard.server.common.data.asset.Asset;
26   -import org.thingsboard.server.common.data.id.AssetId;
27   -import org.thingsboard.server.common.data.id.DeviceId;
28   -import org.thingsboard.server.common.data.relation.EntityRelation;
29   -import org.thingsboard.server.common.data.relation.RelationTypeGroup;
30   -import org.thingsboard.server.dao.exception.DataValidationException;
31   -import org.thingsboard.server.dao.relation.EntityRelationsQuery;
32   -import org.thingsboard.server.dao.relation.EntitySearchDirection;
33   -import org.thingsboard.server.dao.relation.EntityTypeFilter;
34   -import org.thingsboard.server.dao.relation.RelationsSearchParameters;
35   -
36   -import java.util.Collections;
37   -import java.util.List;
38   -import java.util.concurrent.ExecutionException;
39   -
40   -public class RelationServiceTest extends AbstractServiceTest {
41   -
42   - @Before
43   - public void before() {
44   - }
45   -
46   - @After
47   - public void after() {
48   - }
49   -
50   - @Test
51   - public void testSaveRelation() throws ExecutionException, InterruptedException {
52   - AssetId parentId = new AssetId(UUIDs.timeBased());
53   - AssetId childId = new AssetId(UUIDs.timeBased());
54   -
55   - EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
56   -
57   - Assert.assertTrue(saveRelation(relation));
58   -
59   - Assert.assertTrue(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
60   -
61   - Assert.assertFalse(relationService.checkRelation(parentId, childId, "NOT_EXISTING_TYPE", RelationTypeGroup.COMMON).get());
62   -
63   - Assert.assertFalse(relationService.checkRelation(childId, parentId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
64   -
65   - Assert.assertFalse(relationService.checkRelation(childId, parentId, "NOT_EXISTING_TYPE", RelationTypeGroup.COMMON).get());
66   - }
67   -
68   - @Test
69   - public void testDeleteRelation() throws ExecutionException, InterruptedException {
70   - AssetId parentId = new AssetId(UUIDs.timeBased());
71   - AssetId childId = new AssetId(UUIDs.timeBased());
72   - AssetId subChildId = new AssetId(UUIDs.timeBased());
73   -
74   - EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
75   - EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE);
76   -
77   - saveRelation(relationA);
78   - saveRelation(relationB);
79   -
80   - Assert.assertTrue(relationService.deleteRelation(relationA).get());
81   -
82   - Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
83   -
84   - Assert.assertTrue(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
85   -
86   - Assert.assertTrue(relationService.deleteRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
87   - }
88   -
89   - @Test
90   - public void testDeleteEntityRelations() throws ExecutionException, InterruptedException {
91   - AssetId parentId = new AssetId(UUIDs.timeBased());
92   - AssetId childId = new AssetId(UUIDs.timeBased());
93   - AssetId subChildId = new AssetId(UUIDs.timeBased());
94   -
95   - EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
96   - EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE);
97   -
98   - saveRelation(relationA);
99   - saveRelation(relationB);
100   -
101   - Assert.assertTrue(relationService.deleteEntityRelations(childId).get());
102   -
103   - Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
104   -
105   - Assert.assertFalse(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
106   - }
107   -
108   - @Test
109   - public void testFindFrom() throws ExecutionException, InterruptedException {
110   - AssetId parentA = new AssetId(UUIDs.timeBased());
111   - AssetId parentB = new AssetId(UUIDs.timeBased());
112   - AssetId childA = new AssetId(UUIDs.timeBased());
113   - AssetId childB = new AssetId(UUIDs.timeBased());
114   -
115   - EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
116   - EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
117   -
118   - EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
119   - EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
120   -
121   - saveRelation(relationA1);
122   - saveRelation(relationA2);
123   -
124   - saveRelation(relationB1);
125   - saveRelation(relationB2);
126   -
127   - List<EntityRelation> relations = relationService.findByFrom(parentA, RelationTypeGroup.COMMON).get();
128   - Assert.assertEquals(2, relations.size());
129   - for (EntityRelation relation : relations) {
130   - Assert.assertEquals(EntityRelation.CONTAINS_TYPE, relation.getType());
131   - Assert.assertEquals(parentA, relation.getFrom());
132   - Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
133   - }
134   -
135   - relations = relationService.findByFromAndType(parentA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
136   - Assert.assertEquals(2, relations.size());
137   -
138   - relations = relationService.findByFromAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
139   - Assert.assertEquals(0, relations.size());
140   -
141   - relations = relationService.findByFrom(parentB, RelationTypeGroup.COMMON).get();
142   - Assert.assertEquals(2, relations.size());
143   - for (EntityRelation relation : relations) {
144   - Assert.assertEquals(EntityRelation.MANAGES_TYPE, relation.getType());
145   - Assert.assertEquals(parentB, relation.getFrom());
146   - Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
147   - }
148   -
149   - relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
150   - Assert.assertEquals(0, relations.size());
151   -
152   - relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
153   - Assert.assertEquals(0, relations.size());
154   - }
155   -
156   - private Boolean saveRelation(EntityRelation relationA1) throws ExecutionException, InterruptedException {
157   - return relationService.saveRelation(relationA1).get();
158   - }
159   -
160   - @Test
161   - public void testFindTo() throws ExecutionException, InterruptedException {
162   - AssetId parentA = new AssetId(UUIDs.timeBased());
163   - AssetId parentB = new AssetId(UUIDs.timeBased());
164   - AssetId childA = new AssetId(UUIDs.timeBased());
165   - AssetId childB = new AssetId(UUIDs.timeBased());
166   -
167   - EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
168   - EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
169   -
170   - EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
171   - EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
172   -
173   - saveRelation(relationA1);
174   - saveRelation(relationA2);
175   -
176   - saveRelation(relationB1);
177   - saveRelation(relationB2);
178   -
179   - // Data propagation to views is async
180   - Thread.sleep(3000);
181   -
182   - List<EntityRelation> relations = relationService.findByTo(childA, RelationTypeGroup.COMMON).get();
183   - Assert.assertEquals(2, relations.size());
184   - for (EntityRelation relation : relations) {
185   - Assert.assertEquals(childA, relation.getTo());
186   - Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
187   - }
188   -
189   - relations = relationService.findByToAndType(childA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
190   - Assert.assertEquals(1, relations.size());
191   -
192   - relations = relationService.findByToAndType(childB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
193   - Assert.assertEquals(1, relations.size());
194   -
195   - relations = relationService.findByToAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
196   - Assert.assertEquals(0, relations.size());
197   -
198   - relations = relationService.findByToAndType(parentB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
199   - Assert.assertEquals(0, relations.size());
200   -
201   - relations = relationService.findByTo(childB, RelationTypeGroup.COMMON).get();
202   - Assert.assertEquals(2, relations.size());
203   - for (EntityRelation relation : relations) {
204   - Assert.assertEquals(childB, relation.getTo());
205   - Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
206   - }
207   - }
208   -
209   - @Test
210   - public void testCyclicRecursiveRelation() throws ExecutionException, InterruptedException {
211   - // A -> B -> C -> A
212   - AssetId assetA = new AssetId(UUIDs.timeBased());
213   - AssetId assetB = new AssetId(UUIDs.timeBased());
214   - AssetId assetC = new AssetId(UUIDs.timeBased());
215   -
216   - EntityRelation relationA = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
217   - EntityRelation relationB = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
218   - EntityRelation relationC = new EntityRelation(assetC, assetA, EntityRelation.CONTAINS_TYPE);
219   -
220   - saveRelation(relationA);
221   - saveRelation(relationB);
222   - saveRelation(relationC);
223   -
224   - EntityRelationsQuery query = new EntityRelationsQuery();
225   - query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
226   - query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
227   - List<EntityRelation> relations = relationService.findByQuery(query).get();
228   - Assert.assertEquals(3, relations.size());
229   - Assert.assertTrue(relations.contains(relationA));
230   - Assert.assertTrue(relations.contains(relationB));
231   - Assert.assertTrue(relations.contains(relationC));
232   - }
233   -
234   - @Test
235   - public void testRecursiveRelation() throws ExecutionException, InterruptedException {
236   - // A -> B -> [C,D]
237   - AssetId assetA = new AssetId(UUIDs.timeBased());
238   - AssetId assetB = new AssetId(UUIDs.timeBased());
239   - AssetId assetC = new AssetId(UUIDs.timeBased());
240   - DeviceId deviceD = new DeviceId(UUIDs.timeBased());
241   -
242   - EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
243   - EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
244   - EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE);
245   -
246   -
247   - saveRelation(relationAB);
248   - saveRelation(relationBC);
249   - saveRelation(relationBD);
250   -
251   - EntityRelationsQuery query = new EntityRelationsQuery();
252   - query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
253   - query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
254   - List<EntityRelation> relations = relationService.findByQuery(query).get();
255   - Assert.assertEquals(2, relations.size());
256   - Assert.assertTrue(relations.contains(relationAB));
257   - Assert.assertTrue(relations.contains(relationBC));
258   - }
259   -
260   -
261   - @Test(expected = DataValidationException.class)
262   - public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException {
263   - EntityRelation relation = new EntityRelation();
264   - relation.setTo(new AssetId(UUIDs.timeBased()));
265   - relation.setType(EntityRelation.CONTAINS_TYPE);
266   - Assert.assertTrue(saveRelation(relation));
267   - }
268   -
269   - @Test(expected = DataValidationException.class)
270   - public void testSaveRelationWithEmptyTo() throws ExecutionException, InterruptedException {
271   - EntityRelation relation = new EntityRelation();
272   - relation.setFrom(new AssetId(UUIDs.timeBased()));
273   - relation.setType(EntityRelation.CONTAINS_TYPE);
274   - Assert.assertTrue(saveRelation(relation));
275   - }
276   -
277   - @Test(expected = DataValidationException.class)
278   - public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException {
279   - EntityRelation relation = new EntityRelation();
280   - relation.setFrom(new AssetId(UUIDs.timeBased()));
281   - relation.setTo(new AssetId(UUIDs.timeBased()));
282   - Assert.assertTrue(saveRelation(relation));
283   - }
284   -}
... ... @@ -5,8 +5,6 @@ sql.enabled=true
5 5 spring.jpa.show-sql=false
6 6 spring.jpa.hibernate.ddl-auto=validate
7 7
8   -spring.datasource.driverClassName=org.h2.Driver
9   -spring.datasource.url=jdbc:h2:mem:test;MODE=PostgreSQL
10   -spring.datasource.schema=classpath:postgres/schema.sql
11   -spring.datasource.data=classpath:postgres/system-data.sql;classpath:system-test.sql
12   -
  8 +spring.datasource.url=jdbc:postgresql://localhost:5432/thingsboard
  9 +spring.datasource.username=postgres
  10 +spring.datasource.password=postgres
\ No newline at end of file
... ...
  1 +TRUNCATE TABLE plugin;
  2 +TRUNCATE TABLE rule;
\ No newline at end of file
... ...