Browse Source

[Improvement-12296][Test] Close static mock (#12300)

3.2.0-release
rickchengx 2 years ago committed by GitHub
parent
commit
05099bd8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 69
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java
  2. 35
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/test/java/org/apache/dolphinscheduler/plugin/datasource/api/utils/CommonUtilsTest.java
  3. 14
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-athena/src/test/java/org/apache/dolphinscheduler/plugin/datasource/athena/param/AthenaDataSourceProcessorTest.java
  4. 16
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-db2/src/test/java/org/apache/dolphinscheduler/plugin/datasource/db2/param/Db2DataSourceProcessorTest.java
  5. 20
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/test/java/org/apache/dolphinscheduler/plugin/datasource/hive/param/HiveDataSourceProcessorTest.java
  6. 6
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/provider/JDBCDataSourceProviderTest.java
  7. 51
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/utils/DataSourceUtilsTest.java
  8. 17
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java
  9. 16
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-postgresql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/postgresql/param/PostgreSQLDataSourceProcessorTest.java
  10. 14
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-presto/src/test/java/org/apache/dolphinscheduler/plugin/datasource/presto/param/PrestoDataSourceProcessorTest.java
  11. 15
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-redshift/src/test/java/org/apache/dolphinscheduler/plugin/datasource/redshift/param/RedshiftDataSourceProcessorTest.java
  12. 20
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-spark/src/test/java/org/apache/dolphinscheduler/plugin/datasource/spark/param/SparkDataSourceProcessorTest.java
  13. 17
      dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java
  14. 19
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java
  15. 58
      dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/utils/ProcessUtilsTest.java
  16. 11
      dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java
  17. 40
      dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java

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

@ -28,6 +28,7 @@ import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.PropertyUtils;
import org.apache.dolphinscheduler.dao.entity.DataSource; import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper; import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper;
@ -42,7 +43,6 @@ import org.apache.dolphinscheduler.plugin.datasource.postgresql.param.PostgreSQL
import org.apache.dolphinscheduler.spi.datasource.ConnectionParam; import org.apache.dolphinscheduler.spi.datasource.ConnectionParam;
import org.apache.dolphinscheduler.spi.enums.DbConnectType; import org.apache.dolphinscheduler.spi.enums.DbConnectType;
import org.apache.dolphinscheduler.spi.enums.DbType; import org.apache.dolphinscheduler.spi.enums.DbType;
import org.apache.dolphinscheduler.common.utils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -390,27 +390,27 @@ public class DataSourceServiceTest {
+ "\"driverClassName\":\"oracle.jdbc.OracleDriver\",\"validationQuery\":\"select 1 from dual\",\"connectType\":\"ORACLE_SERVICE_NAME\"}"; + "\"driverClassName\":\"oracle.jdbc.OracleDriver\",\"validationQuery\":\"select 1 from dual\",\"connectType\":\"ORACLE_SERVICE_NAME\"}";
Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam)); Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam));
Mockito.mockStatic(CommonUtils.class); try (MockedStatic<CommonUtils> mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) {
Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(true); mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(true);
HiveDataSourceParamDTO hiveDataSourceParamDTO = new HiveDataSourceParamDTO(); HiveDataSourceParamDTO hiveDataSourceParamDTO = new HiveDataSourceParamDTO();
hiveDataSourceParamDTO.setHost("192.168.9.1"); hiveDataSourceParamDTO.setHost("192.168.9.1");
hiveDataSourceParamDTO.setPort(10000); hiveDataSourceParamDTO.setPort(10000);
hiveDataSourceParamDTO.setDatabase("im"); hiveDataSourceParamDTO.setDatabase("im");
hiveDataSourceParamDTO.setPrincipal("hive/hdfs-mycluster@ESZ.COM"); hiveDataSourceParamDTO.setPrincipal("hive/hdfs-mycluster@ESZ.COM");
hiveDataSourceParamDTO.setUserName("test"); hiveDataSourceParamDTO.setUserName("test");
hiveDataSourceParamDTO.setPassword("test"); hiveDataSourceParamDTO.setPassword("test");
hiveDataSourceParamDTO.setJavaSecurityKrb5Conf("/opt/krb5.conf"); hiveDataSourceParamDTO.setJavaSecurityKrb5Conf("/opt/krb5.conf");
hiveDataSourceParamDTO.setLoginUserKeytabPath("/opt/hdfs.headless.keytab"); hiveDataSourceParamDTO.setLoginUserKeytabPath("/opt/hdfs.headless.keytab");
hiveDataSourceParamDTO.setLoginUserKeytabUsername("test2/hdfs-mycluster@ESZ.COM"); hiveDataSourceParamDTO.setLoginUserKeytabUsername("test2/hdfs-mycluster@ESZ.COM");
connectionParam = DataSourceUtils.buildConnectionParams(hiveDataSourceParamDTO); connectionParam = DataSourceUtils.buildConnectionParams(hiveDataSourceParamDTO);
expected = expected =
"{\"user\":\"test\",\"password\":\"test\",\"address\":\"jdbc:hive2://192.168.9.1:10000\",\"database\":\"im\"," "{\"user\":\"test\",\"password\":\"test\",\"address\":\"jdbc:hive2://192.168.9.1:10000\",\"database\":\"im\","
+ "\"jdbcUrl\":\"jdbc:hive2://192.168.9.1:10000/im\",\"driverClassName\":\"org.apache.hive.jdbc.HiveDriver\",\"validationQuery\":\"select 1\"," + "\"jdbcUrl\":\"jdbc:hive2://192.168.9.1:10000/im\",\"driverClassName\":\"org.apache.hive.jdbc.HiveDriver\",\"validationQuery\":\"select 1\","
+ "\"principal\":\"hive/hdfs-mycluster@ESZ.COM\",\"javaSecurityKrb5Conf\":\"/opt/krb5.conf\",\"loginUserKeytabUsername\":\"test2/hdfs-mycluster@ESZ.COM\"," + "\"principal\":\"hive/hdfs-mycluster@ESZ.COM\",\"javaSecurityKrb5Conf\":\"/opt/krb5.conf\",\"loginUserKeytabUsername\":\"test2/hdfs-mycluster@ESZ.COM\","
+ "\"loginUserKeytabPath\":\"/opt/hdfs.headless.keytab\"}"; + "\"loginUserKeytabPath\":\"/opt/hdfs.headless.keytab\"}";
Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam)); Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam));
}
} }
@Test @Test
@ -481,18 +481,21 @@ public class DataSourceServiceTest {
postgreSqlDatasourceParam.setPassword(""); postgreSqlDatasourceParam.setPassword("");
ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(postgreSqlDatasourceParam); ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(postgreSqlDatasourceParam);
Mockito.mockStatic(DataSourceClientProvider.class); try (
DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); MockedStatic<DataSourceClientProvider> mockedStaticDataSourceClientProvider =
Mockito.when(DataSourceClientProvider.getInstance()).thenReturn(clientProvider); Mockito.mockStatic(DataSourceClientProvider.class)) {
DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class);
Mockito.when(DataSourceClientProvider.getInstance()).thenReturn(clientProvider);
mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider);
Result result = dataSourceService.checkConnection(dataSourceType, connectionParam); Result result = dataSourceService.checkConnection(dataSourceType, connectionParam);
Assert.assertEquals(Status.CONNECTION_TEST_FAILURE.getCode(), result.getCode().intValue()); Assert.assertEquals(Status.CONNECTION_TEST_FAILURE.getCode(), result.getCode().intValue());
Connection connection = Mockito.mock(Connection.class);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
result = dataSourceService.checkConnection(dataSourceType, connectionParam);
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
Connection connection = Mockito.mock(Connection.class);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
result = dataSourceService.checkConnection(dataSourceType, connectionParam);
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
} }
} }

