JPA Dao misc fixes due to test failures

This commit is contained in:
Volodymyr Babak 2017-06-19 10:18:51 +03:00
parent 73d6eee4fc
commit 5ab6e886bc
24 changed files with 97 additions and 374 deletions

View File

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2017 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.dao.annotation; package org.thingsboard.server.dao.annotation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

View File

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2017 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.dao.annotation; package org.thingsboard.server.dao.annotation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

View File

@ -39,7 +39,6 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.dao.customer.CustomerDao; import org.thingsboard.server.dao.customer.CustomerDao;
import org.thingsboard.server.dao.entity.AbstractEntityService; import org.thingsboard.server.dao.entity.AbstractEntityService;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.model.*;
import org.thingsboard.server.dao.relation.EntitySearchDirection; import org.thingsboard.server.dao.relation.EntitySearchDirection;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover; import org.thingsboard.server.dao.service.PaginatedRemover;
@ -150,7 +149,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
public void deleteAssetsByTenantId(TenantId tenantId) { public void deleteAssetsByTenantId(TenantId tenantId) {
log.trace("Executing deleteAssetsByTenantId, tenantId [{}]", tenantId); log.trace("Executing deleteAssetsByTenantId, tenantId [{}]", tenantId);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
tenantAssetsRemover.removeEntitites(tenantId); tenantAssetsRemover.removeEntities(tenantId);
} }
@Override @Override
@ -188,7 +187,7 @@ public class BaseAssetService extends AbstractEntityService implements AssetServ
log.trace("Executing unassignCustomerAssets, tenantId [{}], customerId [{}]", tenantId, customerId); log.trace("Executing unassignCustomerAssets, tenantId [{}], customerId [{}]", tenantId, customerId);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
validateId(customerId, "Incorrect customerId " + customerId); validateId(customerId, "Incorrect customerId " + customerId);
new CustomerAssetsUnassigner(tenantId).removeEntitites(customerId); new CustomerAssetsUnassigner(tenantId).removeEntities(customerId);
} }
@Override @Override

View File

@ -135,7 +135,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
public void deleteCustomersByTenantId(TenantId tenantId) { public void deleteCustomersByTenantId(TenantId tenantId) {
log.trace("Executing deleteCustomersByTenantId, tenantId [{}]", tenantId); log.trace("Executing deleteCustomersByTenantId, tenantId [{}]", tenantId);
Validator.validateId(tenantId, "Incorrect tenantId " + tenantId); Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
customersByTenantRemover.removeEntitites(tenantId); customersByTenantRemover.removeEntities(tenantId);
} }
private DataValidator<Customer> customerValidator = private DataValidator<Customer> customerValidator =

View File

@ -128,7 +128,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
public void deleteDashboardsByTenantId(TenantId tenantId) { public void deleteDashboardsByTenantId(TenantId tenantId) {
log.trace("Executing deleteDashboardsByTenantId, tenantId [{}]", tenantId); log.trace("Executing deleteDashboardsByTenantId, tenantId [{}]", tenantId);
Validator.validateId(tenantId, "Incorrect tenantId " + tenantId); Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
tenantDashboardsRemover.removeEntitites(tenantId); tenantDashboardsRemover.removeEntities(tenantId);
} }
@Override @Override
@ -146,7 +146,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
log.trace("Executing unassignCustomerDashboards, tenantId [{}], customerId [{}]", tenantId, customerId); log.trace("Executing unassignCustomerDashboards, tenantId [{}], customerId [{}]", tenantId, customerId);
Validator.validateId(tenantId, "Incorrect tenantId " + tenantId); Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
Validator.validateId(customerId, "Incorrect customerId " + customerId); Validator.validateId(customerId, "Incorrect customerId " + customerId);
new CustomerDashboardsUnassigner(tenantId).removeEntitites(customerId); new CustomerDashboardsUnassigner(tenantId).removeEntities(customerId);
} }
private DataValidator<Dashboard> dashboardValidator = private DataValidator<Dashboard> dashboardValidator =

View File

