Browse Source

DataSourceMapperTest UT modify #1465 (#1642)

* remove LogViewServiceGrpc.java file and pom modify

* remove kazoo

* remove kazoo

* remove kazoo

* remove common monitor package

* add license

* remove kazoo modify

* remove kazoo modify

* remove kazoo modify

* remove kazoo modify

* remove kazoo modify

* remove kazoo modify

* install.sh remove python kazoo

* add system param whether repeat running

* remove kazoo modify

* BusinessTimeUtils remove whther repeat running inner param

* add AccessTokenMapperTest UT

* CI UT yml modify,start postgresql and zookeeper by default

* add AlertGroupMapperTest UT in github action

* Conflicts reslove

* AlertMappert UT modify

* AlertMappert UT modify

* AlertMappert UT modify

* CommandMapperTest UT modify

* DataSourceMapperTest UT modify
pull/2/head
qiaozhanwei 5 years ago committed by khadgarmage
parent
commit
b73979360e
  1. 299
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/DataSourceMapperTest.java

299
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/DataSourceMapperTest.java

@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.dao.mapper;
import org.apache.dolphinscheduler.common.enums.DbType; import org.apache.dolphinscheduler.common.enums.DbType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.DataSource; import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.DatasourceUser; import org.apache.dolphinscheduler.dao.entity.DatasourceUser;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
@ -27,88 +28,116 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.*;
import java.util.List;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* datasource mapper test
*/
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@Transactional
@Rollback(true)
public class DataSourceMapperTest { public class DataSourceMapperTest {
/**
* datasource mapper
*/
@Autowired @Autowired
DataSourceMapper dataSourceMapper; DataSourceMapper dataSourceMapper;
/**
* datasource user relation mapper
*/
@Autowired @Autowired
DataSourceUserMapper dataSourceUserMapper; DataSourceUserMapper dataSourceUserMapper;
/** /**
* insert * test insert
* @return DataSource
*/ */
private DataSource insertOne(){ @Test
//insertOne public void testInsert(){
DataSource dataSource = new DataSource(); DataSource dataSource = createDataSource();
dataSource.setUserId(4); assertNotNull(dataSource.getId());
dataSource.setName("data source test"); assertThat(dataSource.getId(), greaterThan(0));
dataSource.setType(DbType.MYSQL);
dataSource.setNote("mysql test");
dataSource.setConnectionParams("hello mysql");
dataSource.setUpdateTime(new Date());
dataSource.setCreateTime(new Date());
dataSourceMapper.insert(dataSource);
return dataSource;
} }
/** /**
* test update * test query
*/ */
@Test @Test
public void testUpdate(){ public void testSelectById() {
//insertOne DataSource expectedDataSource = createDataSource();
DataSource dataSource = insertOne(); DataSource actualDataSource = dataSourceMapper.selectById(expectedDataSource.getId());
//update assertEquals(expectedDataSource, actualDataSource);
dataSource.setUpdateTime(new Date());
int update = dataSourceMapper.updateById(dataSource);
Assert.assertEquals(update, 1);
dataSourceMapper.deleteById(dataSource.getId());
} }
/** /**
* test delete * test query
*/ */
@Test @Test
public void testDelete(){ public void testUpdate() {
DataSource expectedDataSource = createDataSource();
expectedDataSource.setName("modify " + expectedDataSource.getName());
expectedDataSource.setNote("modifiy " + expectedDataSource.getNote());
expectedDataSource.setUserId(2);
expectedDataSource.setType(DbType.HIVE);
expectedDataSource.setConnectionParams("modify " + expectedDataSource.getConnectionParams());
expectedDataSource.setUpdateTime(DateUtils.getCurrentDate());
DataSource dataSource = insertOne(); dataSourceMapper.updateById(expectedDataSource);
int delete = dataSourceMapper.deleteById(dataSource.getId());
Assert.assertEquals(delete, 1); DataSource actualDataSource = dataSourceMapper.selectById(expectedDataSource.getId());
assertEquals(expectedDataSource, actualDataSource);
} }
/** /**
* test query * test delete
*/ */
@Test @Test
public void testQuery() { public void testDelete(){
DataSource dataSource = insertOne(); DataSource expectedDataSource = createDataSource();
//query
List<DataSource> dataSources = dataSourceMapper.selectList(null); dataSourceMapper.deleteById(expectedDataSource.getId());
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId()); DataSource actualDataSource = dataSourceMapper.selectById(expectedDataSource.getId());
assertNull(actualDataSource);
} }
/** /**
* test query datasource by type * test query datasource by type
*/ */
@Test @Test
public void testQueryDataSourceByType() { public void testQueryDataSourceByType() {
DataSource dataSource = insertOne(); Integer userId = 1;
//query
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByType( Map<Integer, DataSource> datasourceMap = createDataSourceMap(userId, "test");
0, DbType.MYSQL.ordinal()
); List<DataSource> actualDataSources = dataSourceMapper.queryDataSourceByType(
Assert.assertNotEquals(dataSources.size(), 0); 0, DbType.MYSQL.ordinal());
dataSourceMapper.deleteById(dataSource.getId());
assertThat(actualDataSources.size(), greaterThanOrEqualTo(2));
for (DataSource actualDataSource : actualDataSources){
DataSource expectedDataSource = datasourceMap.get(actualDataSource.getId());
if (expectedDataSource != null){
assertEquals(expectedDataSource,actualDataSource);
}
}
} }
/** /**
@ -116,12 +145,23 @@ public class DataSourceMapperTest {
*/ */
@Test @Test
public void testSelectPaging() { public void testSelectPaging() {
DataSource dataSource = insertOne(); String name = "test";
Page page = new Page(1, 3); Integer userId = 1;
IPage<DataSource> dataSourceIPage = dataSourceMapper.selectPaging(page,
4, null); Map<Integer, DataSource> expectedDataSourceMap = createDataSourceMap(userId, name);
Assert.assertNotEquals(dataSourceIPage.getTotal(), 0);
dataSourceMapper.deleteById(dataSource.getId()); Page page = new Page(0, 4);
IPage<DataSource> dataSourceIPage = dataSourceMapper.selectPaging(page, userId, name);
List<DataSource> actualDataSources = dataSourceIPage.getRecords();
for (DataSource actualDataSource : actualDataSources){
DataSource expectedDataSource = expectedDataSourceMap.get(actualDataSource.getId());
if (expectedDataSource != null){
assertEquals(expectedDataSource,actualDataSource);
}
}
} }
/** /**
@ -129,10 +169,17 @@ public class DataSourceMapperTest {
*/ */
@Test @Test
public void testQueryDataSourceByName() { public void testQueryDataSourceByName() {
DataSource dataSource = insertOne(); String name = "test";
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByName("data source test"); DataSource expectedDataSource = createDataSource(name);
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId()); List<DataSource> actualDataSources = dataSourceMapper.queryDataSourceByName(name);
for (DataSource actualDataSource : actualDataSources){
if (expectedDataSource.getId() == actualDataSource.getId()){
assertEquals(expectedDataSource,actualDataSource);
}
}
} }
/** /**
@ -140,17 +187,20 @@ public class DataSourceMapperTest {
*/ */
@Test @Test
public void testQueryAuthedDatasource() { public void testQueryAuthedDatasource() {
String name = "test";
Integer userId = 1;
DataSource dataSource = insertOne(); Map<Integer, DataSource> expectedDataSourceMap = createDataSourceMap(userId, name);
DatasourceUser datasourceUser = new DatasourceUser();
datasourceUser.setUserId(3); List<DataSource> actualDataSources = dataSourceMapper.queryAuthedDatasource(userId);
datasourceUser.setDatasourceId(dataSource.getId());
dataSourceUserMapper.insert(datasourceUser); for (DataSource actualDataSource : actualDataSources){
DataSource expectedDataSource = expectedDataSourceMap.get(actualDataSource.getId());
if (expectedDataSource != null){
assertEquals(expectedDataSource,actualDataSource);
}
}
List<DataSource> dataSources = dataSourceMapper.queryAuthedDatasource(3);
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
dataSourceUserMapper.deleteById(datasourceUser.getId());
} }
/** /**
@ -158,10 +208,19 @@ public class DataSourceMapperTest {
*/ */
@Test @Test
public void testQueryDatasourceExceptUserId() { public void testQueryDatasourceExceptUserId() {
DataSource dataSource = insertOne(); String name = "test";
List<DataSource> dataSources = dataSourceMapper.queryDatasourceExceptUserId(3); Integer userId = 1;
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId()); Map<Integer, DataSource> expectedDataSourceMap = createDataSourceMap(userId, name);
List<DataSource> actualDataSources = dataSourceMapper.queryDatasourceExceptUserId(userId);
for (DataSource actualDataSource : actualDataSources){
DataSource expectedDataSource = expectedDataSourceMap.get(actualDataSource.getId());
if (expectedDataSource != null){
assertEquals(expectedDataSource,actualDataSource);
}
}
} }
/** /**
@ -169,13 +228,107 @@ public class DataSourceMapperTest {
*/ */
@Test @Test
public void testListAllDataSourceByType() { public void testListAllDataSourceByType() {
Integer count = 10;
Map<Integer, DataSource> expectedDataSourceMap = createDataSourceMap(count);
List<DataSource> actualDataSources = dataSourceMapper.listAllDataSourceByType(DbType.MYSQL.ordinal());
assertThat(actualDataSources.size(), greaterThanOrEqualTo(count));
for (DataSource actualDataSource : actualDataSources){
DataSource expectedDataSource = expectedDataSourceMap.get(actualDataSource.getId());
if (expectedDataSource != null){
assertEquals(expectedDataSource,actualDataSource);
}
}
}
/**
* create datasource relation
* @param userId
*/
private Map<Integer,DataSource> createDataSourceMap(Integer userId,String name){
Map<Integer,DataSource> dataSourceMap = new HashMap<>();
DataSource dataSource = createDataSource(userId, name);
dataSourceMap.put(dataSource.getId(),dataSource);
DataSource otherDataSource = createDataSource(userId + 1,name);
DatasourceUser datasourceUser = new DatasourceUser();
datasourceUser.setDatasourceId(otherDataSource.getId());
datasourceUser.setUserId(userId);
datasourceUser.setPerm(7);
datasourceUser.setCreateTime(DateUtils.getCurrentDate());
datasourceUser.setUpdateTime(DateUtils.getCurrentDate());
dataSourceUserMapper.insert(datasourceUser);
dataSourceMap.put(otherDataSource.getId(), otherDataSource);
return dataSourceMap;
}
DataSource dataSource = insertOne(); /**
* create datasource map
* @param count datasource count
* @return datasource map
*/
private Map<Integer,DataSource> createDataSourceMap(Integer count){
Map<Integer,DataSource> dataSourceMap = new HashMap<>();
for (int i = 0 ; i < count ;i++){
DataSource dataSource = createDataSource("test");
dataSourceMap.put(dataSource.getId(),dataSource);
}
return dataSourceMap;
}
/**
* create datasource
* @return datasource
*/
private DataSource createDataSource(){
return createDataSource(1,"test");
}
/**
* create datasource
* @param name name
* @return datasource
*/
private DataSource createDataSource(String name){
return createDataSource(1,name);
}
/**
* create datasource
* @param userId userId
* @param name name
* @return datasource
*/
private DataSource createDataSource(Integer userId,String name){
Random random = new Random();
DataSource dataSource = new DataSource();
dataSource.setUserId(userId);
dataSource.setName(name);
dataSource.setType(DbType.MYSQL);
dataSource.setNote("mysql test");
dataSource.setConnectionParams("hello mysql");
dataSource.setUpdateTime(DateUtils.getCurrentDate());
dataSource.setCreateTime(DateUtils.getCurrentDate());
dataSourceMapper.insert(dataSource);
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByType(4, DbType.MYSQL.ordinal()); return dataSource;
Assert.assertNotEquals(dataSources.size(), 0);
List<DataSource> dataSources2 = dataSourceMapper.queryDataSourceByType(10091, DbType.MYSQL.ordinal());
Assert.assertEquals(dataSources2.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
} }
} }
Loading…
Cancel
Save