Merge pull request #5890 from smatvienko-tb/dao-test-fix
dao test some fixed
This commit is contained in:
		
						commit
						6ea3e019da
					
				@ -26,7 +26,7 @@ import org.thingsboard.server.dao.util.SqlTsDao;
 | 
			
		||||
 | 
			
		||||
@Configuration
 | 
			
		||||
@EnableAutoConfiguration
 | 
			
		||||
@ComponentScan({"org.thingsboard.server.dao.sqlts.psql"})
 | 
			
		||||
@ComponentScan({"org.thingsboard.server.dao.sqlts.psql", "org.thingsboard.server.dao.sqlts.insert.psql"})
 | 
			
		||||
@EnableJpaRepositories({"org.thingsboard.server.dao.sqlts.ts", "org.thingsboard.server.dao.sqlts.insert.psql"})
 | 
			
		||||
@EntityScan({"org.thingsboard.server.dao.model.sqlts.ts"})
 | 
			
		||||
@EnableTransactionManagement
 | 
			
		||||
 | 
			
		||||
@ -15,28 +15,29 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao;
 | 
			
		||||
 | 
			
		||||
import com.github.springtestdbunit.DbUnitTestExecutionListener;
 | 
			
		||||
import com.github.springtestdbunit.annotation.DbUnitConfiguration;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.springframework.boot.test.mock.mockito.MockBean;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.TestExecutionListeners;
 | 
			
		||||
import org.springframework.test.context.TestPropertySource;
 | 
			
		||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
 | 
			
		||||
import org.springframework.test.context.junit4.SpringRunner;
 | 
			
		||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
 | 
			
		||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
 | 
			
		||||
import org.thingsboard.server.common.stats.StatsFactory;
 | 
			
		||||
import org.thingsboard.server.dao.service.DaoSqlTest;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by Valerii Sosliuk on 4/22/2017.
 | 
			
		||||
 */
 | 
			
		||||
@RunWith(SpringRunner.class)
 | 
			
		||||
@ContextConfiguration(classes = {JpaDaoConfig.class, HsqlTsDaoConfig.class, JpaDbunitTestConfig.class})
 | 
			
		||||
@TestPropertySource("classpath:sql-test.properties")
 | 
			
		||||
@ContextConfiguration(classes = {JpaDaoConfig.class, PsqlTsDaoConfig.class, PsqlTsLatestDaoConfig.class, SqlTimeseriesDaoConfig.class})
 | 
			
		||||
@DaoSqlTest
 | 
			
		||||
@TestExecutionListeners({
 | 
			
		||||
        DependencyInjectionTestExecutionListener.class,
 | 
			
		||||
        DirtiesContextTestExecutionListener.class,
 | 
			
		||||
        DbUnitTestExecutionListener.class })
 | 
			
		||||
@DbUnitConfiguration(databaseConnection = "dbUnitDatabaseConnection")
 | 
			
		||||
        DirtiesContextTestExecutionListener.class})
 | 
			
		||||
public abstract class AbstractJpaDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
 | 
			
		||||
 | 
			
		||||
    @MockBean
 | 
			
		||||
    StatsFactory statsFactory;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -21,6 +21,11 @@ import org.junit.runner.RunWith;
 | 
			
		||||
 | 
			
		||||
@RunWith(ClasspathSuite.class)
 | 
			
		||||
@ClassnameFilters({
 | 
			
		||||
        "org.thingsboard.server.dao.sql.tenant.*Test",
 | 
			
		||||
        "org.thingsboard.server.dao.sql.component.*Test",
 | 
			
		||||
        "org.thingsboard.server.dao.sql.customer.*Test",
 | 
			
		||||
        "org.thingsboard.server.dao.sql.dashboard.*Test",
 | 
			
		||||
        "org.thingsboard.server.dao.sql.query.*Test",
 | 
			
		||||
        "org.thingsboard.server.dao.sql.*THIS_MUST_BE_FIXED_Test",
 | 
			
		||||
})
 | 
			
		||||
public class JpaDaoTestSuite {
 | 
			
		||||
 | 
			
		||||
@ -31,6 +31,7 @@ import java.sql.SQLException;
 | 
			
		||||
 * Created by Valerii Sosliuk on 5/6/2017.
 | 
			
		||||
 */
 | 
			
		||||
@Configuration
 | 
			
		||||
@Deprecated
 | 
			
		||||
public class JpaDbunitTestConfig {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
 | 
			
