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.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.DatasourceUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -27,88 +28,116 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* datasource mapper test
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
@Rollback(true)
public class DataSourceMapperTest {
/**
* datasource mapper
*/
@Autowired
DataSourceMapper dataSourceMapper;
/**
* datasource user relation mapper
*/
@Autowired
DataSourceUserMapper dataSourceUserMapper;
/**
* insert
* @return DataSource
* test insert
*/
private DataSource insertOne(){
//insertOne
DataSource dataSource = new DataSource();
dataSource.setUserId(4);
dataSource.setName("data source test");
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
public void testInsert(){
DataSource dataSource = createDataSource();
assertNotNull(dataSource.getId());
assertThat(dataSource.getId(), greaterThan(0));
}
/**
* test update
* test query
*/
@Test
public void testUpdate(){
//insertOne
DataSource dataSource = insertOne();
//update
dataSource.setUpdateTime(new Date());
int update = dataSourceMapper.updateById(dataSource);
Assert.assertEquals(update, 1);
dataSourceMapper.deleteById(dataSource.getId());
public void testSelectById() {
DataSource expectedDataSource = createDataSource();
DataSource actualDataSource = dataSourceMapper.selectById(expectedDataSource.getId());
assertEquals(expectedDataSource, actualDataSource);
}
/**
* test delete
* test query
*/
@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());
dataSourceMapper.updateById(expectedDataSource);
DataSource actualDataSource = dataSourceMapper.selectById(expectedDataSource.getId());
DataSource dataSource = insertOne();
int delete = dataSourceMapper.deleteById(dataSource.getId());
Assert.assertEquals(delete, 1);
assertEquals(expectedDataSource, actualDataSource);
}
/**
* test query
* test delete
*/
@Test
public void testQuery() {
DataSource dataSource = insertOne();
//query
List<DataSource> dataSources = dataSourceMapper.selectList(null);
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
public void testDelete(){
DataSource expectedDataSource = createDataSource();
dataSourceMapper.deleteById(expectedDataSource.getId());
DataSource actualDataSource = dataSourceMapper.selectById(expectedDataSource.getId());
assertNull(actualDataSource);
}
/**
* test query datasource by type
*/
@Test
public void testQueryDataSourceByType() {
DataSource dataSource = insertOne();
//query
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByType(
0, DbType.MYSQL.ordinal()
);
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
Integer userId = 1;
Map<Integer, DataSource> datasourceMap = createDataSourceMap(userId, "test");
List<DataSource> actualDataSources = dataSourceMapper.queryDataSourceByType(
0, DbType.MYSQL.ordinal());
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
public void testSelectPaging() {
DataSource dataSource = insertOne();
Page page = new Page(1, 3);
IPage<DataSource> dataSourceIPage = dataSourceMapper.selectPaging(page,
4, null);
Assert.assertNotEquals(dataSourceIPage.getTotal(), 0);
dataSourceMapper.deleteById(dataSource.getId());
String name = "test";
Integer userId = 1;
Map<Integer, DataSource> expectedDataSourceMap = createDataSourceMap(userId, name);
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
public void testQueryDataSourceByName() {
DataSource dataSource = insertOne();
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByName("data source test");
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
String name = "test";
DataSource expectedDataSource = createDataSource(name);
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
public void testQueryAuthedDatasource() {
String name = "test";
Integer userId = 1;
DataSource dataSource = insertOne();
DatasourceUser datasourceUser = new DatasourceUser();
datasourceUser.setUserId(3);
datasourceUser.setDatasourceId(dataSource.getId());
dataSourceUserMapper.insert(datasourceUser);
Map<Integer, DataSource> expectedDataSourceMap = createDataSourceMap(userId, name);
List<DataSource> actualDataSources = dataSourceMapper.queryAuthedDatasource(userId);
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
public void testQueryDatasourceExceptUserId() {
DataSource dataSource = insertOne();
List<DataSource> dataSources = dataSourceMapper.queryDatasourceExceptUserId(3);
Assert.assertNotEquals(dataSources.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
String name = "test";
Integer userId = 1;
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
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();
DataSource dataSource = insertOne();
datasourceUser.setDatasourceId(otherDataSource.getId());
datasourceUser.setUserId(userId);
datasourceUser.setPerm(7);
datasourceUser.setCreateTime(DateUtils.getCurrentDate());
datasourceUser.setUpdateTime(DateUtils.getCurrentDate());
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByType(4, DbType.MYSQL.ordinal());
Assert.assertNotEquals(dataSources.size(), 0);
List<DataSource> dataSources2 = dataSourceMapper.queryDataSourceByType(10091, DbType.MYSQL.ordinal());
Assert.assertEquals(dataSources2.size(), 0);
dataSourceMapper.deleteById(dataSource.getId());
dataSourceUserMapper.insert(datasourceUser);
dataSourceMap.put(otherDataSource.getId(), otherDataSource);
return dataSourceMap;
}
/**
* 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);
return dataSource;
}
}
Loading…
Cancel
Save