35
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/test/java/org/apache/dolphinscheduler/plugin/datasource/api/utils/CommonUtilsTest.java

@ -41,13 +41,15 @@ public class CommonUtilsTest {
try ( try (
MockedStatic<CommonUtils> mockedCommonUtils = Mockito.mockStatic(CommonUtils.class); MockedStatic<CommonUtils> mockedCommonUtils = Mockito.mockStatic(CommonUtils.class);
MockedStatic<PropertyUtils> mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) { MockedStatic<PropertyUtils> mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) {
Mockito.when(CommonUtils.getKerberosStartupState()).thenAnswer((Answer<Boolean>) invocation -> false); mockedCommonUtils.when(CommonUtils::getKerberosStartupState)
.thenAnswer((Answer<Boolean>) invocation -> false);
boolean kerberosStartupState = CommonUtils.getKerberosStartupState(); boolean kerberosStartupState = CommonUtils.getKerberosStartupState();
Assert.assertFalse(kerberosStartupState); Assert.assertFalse(kerberosStartupState);
Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) mockedPropertyUtils.when(() -> PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
.thenAnswer((Answer<String>) invocation -> "HDFS"); .thenAnswer((Answer<String>) invocation -> "HDFS");
Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, true)) mockedPropertyUtils
.when(() -> PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, true))
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); .thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE);
kerberosStartupState = CommonUtils.getKerberosStartupState(); kerberosStartupState = CommonUtils.getKerberosStartupState();
Assert.assertFalse(kerberosStartupState); Assert.assertFalse(kerberosStartupState);
@ -61,15 +63,16 @@ public class CommonUtilsTest {
MockedStatic<UserGroupInformation> mockedUserGroupInformation = MockedStatic<UserGroupInformation> mockedUserGroupInformation =
Mockito.mockStatic(UserGroupInformation.class)) { Mockito.mockStatic(UserGroupInformation.class)) {
try { try {
Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) mockedPropertyUtils.when(() -> PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
.thenAnswer((Answer<String>) invocation -> "HDFS"); .thenAnswer((Answer<String>) invocation -> "HDFS");
Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) mockedPropertyUtils.when(
() -> PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false))
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); .thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE);
Mockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)) mockedPropertyUtils.when(() -> PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH))
.thenAnswer((Answer<String>) invocation -> "/opt/krb5.conf"); .thenAnswer((Answer<String>) invocation -> "/opt/krb5.conf");
Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)) mockedPropertyUtils.when(() -> PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME))
.thenAnswer((Answer<String>) invocation -> "hdfs-mycluster@ESZ.COM"); .thenAnswer((Answer<String>) invocation -> "hdfs-mycluster@ESZ.COM");
Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)) mockedPropertyUtils.when(() -> PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH))
.thenAnswer((Answer<String>) invocation -> "/opt/hdfs.headless.keytab"); .thenAnswer((Answer<String>) invocation -> "/opt/hdfs.headless.keytab");
Configuration configuration = Mockito.mock(Configuration.class); Configuration configuration = Mockito.mock(Configuration.class);
boolean result = CommonUtils.loadKerberosConf(configuration); boolean result = CommonUtils.loadKerberosConf(configuration);
@ -94,7 +97,7 @@ public class CommonUtilsTest {
Assert.assertEquals("bnVsbElWRkJXbGhUVjBBPQ==", PasswordUtils.encodePassword("!QAZXSW@")); Assert.assertEquals("bnVsbElWRkJXbGhUVjBBPQ==", PasswordUtils.encodePassword("!QAZXSW@"));
Assert.assertEquals("bnVsbE5XUm1aMlZ5S0VBPQ==", PasswordUtils.encodePassword("5dfger(@")); Assert.assertEquals("bnVsbE5XUm1aMlZ5S0VBPQ==", PasswordUtils.encodePassword("5dfger(@"));
Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) mockedPropertyUtils.when(() -> PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false))
.thenReturn(Boolean.FALSE); .thenReturn(Boolean.FALSE);
Assert.assertEquals("", PasswordUtils.encodePassword("")); Assert.assertEquals("", PasswordUtils.encodePassword(""));
@ -114,9 +117,11 @@ public class CommonUtilsTest {
PropertyUtils.setValue(DATASOURCE_ENCRYPTION_ENABLE, "true"); PropertyUtils.setValue(DATASOURCE_ENCRYPTION_ENABLE, "true");
Mockito.when(PasswordUtils.decodePassword("bnVsbE1USXpORFUy")).thenReturn("123456"); mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("bnVsbE1USXpORFUy")).thenReturn("123456");
Mockito.when(PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")).thenReturn("!QAZXSW@"); mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ=="))
Mockito.when(PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")).thenReturn("5dfger(@"); .thenReturn("!QAZXSW@");
mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ=="))
.thenReturn("5dfger(@");
Assert.assertEquals(null, PasswordUtils.decodePassword("")); Assert.assertEquals(null, PasswordUtils.decodePassword(""));
Assert.assertEquals("123456", PasswordUtils.decodePassword("bnVsbE1USXpORFUy")); Assert.assertEquals("123456", PasswordUtils.decodePassword("bnVsbE1USXpORFUy"));
@ -126,9 +131,9 @@ public class CommonUtilsTest {
Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false))
.thenAnswer((Answer<Boolean>) invocation -> Boolean.FALSE); .thenAnswer((Answer<Boolean>) invocation -> Boolean.FALSE);
Mockito.when(PasswordUtils.decodePassword("123456")).thenReturn("123456"); mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("123456")).thenReturn("123456");
Mockito.when(PasswordUtils.decodePassword("!QAZXSW@")).thenReturn("!QAZXSW@"); mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("!QAZXSW@")).thenReturn("!QAZXSW@");
Mockito.when(PasswordUtils.decodePassword("5dfger(@")).thenReturn("5dfger(@"); mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("5dfger(@")).thenReturn("5dfger(@");
Assert.assertEquals(null, PasswordUtils.decodePassword("")); Assert.assertEquals(null, PasswordUtils.decodePassword(""));
Assert.assertEquals("123456", PasswordUtils.decodePassword("123456")); Assert.assertEquals("123456", PasswordUtils.decodePassword("123456"));

