Browse Source

Merge pull request #9526 from dskarzh/feature/widget-tags-search-improvements

[3.6.1] New widget type and widget bundle tag searching logic
pull/9548/head
Andrew Shvayka 3 years ago
committed by GitHub
parent
commit
0074e49b86
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 81
      dao/src/main/java/org/thingsboard/server/dao/sql/widget/WidgetTypeInfoRepository.java
  2. 40
      dao/src/main/java/org/thingsboard/server/dao/sql/widget/WidgetsBundleRepository.java
  3. 173
      dao/src/test/java/org/thingsboard/server/dao/sql/widget/JpaWidgetTypeDaoTest.java
  4. 122
      dao/src/test/java/org/thingsboard/server/dao/sql/widget/JpaWidgetsBundleDaoTest.java

81
dao/src/main/java/org/thingsboard/server/dao/sql/widget/WidgetTypeInfoRepository.java

@ -33,14 +33,29 @@ public interface WidgetTypeInfoRepository extends JpaRepository<WidgetTypeInfoEn
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))",
// "OR to_tsvector(lower(array_to_string(wti.tags, ' '))) @@ to_tsquery(lower(:searchText)))))",
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))",
countQuery = "SELECT count(*) FROM widget_type_info_view wti WHERE wti.tenant_id = :systemTenantId " +
"AND ((:deprecatedFilterEnabled) IS FALSE OR wti.deprecated = :deprecatedFilter) " +
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))"
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))"
)
Page<WidgetTypeInfoEntity> findSystemWidgetTypes(@Param("systemTenantId") UUID systemTenantId,
@Param("searchText") String searchText,
@ -57,13 +72,29 @@ public interface WidgetTypeInfoRepository extends JpaRepository<WidgetTypeInfoEn
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))",
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))",
countQuery = "SELECT count(*) FROM widget_type_info_view wti WHERE wti.tenant_id IN (:tenantId, :nullTenantId) " +
"AND ((:deprecatedFilterEnabled) IS FALSE OR wti.deprecated = :deprecatedFilter) " +
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))"
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))"
)
Page<WidgetTypeInfoEntity> findAllTenantWidgetTypesByTenantId(@Param("tenantId") UUID tenantId,
@Param("nullTenantId") UUID nullTenantId,
@ -81,13 +112,29 @@ public interface WidgetTypeInfoRepository extends JpaRepository<WidgetTypeInfoEn
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))",
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))",
countQuery = "SELECT count(*) FROM widget_type_info_view wti WHERE wti.tenant_id = :tenantId " +
"AND ((:deprecatedFilterEnabled) IS FALSE OR wti.deprecated = :deprecatedFilter) " +
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))"
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))"
)
Page<WidgetTypeInfoEntity> findTenantWidgetTypesByTenantId(@Param("tenantId") UUID tenantId,
@Param("searchText") String searchText,
@ -111,7 +158,15 @@ public interface WidgetTypeInfoRepository extends JpaRepository<WidgetTypeInfoEn
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' ')))) " +
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
")))) " +
"ORDER BY wbw.widget_type_order",
countQuery = "SELECT count(*) FROM widget_type_info_view wti, widgets_bundle_widget wbw " +
"WHERE wbw.widgets_bundle_id = :widgetsBundleId " +
@ -120,7 +175,15 @@ public interface WidgetTypeInfoRepository extends JpaRepository<WidgetTypeInfoEn
"AND ((:widgetTypesEmpty) IS TRUE OR wti.widget_type IN (:widgetTypes)) " +
"AND (wti.name ILIKE CONCAT('%', :searchText, '%') " +
"OR ((:fullSearch) IS TRUE AND (wti.description ILIKE CONCAT('%', :searchText, '%') " +
"OR lower(wti.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:searchText), ' '))))"
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wti.tags) AS currentTag " +
"WHERE :searchText ILIKE '%' || currentTag || '%' " +
"AND (length(:searchText) = length(currentTag) " +
"OR :searchText ILIKE currentTag || ' %' " +
"OR :searchText ILIKE '% ' || currentTag " +
"OR :searchText ILIKE '% ' || currentTag || ' %')" +
"))))"
)
Page<WidgetTypeInfoEntity> findWidgetTypesInfosByWidgetsBundleId(@Param("widgetsBundleId") UUID widgetsBundleId,
@Param("searchText") String searchText,

40
dao/src/main/java/org/thingsboard/server/dao/sql/widget/WidgetsBundleRepository.java

@ -47,7 +47,15 @@ public interface WidgetsBundleRepository extends JpaRepository<WidgetsBundleEnti
"WHERE wtd.id = wbw.widget_type_id " +
"AND (:textSearch IS NULL OR wtd.name ILIKE CONCAT('%', :textSearch, '%') " +
"OR wtd.description ILIKE CONCAT('%', :textSearch, '%') " +
"OR lower(wtd.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:textSearch), ' '))))",
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wtd.tags) AS currentTag " +
"WHERE :textSearch ILIKE '%' || currentTag || '%' " +
"AND (length(:textSearch) = length(currentTag) " +
"OR :textSearch ILIKE currentTag || ' %' " +
"OR :textSearch ILIKE '% ' || currentTag " +
"OR :textSearch ILIKE '% ' || currentTag || ' %')" +
"))))",
countQuery = "SELECT count(*) FROM widgets_bundle wb WHERE wb.tenant_id = :systemTenantId " +
"AND (:textSearch IS NULL OR wb.title ILIKE CONCAT('%', :textSearch, '%') " +
"OR wb.description ILIKE CONCAT('%', :textSearch, '%') " +
@ -55,7 +63,15 @@ public interface WidgetsBundleRepository extends JpaRepository<WidgetsBundleEnti
"WHERE wtd.id = wbw.widget_type_id " +
"AND (:textSearch IS NULL OR wtd.name ILIKE CONCAT('%', :textSearch, '%') " +
"OR wtd.description ILIKE CONCAT('%', :textSearch, '%') " +
"OR lower(wtd.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:textSearch), ' '))))"
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wtd.tags) AS currentTag " +
"WHERE :textSearch ILIKE '%' || currentTag || '%' " +
"AND (length(:textSearch) = length(currentTag) " +
"OR :textSearch ILIKE currentTag || ' %' " +
"OR :textSearch ILIKE '% ' || currentTag " +
"OR :textSearch ILIKE '% ' || currentTag || ' %')" +
"))))"
)
Page<WidgetsBundleEntity> findSystemWidgetsBundlesFullSearch(@Param("systemTenantId") UUID systemTenantId,
@Param("textSearch") String textSearch,
@ -81,7 +97,15 @@ public interface WidgetsBundleRepository extends JpaRepository<WidgetsBundleEnti
"WHERE wtd.id = wbw.widget_type_id " +
"AND (:textSearch IS NULL OR wtd.name ILIKE CONCAT('%', :textSearch, '%') " +
"OR wtd.description ILIKE CONCAT('%', :textSearch, '%') " +
"OR lower(wtd.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:textSearch), ' '))))",
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wtd.tags) AS currentTag " +
"WHERE :textSearch ILIKE '%' || currentTag || '%' " +
"AND (length(:textSearch) = length(currentTag) " +
"OR :textSearch ILIKE currentTag || ' %' " +
"OR :textSearch ILIKE '% ' || currentTag " +
"OR :textSearch ILIKE '% ' || currentTag || ' %')" +
"))))",
countQuery = "SELECT count(*) FROM widgets_bundle wb WHERE wb.tenant_id IN (:tenantIds) " +
"AND (:textSearch IS NULL OR wb.title ILIKE CONCAT('%', :textSearch, '%') " +
"OR wb.description ILIKE CONCAT('%', :textSearch, '%') " +
@ -89,7 +113,15 @@ public interface WidgetsBundleRepository extends JpaRepository<WidgetsBundleEnti
"WHERE wtd.id = wbw.widget_type_id " +
"AND (:textSearch IS NULL OR wtd.name ILIKE CONCAT('%', :textSearch, '%') " +
"OR wtd.description ILIKE CONCAT('%', :textSearch, '%') " +
"OR lower(wtd.tags\\:\\:text)\\:\\:text[] && string_to_array(lower(:textSearch), ' '))))"
"OR EXISTS (" +
"SELECT 1 " +
"FROM unnest(wtd.tags) AS currentTag " +
"WHERE :textSearch ILIKE '%' || currentTag || '%' " +
"AND (length(:textSearch) = length(currentTag) " +
"OR :textSearch ILIKE currentTag || ' %' " +
"OR :textSearch ILIKE '% ' || currentTag " +
"OR :textSearch ILIKE '% ' || currentTag || ' %')" +
"))))"
)
Page<WidgetsBundleEntity> findAllTenantWidgetsBundlesByTenantIdsFullSearch(@Param("tenantIds") List<UUID> tenantIds,
@Param("textSearch") String textSearch,

173
dao/src/test/java/org/thingsboard/server/dao/sql/widget/JpaWidgetTypeDaoTest.java

@ -41,8 +41,10 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -51,6 +53,26 @@ import static org.junit.Assert.assertNotNull;
*/
public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
// given search text should find a widget with tags, when searching by tags
final Map<String, String[]> SHOULD_FIND_SEARCH_TO_TAGS_MAP = Map.of(
"A", new String[]{"a", "b", "c"},
"test A test", new String[]{"a", "b", "c"},
"test x y test", new String[]{"x y", "b", "c"},
"x y test", new String[]{"x y", "b", "c"},
"x y", new String[]{"x y", "b", "c"},
"test x y", new String[]{"x y", "b", "c"}
);
// given search text should not find a widget with tags, when searching by tags
final Map<String, String[]> SHOULDNT_FIND_SEARCH_TO_TAGS_MAP = Map.of(
"testA test", new String[]{"a", "b", "c"},
"testx y test", new String[]{"x y", "b", "c"},
"testx ytest", new String[]{"x y", "b", "c"},
"x ytest", new String[]{"x y", "b", "c"},
"testx y", new String[]{"x y", "b", "c"},
"x", new String[]{"x y", "b", "c"}
);
final String BUNDLE_ALIAS = "BUNDLE_ALIAS";
final int WIDGET_TYPE_COUNT = 3;
List<WidgetTypeDetails> widgetTypeList;
@ -96,6 +118,19 @@ public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
return widgetTypeDao.save(TenantId.SYS_TENANT_ID, widgetType);
}
WidgetTypeDetails createAndSaveWidgetType(TenantId tenantId, int number, String[] tags) {
WidgetTypeDetails widgetType = new WidgetTypeDetails();
widgetType.setTenantId(tenantId);
widgetType.setName("WIDGET_TYPE_" + number);
widgetType.setDescription("WIDGET_TYPE_DESCRIPTION" + number);
widgetType.setFqn("FQN_" + number);
var descriptor = JacksonUtil.newObjectNode();
descriptor.put("type", number % 2 == 0 ? "latest" : "static");
widgetType.setDescriptor(descriptor);
widgetType.setTags(tags);
return widgetTypeDao.save(TenantId.SYS_TENANT_ID, widgetType);
}
@After
public void tearDown() {
widgetsBundleDao.removeById(TenantId.SYS_TENANT_ID, widgetsBundle.getUuidId());
@ -123,6 +158,40 @@ public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
assertEquals(new WidgetTypeInfo(widgetTypeList.get(2)), widgetTypes.getData().get(0));
}
@Test
public void testTagsSearchInFindBySystemWidgetTypes() {
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findSystemWidgetTypes(
TenantId.SYS_TENANT_ID, true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(1);
assertThat(widgetTypes.getData().get(0).getId()).isEqualTo(savedWidgetType.getId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
for (var entry : SHOULDNT_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findSystemWidgetTypes(
TenantId.SYS_TENANT_ID, true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(0);
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
}
@Test
public void testFindTenantWidgetTypesByTenantId() {
UUID tenantId = Uuids.timeBased();
@ -138,6 +207,74 @@ public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
assertEquals(new WidgetTypeInfo(widgetTypeList.get(5)), widgetTypes.getData().get(2));
}
@Test
public void testTagsSearchInFindTenantWidgetTypesByTenantId() {
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findTenantWidgetTypesByTenantId(
TenantId.SYS_TENANT_ID.getId(), true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(1);
assertThat(widgetTypes.getData().get(0).getId()).isEqualTo(savedWidgetType.getId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
for (var entry : SHOULDNT_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findTenantWidgetTypesByTenantId(
TenantId.SYS_TENANT_ID.getId(), true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(0);
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
}
@Test
public void testTagsSearchInFindAllTenantWidgetTypesByTenantId() {
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findAllTenantWidgetTypesByTenantId(
TenantId.SYS_TENANT_ID.getId(), true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(1);
assertThat(widgetTypes.getData().get(0).getId()).isEqualTo(savedWidgetType.getId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
for (var entry : SHOULDNT_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findAllTenantWidgetTypesByTenantId(
TenantId.SYS_TENANT_ID.getId(), true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(0);
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
}
@Test
public void testFindByWidgetTypeInfosByBundleId() {
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findWidgetTypesInfosByWidgetsBundleId(TenantId.SYS_TENANT_ID.getId(), widgetsBundle.getUuidId(),true, DeprecatedFilter.ALL, Collections.singletonList("latest"),
@ -152,6 +289,42 @@ public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest {
assertEquals(new WidgetTypeInfo(widgetTypeList.get(0)), widgetTypes.getData().get(0));
}
@Test
public void testTagsSearchInFindByWidgetTypeInfosByBundleId() {
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
widgetTypeDao.saveWidgetsBundleWidget(new WidgetsBundleWidget(this.widgetsBundle.getId(), savedWidgetType.getId(), WIDGET_TYPE_COUNT + 1));
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findWidgetTypesInfosByWidgetsBundleId(
TenantId.SYS_TENANT_ID.getId(), widgetsBundle.getUuidId(), true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(1);
assertThat(widgetTypes.getData().get(0).getId()).isEqualTo(savedWidgetType.getId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
for (var entry : SHOULDNT_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetTypeDetails savedWidgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, WIDGET_TYPE_COUNT + 1, tags);
widgetTypeDao.saveWidgetsBundleWidget(new WidgetsBundleWidget(this.widgetsBundle.getId(), savedWidgetType.getId(), WIDGET_TYPE_COUNT + 1));
PageData<WidgetTypeInfo> widgetTypes = widgetTypeDao.findWidgetTypesInfosByWidgetsBundleId(
TenantId.SYS_TENANT_ID.getId(), widgetsBundle.getUuidId(), true, DeprecatedFilter.ALL, null, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(0);
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, savedWidgetType.getUuidId());
}
}
@Test
public void testFindByTenantIdAndFqn() {
WidgetType result = widgetTypeList.get(0);

122
dao/src/test/java/org/thingsboard/server/dao/sql/widget/JpaWidgetsBundleDaoTest.java

@ -36,9 +36,11 @@ import org.thingsboard.server.dao.widget.WidgetsBundleDao;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
/**
@ -46,6 +48,27 @@ import static org.junit.Assert.assertEquals;
*/
public class JpaWidgetsBundleDaoTest extends AbstractJpaDaoTest {
// given search text should find a widget with tags, when searching by tags
final Map<String, String[]> SHOULD_FIND_SEARCH_TO_TAGS_MAP = Map.of(
"A", new String[]{"a", "b", "c"},
"test A test", new String[]{"a", "b", "c"},
"test x y test", new String[]{"x y", "b", "c"},
"x y test", new String[]{"x y", "b", "c"},
"x y", new String[]{"x y", "b", "c"},
"test x y", new String[]{"x y", "b", "c"}
);
// given search text should not find a widget with tags, when searching by tags
final Map<String, String[]> SHOULDNT_FIND_SEARCH_TO_TAGS_MAP = Map.of(
"testA test", new String[]{"a", "b", "c"},
"testx y test", new String[]{"x y", "b", "c"},
"testx ytest", new String[]{"x y", "b", "c"},
"x ytest", new String[]{"x y", "b", "c"},
"testx y", new String[]{"x y", "b", "c"},
"x", new String[]{"x y", "b", "c"}
);
List<WidgetsBundle> widgetsBundles;
List<WidgetType> widgetTypeList;
@ -140,6 +163,51 @@ public class JpaWidgetsBundleDaoTest extends AbstractJpaDaoTest {
assertEquals(widgetsBundle3, widgetsBundles3.getData().get(1));
}
@Test
public void testTagsSearchInFindBySystemWidgetTypes() {
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetsBundle systemWidgetBundle = createSystemWidgetBundle("Test Widget Bundle Alias", "Test Widget Bundle Title");
WidgetType widgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, 0, "Test Widget Type Name", "Test Widget Type Description", tags);
widgetTypeDao.saveWidgetsBundleWidget(new WidgetsBundleWidget(systemWidgetBundle.getId(), widgetType.getId(), 0));
PageData<WidgetsBundle> widgetTypes = widgetsBundleDao.findSystemWidgetsBundles(
TenantId.SYS_TENANT_ID, true, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(1);
assertThat(widgetTypes.getData().get(0).getId()).isEqualTo(systemWidgetBundle.getId());
widgetTypeDao.removeWidgetTypeFromWidgetsBundle(systemWidgetBundle.getUuidId(), widgetType.getUuidId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, widgetType.getUuidId());
widgetsBundleDao.removeById(TenantId.SYS_TENANT_ID, systemWidgetBundle.getUuidId());
}
for (var entry : SHOULDNT_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetsBundle systemWidgetBundle = createSystemWidgetBundle("Test Widget Bundle Alias", "Test Widget Bundle Title");
WidgetType widgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, 0, "Test Widget Type Name", "Test Widget Type Description", tags);
widgetTypeDao.saveWidgetsBundleWidget(new WidgetsBundleWidget(systemWidgetBundle.getId(), widgetType.getId(), 0));
PageData<WidgetsBundle> widgetTypes = widgetsBundleDao.findSystemWidgetsBundles(
TenantId.SYS_TENANT_ID, true, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(0);
widgetTypeDao.removeWidgetTypeFromWidgetsBundle(systemWidgetBundle.getUuidId(), widgetType.getUuidId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, widgetType.getUuidId());
widgetsBundleDao.removeById(TenantId.SYS_TENANT_ID, systemWidgetBundle.getUuidId());
}
widgetsBundles = new ArrayList<>();
widgetTypeList = new ArrayList<>();
}
@Test
public void testFindWidgetsBundlesByTenantId() {
UUID tenantId1 = Uuids.timeBased();
@ -245,6 +313,51 @@ public class JpaWidgetsBundleDaoTest extends AbstractJpaDaoTest {
assertEquals(widgetsBundle3, widgetsBundles5.getData().get(0));
}
@Test
public void testTagsSearchInFindAllWidgetsBundlesByTenantId() {
for (var entry : SHOULD_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetsBundle systemWidgetBundle = createSystemWidgetBundle("Test Widget Bundle Alias", "Test Widget Bundle Title");
WidgetType widgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, 0, "Test Widget Type Name", "Test Widget Type Description", tags);
widgetTypeDao.saveWidgetsBundleWidget(new WidgetsBundleWidget(systemWidgetBundle.getId(), widgetType.getId(), 0));
PageData<WidgetsBundle> widgetTypes = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(
TenantId.SYS_TENANT_ID.getId(), true, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(1);
assertThat(widgetTypes.getData().get(0).getId()).isEqualTo(systemWidgetBundle.getId());
widgetTypeDao.removeWidgetTypeFromWidgetsBundle(systemWidgetBundle.getUuidId(), widgetType.getUuidId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, widgetType.getUuidId());
widgetsBundleDao.removeById(TenantId.SYS_TENANT_ID, systemWidgetBundle.getUuidId());
}
for (var entry : SHOULDNT_FIND_SEARCH_TO_TAGS_MAP.entrySet()) {
String searchText = entry.getKey();
String[] tags = entry.getValue();
WidgetsBundle systemWidgetBundle = createSystemWidgetBundle("Test Widget Bundle Alias", "Test Widget Bundle Title");
WidgetType widgetType = createAndSaveWidgetType(TenantId.SYS_TENANT_ID, 0, "Test Widget Type Name", "Test Widget Type Description", tags);
widgetTypeDao.saveWidgetsBundleWidget(new WidgetsBundleWidget(systemWidgetBundle.getId(), widgetType.getId(), 0));
PageData<WidgetsBundle> widgetTypes = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(
TenantId.SYS_TENANT_ID.getId(), true, new PageLink(10, 0, searchText)
);
assertThat(widgetTypes.getData()).hasSize(0);
widgetTypeDao.removeWidgetTypeFromWidgetsBundle(systemWidgetBundle.getUuidId(), widgetType.getUuidId());
widgetTypeDao.removeById(TenantId.SYS_TENANT_ID, widgetType.getUuidId());
widgetsBundleDao.removeById(TenantId.SYS_TENANT_ID, systemWidgetBundle.getUuidId());
}
widgetsBundles = new ArrayList<>();
widgetTypeList = new ArrayList<>();
}
@Test
public void testSearchTextNotFound() {
UUID tenantId = Uuids.timeBased();
@ -279,6 +392,15 @@ public class JpaWidgetsBundleDaoTest extends AbstractJpaDaoTest {
}
}
private WidgetsBundle createSystemWidgetBundle(String alias, String title) {
WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setAlias(alias);
widgetsBundle.setTitle(title);
widgetsBundle.setTenantId(TenantId.SYS_TENANT_ID);
widgetsBundle.setId(new WidgetsBundleId(Uuids.timeBased()));
return widgetsBundleDao.save(TenantId.SYS_TENANT_ID, widgetsBundle);
}
WidgetType createAndSaveWidgetType(TenantId tenantId, int number, String name, String description, String[] tags) {
WidgetTypeDetails widgetType = new WidgetTypeDetails();
widgetType.setTenantId(tenantId);

Loading…
Cancel
Save