Commit 66d0e0ccc3a105381df598befc9d492034f92e12
1 parent
a6e9ca9e
JpaWidgetsBundleDao - added missing implementations and tests
Showing
4 changed files
with
134 additions
and
33 deletions
... | ... | @@ -15,24 +15,14 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao; |
17 | 17 | |
18 | -import org.springframework.beans.factory.annotation.Value; | |
19 | 18 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
20 | 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
21 | 20 | import org.springframework.boot.autoconfigure.domain.EntityScan; |
22 | -import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; | |
23 | -import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | |
24 | -import org.springframework.context.annotation.Bean; | |
25 | 21 | import org.springframework.context.annotation.ComponentScan; |
26 | 22 | import org.springframework.context.annotation.Configuration; |
27 | 23 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
28 | -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
29 | -import org.springframework.test.context.TestPropertySource; | |
30 | 24 | import org.springframework.transaction.annotation.EnableTransactionManagement; |
31 | 25 | |
32 | -import javax.persistence.EntityManager; | |
33 | -import javax.persistence.EntityManagerFactory; | |
34 | -import javax.sql.DataSource; | |
35 | - | |
36 | 26 | /** |
37 | 27 | * @author Valerii Sosliuk |
38 | 28 | */ | ... | ... |
... | ... | @@ -16,8 +16,7 @@ |
16 | 16 | package org.thingsboard.server.dao.sql.widget; |
17 | 17 | |
18 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
19 | -import org.springframework.data.domain.PageRequest; | |
20 | -import org.springframework.data.domain.Pageable; | |
19 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
21 | 20 | import org.springframework.data.repository.CrudRepository; |
22 | 21 | import org.springframework.stereotype.Component; |
23 | 22 | import org.thingsboard.server.common.data.page.TextPageLink; |
... | ... | @@ -36,6 +35,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.WIDGETS_BUNDLE_COL |
36 | 35 | * Created by Valerii Sosliuk on 4/23/2017. |
37 | 36 | */ |
38 | 37 | @Component |
38 | +@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) | |
39 | 39 | public class JpaWidgetsBundleDao extends JpaAbstractDao<WidgetsBundleEntity, WidgetsBundle> implements WidgetsBundleDao { |
40 | 40 | |
41 | 41 | @Autowired |
... | ... | @@ -64,24 +64,34 @@ public class JpaWidgetsBundleDao extends JpaAbstractDao<WidgetsBundleEntity, Wid |
64 | 64 | @Override |
65 | 65 | public List<WidgetsBundle> findSystemWidgetsBundles(TextPageLink pageLink) { |
66 | 66 | if (pageLink.getIdOffset() == null) { |
67 | - return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesFirstPage(pageLink.getLimit() | |
68 | - , pageLink.getTextSearch())); | |
67 | + return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesFirstPage(pageLink.getLimit(), | |
68 | + pageLink.getTextSearch())); | |
69 | 69 | } else { |
70 | - return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesNextPage(pageLink.getLimit() | |
71 | - , pageLink.getTextSearch(), pageLink.getIdOffset())); | |
70 | + return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesNextPage(pageLink.getLimit(), | |
71 | + pageLink.getTextSearch(), pageLink.getIdOffset())); | |
72 | 72 | } |
73 | - //return DaoUtil.convertDataList(widgetsBundleRepository.findBySearchTextStartsWithIgnoreCase(pageLink.getTextSearch().toLowerCase())); | |
74 | - //return DaoUtil.convertDataList(widgetsBundleRepository.findBySearchTextStartsWithIgnoreCase(pageLink.getTextSearch().toLowerCase() ,pageable)); | |
75 | 73 | } |
76 | 74 | |
77 | 75 | @Override |
78 | 76 | public List<WidgetsBundle> findTenantWidgetsBundlesByTenantId(UUID tenantId, TextPageLink pageLink) { |
79 | - throw new RuntimeException("Not implemented"); | |
77 | + if (pageLink.getIdOffset() == null) { | |
78 | + return DaoUtil.convertDataList(widgetsBundleRepository.findTenantWidgetsBundlesByTenantIdFirstPage(pageLink.getLimit(), | |
79 | + tenantId, pageLink.getTextSearch())); | |
80 | + } else { | |
81 | + return DaoUtil.convertDataList(widgetsBundleRepository.findTenantWidgetsBundlesByTenantIdNextPage(pageLink.getLimit(), | |
82 | + tenantId, pageLink.getTextSearch(), pageLink.getIdOffset())); | |
83 | + } | |
80 | 84 | } |
81 | 85 | |
82 | 86 | @Override |
83 | 87 | public List<WidgetsBundle> findAllTenantWidgetsBundlesByTenantId(UUID tenantId, TextPageLink pageLink) { |
84 | - throw new RuntimeException("Not implemented"); | |
88 | + if (pageLink.getIdOffset() == null) { | |
89 | + return DaoUtil.convertDataList(widgetsBundleRepository.findAllTenantWidgetsBundlesByTenantIdFirstPage(pageLink.getLimit(), | |
90 | + tenantId, pageLink.getTextSearch())); | |
91 | + } else { | |
92 | + return DaoUtil.convertDataList(widgetsBundleRepository.findAllTenantWidgetsBundlesByTenantIdNextPage(pageLink.getLimit(), | |
93 | + tenantId, pageLink.getTextSearch(), pageLink.getIdOffset())); | |
94 | + } | |
85 | 95 | } |
86 | 96 | |
87 | 97 | @Override | ... | ... |
... | ... | @@ -47,4 +47,23 @@ public interface WidgetsBundleRepository extends JpaRepository<WidgetsBundleEnti |
47 | 47 | "AND ID > ?3 ORDER BY ID LIMIT ?1") |
48 | 48 | List<WidgetsBundleEntity> findSystemWidgetsBundlesNextPage(Integer limit, String searchText, UUID idOffset); |
49 | 49 | |
50 | + @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE TENANT_ID = ?2 " + | |
51 | + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + | |
52 | + "ORDER BY ID LIMIT ?1") | |
53 | + List<WidgetsBundleEntity> findTenantWidgetsBundlesByTenantIdFirstPage(int limit, UUID tenantId, String textSearch); | |
54 | + | |
55 | + @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE TENANT_ID = ?2 " + | |
56 | + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + | |
57 | + "AND ID > ?4 ORDER BY ID LIMIT ?1") | |
58 | + List<WidgetsBundleEntity> findTenantWidgetsBundlesByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); | |
59 | + | |
60 | + @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE (TENANT_ID IS NULL OR TENANT_ID = ?2) " + | |
61 | + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + | |
62 | + "ORDER BY ID LIMIT ?1") | |
63 | + List<WidgetsBundleEntity> findAllTenantWidgetsBundlesByTenantIdFirstPage(int limit, UUID tenantId, String textSearch); | |
64 | + | |
65 | + @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE (TENANT_ID IS NULL OR TENANT_ID = ?2) " + | |
66 | + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + | |
67 | + "AND ID > ?4 ORDER BY ID LIMIT ?1") | |
68 | + List<WidgetsBundleEntity> findAllTenantWidgetsBundlesByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); | |
50 | 69 | } | ... | ... |
... | ... | @@ -20,6 +20,7 @@ import com.github.springtestdbunit.annotation.DatabaseSetup; |
20 | 20 | import org.junit.Ignore; |
21 | 21 | import org.junit.Test; |
22 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
23 | +import org.thingsboard.server.common.data.id.TenantId; | |
23 | 24 | import org.thingsboard.server.common.data.id.WidgetsBundleId; |
24 | 25 | import org.thingsboard.server.common.data.page.TextPageLink; |
25 | 26 | import org.thingsboard.server.common.data.widget.WidgetsBundle; |
... | ... | @@ -56,27 +57,108 @@ public class JpaWidgetsBundleDaoTest extends AbstractJpaDaoTest { |
56 | 57 | @Test |
57 | 58 | @DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
58 | 59 | public void testFindSystemWidgetsBundles() { |
59 | - for (int i = 0; i < 30; i++) { | |
60 | - WidgetsBundle widgetsBundle = new WidgetsBundle(); | |
61 | - widgetsBundle.setAlias("WB" + i); | |
62 | - widgetsBundle.setTitle("WB" + i); | |
63 | - widgetsBundle.setId(new WidgetsBundleId(UUIDs.timeBased())); | |
64 | - widgetsBundleDao.save(widgetsBundle); | |
65 | - } | |
60 | + createSystemWidgetBundles(30, "WB_"); | |
66 | 61 | assertEquals(30, widgetsBundleDao.find().size()); |
67 | 62 | // Get first page |
68 | 63 | TextPageLink textPageLink1 = new TextPageLink(10, "WB"); |
69 | 64 | List<WidgetsBundle> widgetsBundles1 = widgetsBundleDao.findSystemWidgetsBundles(textPageLink1); |
70 | 65 | assertEquals(10, widgetsBundles1.size()); |
71 | - for (WidgetsBundle widgetsBundle : widgetsBundles1) { | |
72 | - System.out.println(widgetsBundle.getSearchText()); | |
73 | - } | |
66 | + // Get next page | |
74 | 67 | TextPageLink textPageLink2 = new TextPageLink(10, "WB", widgetsBundles1.get(9).getId().getId(), null); |
75 | 68 | List<WidgetsBundle> widgetsBundles2 = widgetsBundleDao.findSystemWidgetsBundles(textPageLink2); |
76 | - assertEquals(10, widgetsBundles1.size()); | |
77 | - for (WidgetsBundle widgetsBundle : widgetsBundles2) { | |
78 | - System.out.println(widgetsBundle.getSearchText()); | |
69 | + assertEquals(10, widgetsBundles2.size()); | |
70 | + } | |
71 | + | |
72 | + @Test | |
73 | + @DatabaseSetup("classpath:dbunit/empty_dataset.xml") | |
74 | + public void testFindWidgetsBundlesByTenantId() { | |
75 | + UUID tenantId1 = UUIDs.timeBased(); | |
76 | + UUID tenantId2 = UUIDs.timeBased(); | |
77 | + // Create a bunch of widgetBundles | |
78 | + for (int i= 0; i < 10; i++) { | |
79 | + createWidgetBundles(3, tenantId1, "WB1_"); | |
80 | + createWidgetBundles(5, tenantId2, "WB2_"); | |
81 | + createSystemWidgetBundles(10, "WB_SYS_"); | |
79 | 82 | } |
83 | + assertEquals(180, widgetsBundleDao.find().size()); | |
84 | + | |
85 | + TextPageLink textPageLink1 = new TextPageLink(40, "WB"); | |
86 | + List<WidgetsBundle> widgetsBundles1 = widgetsBundleDao.findTenantWidgetsBundlesByTenantId(tenantId1, textPageLink1); | |
87 | + assertEquals(30, widgetsBundles1.size()); | |
88 | + | |
89 | + TextPageLink textPageLink2 = new TextPageLink(40, "WB"); | |
90 | + List<WidgetsBundle> widgetsBundles2 = widgetsBundleDao.findTenantWidgetsBundlesByTenantId(tenantId2, textPageLink2); | |
91 | + assertEquals(40, widgetsBundles2.size()); | |
92 | + | |
93 | + TextPageLink textPageLink3 = new TextPageLink(40, "WB", | |
94 | + widgetsBundles2.get(39).getId().getId(), null); | |
95 | + List<WidgetsBundle> widgetsBundles3 = widgetsBundleDao.findTenantWidgetsBundlesByTenantId(tenantId2, textPageLink3); | |
96 | + assertEquals(10, widgetsBundles3.size()); | |
97 | + } | |
98 | + | |
99 | + @Test | |
100 | + @DatabaseSetup("classpath:dbunit/empty_dataset.xml") | |
101 | + public void testFindAllWidgetsBundlesByTenantId() { | |
102 | + UUID tenantId1 = UUIDs.timeBased(); | |
103 | + UUID tenantId2 = UUIDs.timeBased(); | |
104 | + // Create a bunch of widgetBundles | |
105 | + for (int i= 0; i < 10; i++) { | |
106 | + createWidgetBundles( 5, tenantId1,"WB1_"); | |
107 | + createWidgetBundles(3, tenantId2, "WB2_"); | |
108 | + createSystemWidgetBundles(2, "WB_SYS_"); | |
109 | + } | |
110 | + | |
111 | + TextPageLink textPageLink1 = new TextPageLink(30, "WB"); | |
112 | + List<WidgetsBundle> widgetsBundles1 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink1); | |
113 | + assertEquals(30, widgetsBundles1.size()); | |
114 | + | |
115 | + TextPageLink textPageLink2 = new TextPageLink(30, "WB", | |
116 | + widgetsBundles1.get(29).getId().getId(), null); | |
117 | + List<WidgetsBundle> widgetsBundles2 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink2); | |
80 | 118 | |
119 | + assertEquals(30, widgetsBundles2.size()); | |
120 | + | |
121 | + TextPageLink textPageLink3 = new TextPageLink(30, "WB", | |
122 | + widgetsBundles2.get(29).getId().getId(), null); | |
123 | + List<WidgetsBundle> widgetsBundles3 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink3); | |
124 | + assertEquals(10, widgetsBundles3.size()); | |
125 | + | |
126 | + TextPageLink textPageLink4 = new TextPageLink(30, "WB", | |
127 | + widgetsBundles3.get(9).getId().getId(), null); | |
128 | + List<WidgetsBundle> widgetsBundles4 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink4); | |
129 | + assertEquals(0, widgetsBundles4.size()); | |
130 | + } | |
131 | + | |
132 | + | |
133 | + @Test | |
134 | + @DatabaseSetup("classpath:dbunit/empty_dataset.xml") | |
135 | + public void testNonSearchTextNotFound() { | |
136 | + UUID tenantId = UUIDs.timeBased(); | |
137 | + createWidgetBundles(5, tenantId, "ABC_"); | |
138 | + createSystemWidgetBundles(5, "SYS_"); | |
139 | + | |
140 | + TextPageLink textPageLink = new TextPageLink(30, "WB"); | |
141 | + List<WidgetsBundle> widgetsBundles4 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId, textPageLink); | |
142 | + assertEquals(0, widgetsBundles4.size()); | |
143 | + } | |
144 | + | |
145 | + private void createWidgetBundles(int count, UUID tenantId, String prefix) { | |
146 | + for (int i = 0; i < count; i++) { | |
147 | + WidgetsBundle widgetsBundle = new WidgetsBundle(); | |
148 | + widgetsBundle.setAlias(prefix + i); | |
149 | + widgetsBundle.setTitle(prefix + i); | |
150 | + widgetsBundle.setId(new WidgetsBundleId(UUIDs.timeBased())); | |
151 | + widgetsBundle.setTenantId(new TenantId(tenantId)); | |
152 | + widgetsBundleDao.save(widgetsBundle); | |
153 | + } | |
154 | + } | |
155 | + private void createSystemWidgetBundles(int count, String prefix) { | |
156 | + for (int i = 0; i < count; i++) { | |
157 | + WidgetsBundle widgetsBundle = new WidgetsBundle(); | |
158 | + widgetsBundle.setAlias(prefix + i); | |
159 | + widgetsBundle.setTitle(prefix + i); | |
160 | + widgetsBundle.setId(new WidgetsBundleId(UUIDs.timeBased())); | |
161 | + widgetsBundleDao.save(widgetsBundle); | |
162 | + } | |
81 | 163 | } |
82 | 164 | } | ... | ... |