14
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-athena/src/test/java/org/apache/dolphinscheduler/plugin/datasource/athena/param/AthenaDataSourceProcessorTest.java

@ -27,6 +27,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -45,12 +46,13 @@ public class AthenaDataSourceProcessorTest {
athenaDataSourceParamDTO.setPassword("123456"); athenaDataSourceParamDTO.setPassword("123456");
athenaDataSourceParamDTO.setAwsRegion("cn-north-1"); athenaDataSourceParamDTO.setAwsRegion("cn-north-1");
athenaDataSourceParamDTO.setOther(props); athenaDataSourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
AthenaConnectionParam connectionParams = (AthenaConnectionParam) this.athenaDataSourceProcessor AthenaConnectionParam connectionParams = (AthenaConnectionParam) this.athenaDataSourceProcessor
.createConnectionParams(athenaDataSourceParamDTO); .createConnectionParams(athenaDataSourceParamDTO);
Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getAddress()); Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getAddress());
Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getJdbcUrl());
}
} }
@Test @Test

16
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-db2/src/test/java/org/apache/dolphinscheduler/plugin/datasource/db2/param/Db2DataSourceProcessorTest.java

@ -27,6 +27,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -46,14 +47,15 @@ public class Db2DataSourceProcessorTest {
db2DatasourceParamDTO.setPort(5142); db2DatasourceParamDTO.setPort(5142);
db2DatasourceParamDTO.setDatabase("default"); db2DatasourceParamDTO.setDatabase("default");
db2DatasourceParamDTO.setOther(props); db2DatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
Db2ConnectionParam connectionParams = (Db2ConnectionParam) db2DatasourceProcessor Db2ConnectionParam connectionParams = (Db2ConnectionParam) db2DatasourceProcessor
.createConnectionParams(db2DatasourceParamDTO); .createConnectionParams(db2DatasourceParamDTO);
Assert.assertNotNull(connectionParams); Assert.assertNotNull(connectionParams);
Assert.assertEquals("jdbc:db2://localhost:5142", connectionParams.getAddress()); Assert.assertEquals("jdbc:db2://localhost:5142", connectionParams.getAddress());
Assert.assertEquals("jdbc:db2://localhost:5142/default", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:db2://localhost:5142/default", connectionParams.getJdbcUrl());
}
} }
@Test @Test

