Browse Source

[improve]Source skip check (#12900)

* Source skip check
3.2.0-release
insist777 2 years ago committed by GitHub
parent
commit
528f45acc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java
  2. 13
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java

11
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java

@ -119,13 +119,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR);
return result; return result;
} }
// check connect
ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(datasourceParam); ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(datasourceParam);
Result<Object> isConnection = checkConnection(datasourceParam.getType(), connectionParam);
if (Status.SUCCESS.getCode() != isConnection.getCode()) {
putMsg(result, Status.DATASOURCE_CONNECT_FAILED);
return result;
}
// build datasource // build datasource
DataSource dataSource = new DataSource(); DataSource dataSource = new DataSource();
@ -202,11 +196,6 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
connectionParam.setPassword(oldParams.path(Constants.PASSWORD).asText()); connectionParam.setPassword(oldParams.path(Constants.PASSWORD).asText());
} }
Result<Object> isConnection = checkConnection(dataSource.getType(), connectionParam);
if (isConnection.isFailed()) {
return isConnection;
}
Date now = new Date(); Date now = new Date();
dataSource.setName(dataSourceParam.getName().trim()); dataSource.setName(dataSourceParam.getName().trim());

13
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java

@ -128,19 +128,12 @@ public class DataSourceServiceTest {
try ( try (
MockedStatic<DataSourceClientProvider> mockedStaticDataSourceClientProvider = MockedStatic<DataSourceClientProvider> mockedStaticDataSourceClientProvider =
Mockito.mockStatic(DataSourceClientProvider.class)) { Mockito.mockStatic(DataSourceClientProvider.class)) {
// DATASOURCE_CONNECT_FAILED
DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class);
mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider); mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider);
Mockito.when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null); Mockito.when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(null);
Result connectFailedResult = dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam);
Assertions.assertEquals(Status.DATASOURCE_CONNECT_FAILED.getCode(),
connectFailedResult.getCode().intValue());
// SUCCESS // SUCCESS
Connection connection = Mockito.mock(Connection.class);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
Result success = dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam); Result success = dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam);
Assertions.assertEquals(Status.SUCCESS.getCode(), success.getCode().intValue()); Assertions.assertEquals(Status.SUCCESS.getCode(), success.getCode().intValue());
} }
@ -204,15 +197,9 @@ public class DataSourceServiceTest {
DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class);
mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider); mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(null);
Mockito.when(dataSourceMapper.queryDataSourceByName(postgreSqlDatasourceParam.getName())).thenReturn(null); Mockito.when(dataSourceMapper.queryDataSourceByName(postgreSqlDatasourceParam.getName())).thenReturn(null);
Result connectFailed =
dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam);
Assertions.assertEquals(Status.CONNECTION_TEST_FAILURE.getCode(), connectFailed.getCode().intValue());
// SUCCESS // SUCCESS
Connection connection = Mockito.mock(Connection.class);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
Result success = dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam); Result success = dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam);
Assertions.assertEquals(Status.SUCCESS.getCode(), success.getCode().intValue()); Assertions.assertEquals(Status.SUCCESS.getCode(), success.getCode().intValue());
} }

Loading…
Cancel
Save