@ -168,7 +168,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
public void deleteDevicesByTenantId(TenantId tenantId) { public void deleteDevicesByTenantId(TenantId tenantId) {
log.trace("Executing deleteDevicesByTenantId, tenantId [{}]", tenantId); log.trace("Executing deleteDevicesByTenantId, tenantId [{}]", tenantId);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
tenantDevicesRemover.removeEntitites(tenantId); tenantDevicesRemover.removeEntities(tenantId);
} }
@Override @Override
@ -207,7 +207,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
log.trace("Executing unassignCustomerDevices, tenantId [{}], customerId [{}]", tenantId, customerId); log.trace("Executing unassignCustomerDevices, tenantId [{}], customerId [{}]", tenantId, customerId);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
validateId(customerId, "Incorrect customerId " + customerId); validateId(customerId, "Incorrect customerId " + customerId);
new CustomerDevicesUnassigner(tenantId).removeEntitites(customerId); new CustomerDevicesUnassigner(tenantId).removeEntities(customerId);
} }
@Override @Override

View File

@ -82,15 +82,6 @@ public final class DashboardEntity implements SearchTextEntity<Dashboard> {
this.configuration = dashboard.getConfiguration(); this.configuration = dashboard.getConfiguration();
} }
@Override
public UUID getId() {
return id;
}
@Override
public void setId(UUID id) {
}
@Override @Override
public String getSearchTextSource() { public String getSearchTextSource() {
return title; return title;
@ -101,10 +92,6 @@ public final class DashboardEntity implements SearchTextEntity<Dashboard> {
this.searchText = searchText; this.searchText = searchText;
} }
public String getSearchText() {
return searchText;
}
@Override @Override
public Dashboard toData() { public Dashboard toData() {
Dashboard dashboard = new Dashboard(new DashboardId(id)); Dashboard dashboard = new Dashboard(new DashboardId(id));

View File

@ -96,10 +96,6 @@ public final class DeviceEntity implements SearchTextEntity<Device> {
this.searchText = searchText; this.searchText = searchText;
} }
public String getSearchText() {
return searchText;
}
@Override @Override
public Device toData() { public Device toData() {
Device device = new Device(new DeviceId(id)); Device device = new Device(new DeviceId(id));

View File

@ -99,7 +99,7 @@ public class UserEntity implements SearchTextEntity<User> {
@Override @Override
public String getSearchTextSource() { public String getSearchTextSource() {
return searchText; return email;
} }
@Override @Override

View File

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.server.dao.plugin; package org.thingsboard.server.dao.plugin;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -223,7 +222,7 @@ public class BasePluginService extends AbstractEntityService implements PluginSe
@Override @Override
public void deletePluginsByTenantId(TenantId tenantId) { public void deletePluginsByTenantId(TenantId tenantId) {
Validator.validateId(tenantId, "Incorrect tenant id for delete plugins request."); Validator.validateId(tenantId, "Incorrect tenant id for delete plugins request.");
tenantPluginRemover.removeEntitites(tenantId); tenantPluginRemover.removeEntities(tenantId);
} }

View File

@ -265,7 +265,7 @@ public class BaseRuleService extends AbstractEntityService implements RuleServic
@Override @Override
public void deleteRulesByTenantId(TenantId tenantId) { public void deleteRulesByTenantId(TenantId tenantId) {
validateId(tenantId, "Incorrect tenant id for delete rules request."); validateId(tenantId, "Incorrect tenant id for delete rules request.");
tenantRulesRemover.removeEntitites(tenantId); tenantRulesRemover.removeEntities(tenantId);
} }
private DataValidator<RuleMetaData> ruleValidator = private DataValidator<RuleMetaData> ruleValidator =

View File

@ -25,7 +25,7 @@ public abstract class PaginatedRemover<I, D extends IdBased<?>> {
private static final int DEFAULT_LIMIT = 100; private static final int DEFAULT_LIMIT = 100;
public void removeEntitites(I id) { public void removeEntities(I id) {
TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT); TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT);
boolean hasNext = true; boolean hasNext = true;
while (hasNext) { while (hasNext) {

View File

@ -178,21 +178,21 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
public Predicate toPredicate(Root<RelationEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { public Predicate toPredicate(Root<RelationEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>(); List<Predicate> predicates = new ArrayList<>();
if (from != null) { if (from != null) {
Predicate fromIdPredicate = criteriaBuilder.equal(root.get(RELATION_FROM_ID_PROPERTY), from.getId()); Predicate fromIdPredicate = criteriaBuilder.equal(root.get("fromId"), from.getId());
predicates.add(fromIdPredicate); predicates.add(fromIdPredicate);
Predicate fromEntityTypePredicate = criteriaBuilder.equal(root.get(RELATION_FROM_TYPE_PROPERTY), from.getEntityType().name()); Predicate fromEntityTypePredicate = criteriaBuilder.equal(root.get("fromType"), from.getEntityType().name());
predicates.add(fromEntityTypePredicate); predicates.add(fromEntityTypePredicate);
} }
if (relationType != null) { if (relationType != null) {
Predicate relationTypePredicate = criteriaBuilder.equal(root.get(RELATION_TYPE_PROPERTY), relationType); Predicate relationTypePredicate = criteriaBuilder.equal(root.get("relationType"), relationType);
predicates.add(relationTypePredicate); predicates.add(relationTypePredicate);
} }
if (typeGroup != null) { if (typeGroup != null) {
Predicate typeGroupPredicate = criteriaBuilder.equal(root.get(RELATION_TYPE_GROUP_PROPERTY), typeGroup); Predicate typeGroupPredicate = criteriaBuilder.equal(root.get("relationTypeGroup"), typeGroup);
predicates.add(typeGroupPredicate); predicates.add(typeGroupPredicate);
} }
if (childType != null) { if (childType != null) {
Predicate childTypePredicate = criteriaBuilder.equal(root.get(RELATION_TO_TYPE_PROPERTY), childType.name()); Predicate childTypePredicate = criteriaBuilder.equal(root.get("toType"), childType.name());
predicates.add(childTypePredicate); predicates.add(childTypePredicate);
} }
return criteriaBuilder.and(predicates.toArray(new Predicate[0])); return criteriaBuilder.and(predicates.toArray(new Predicate[0]));

View File

@ -31,7 +31,7 @@ import java.util.UUID;
@Component @Component
@Slf4j @Slf4j
@SqlDao @SqlDao
public class JpaAdminSettingsDao extends JpaAbstractDao<AdminSettingsEntity, AdminSettings> implements AdminSettingsDao{ public class JpaAdminSettingsDao extends JpaAbstractDao<AdminSettingsEntity, AdminSettings> implements AdminSettingsDao {
@Autowired @Autowired
private AdminSettingsRepository adminSettingsRepository; private AdminSettingsRepository adminSettingsRepository;

View File

@ -20,10 +20,12 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.page.TextPageLink; import org.thingsboard.server.common.data.page.TextPageLink;
import org.thingsboard.server.common.data.security.Authority;
import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.DaoUtil;
import org.thingsboard.server.dao.annotation.SqlDao; import org.thingsboard.server.dao.annotation.SqlDao;
import org.thingsboard.server.dao.model.sql.UserEntity; import org.thingsboard.server.dao.model.sql.UserEntity;
import org.thingsboard.server.dao.sql.JpaAbstractDao; import org.thingsboard.server.dao.sql.JpaAbstractDao;
import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao;
import org.thingsboard.server.dao.user.UserDao; import org.thingsboard.server.dao.user.UserDao;
import java.util.List; import java.util.List;
@ -36,7 +38,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
*/ */
@Component @Component
@SqlDao @SqlDao
public class JpaUserDao extends JpaAbstractDao<UserEntity, User> implements UserDao { public class JpaUserDao extends JpaAbstractSearchTextDao<UserEntity, User> implements UserDao {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@ -60,20 +62,26 @@ public class JpaUserDao extends JpaAbstractDao<UserEntity, User> implements User
public List<User> findTenantAdmins(UUID tenantId, TextPageLink pageLink) { public List<User> findTenantAdmins(UUID tenantId, TextPageLink pageLink) {
return DaoUtil.convertDataList( return DaoUtil.convertDataList(
userRepository userRepository
.findTenantAdmins( .findUsersByAuthority(
pageLink.getLimit(),
tenantId, tenantId,
pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())); NULL_UUID,
pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset(),
pageLink.getTextSearch(),
Authority.TENANT_ADMIN.name(),
pageLink.getLimit()));
} }
@Override @Override
public List<User> findCustomerUsers(UUID tenantId, UUID customerId, TextPageLink pageLink) { public List<User> findCustomerUsers(UUID tenantId, UUID customerId, TextPageLink pageLink) {
return DaoUtil.convertDataList( return DaoUtil.convertDataList(
userRepository userRepository
.findCustomerUsers( .findUsersByAuthority(
pageLink.getLimit(),
tenantId, tenantId,
customerId, customerId,
pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())); pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset(),
pageLink.getTextSearch(),
Authority.CUSTOMER_USER.name(),
pageLink.getLimit()));
} }
} }

View File

@ -33,18 +33,14 @@ public interface UserRepository extends CrudRepository<UserEntity, UUID> {
UserEntity findByEmail(String email); UserEntity findByEmail(String email);
@Query(nativeQuery = true, value = "SELECT * FROM TB_USER WHERE TENANT_ID = :tenantId " + @Query(nativeQuery = true, value = "SELECT * FROM TB_USER WHERE TENANT_ID = :tenantId " +
"AND CUSTOMER_ID IS NULL AND AUTHORITY = 'TENANT_ADMIN' " + "AND CUSTOMER_ID = :customerId AND AUTHORITY = :authority " +
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:searchText, '%'))" +
"AND ID > :idOffset ORDER BY ID LIMIT :limit") "AND ID > :idOffset ORDER BY ID LIMIT :limit")
List<UserEntity> findTenantAdmins(@Param("limit") int limit, List<UserEntity> findUsersByAuthority(@Param("tenantId") UUID tenantId,
@Param("tenantId") UUID tenantId,
@Param("idOffset") UUID idOffset);
@Query(nativeQuery = true, value = "SELECT * FROM TB_USER WHERE TENANT_ID = :tenantId " +
"AND CUSTOMER_ID = :customerId AND AUTHORITY = 'CUSTOMER_USER' " +
"AND ID > :idOffset ORDER BY ID LIMIT :limit")
List<UserEntity> findCustomerUsers(@Param("limit") int limit,
@Param("tenantId") UUID tenantId,
@Param("customerId") UUID customerId, @Param("customerId") UUID customerId,
@Param("idOffset") UUID idOffset); @Param("idOffset") UUID idOffset,
@Param("searchText") String searchText,
@Param("authority") String authority,
@Param("limit") int limit);
} }

View File

@ -119,7 +119,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
@Override @Override
public void deleteTenants() { public void deleteTenants() {
log.trace("Executing deleteTenants"); log.trace("Executing deleteTenants");
tenantsRemover.removeEntitites(DEFAULT_TENANT_REGION); tenantsRemover.removeEntities(DEFAULT_TENANT_REGION);
} }
private DataValidator<Tenant> tenantValidator = private DataValidator<Tenant> tenantValidator =

View File

@ -15,14 +15,12 @@
*/ */
package org.thingsboard.server.dao.user; package org.thingsboard.server.dao.user;
import static org.thingsboard.server.dao.DaoUtil.getData;
import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.service.Validator.validateId;
import static org.thingsboard.server.dao.service.Validator.validatePageLink; import static org.thingsboard.server.dao.service.Validator.validatePageLink;
import static org.thingsboard.server.dao.service.Validator.validateString; import static org.thingsboard.server.dao.service.Validator.validateString;
import java.util.List; import java.util.List;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
@ -48,10 +46,6 @@ import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover; import org.thingsboard.server.dao.service.PaginatedRemover;
import org.thingsboard.server.dao.tenant.TenantDao; import org.thingsboard.server.dao.tenant.TenantDao;
import java.util.List;
import static org.thingsboard.server.dao.service.Validator.*;
@Service @Service
@Slf4j @Slf4j
public class UserServiceImpl extends AbstractEntityService implements UserService { public class UserServiceImpl extends AbstractEntityService implements UserService {
@ -193,7 +187,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
public void deleteTenantAdmins(TenantId tenantId) { public void deleteTenantAdmins(TenantId tenantId) {
log.trace("Executing deleteTenantAdmins, tenantId [{}]", tenantId); log.trace("Executing deleteTenantAdmins, tenantId [{}]", tenantId);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
tenantAdminsRemover.removeEntitites(tenantId); tenantAdminsRemover.removeEntities(tenantId);
} }
@Override @Override
@ -211,7 +205,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
log.trace("Executing deleteCustomerUsers, customerId [{}]", customerId); log.trace("Executing deleteCustomerUsers, customerId [{}]", customerId);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
validateId(customerId, "Incorrect customerId " + customerId); validateId(customerId, "Incorrect customerId " + customerId);
new CustomerUsersRemover(tenantId).removeEntitites(customerId); new CustomerUsersRemover(tenantId).removeEntities(customerId);
} }
private DataValidator<User> userValidator = private DataValidator<User> userValidator =

View File

@ -145,7 +145,7 @@ public class WidgetsBundleServiceImpl implements WidgetsBundleService {
public void deleteWidgetsBundlesByTenantId(TenantId tenantId) { public void deleteWidgetsBundlesByTenantId(TenantId tenantId) {
log.trace("Executing deleteWidgetsBundlesByTenantId, tenantId [{}]", tenantId); log.trace("Executing deleteWidgetsBundlesByTenantId, tenantId [{}]", tenantId);
Validator.validateId(tenantId, "Incorrect tenantId " + tenantId); Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
tenantWidgetsBundleRemover.removeEntitites(tenantId); tenantWidgetsBundleRemover.removeEntities(tenantId);
} }
private DataValidator<WidgetsBundle> widgetsBundleValidator = private DataValidator<WidgetsBundle> widgetsBundleValidator =

View File

@ -17,7 +17,7 @@
CREATE TABLE IF NOT EXISTS admin_settings ( CREATE TABLE IF NOT EXISTS admin_settings (
id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT admin_settings_pkey PRIMARY KEY,
json_value varchar, json_value text,
key character varying(255) key character varying(255)
); );
@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS alarm (
id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT alarm_pkey PRIMARY KEY,
ack_ts bigint, ack_ts bigint,
clear_ts bigint, clear_ts bigint,
additional_info varchar, additional_info text,
end_ts bigint, end_ts bigint,
originator_id uuid, originator_id uuid,
originator_type integer, originator_type integer,
@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS alarm (
CREATE TABLE IF NOT EXISTS asset ( CREATE TABLE IF NOT EXISTS asset (
id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT asset_pkey PRIMARY KEY,
additional_info varchar, additional_info text,
customer_id uuid, customer_id uuid,
name character varying(255), name character varying(255),
search_text character varying(255), search_text character varying(255),
@ -64,7 +64,7 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT component_descriptor_pkey PRIMARY KEY,
actions character varying(255), actions character varying(255),
clazz character varying(255), clazz character varying(255),
configuration_descriptor varchar, configuration_descriptor text,
name character varying(255), name character varying(255),
scope character varying(255), scope character varying(255),
search_text character varying(255), search_text character varying(255),
@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS component_descriptor (
CREATE TABLE IF NOT EXISTS customer ( CREATE TABLE IF NOT EXISTS customer (
id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT customer_pkey PRIMARY KEY,
additional_info varchar, additional_info text,
address character varying(255), address character varying(255),
address2 character varying(255), address2 character varying(255),
city character varying(255), city character varying(255),
@ -89,7 +89,7 @@ CREATE TABLE IF NOT EXISTS customer (
CREATE TABLE IF NOT EXISTS dashboard ( CREATE TABLE IF NOT EXISTS dashboard (
id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT dashboard_pkey PRIMARY KEY,
configuration varchar, configuration text,
customer_id uuid, customer_id uuid,
search_text character varying(255), search_text character varying(255),
tenant_id uuid, tenant_id uuid,
@ -98,7 +98,7 @@ CREATE TABLE IF NOT EXISTS dashboard (
CREATE TABLE IF NOT EXISTS device ( CREATE TABLE IF NOT EXISTS device (
id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT device_pkey PRIMARY KEY,
additional_info varchar, additional_info text,
customer_id uuid, customer_id uuid,
type character varying(255), type character varying(255),
name character varying(255), name character varying(255),
@ -116,7 +116,7 @@ CREATE TABLE IF NOT EXISTS device_credentials (
CREATE TABLE IF NOT EXISTS event ( CREATE TABLE IF NOT EXISTS event (
id uuid NOT NULL CONSTRAINT event_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT event_pkey PRIMARY KEY,
body varchar, body text,
entity_id uuid, entity_id uuid,
entity_type character varying(255), entity_type character varying(255),
event_type character varying(255), event_type character varying(255),
@ -126,10 +126,10 @@ CREATE TABLE IF NOT EXISTS event (
CREATE TABLE IF NOT EXISTS plugin ( CREATE TABLE IF NOT EXISTS plugin (
id uuid NOT NULL CONSTRAINT plugin_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT plugin_pkey PRIMARY KEY,
additional_info varchar, additional_info text,
api_token character varying(255), api_token character varying(255),
plugin_class character varying(255), plugin_class character varying(255),
configuration varchar, configuration text,
name character varying(255), name character varying(255),
public_access boolean, public_access boolean,
search_text character varying(255), search_text character varying(255),
@ -144,18 +144,18 @@ CREATE TABLE IF NOT EXISTS relation (
to_type character varying(255), to_type character varying(255),
relation_type_group character varying(255), relation_type_group character varying(255),
relation_type character varying(255), relation_type character varying(255),
additional_info varchar, additional_info text,
CONSTRAINT relation_unq_key UNIQUE (from_id, from_type, relation_type_group, relation_type, to_id, to_type) CONSTRAINT relation_unq_key UNIQUE (from_id, from_type, relation_type_group, relation_type, to_id, to_type)
); );
CREATE TABLE IF NOT EXISTS rule ( CREATE TABLE IF NOT EXISTS rule (
id uuid NOT NULL CONSTRAINT rule_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT rule_pkey PRIMARY KEY,
action varchar, action text,
additional_info varchar, additional_info text,
filters varchar, filters text,
name character varying(255), name character varying(255),
plugin_token character varying(255), plugin_token character varying(255),
processor varchar, processor text,
search_text character varying(255), search_text character varying(255),
state character varying(255), state character varying(255),
tenant_id uuid, tenant_id uuid,
@ -164,7 +164,7 @@ CREATE TABLE IF NOT EXISTS rule (
CREATE TABLE IF NOT EXISTS tb_user ( CREATE TABLE IF NOT EXISTS tb_user (
id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT tb_user_pkey PRIMARY KEY,
additional_info varchar, additional_info text,
authority character varying(255), authority character varying(255),
customer_id uuid, customer_id uuid,
email character varying(255) UNIQUE, email character varying(255) UNIQUE,
@ -176,7 +176,7 @@ CREATE TABLE IF NOT EXISTS tb_user (
CREATE TABLE IF NOT EXISTS tenant ( CREATE TABLE IF NOT EXISTS tenant (
id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT tenant_pkey PRIMARY KEY,
additional_info varchar, additional_info text,
address character varying(255), address character varying(255),
address2 character varying(255), address2 character varying(255),
city character varying(255), city character varying(255),
@ -227,7 +227,7 @@ CREATE TABLE IF NOT EXISTS widget_type (
id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT widget_type_pkey PRIMARY KEY,
alias character varying(255), alias character varying(255),
bundle_alias character varying(255), bundle_alias character varying(255),
descriptor varchar, descriptor text,
name character varying(255), name character varying(255),
tenant_id uuid tenant_id uuid
); );

View File

@ -16,13 +16,11 @@
package org.thingsboard.server.dao.service; package org.thingsboard.server.dao.service;
import com.datastax.driver.core.utils.UUIDs; import com.datastax.driver.core.utils.UUIDs;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntityRelation;

View File

@ -1,284 +0,0 @@
/**
* Copyright © 2016-2017 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.dao.service;
import com.datastax.driver.core.utils.UUIDs;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.relation.EntityRelationsQuery;
import org.thingsboard.server.dao.relation.EntitySearchDirection;
import org.thingsboard.server.dao.relation.EntityTypeFilter;
import org.thingsboard.server.dao.relation.RelationsSearchParameters;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class RelationServiceTest extends AbstractServiceTest {
@Before
public void before() {
}
@After
public void after() {
}
@Test
public void testSaveRelation() throws ExecutionException, InterruptedException {
AssetId parentId = new AssetId(UUIDs.timeBased());
AssetId childId = new AssetId(UUIDs.timeBased());
EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
Assert.assertTrue(saveRelation(relation));
Assert.assertTrue(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
Assert.assertFalse(relationService.checkRelation(parentId, childId, "NOT_EXISTING_TYPE", RelationTypeGroup.COMMON).get());
Assert.assertFalse(relationService.checkRelation(childId, parentId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
Assert.assertFalse(relationService.checkRelation(childId, parentId, "NOT_EXISTING_TYPE", RelationTypeGroup.COMMON).get());
}
@Test
public void testDeleteRelation() throws ExecutionException, InterruptedException {
AssetId parentId = new AssetId(UUIDs.timeBased());
AssetId childId = new AssetId(UUIDs.timeBased());
AssetId subChildId = new AssetId(UUIDs.timeBased());
EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE);
saveRelation(relationA);
saveRelation(relationB);
Assert.assertTrue(relationService.deleteRelation(relationA).get());
Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
Assert.assertTrue(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
Assert.assertTrue(relationService.deleteRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
}
@Test
public void testDeleteEntityRelations() throws ExecutionException, InterruptedException {
AssetId parentId = new AssetId(UUIDs.timeBased());
AssetId childId = new AssetId(UUIDs.timeBased());
AssetId subChildId = new AssetId(UUIDs.timeBased());
EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE);
saveRelation(relationA);
saveRelation(relationB);
Assert.assertTrue(relationService.deleteEntityRelations(childId).get());
Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
Assert.assertFalse(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
}
@Test
public void testFindFrom() throws ExecutionException, InterruptedException {
AssetId parentA = new AssetId(UUIDs.timeBased());
AssetId parentB = new AssetId(UUIDs.timeBased());
AssetId childA = new AssetId(UUIDs.timeBased());
AssetId childB = new AssetId(UUIDs.timeBased());
EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
saveRelation(relationA1);
saveRelation(relationA2);
saveRelation(relationB1);
saveRelation(relationB2);
List<EntityRelation> relations = relationService.findByFrom(parentA, RelationTypeGroup.COMMON).get();
Assert.assertEquals(2, relations.size());
for (EntityRelation relation : relations) {
Assert.assertEquals(EntityRelation.CONTAINS_TYPE, relation.getType());
Assert.assertEquals(parentA, relation.getFrom());
Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
}
relations = relationService.findByFromAndType(parentA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(2, relations.size());
relations = relationService.findByFromAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(0, relations.size());
relations = relationService.findByFrom(parentB, RelationTypeGroup.COMMON).get();
Assert.assertEquals(2, relations.size());
for (EntityRelation relation : relations) {
Assert.assertEquals(EntityRelation.MANAGES_TYPE, relation.getType());
Assert.assertEquals(parentB, relation.getFrom());
Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
}
relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(0, relations.size());
relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(0, relations.size());
}
private Boolean saveRelation(EntityRelation relationA1) throws ExecutionException, InterruptedException {
return relationService.saveRelation(relationA1).get();
}
@Test
public void testFindTo() throws ExecutionException, InterruptedException {
AssetId parentA = new AssetId(UUIDs.timeBased());
AssetId parentB = new AssetId(UUIDs.timeBased());
AssetId childA = new AssetId(UUIDs.timeBased());
AssetId childB = new AssetId(UUIDs.timeBased());
EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
saveRelation(relationA1);
saveRelation(relationA2);
saveRelation(relationB1);
saveRelation(relationB2);
// Data propagation to views is async
Thread.sleep(3000);
List<EntityRelation> relations = relationService.findByTo(childA, RelationTypeGroup.COMMON).get();
Assert.assertEquals(2, relations.size());
for (EntityRelation relation : relations) {
Assert.assertEquals(childA, relation.getTo());
Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
}
relations = relationService.findByToAndType(childA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(1, relations.size());
relations = relationService.findByToAndType(childB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(1, relations.size());
relations = relationService.findByToAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(0, relations.size());
relations = relationService.findByToAndType(parentB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get();
Assert.assertEquals(0, relations.size());
relations = relationService.findByTo(childB, RelationTypeGroup.COMMON).get();
Assert.assertEquals(2, relations.size());
for (EntityRelation relation : relations) {
Assert.assertEquals(childB, relation.getTo());
Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
}
}
@Test
public void testCyclicRecursiveRelation() throws ExecutionException, InterruptedException {
// A -> B -> C -> A
AssetId assetA = new AssetId(UUIDs.timeBased());
AssetId assetB = new AssetId(UUIDs.timeBased());
AssetId assetC = new AssetId(UUIDs.timeBased());
EntityRelation relationA = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
EntityRelation relationB = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
EntityRelation relationC = new EntityRelation(assetC, assetA, EntityRelation.CONTAINS_TYPE);
saveRelation(relationA);
saveRelation(relationB);
saveRelation(relationC);
EntityRelationsQuery query = new EntityRelationsQuery();
query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
List<EntityRelation> relations = relationService.findByQuery(query).get();
Assert.assertEquals(3, relations.size());
Assert.assertTrue(relations.contains(relationA));
Assert.assertTrue(relations.contains(relationB));
Assert.assertTrue(relations.contains(relationC));
}
@Test
public void testRecursiveRelation() throws ExecutionException, InterruptedException {
// A -> B -> [C,D]
AssetId assetA = new AssetId(UUIDs.timeBased());
AssetId assetB = new AssetId(UUIDs.timeBased());
AssetId assetC = new AssetId(UUIDs.timeBased());
DeviceId deviceD = new DeviceId(UUIDs.timeBased());
EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE);
saveRelation(relationAB);
saveRelation(relationBC);
saveRelation(relationBD);
EntityRelationsQuery query = new EntityRelationsQuery();
query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
List<EntityRelation> relations = relationService.findByQuery(query).get();
Assert.assertEquals(2, relations.size());
Assert.assertTrue(relations.contains(relationAB));
Assert.assertTrue(relations.contains(relationBC));
}
@Test(expected = DataValidationException.class)
public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException {
EntityRelation relation = new EntityRelation();
relation.setTo(new AssetId(UUIDs.timeBased()));
relation.setType(EntityRelation.CONTAINS_TYPE);
Assert.assertTrue(saveRelation(relation));
}
@Test(expected = DataValidationException.class)
public void testSaveRelationWithEmptyTo() throws ExecutionException, InterruptedException {
EntityRelation relation = new EntityRelation();
relation.setFrom(new AssetId(UUIDs.timeBased()));
relation.setType(EntityRelation.CONTAINS_TYPE);
Assert.assertTrue(saveRelation(relation));
}
@Test(expected = DataValidationException.class)
public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException {
EntityRelation relation = new EntityRelation();
relation.setFrom(new AssetId(UUIDs.timeBased()));
relation.setTo(new AssetId(UUIDs.timeBased()));
Assert.assertTrue(saveRelation(relation));
}
}

View File

@ -5,8 +5,6 @@ sql.enabled=true
spring.jpa.show-sql=false spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate spring.jpa.hibernate.ddl-auto=validate
spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/thingsboard
spring.datasource.url=jdbc:h2:mem:test;MODE=PostgreSQL spring.datasource.username=postgres
spring.datasource.schema=classpath:postgres/schema.sql spring.datasource.password=postgres
spring.datasource.data=classpath:postgres/system-data.sql;classpath:system-test.sql

View File

@ -0,0 +1,2 @@
TRUNCATE TABLE plugin;
TRUNCATE TABLE rule;