20
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/test/java/org/apache/dolphinscheduler/plugin/datasource/hive/param/HiveDataSourceProcessorTest.java

@ -28,6 +28,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -46,14 +47,17 @@ public class HiveDataSourceProcessorTest {
hiveDataSourceParamDTO.setUserName("default"); hiveDataSourceParamDTO.setUserName("default");
hiveDataSourceParamDTO.setDatabase("default"); hiveDataSourceParamDTO.setDatabase("default");
hiveDataSourceParamDTO.setOther(props); hiveDataSourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class);
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); try (
Mockito.mockStatic(CommonUtils.class); MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class);
Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(false); MockedStatic<CommonUtils> mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) {
HiveConnectionParam connectionParams = (HiveConnectionParam) hiveDatasourceProcessor mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
.createConnectionParams(hiveDataSourceParamDTO); mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(false);
Assert.assertNotNull(connectionParams); HiveConnectionParam connectionParams = (HiveConnectionParam) hiveDatasourceProcessor
Assert.assertEquals("jdbc:hive2://localhost1:5142,localhost2:5142", connectionParams.getAddress()); .createConnectionParams(hiveDataSourceParamDTO);
Assert.assertNotNull(connectionParams);
Assert.assertEquals("jdbc:hive2://localhost1:5142,localhost2:5142", connectionParams.getAddress());
}
} }
@Test @Test

6
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/provider/JDBCDataSourceProviderTest.java

@ -39,7 +39,8 @@ public class JDBCDataSourceProviderTest {
MockedStatic<JDBCDataSourceProvider> mockedJDBCDataSourceProvider = MockedStatic<JDBCDataSourceProvider> mockedJDBCDataSourceProvider =
Mockito.mockStatic(JDBCDataSourceProvider.class)) { Mockito.mockStatic(JDBCDataSourceProvider.class)) {
HikariDataSource dataSource = Mockito.mock(HikariDataSource.class); HikariDataSource dataSource = Mockito.mock(HikariDataSource.class);
Mockito.when(JDBCDataSourceProvider.createJdbcDataSource(Mockito.any(), Mockito.any())) mockedJDBCDataSourceProvider
.when(() -> JDBCDataSourceProvider.createJdbcDataSource(Mockito.any(), Mockito.any()))
.thenReturn(dataSource); .thenReturn(dataSource);
Assert.assertNotNull(JDBCDataSourceProvider.createJdbcDataSource(new MySQLConnectionParam(), DbType.MYSQL)); Assert.assertNotNull(JDBCDataSourceProvider.createJdbcDataSource(new MySQLConnectionParam(), DbType.MYSQL));
} }
@ -51,7 +52,8 @@ public class JDBCDataSourceProviderTest {
MockedStatic<JDBCDataSourceProvider> mockedJDBCDataSourceProvider = MockedStatic<JDBCDataSourceProvider> mockedJDBCDataSourceProvider =
Mockito.mockStatic(JDBCDataSourceProvider.class)) { Mockito.mockStatic(JDBCDataSourceProvider.class)) {
HikariDataSource dataSource = Mockito.mock(HikariDataSource.class); HikariDataSource dataSource = Mockito.mock(HikariDataSource.class);
Mockito.when(JDBCDataSourceProvider.createOneSessionJdbcDataSource(Mockito.any(), Mockito.any())) mockedJDBCDataSourceProvider
.when(() -> JDBCDataSourceProvider.createOneSessionJdbcDataSource(Mockito.any(), Mockito.any()))
.thenReturn(dataSource); .thenReturn(dataSource);
Assert.assertNotNull( Assert.assertNotNull(
JDBCDataSourceProvider.createOneSessionJdbcDataSource(new MySQLConnectionParam(), DbType.MYSQL)); JDBCDataSourceProvider.createOneSessionJdbcDataSource(new MySQLConnectionParam(), DbType.MYSQL));

51
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/utils/DataSourceUtilsTest.java

@ -36,6 +36,7 @@ import java.util.concurrent.ExecutionException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -64,12 +65,16 @@ public class DataSourceUtilsTest {
mysqlDatasourceParamDTO.setUserName("root"); mysqlDatasourceParamDTO.setUserName("root");
mysqlDatasourceParamDTO.setPort(3306); mysqlDatasourceParamDTO.setPort(3306);
mysqlDatasourceParamDTO.setPassword("123456"); mysqlDatasourceParamDTO.setPassword("123456");
Mockito.mockStatic(PasswordUtils.class);
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("123456"); try (
Mockito.mockStatic(CommonUtils.class); MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class);
Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(false); MockedStatic<CommonUtils> mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) {
ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(mysqlDatasourceParamDTO); mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString()))
Assert.assertNotNull(connectionParam); .thenReturn("123456");
mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(false);
ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(mysqlDatasourceParamDTO);
Assert.assertNotNull(connectionParam);
}
} }
@Test @Test
@ -87,22 +92,24 @@ public class DataSourceUtilsTest {
@Test @Test
public void testGetConnection() throws ExecutionException { public void testGetConnection() throws ExecutionException {
Mockito.mockStatic(PropertyUtils.class); try (
Mockito.when(PropertyUtils.getLong("kerberos.expire.time", 24L)).thenReturn(24L); MockedStatic<PropertyUtils> mockedStaticPropertyUtils = Mockito.mockStatic(PropertyUtils.class);
Mockito.mockStatic(DataSourceClientProvider.class); MockedStatic<DataSourceClientProvider> mockedStaticDataSourceClientProvider =
DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); Mockito.mockStatic(DataSourceClientProvider.class)) {
Mockito.when(DataSourceClientProvider.getInstance()).thenReturn(clientProvider); mockedStaticPropertyUtils.when(() -> PropertyUtils.getLong("kerberos.expire.time", 24L)).thenReturn(24L);
DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class);
Connection connection = Mockito.mock(Connection.class); mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider);
Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
Connection connection = Mockito.mock(Connection.class);
MySQLConnectionParam connectionParam = new MySQLConnectionParam(); Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
connectionParam.setUser("root");
connectionParam.setPassword("123456"); MySQLConnectionParam connectionParam = new MySQLConnectionParam();
connection = DataSourceClientProvider.getInstance().getConnection(DbType.MYSQL, connectionParam); connectionParam.setUser("root");
connectionParam.setPassword("123456");
Assert.assertNotNull(connection); connection = DataSourceClientProvider.getInstance().getConnection(DbType.MYSQL, connectionParam);
Assert.assertNotNull(connection);
}
} }
@Test @Test

