JpaWidgetsBundleDao - added missing implementations and tests
This commit is contained in:
		
							parent
							
								
									a6e9ca9eb1
								
							
						
					
					
						commit
						66d0e0ccc3
					
				@ -15,24 +15,14 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
 | 
			
		||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
 | 
			
		||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.ComponentScan;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 | 
			
		||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
 | 
			
		||||
import org.springframework.test.context.TestPropertySource;
 | 
			
		||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.EntityManager;
 | 
			
		||||
import javax.persistence.EntityManagerFactory;
 | 
			
		||||
import javax.sql.DataSource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Valerii Sosliuk
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
@ -16,8 +16,7 @@
 | 
			
		||||
package org.thingsboard.server.dao.sql.widget;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.data.domain.PageRequest;
 | 
			
		||||
import org.springframework.data.domain.Pageable;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.data.repository.CrudRepository;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TextPageLink;
 | 
			
		||||
@ -36,6 +35,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.WIDGETS_BUNDLE_COL
 | 
			
		||||
 * Created by Valerii Sosliuk on 4/23/2017.
 | 
			
		||||
 */
 | 
			
		||||
@Component
 | 
			
		||||
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false)
 | 
			
		||||
public class JpaWidgetsBundleDao extends JpaAbstractDao<WidgetsBundleEntity, WidgetsBundle> implements WidgetsBundleDao {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
@ -64,24 +64,34 @@ public class JpaWidgetsBundleDao extends JpaAbstractDao<WidgetsBundleEntity, Wid
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<WidgetsBundle> findSystemWidgetsBundles(TextPageLink pageLink) {
 | 
			
		||||
        if (pageLink.getIdOffset() == null) {
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesFirstPage(pageLink.getLimit()
 | 
			
		||||
                    , pageLink.getTextSearch()));
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesFirstPage(pageLink.getLimit(),
 | 
			
		||||
                    pageLink.getTextSearch()));
 | 
			
