|
|
|
@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.server.worker.task.sql;
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.datasource.DatasourceUtil; |
|
|
|
|
import org.apache.dolphinscheduler.common.task.sql.SqlParameters; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.ParameterUtils; |
|
|
|
|
import org.apache.dolphinscheduler.dao.AlertDao; |
|
|
|
|
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand; |
|
|
|
@ -148,4 +149,60 @@ public class SqlTaskTest {
|
|
|
|
|
String result = Whitebox.invokeMethod(sqlTask, "resultProcess", resultSet); |
|
|
|
|
Assert.assertNotNull(result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testQueryBySQLUsingLimit() throws Exception { |
|
|
|
|
TaskExecutionContext localTaskExecutionContext; |
|
|
|
|
TaskProps props = new TaskProps(); |
|
|
|
|
props.setExecutePath("/tmp"); |
|
|
|
|
props.setTaskAppId(String.valueOf(System.currentTimeMillis())); |
|
|
|
|
props.setTaskInstanceId(1); |
|
|
|
|
props.setTenantCode("1"); |
|
|
|
|
props.setEnvFile(".dolphinscheduler_env.sh"); |
|
|
|
|
props.setTaskStartTime(new Date()); |
|
|
|
|
props.setTaskTimeout(0); |
|
|
|
|
props.setTaskParams( |
|
|
|
|
"{\"localParams\":[{\"prop\":\"ret\", \"direct\":\"OUT\", \"type\":\"VARCHAR\", \"value\":\"\"}]," |
|
|
|
|
+ "\"type\":\"POSTGRESQL\",\"datasource\":1,\"sql\":\"SELECT * FROM tb_1\"," |
|
|
|
|
+ "\"sqlType\":0, \"limit\":1, \"sendEmail\":\"false\"}"); |
|
|
|
|
|
|
|
|
|
localTaskExecutionContext = PowerMockito.mock(TaskExecutionContext.class); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getTaskParams()).thenReturn(props.getTaskParams()); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getExecutePath()).thenReturn("/tmp"); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getTaskAppId()).thenReturn("1"); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getTenantCode()).thenReturn("root"); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getStartTime()).thenReturn(new Date()); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getTaskTimeout()).thenReturn(10000); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getLogPath()).thenReturn("/tmp/dx"); |
|
|
|
|
|
|
|
|
|
SQLTaskExecutionContext sqlTaskExecutionContext = new SQLTaskExecutionContext(); |
|
|
|
|
sqlTaskExecutionContext.setConnectionParams(CONNECTION_PARAMS); |
|
|
|
|
PowerMockito.when(localTaskExecutionContext.getSqlTaskExecutionContext()).thenReturn(sqlTaskExecutionContext); |
|
|
|
|
|
|
|
|
|
PowerMockito.mockStatic(SpringApplicationContext.class); |
|
|
|
|
PowerMockito.when(SpringApplicationContext.getBean(Mockito.any())).thenReturn(new AlertDao()); |
|
|
|
|
AlertClientService localAlertClientService = PowerMockito.mock(AlertClientService.class); |
|
|
|
|
SqlTask localSqlTask = new SqlTask(localTaskExecutionContext, logger, localAlertClientService); |
|
|
|
|
localSqlTask.init(); |
|
|
|
|
|
|
|
|
|
ResultSet resultSet = PowerMockito.mock(ResultSet.class); |
|
|
|
|
ResultSetMetaData mockResultMetaData = PowerMockito.mock(ResultSetMetaData.class); |
|
|
|
|
PowerMockito.when(resultSet.getMetaData()).thenReturn(mockResultMetaData); |
|
|
|
|
PowerMockito.when(mockResultMetaData.getColumnCount()).thenReturn(2); |
|
|
|
|
PowerMockito.when(resultSet.next()).thenReturn(true); |
|
|
|
|
PowerMockito.when(resultSet.getObject(Mockito.anyInt())).thenReturn(1); |
|
|
|
|
PowerMockito.when(mockResultMetaData.getColumnLabel(Mockito.anyInt())).thenReturn("a"); |
|
|
|
|
|
|
|
|
|
AlertSendResponseCommand mockResponseCommand = PowerMockito.mock(AlertSendResponseCommand.class); |
|
|
|
|
PowerMockito.when(mockResponseCommand.getResStatus()).thenReturn(true); |
|
|
|
|
PowerMockito.when(localAlertClientService.sendAlert(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())) |
|
|
|
|
.thenReturn(mockResponseCommand); |
|
|
|
|
|
|
|
|
|
String result = Whitebox.invokeMethod(localSqlTask, "resultProcess", resultSet); |
|
|
|
|
Assert.assertEquals(1, ((SqlParameters) localSqlTask.getParameters()).getLimit()); |
|
|
|
|
|
|
|
|
|
// In fact, the target table has 2 rows, as we set the limit to 1, if the limit works, the `resultProcess` method
|
|
|
|
|
// should return [{"a":1}] rather then [{"a":1},{"a":1}]
|
|
|
|
|
Assert.assertEquals("[{\"a\":1}]", result); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|