17
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java

@ -28,6 +28,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -48,13 +49,15 @@ public class OracleDataSourceProcessorTest {
oracleDatasourceParamDTO.setPassword("123456"); oracleDatasourceParamDTO.setPassword("123456");
oracleDatasourceParamDTO.setDatabase("default"); oracleDatasourceParamDTO.setDatabase("default");
oracleDatasourceParamDTO.setOther(props); oracleDatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class);
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
OracleConnectionParam connectionParams = (OracleConnectionParam) oracleDatasourceProcessor mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
.createConnectionParams(oracleDatasourceParamDTO); OracleConnectionParam connectionParams = (OracleConnectionParam) oracleDatasourceProcessor
Assert.assertNotNull(connectionParams); .createConnectionParams(oracleDatasourceParamDTO);
Assert.assertEquals("jdbc:oracle:thin:@localhost:3308", connectionParams.getAddress()); Assert.assertNotNull(connectionParams);
Assert.assertEquals("jdbc:oracle:thin:@localhost:3308:default", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:oracle:thin:@localhost:3308", connectionParams.getAddress());
Assert.assertEquals("jdbc:oracle:thin:@localhost:3308:default", connectionParams.getJdbcUrl());
}
} }
@Test @Test

16
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-postgresql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/postgresql/param/PostgreSQLDataSourceProcessorTest.java

@ -27,6 +27,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -46,13 +47,14 @@ public class PostgreSQLDataSourceProcessorTest {
postgreSqlDatasourceParamDTO.setPort(3308); postgreSqlDatasourceParamDTO.setPort(3308);
postgreSqlDatasourceParamDTO.setDatabase("default"); postgreSqlDatasourceParamDTO.setDatabase("default");
postgreSqlDatasourceParamDTO.setOther(props); postgreSqlDatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
PostgreSQLConnectionParam connectionParams = (PostgreSQLConnectionParam) postgreSqlDatasourceProcessor PostgreSQLConnectionParam connectionParams = (PostgreSQLConnectionParam) postgreSqlDatasourceProcessor
.createConnectionParams(postgreSqlDatasourceParamDTO); .createConnectionParams(postgreSqlDatasourceParamDTO);
Assert.assertEquals("jdbc:postgresql://localhost:3308", connectionParams.getAddress()); Assert.assertEquals("jdbc:postgresql://localhost:3308", connectionParams.getAddress());
Assert.assertEquals("jdbc:postgresql://localhost:3308/default", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:postgresql://localhost:3308/default", connectionParams.getJdbcUrl());
Assert.assertEquals("root", connectionParams.getUser()); Assert.assertEquals("root", connectionParams.getUser());
}
} }
@Test @Test

