@ -29,6 +29,8 @@ import org.apache.dolphinscheduler.dao.datasource.BaseDataSource;
import org.apache.dolphinscheduler.dao.datasource.DataSourceFactory ;
import org.apache.dolphinscheduler.dao.entity.DataSource ;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance ;
import org.apache.dolphinscheduler.server.entity.DataxTaskExecutionContext ;
import org.apache.dolphinscheduler.server.entity.TaskExecutionContext ;
import org.apache.dolphinscheduler.server.utils.DataxUtils ;
import org.apache.dolphinscheduler.server.worker.task.ShellCommandExecutor ;
import org.apache.dolphinscheduler.server.worker.task.TaskProps ;
@ -51,6 +53,8 @@ public class DataxTaskTest {
private static final Logger logger = LoggerFactory . getLogger ( DataxTaskTest . class ) ;
private static final String CONNECTION_PARAMS = "{\"user\":\"root\",\"password\":\"123456\",\"address\":\"jdbc:mysql://127.0.0.1:3306\",\"database\":\"test\",\"jdbcUrl\":\"jdbc:mysql://127.0.0.1:3306/test\"}" ;
private DataxTask dataxTask ;
private ProcessService processService ;
@ -59,6 +63,8 @@ public class DataxTaskTest {
private ApplicationContext applicationContext ;
private TaskExecutionContext taskExecutionContext ;
@Before
public void before ( )
throws Exception {
@ -80,7 +86,25 @@ public class DataxTaskTest {
props . setTaskTimeout ( 0 ) ;
props . setTaskParams (
"{\"targetTable\":\"test\",\"postStatements\":[],\"jobSpeedByte\":1024,\"jobSpeedRecord\":1000,\"dtType\":\"MYSQL\",\"datasource\":1,\"dsType\":\"MYSQL\",\"datatarget\":2,\"jobSpeedByte\":0,\"sql\":\"select 1 as test from dual\",\"preStatements\":[\"delete from test\"],\"postStatements\":[\"delete from test\"]}" ) ;
dataxTask = PowerMockito . spy ( new DataxTask ( null , logger ) ) ;
taskExecutionContext = Mockito . mock ( TaskExecutionContext . class ) ;
Mockito . when ( taskExecutionContext . getTaskParams ( ) ) . thenReturn ( props . getTaskParams ( ) ) ;
Mockito . when ( taskExecutionContext . getExecutePath ( ) ) . thenReturn ( "/tmp" ) ;
Mockito . when ( taskExecutionContext . getTaskAppId ( ) ) . thenReturn ( "1" ) ;
Mockito . when ( taskExecutionContext . getTenantCode ( ) ) . thenReturn ( "root" ) ;
Mockito . when ( taskExecutionContext . getStartTime ( ) ) . thenReturn ( new Date ( ) ) ;
Mockito . when ( taskExecutionContext . getTaskTimeout ( ) ) . thenReturn ( 10000 ) ;
Mockito . when ( taskExecutionContext . getLogPath ( ) ) . thenReturn ( "/tmp/dx" ) ;
DataxTaskExecutionContext dataxTaskExecutionContext = new DataxTaskExecutionContext ( ) ;
dataxTaskExecutionContext . setSourcetype ( 0 ) ;
dataxTaskExecutionContext . setTargetType ( 0 ) ;
dataxTaskExecutionContext . setSourceConnectionParams ( CONNECTION_PARAMS ) ;
dataxTaskExecutionContext . setTargetConnectionParams ( CONNECTION_PARAMS ) ;
Mockito . when ( taskExecutionContext . getDataxTaskExecutionContext ( ) ) . thenReturn ( dataxTaskExecutionContext ) ;
dataxTask = PowerMockito . spy ( new DataxTask ( taskExecutionContext , logger ) ) ;
dataxTask . init ( ) ;
Mockito . when ( processService . findDataSourceById ( 1 ) ) . thenReturn ( getDataSource ( ) ) ;
@ -94,8 +118,7 @@ public class DataxTaskTest {
private DataSource getDataSource ( ) {
DataSource dataSource = new DataSource ( ) ;
dataSource . setType ( DbType . MYSQL ) ;
dataSource . setConnectionParams (
"{\"user\":\"root\",\"password\":\"123456\",\"address\":\"jdbc:mysql://127.0.0.1:3306\",\"database\":\"test\",\"jdbcUrl\":\"jdbc:mysql://127.0.0.1:3306/test\"}" ) ;
dataSource . setConnectionParams ( CONNECTION_PARAMS ) ;
dataSource . setUserId ( 1 ) ;
return dataSource ;
}
@ -144,13 +167,7 @@ public class DataxTaskTest {
@Test
public void testHandle ( )
throws Exception {
try {
dataxTask . handle ( ) ;
} catch ( RuntimeException e ) {
if ( e . getMessage ( ) . indexOf ( "process error . exitCode is : -1" ) < 0 ) {
Assert . fail ( ) ;
}
}
//TODO Test goes here...
}
/ * *