		||||
@ -259,7 +259,7 @@ public abstract class BaseTenantProfileServiceTest extends AbstractServiceTest {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private TenantProfile createTenantProfile(String name) {
 | 
			
		||||
    public static TenantProfile createTenantProfile(String name) {
 | 
			
		||||
        TenantProfile tenantProfile = new TenantProfile();
 | 
			
		||||
        tenantProfile.setName(name);
 | 
			
		||||
        tenantProfile.setDescription(name + " Test");
 | 
			
		||||
 | 
			
		||||
@ -16,17 +16,23 @@
 | 
			
		||||
package org.thingsboard.server.dao.sql.tenant;
 | 
			
		||||
 | 
			
		||||
import com.datastax.oss.driver.api.core.uuid.Uuids;
 | 
			
		||||
import com.github.springtestdbunit.annotation.DatabaseSetup;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.thingsboard.server.common.data.Tenant;
 | 
			
		||||
import org.thingsboard.server.common.data.TenantProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageLink;
 | 
			
		||||
import org.thingsboard.server.dao.AbstractJpaDaoTest;
 | 
			
		||||
import org.thingsboard.server.dao.service.AbstractServiceTest;
 | 
			
		||||
import org.thingsboard.server.dao.service.BaseTenantProfileServiceTest;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantDao;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantProfileDao;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
@ -40,8 +46,26 @@ public class JpaTenantDaoTest extends AbstractJpaDaoTest {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private TenantDao tenantDao;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private TenantProfileDao tenantProfileDao;
 | 
			
		||||
 | 
			
		||||
    List<Tenant> createdTenants = new ArrayList<>();
 | 
			
		||||
    TenantProfile tenantProfile;
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void setUp() throws Exception {
 | 
			
		||||
        tenantProfile = tenantProfileDao.save(TenantId.SYS_TENANT_ID, BaseTenantProfileServiceTest.createTenantProfile("default tenant profile"));
 | 
			
		||||
        assertThat(tenantProfile).as("tenant profile").isNotNull();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @After
 | 
			
		||||
    public void tearDown() throws Exception {
 | 
			
		||||
        createdTenants.forEach((tenant)-> tenantDao.removeById(TenantId.SYS_TENANT_ID, tenant.getUuidId()));
 | 
			
		||||
        tenantProfileDao.removeById(TenantId.SYS_TENANT_ID, tenantProfile.getUuidId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    //@DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    public void testFindTenantsByRegion() {
 | 
			
		||||
        createTenants();
 | 
			
		||||
        assertEquals(60, tenantDao.find(AbstractServiceTest.SYSTEM_TENANT_ID).size());
 | 
			
		||||
@ -51,12 +75,12 @@ public class JpaTenantDaoTest extends AbstractJpaDaoTest {
 | 
			
		||||
        assertEquals(20, tenants1.getData().size());
 | 
			
		||||
 | 
			
		||||
        pageLink = pageLink.nextPageLink();
 | 
			
		||||
        PageData<Tenant> tenants2 = tenantDao.findTenantsByRegion(AbstractServiceTest.SYSTEM_TENANT_ID,"REGION_1",
 | 
			
		||||
        PageData<Tenant> tenants2 = tenantDao.findTenantsByRegion(AbstractServiceTest.SYSTEM_TENANT_ID, "REGION_1",
 | 
			
		||||
                pageLink);
 | 
			
		||||
        assertEquals(10, tenants2.getData().size());
 | 
			
		||||
 | 
			
		||||
        pageLink = pageLink.nextPageLink();
 | 
			
		||||
        PageData<Tenant> tenants3 = tenantDao.findTenantsByRegion(AbstractServiceTest.SYSTEM_TENANT_ID,"REGION_1",
 | 
			
		||||
        PageData<Tenant> tenants3 = tenantDao.findTenantsByRegion(AbstractServiceTest.SYSTEM_TENANT_ID, "REGION_1",
 | 
			
		||||
                pageLink);
 | 
			
		||||
        assertEquals(0, tenants3.getData().size());
 | 
			
		||||
    }
 | 
			
		||||
@ -68,16 +92,17 @@ public class JpaTenantDaoTest extends AbstractJpaDaoTest {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void createTenant(String region, String title, int index) {
 | 
			
		||||
    void createTenant(String region, String title, int index) {
 | 
			
		||||
        Tenant tenant = new Tenant();
 | 
			
		||||
        tenant.setId(new TenantId(Uuids.timeBased()));
 | 
			
		||||
        tenant.setRegion(region);
 | 
			
		||||
        tenant.setTitle(title + "_" + index);
 | 
			
		||||
        tenantDao.save(AbstractServiceTest.SYSTEM_TENANT_ID, tenant);
 | 
			
		||||
        tenant.setTenantProfileId(tenantProfile.getId());
 | 
			
		||||
        createdTenants.add(tenantDao.save(TenantId.SYS_TENANT_ID, tenant));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    //@DatabaseSetup("classpath:dbunit/empty_dataset.xml")
 | 
			
		||||
    public void testIsExistsTenantById() {
 | 
			
		||||
        final UUID uuid = Uuids.timeBased();
 | 
			
		||||
        final TenantId tenantId = new TenantId(uuid);
 | 
			
		||||
@ -86,9 +111,12 @@ public class JpaTenantDaoTest extends AbstractJpaDaoTest {
 | 
			
		||||
        final Tenant tenant = new Tenant();
 | 
			
		||||
        tenant.setId(tenantId);
 | 
			
		||||
        tenant.setTitle("Tenant " + uuid);
 | 
			
		||||
        tenantDao.save(AbstractServiceTest.SYSTEM_TENANT_ID, tenant);
 | 
			
		||||
        tenant.setTenantProfileId(tenantProfile.getId());
 | 
			
		||||
 | 
			
		||||
        createdTenants.add(tenantDao.save(TenantId.SYS_TENANT_ID, tenant));
 | 
			
		||||
 | 
			
		||||
        assertThat(tenantDao.existsById(tenantId, uuid)).as("Is tenant exists after save").isTrue();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user