14
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-presto/src/test/java/org/apache/dolphinscheduler/plugin/datasource/presto/param/PrestoDataSourceProcessorTest.java

@ -27,6 +27,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -46,12 +47,13 @@ public class PrestoDataSourceProcessorTest {
prestoDatasourceParamDTO.setUserName("root"); prestoDatasourceParamDTO.setUserName("root");
prestoDatasourceParamDTO.setPassword("123456"); prestoDatasourceParamDTO.setPassword("123456");
prestoDatasourceParamDTO.setOther(props); prestoDatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
PrestoConnectionParam connectionParams = (PrestoConnectionParam) prestoDatasourceProcessor PrestoConnectionParam connectionParams = (PrestoConnectionParam) prestoDatasourceProcessor
.createConnectionParams(prestoDatasourceParamDTO); .createConnectionParams(prestoDatasourceParamDTO);
Assert.assertEquals("jdbc:presto://localhost:1234", connectionParams.getAddress()); Assert.assertEquals("jdbc:presto://localhost:1234", connectionParams.getAddress());
Assert.assertEquals("jdbc:presto://localhost:1234/default", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:presto://localhost:1234/default", connectionParams.getJdbcUrl());
}
} }
@Test @Test

15
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-redshift/src/test/java/org/apache/dolphinscheduler/plugin/datasource/redshift/param/RedshiftDataSourceProcessorTest.java

@ -27,6 +27,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -46,12 +47,14 @@ public class RedshiftDataSourceProcessorTest {
redshiftDatasourceParamDTO.setUserName("awsuser"); redshiftDatasourceParamDTO.setUserName("awsuser");
redshiftDatasourceParamDTO.setPassword("123456"); redshiftDatasourceParamDTO.setPassword("123456");
redshiftDatasourceParamDTO.setOther(props); redshiftDatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class);
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
RedshiftConnectionParam connectionParams = (RedshiftConnectionParam) redshiftDatasourceProcessor mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
.createConnectionParams(redshiftDatasourceParamDTO); RedshiftConnectionParam connectionParams = (RedshiftConnectionParam) redshiftDatasourceProcessor
Assert.assertEquals("jdbc:redshift://localhost:5439", connectionParams.getAddress()); .createConnectionParams(redshiftDatasourceParamDTO);
Assert.assertEquals("jdbc:redshift://localhost:5439/dev", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:redshift://localhost:5439", connectionParams.getAddress());
Assert.assertEquals("jdbc:redshift://localhost:5439/dev", connectionParams.getJdbcUrl());
}
} }
@Test @Test

20
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-spark/src/test/java/org/apache/dolphinscheduler/plugin/datasource/spark/param/SparkDataSourceProcessorTest.java

@ -28,6 +28,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -47,14 +48,17 @@ public class SparkDataSourceProcessorTest {
sparkDatasourceParamDTO.setPort(1234); sparkDatasourceParamDTO.setPort(1234);
sparkDatasourceParamDTO.setDatabase("default"); sparkDatasourceParamDTO.setDatabase("default");
sparkDatasourceParamDTO.setOther(props); sparkDatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class);
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); try (
Mockito.mockStatic(CommonUtils.class); MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class);
Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(false); MockedStatic<CommonUtils> mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) {
SparkConnectionParam connectionParams = (SparkConnectionParam) sparkDatasourceProcessor mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
.createConnectionParams(sparkDatasourceParamDTO); mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(false);
Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234", connectionParams.getAddress()); SparkConnectionParam connectionParams = (SparkConnectionParam) sparkDatasourceProcessor
Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234/default", connectionParams.getJdbcUrl()); .createConnectionParams(sparkDatasourceParamDTO);
Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234", connectionParams.getAddress());
Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234/default", connectionParams.getJdbcUrl());
}
} }
@Test @Test

17
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java

@ -28,6 +28,7 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -47,13 +48,15 @@ public class SQLServerDataSourceProcessorTest {
sqlServerDatasourceParamDTO.setHost("localhost"); sqlServerDatasourceParamDTO.setHost("localhost");
sqlServerDatasourceParamDTO.setPort(1234); sqlServerDatasourceParamDTO.setPort(1234);
sqlServerDatasourceParamDTO.setOther(props); sqlServerDatasourceParamDTO.setOther(props);
Mockito.mockStatic(PasswordUtils.class);
Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); try (MockedStatic<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) {
SQLServerConnectionParam connectionParams = (SQLServerConnectionParam) sqlServerDatasourceProcessor mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test");
.createConnectionParams(sqlServerDatasourceParamDTO); SQLServerConnectionParam connectionParams = (SQLServerConnectionParam) sqlServerDatasourceProcessor
Assert.assertEquals("jdbc:sqlserver://localhost:1234", connectionParams.getAddress()); .createConnectionParams(sqlServerDatasourceParamDTO);
Assert.assertEquals("jdbc:sqlserver://localhost:1234;databaseName=default", connectionParams.getJdbcUrl()); Assert.assertEquals("jdbc:sqlserver://localhost:1234", connectionParams.getAddress());
Assert.assertEquals("root", connectionParams.getUser()); Assert.assertEquals("jdbc:sqlserver://localhost:1234;databaseName=default", connectionParams.getJdbcUrl());
Assert.assertEquals("root", connectionParams.getUser());
}
} }
@Test @Test