		||||
        } else {
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesNextPage(pageLink.getLimit()
 | 
			
		||||
                    , pageLink.getTextSearch(), pageLink.getIdOffset()));
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findSystemWidgetsBundlesNextPage(pageLink.getLimit(),
 | 
			
		||||
                    pageLink.getTextSearch(), pageLink.getIdOffset()));
 | 
			
		||||
        }
 | 
			
		||||
        //return DaoUtil.convertDataList(widgetsBundleRepository.findBySearchTextStartsWithIgnoreCase(pageLink.getTextSearch().toLowerCase()));
 | 
			
		||||
        //return DaoUtil.convertDataList(widgetsBundleRepository.findBySearchTextStartsWithIgnoreCase(pageLink.getTextSearch().toLowerCase() ,pageable));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<WidgetsBundle> findTenantWidgetsBundlesByTenantId(UUID tenantId, TextPageLink pageLink) {
 | 
			
		||||
        throw new RuntimeException("Not implemented");
 | 
			
		||||
        if (pageLink.getIdOffset() == null) {
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findTenantWidgetsBundlesByTenantIdFirstPage(pageLink.getLimit(),
 | 
			
		||||
                    tenantId, pageLink.getTextSearch()));
 | 
			
		||||
        } else {
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findTenantWidgetsBundlesByTenantIdNextPage(pageLink.getLimit(),
 | 
			
		||||
                    tenantId, pageLink.getTextSearch(), pageLink.getIdOffset()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<WidgetsBundle> findAllTenantWidgetsBundlesByTenantId(UUID tenantId, TextPageLink pageLink) {
 | 
			
		||||
        throw new RuntimeException("Not implemented");
 | 
			
		||||
        if (pageLink.getIdOffset() == null) {
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findAllTenantWidgetsBundlesByTenantIdFirstPage(pageLink.getLimit(),
 | 
			
		||||
                    tenantId, pageLink.getTextSearch()));
 | 
			
		||||
        } else {
 | 
			
		||||
            return DaoUtil.convertDataList(widgetsBundleRepository.findAllTenantWidgetsBundlesByTenantIdNextPage(pageLink.getLimit(),
 | 
			
		||||
                    tenantId, pageLink.getTextSearch(), pageLink.getIdOffset()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -47,4 +47,23 @@ public interface WidgetsBundleRepository extends JpaRepository<WidgetsBundleEnti
 | 
			
		||||
            "AND ID > ?3 ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<WidgetsBundleEntity> findSystemWidgetsBundlesNextPage(Integer limit, String searchText, UUID idOffset);
 | 
			
		||||
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE TENANT_ID = ?2 " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " +
 | 
			
		||||
            "ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<WidgetsBundleEntity> findTenantWidgetsBundlesByTenantIdFirstPage(int limit, UUID tenantId, String textSearch);
 | 
			
		||||
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE TENANT_ID = ?2 " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " +
 | 
			
		||||
            "AND ID > ?4 ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<WidgetsBundleEntity> findTenantWidgetsBundlesByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset);
 | 
			
		||||
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE (TENANT_ID IS NULL OR TENANT_ID = ?2) " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " +
 | 
			
		||||
            "ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<WidgetsBundleEntity> findAllTenantWidgetsBundlesByTenantIdFirstPage(int limit, UUID tenantId, String textSearch);
 | 
			
		||||
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM WIDGETS_BUNDLE WHERE (TENANT_ID IS NULL OR TENANT_ID = ?2) " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " +
 | 
			
		||||
            "AND ID > ?4 ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<WidgetsBundleEntity> findAllTenantWidgetsBundlesByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -20,6 +20,7 @@ import com.github.springtestdbunit.annotation.DatabaseSetup;
 | 
			
		||||
import org.junit.Ignore;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.WidgetsBundleId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TextPageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
 | 
			
		||||
@ -56,27 +57,108 @@ public class JpaWidgetsBundleDaoTest extends AbstractJpaDaoTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    @DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    public void testFindSystemWidgetsBundles() {
 | 
			
		||||
        for (int i = 0; i < 30; i++) {
 | 
			
		||||
            WidgetsBundle widgetsBundle = new WidgetsBundle();
 | 
			
		||||
            widgetsBundle.setAlias("WB" + i);
 | 
			
		||||
            widgetsBundle.setTitle("WB" + i);
 | 
			
		||||
            widgetsBundle.setId(new WidgetsBundleId(UUIDs.timeBased()));
 | 
			
		||||
            widgetsBundleDao.save(widgetsBundle);
 | 
			
		||||
        }
 | 
			
		||||
        createSystemWidgetBundles(30, "WB_");
 | 
			
		||||
        assertEquals(30, widgetsBundleDao.find().size());
 | 
			
		||||
        // Get first page
 | 
			
		||||
        TextPageLink textPageLink1 = new TextPageLink(10, "WB");
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles1 = widgetsBundleDao.findSystemWidgetsBundles(textPageLink1);
 | 
			
		||||
        assertEquals(10, widgetsBundles1.size());
 | 
			
		||||
        for (WidgetsBundle widgetsBundle : widgetsBundles1) {
 | 
			
		||||
            System.out.println(widgetsBundle.getSearchText());
 | 
			
		||||
        }
 | 
			
		||||
        // Get next page
 | 
			
		||||
        TextPageLink textPageLink2 = new TextPageLink(10, "WB", widgetsBundles1.get(9).getId().getId(), null);
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles2 = widgetsBundleDao.findSystemWidgetsBundles(textPageLink2);
 | 
			
		||||
        assertEquals(10, widgetsBundles1.size());
 | 
			
		||||
        for (WidgetsBundle widgetsBundle : widgetsBundles2) {
 | 
			
		||||
            System.out.println(widgetsBundle.getSearchText());
 | 
			
		||||
        assertEquals(10, widgetsBundles2.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    public void testFindWidgetsBundlesByTenantId() {
 | 
			
		||||
        UUID tenantId1 = UUIDs.timeBased();
 | 
			
		||||
        UUID tenantId2 = UUIDs.timeBased();
 | 
			
		||||
        // Create a bunch of widgetBundles
 | 
			
		||||
        for (int i= 0; i < 10; i++) {
 | 
			
		||||
            createWidgetBundles(3, tenantId1, "WB1_");
 | 
			
		||||
            createWidgetBundles(5, tenantId2, "WB2_");
 | 
			
		||||
            createSystemWidgetBundles(10, "WB_SYS_");
 | 
			
		||||
        }
 | 
			
		||||
        assertEquals(180, widgetsBundleDao.find().size());
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink1 = new TextPageLink(40, "WB");
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles1 = widgetsBundleDao.findTenantWidgetsBundlesByTenantId(tenantId1, textPageLink1);
 | 
			
		||||
        assertEquals(30, widgetsBundles1.size());
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink2 = new TextPageLink(40, "WB");
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles2 = widgetsBundleDao.findTenantWidgetsBundlesByTenantId(tenantId2, textPageLink2);
 | 
			
		||||
        assertEquals(40, widgetsBundles2.size());
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink3 = new TextPageLink(40, "WB",
 | 
			
		||||
                widgetsBundles2.get(39).getId().getId(), null);
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles3 = widgetsBundleDao.findTenantWidgetsBundlesByTenantId(tenantId2, textPageLink3);
 | 
			
		||||
        assertEquals(10, widgetsBundles3.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    public void testFindAllWidgetsBundlesByTenantId() {
 | 
			
		||||
        UUID tenantId1 = UUIDs.timeBased();
 | 
			
		||||
        UUID tenantId2 = UUIDs.timeBased();
 | 
			
		||||
        // Create a bunch of widgetBundles
 | 
			
		||||
        for (int i= 0; i < 10; i++) {
 | 
			
		||||
            createWidgetBundles( 5, tenantId1,"WB1_");
 | 
			
		||||
            createWidgetBundles(3, tenantId2, "WB2_");
 | 
			
		||||
            createSystemWidgetBundles(2, "WB_SYS_");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink1 = new TextPageLink(30, "WB");
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles1 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink1);
 | 
			
		||||
        assertEquals(30, widgetsBundles1.size());
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink2 = new TextPageLink(30, "WB",
 | 
			
		||||
                widgetsBundles1.get(29).getId().getId(), null);
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles2 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink2);
 | 
			
		||||
 | 
			
		||||
        assertEquals(30, widgetsBundles2.size());
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink3 = new TextPageLink(30, "WB",
 | 
			
		||||
                widgetsBundles2.get(29).getId().getId(), null);
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles3 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink3);
 | 
			
		||||
        assertEquals(10, widgetsBundles3.size());
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink4 = new TextPageLink(30, "WB",
 | 
			
		||||
                widgetsBundles3.get(9).getId().getId(), null);
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles4 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId1, textPageLink4);
 | 
			
		||||
        assertEquals(0, widgetsBundles4.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    public void testNonSearchTextNotFound() {
 | 
			
		||||
        UUID tenantId = UUIDs.timeBased();
 | 
			
		||||
        createWidgetBundles(5, tenantId, "ABC_");
 | 
			
		||||
        createSystemWidgetBundles(5, "SYS_");
 | 
			
		||||
 | 
			
		||||
        TextPageLink textPageLink = new TextPageLink(30, "WB");
 | 
			
		||||
        List<WidgetsBundle> widgetsBundles4 = widgetsBundleDao.findAllTenantWidgetsBundlesByTenantId(tenantId, textPageLink);
 | 
			
		||||
        assertEquals(0, widgetsBundles4.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void createWidgetBundles(int count, UUID tenantId, String prefix) {
 | 
			
		||||
        for (int i = 0; i < count; i++) {
 | 
			
		||||
            WidgetsBundle widgetsBundle = new WidgetsBundle();
 | 
			
		||||
            widgetsBundle.setAlias(prefix + i);
 | 
			
		||||
            widgetsBundle.setTitle(prefix + i);
 | 
			
		||||
            widgetsBundle.setId(new WidgetsBundleId(UUIDs.timeBased()));
 | 
			
		||||
            widgetsBundle.setTenantId(new TenantId(tenantId));
 | 
			
		||||
            widgetsBundleDao.save(widgetsBundle);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    private void createSystemWidgetBundles(int count, String prefix) {
 | 
			
		||||
        for (int i = 0; i < count; i++) {
 | 
			
		||||
            WidgetsBundle widgetsBundle = new WidgetsBundle();
 | 
			
		||||
            widgetsBundle.setAlias(prefix + i);
 | 
			
		||||
            widgetsBundle.setTitle(prefix + i);
 | 
			
		||||
            widgetsBundle.setId(new WidgetsBundleId(UUIDs.timeBased()));
 | 
			
		||||
            widgetsBundleDao.save(widgetsBundle);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user