19
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java

@ -24,11 +24,11 @@ import org.apache.dolphinscheduler.server.master.processor.queue.TaskEventServic
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext;
import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.process.ProcessService;
import java.util.Date; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -47,15 +47,19 @@ public class TaskAckProcessorTest {
private TaskEvent taskResponseEvent; private TaskEvent taskResponseEvent;
private Channel channel; private Channel channel;
private MockedStatic<SpringApplicationContext> mockedStaticSpringApplicationContext;
@Before @Before
public void before() { public void before() {
Mockito.mockStatic(SpringApplicationContext.class); mockedStaticSpringApplicationContext = Mockito.mockStatic(SpringApplicationContext.class);
taskEventService = Mockito.mock(TaskEventService.class); taskEventService = Mockito.mock(TaskEventService.class);
Mockito.when(SpringApplicationContext.getBean(TaskEventService.class)).thenReturn(taskEventService); mockedStaticSpringApplicationContext.when(() -> SpringApplicationContext.getBean(TaskEventService.class))
.thenReturn(taskEventService);
processService = Mockito.mock(ProcessService.class); processService = Mockito.mock(ProcessService.class);
Mockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService); mockedStaticSpringApplicationContext.when(() -> SpringApplicationContext.getBean(ProcessService.class))
.thenReturn(processService);
taskExecuteRunningProcessor = new TaskExecuteRunningProcessor(); taskExecuteRunningProcessor = new TaskExecuteRunningProcessor();
@ -74,6 +78,11 @@ public class TaskAckProcessorTest {
taskExecuteRunningMessage.setProcessInstanceId(1); taskExecuteRunningMessage.setProcessInstanceId(1);
} }
@After
public void after() {
mockedStaticSpringApplicationContext.close();
}
@Test @Test
public void testProcess() { public void testProcess() {
} }

58
dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/utils/ProcessUtilsTest.java

@ -32,6 +32,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
@ -51,24 +52,32 @@ public class ProcessUtilsTest {
@Test @Test
public void getPidsStr() throws Exception { public void getPidsStr() throws Exception {
int processId = 1; int processId = 1;
Mockito.mockStatic(OSUtils.class);
Mockito.when(OSUtils.exeCmd(anyString())).thenReturn(null); try (MockedStatic<OSUtils> mockedStaticOSUtils = Mockito.mockStatic(OSUtils.class)) {
String pidList = ProcessUtils.getPidsStr(processId); mockedStaticOSUtils.when(() -> OSUtils.exeCmd(anyString())).thenReturn(null);
Assert.assertEquals("", pidList); String pidList = ProcessUtils.getPidsStr(processId);
Assert.assertEquals("", pidList);
}
} }
@Test @Test
public void testGetKerberosInitCommand() { public void testGetKerberosInitCommand() {
Mockito.mockStatic(PropertyUtils.class); try (MockedStatic<PropertyUtils> mockedStaticPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) {
Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) mockedStaticPropertyUtils
.thenReturn(true); .when(() -> PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false))
Mockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)).thenReturn("/etc/krb5.conf"); .thenReturn(true);
Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)).thenReturn("/etc/krb5.keytab"); mockedStaticPropertyUtils.when(() -> PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH))
Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)).thenReturn("test@DS.COM"); .thenReturn("/etc/krb5.conf");
Assert.assertNotEquals("", ProcessUtils.getKerberosInitCommand()); mockedStaticPropertyUtils.when(() -> PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH))
Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) .thenReturn("/etc/krb5.keytab");
.thenReturn(false); mockedStaticPropertyUtils.when(() -> PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME))
Assert.assertEquals("", ProcessUtils.getKerberosInitCommand()); .thenReturn("test@DS.COM");
Assert.assertNotEquals("", ProcessUtils.getKerberosInitCommand());
mockedStaticPropertyUtils
.when(() -> PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false))
.thenReturn(false);
Assert.assertEquals("", ProcessUtils.getKerberosInitCommand());
}
} }
@Test @Test
@ -80,17 +89,18 @@ public class ProcessUtilsTest {
String executePath = "/ds-exec/1/1/1"; String executePath = "/ds-exec/1/1/1";
TaskExecutionStatus running = TaskExecutionStatus.RUNNING_EXECUTION; TaskExecutionStatus running = TaskExecutionStatus.RUNNING_EXECUTION;
Mockito.mockStatic(HadoopUtils.class); try (MockedStatic<HadoopUtils> mockedStaticHadoopUtils = Mockito.mockStatic(HadoopUtils.class)) {
HadoopUtils hadoop = HadoopUtils.getInstance(); HadoopUtils hadoop = HadoopUtils.getInstance();
try { try {
Mockito.when(hadoop.getApplicationStatus("application_1585532379175_228491")).thenReturn(running); Mockito.when(hadoop.getApplicationStatus("application_1585532379175_228491")).thenReturn(running);
Mockito.when(hadoop.getApplicationStatus("application_1598885606600_3677")).thenReturn(running); Mockito.when(hadoop.getApplicationStatus("application_1598885606600_3677")).thenReturn(running);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ProcessUtils.cancelApplication(appIds, logger, tenantCode, executePath); ProcessUtils.cancelApplication(appIds, logger, tenantCode, executePath);
} }
Assert.assertNotNull(appIds); Assert.assertNotNull(appIds);
}
} }
} }

11
dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java

@ -28,6 +28,7 @@ import org.apache.dolphinscheduler.spi.utils.JSONUtils;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
@ -108,10 +109,12 @@ public class JupyterTaskTest {
public void jupyterTaskUsePipRequirements() throws Exception { public void jupyterTaskUsePipRequirements() throws Exception {
String jupyterTaskParameters = buildJupyterTaskUsePipRequirementsCommand(); String jupyterTaskParameters = buildJupyterTaskUsePipRequirementsCommand();
JupyterTask jupyterTask = prepareJupyterTaskForTest(jupyterTaskParameters); JupyterTask jupyterTask = prepareJupyterTaskForTest(jupyterTaskParameters);
Mockito.mockStatic(DateUtils.class);
when(DateUtils.getTimestampString()).thenReturn("123456789"); try (MockedStatic<DateUtils> mockedStaticDateUtils = Mockito.mockStatic(DateUtils.class)) {
jupyterTask.init(); mockedStaticDateUtils.when(DateUtils::getTimestampString).thenReturn("123456789");
Assertions.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_PIP_REQUIREMENTS); jupyterTask.init();
Assertions.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_PIP_REQUIREMENTS);
}
} }
private JupyterTask prepareJupyterTaskForTest(final String jupyterTaskParameters) { private JupyterTask prepareJupyterTaskForTest(final String jupyterTaskParameters) {

40
dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java

@ -43,6 +43,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
@ -169,28 +170,29 @@ public class ZeppelinTaskTest {
public void testHandleWithNoteExecutionSuccessWithProductionSetting() throws Exception { public void testHandleWithNoteExecutionSuccessWithProductionSetting() throws Exception {
String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithProductionSetting(); String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithProductionSetting();
TaskExecutionContext taskExecutionContext = mock(TaskExecutionContext.class); TaskExecutionContext taskExecutionContext = mock(TaskExecutionContext.class);
Mockito.mockStatic(DateUtils.class);
when(DateUtils.getTimestampString()).thenReturn("123456789");
when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId);
this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext));
this.zClient = mock(ZeppelinClient.class);
this.noteResult = mock(NoteResult.class);
doReturn(this.zClient).when(this.zeppelinTask).getZeppelinClient(); try (MockedStatic<DateUtils> mockedStaticDateUtils = Mockito.mockStatic(DateUtils.class)) {
when(this.zClient.cloneNote(any(String.class), any(String.class))).thenReturn(MOCK_CLONE_NOTE_ID); when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId);
when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult); this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext));
this.zeppelinTask.init(); this.zClient = mock(ZeppelinClient.class);
this.zeppelinTask.handle(taskCallBack); this.noteResult = mock(NoteResult.class);
Mockito.verify(this.zClient).cloneNote( doReturn(this.zClient).when(this.zeppelinTask).getZeppelinClient();
MOCK_NOTE_ID, when(this.zClient.cloneNote(any(String.class), any(String.class))).thenReturn(MOCK_CLONE_NOTE_ID);
String.format("%s%s_%s", MOCK_PRODUCTION_DIRECTORY, MOCK_NOTE_ID, "123456789")); when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult);
Mockito.verify(this.zClient).executeNote(MOCK_CLONE_NOTE_ID, this.zeppelinTask.init();
(Map<String, String>) mapper.readValue(MOCK_PARAMETERS, Map.class)); when(DateUtils.getTimestampString()).thenReturn("123456789");
Mockito.verify(this.noteResult).getParagraphResultList(); this.zeppelinTask.handle(taskCallBack);
Mockito.verify(this.zClient).deleteNote(MOCK_CLONE_NOTE_ID); Mockito.verify(this.zClient).cloneNote(
Assertions.assertEquals(EXIT_CODE_SUCCESS, this.zeppelinTask.getExitStatusCode()); MOCK_NOTE_ID,
String.format("%s%s_%s", MOCK_PRODUCTION_DIRECTORY, MOCK_NOTE_ID, "123456789"));
Mockito.verify(this.zClient).executeNote(MOCK_CLONE_NOTE_ID,
(Map<String, String>) mapper.readValue(MOCK_PARAMETERS, Map.class));
Mockito.verify(this.noteResult).getParagraphResultList();
Mockito.verify(this.zClient).deleteNote(MOCK_CLONE_NOTE_ID);
Assertions.assertEquals(EXIT_CODE_SUCCESS, this.zeppelinTask.getExitStatusCode());
}
} }
private String buildZeppelinTaskParameters() { private String buildZeppelinTaskParameters() {

Loading…
Cancel
Save