diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index 691fe12ac2..41e378e671 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/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.UserType; 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.User; 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.enums.DbConnectType; import org.apache.dolphinscheduler.spi.enums.DbType; -import org.apache.dolphinscheduler.common.utils.PropertyUtils; 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\"}"; Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam)); - Mockito.mockStatic(CommonUtils.class); - Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(true); - HiveDataSourceParamDTO hiveDataSourceParamDTO = new HiveDataSourceParamDTO(); - hiveDataSourceParamDTO.setHost("192.168.9.1"); - hiveDataSourceParamDTO.setPort(10000); - hiveDataSourceParamDTO.setDatabase("im"); - hiveDataSourceParamDTO.setPrincipal("hive/hdfs-mycluster@ESZ.COM"); - hiveDataSourceParamDTO.setUserName("test"); - hiveDataSourceParamDTO.setPassword("test"); - hiveDataSourceParamDTO.setJavaSecurityKrb5Conf("/opt/krb5.conf"); - hiveDataSourceParamDTO.setLoginUserKeytabPath("/opt/hdfs.headless.keytab"); - hiveDataSourceParamDTO.setLoginUserKeytabUsername("test2/hdfs-mycluster@ESZ.COM"); - connectionParam = DataSourceUtils.buildConnectionParams(hiveDataSourceParamDTO); - - expected = - "{\"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\"," - + "\"principal\":\"hive/hdfs-mycluster@ESZ.COM\",\"javaSecurityKrb5Conf\":\"/opt/krb5.conf\",\"loginUserKeytabUsername\":\"test2/hdfs-mycluster@ESZ.COM\"," - + "\"loginUserKeytabPath\":\"/opt/hdfs.headless.keytab\"}"; - Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam)); + try (MockedStatic mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) { + mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(true); + HiveDataSourceParamDTO hiveDataSourceParamDTO = new HiveDataSourceParamDTO(); + hiveDataSourceParamDTO.setHost("192.168.9.1"); + hiveDataSourceParamDTO.setPort(10000); + hiveDataSourceParamDTO.setDatabase("im"); + hiveDataSourceParamDTO.setPrincipal("hive/hdfs-mycluster@ESZ.COM"); + hiveDataSourceParamDTO.setUserName("test"); + hiveDataSourceParamDTO.setPassword("test"); + hiveDataSourceParamDTO.setJavaSecurityKrb5Conf("/opt/krb5.conf"); + hiveDataSourceParamDTO.setLoginUserKeytabPath("/opt/hdfs.headless.keytab"); + hiveDataSourceParamDTO.setLoginUserKeytabUsername("test2/hdfs-mycluster@ESZ.COM"); + connectionParam = DataSourceUtils.buildConnectionParams(hiveDataSourceParamDTO); + expected = + "{\"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\"," + + "\"principal\":\"hive/hdfs-mycluster@ESZ.COM\",\"javaSecurityKrb5Conf\":\"/opt/krb5.conf\",\"loginUserKeytabUsername\":\"test2/hdfs-mycluster@ESZ.COM\"," + + "\"loginUserKeytabPath\":\"/opt/hdfs.headless.keytab\"}"; + Assert.assertEquals(expected, JSONUtils.toJsonString(connectionParam)); + } } @Test @@ -481,18 +481,21 @@ public class DataSourceServiceTest { postgreSqlDatasourceParam.setPassword(""); ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(postgreSqlDatasourceParam); - Mockito.mockStatic(DataSourceClientProvider.class); - DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); - Mockito.when(DataSourceClientProvider.getInstance()).thenReturn(clientProvider); + try ( + MockedStatic mockedStaticDataSourceClientProvider = + 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); - 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()); + Result result = dataSourceService.checkConnection(dataSourceType, connectionParam); + 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()); + } } -} \ No newline at end of file +} diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/test/java/org/apache/dolphinscheduler/plugin/datasource/api/utils/CommonUtilsTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/test/java/org/apache/dolphinscheduler/plugin/datasource/api/utils/CommonUtilsTest.java index 20cf2d2c58..c12fd24139 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/test/java/org/apache/dolphinscheduler/plugin/datasource/api/utils/CommonUtilsTest.java +++ b/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 ( MockedStatic mockedCommonUtils = Mockito.mockStatic(CommonUtils.class); MockedStatic mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) { - Mockito.when(CommonUtils.getKerberosStartupState()).thenAnswer((Answer) invocation -> false); + mockedCommonUtils.when(CommonUtils::getKerberosStartupState) + .thenAnswer((Answer) invocation -> false); boolean kerberosStartupState = CommonUtils.getKerberosStartupState(); Assert.assertFalse(kerberosStartupState); - Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) + mockedPropertyUtils.when(() -> PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) .thenAnswer((Answer) 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) invocation -> Boolean.TRUE); kerberosStartupState = CommonUtils.getKerberosStartupState(); Assert.assertFalse(kerberosStartupState); @@ -61,15 +63,16 @@ public class CommonUtilsTest { MockedStatic mockedUserGroupInformation = Mockito.mockStatic(UserGroupInformation.class)) { try { - Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) + mockedPropertyUtils.when(() -> PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) .thenAnswer((Answer) 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) 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) 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) 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) invocation -> "/opt/hdfs.headless.keytab"); Configuration configuration = Mockito.mock(Configuration.class); boolean result = CommonUtils.loadKerberosConf(configuration); @@ -94,7 +97,7 @@ public class CommonUtilsTest { Assert.assertEquals("bnVsbElWRkJXbGhUVjBBPQ==", PasswordUtils.encodePassword("!QAZXSW@")); 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); Assert.assertEquals("", PasswordUtils.encodePassword("")); @@ -114,9 +117,11 @@ public class CommonUtilsTest { PropertyUtils.setValue(DATASOURCE_ENCRYPTION_ENABLE, "true"); - Mockito.when(PasswordUtils.decodePassword("bnVsbE1USXpORFUy")).thenReturn("123456"); - Mockito.when(PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")).thenReturn("!QAZXSW@"); - Mockito.when(PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")).thenReturn("5dfger(@"); + mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("bnVsbE1USXpORFUy")).thenReturn("123456"); + mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")) + .thenReturn("!QAZXSW@"); + mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")) + .thenReturn("5dfger(@"); Assert.assertEquals(null, PasswordUtils.decodePassword("")); Assert.assertEquals("123456", PasswordUtils.decodePassword("bnVsbE1USXpORFUy")); @@ -126,9 +131,9 @@ public class CommonUtilsTest { Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) .thenAnswer((Answer) invocation -> Boolean.FALSE); - Mockito.when(PasswordUtils.decodePassword("123456")).thenReturn("123456"); - Mockito.when(PasswordUtils.decodePassword("!QAZXSW@")).thenReturn("!QAZXSW@"); - Mockito.when(PasswordUtils.decodePassword("5dfger(@")).thenReturn("5dfger(@"); + mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("123456")).thenReturn("123456"); + mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("!QAZXSW@")).thenReturn("!QAZXSW@"); + mockedPasswordUtils.when(() -> PasswordUtils.decodePassword("5dfger(@")).thenReturn("5dfger(@"); Assert.assertEquals(null, PasswordUtils.decodePassword("")); Assert.assertEquals("123456", PasswordUtils.decodePassword("123456")); diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-athena/src/test/java/org/apache/dolphinscheduler/plugin/datasource/athena/param/AthenaDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-athena/src/test/java/org/apache/dolphinscheduler/plugin/datasource/athena/param/AthenaDataSourceProcessorTest.java index 41aeab061f..21033ef659 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-athena/src/test/java/org/apache/dolphinscheduler/plugin/datasource/athena/param/AthenaDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -45,12 +46,13 @@ public class AthenaDataSourceProcessorTest { athenaDataSourceParamDTO.setPassword("123456"); athenaDataSourceParamDTO.setAwsRegion("cn-north-1"); athenaDataSourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - AthenaConnectionParam connectionParams = (AthenaConnectionParam) this.athenaDataSourceProcessor - .createConnectionParams(athenaDataSourceParamDTO); - Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getAddress()); - Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getJdbcUrl()); + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + AthenaConnectionParam connectionParams = (AthenaConnectionParam) this.athenaDataSourceProcessor + .createConnectionParams(athenaDataSourceParamDTO); + Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getAddress()); + Assert.assertEquals("jdbc:awsathena://AwsRegion=cn-north-1;", connectionParams.getJdbcUrl()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-db2/src/test/java/org/apache/dolphinscheduler/plugin/datasource/db2/param/Db2DataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-db2/src/test/java/org/apache/dolphinscheduler/plugin/datasource/db2/param/Db2DataSourceProcessorTest.java index 071534c8c1..628a1236fb 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-db2/src/test/java/org/apache/dolphinscheduler/plugin/datasource/db2/param/Db2DataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -46,14 +47,15 @@ public class Db2DataSourceProcessorTest { db2DatasourceParamDTO.setPort(5142); db2DatasourceParamDTO.setDatabase("default"); db2DatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - Db2ConnectionParam connectionParams = (Db2ConnectionParam) db2DatasourceProcessor - .createConnectionParams(db2DatasourceParamDTO); - Assert.assertNotNull(connectionParams); - Assert.assertEquals("jdbc:db2://localhost:5142", connectionParams.getAddress()); - Assert.assertEquals("jdbc:db2://localhost:5142/default", connectionParams.getJdbcUrl()); + Db2ConnectionParam connectionParams = (Db2ConnectionParam) db2DatasourceProcessor + .createConnectionParams(db2DatasourceParamDTO); + Assert.assertNotNull(connectionParams); + Assert.assertEquals("jdbc:db2://localhost:5142", connectionParams.getAddress()); + Assert.assertEquals("jdbc:db2://localhost:5142/default", connectionParams.getJdbcUrl()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/test/java/org/apache/dolphinscheduler/plugin/datasource/hive/param/HiveDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/test/java/org/apache/dolphinscheduler/plugin/datasource/hive/param/HiveDataSourceProcessorTest.java index 453da8e096..7aede4a94c 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/test/java/org/apache/dolphinscheduler/plugin/datasource/hive/param/HiveDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -46,14 +47,17 @@ public class HiveDataSourceProcessorTest { hiveDataSourceParamDTO.setUserName("default"); hiveDataSourceParamDTO.setDatabase("default"); hiveDataSourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - Mockito.mockStatic(CommonUtils.class); - Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(false); - HiveConnectionParam connectionParams = (HiveConnectionParam) hiveDatasourceProcessor - .createConnectionParams(hiveDataSourceParamDTO); - Assert.assertNotNull(connectionParams); - Assert.assertEquals("jdbc:hive2://localhost1:5142,localhost2:5142", connectionParams.getAddress()); + + try ( + MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class); + MockedStatic mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(false); + HiveConnectionParam connectionParams = (HiveConnectionParam) hiveDatasourceProcessor + .createConnectionParams(hiveDataSourceParamDTO); + Assert.assertNotNull(connectionParams); + Assert.assertEquals("jdbc:hive2://localhost1:5142,localhost2:5142", connectionParams.getAddress()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/provider/JDBCDataSourceProviderTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/provider/JDBCDataSourceProviderTest.java index aaa9fdaa1e..f85e62403f 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/provider/JDBCDataSourceProviderTest.java +++ b/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 mockedJDBCDataSourceProvider = Mockito.mockStatic(JDBCDataSourceProvider.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); Assert.assertNotNull(JDBCDataSourceProvider.createJdbcDataSource(new MySQLConnectionParam(), DbType.MYSQL)); } @@ -51,7 +52,8 @@ public class JDBCDataSourceProviderTest { MockedStatic mockedJDBCDataSourceProvider = Mockito.mockStatic(JDBCDataSourceProvider.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); Assert.assertNotNull( JDBCDataSourceProvider.createOneSessionJdbcDataSource(new MySQLConnectionParam(), DbType.MYSQL)); diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/utils/DataSourceUtilsTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/utils/DataSourceUtilsTest.java index cfc35f7b11..dcf2529b2f 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-mysql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/mysql/utils/DataSourceUtilsTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -64,12 +65,16 @@ public class DataSourceUtilsTest { mysqlDatasourceParamDTO.setUserName("root"); mysqlDatasourceParamDTO.setPort(3306); mysqlDatasourceParamDTO.setPassword("123456"); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("123456"); - Mockito.mockStatic(CommonUtils.class); - Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(false); - ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(mysqlDatasourceParamDTO); - Assert.assertNotNull(connectionParam); + + try ( + MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class); + MockedStatic mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())) + .thenReturn("123456"); + mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(false); + ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(mysqlDatasourceParamDTO); + Assert.assertNotNull(connectionParam); + } } @Test @@ -87,22 +92,24 @@ public class DataSourceUtilsTest { @Test public void testGetConnection() throws ExecutionException { - Mockito.mockStatic(PropertyUtils.class); - Mockito.when(PropertyUtils.getLong("kerberos.expire.time", 24L)).thenReturn(24L); - Mockito.mockStatic(DataSourceClientProvider.class); - DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); - Mockito.when(DataSourceClientProvider.getInstance()).thenReturn(clientProvider); + try ( + MockedStatic mockedStaticPropertyUtils = Mockito.mockStatic(PropertyUtils.class); + MockedStatic mockedStaticDataSourceClientProvider = + Mockito.mockStatic(DataSourceClientProvider.class)) { + mockedStaticPropertyUtils.when(() -> PropertyUtils.getLong("kerberos.expire.time", 24L)).thenReturn(24L); + DataSourceClientProvider clientProvider = Mockito.mock(DataSourceClientProvider.class); + mockedStaticDataSourceClientProvider.when(DataSourceClientProvider::getInstance).thenReturn(clientProvider); - Connection connection = Mockito.mock(Connection.class); - Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection); + Connection connection = Mockito.mock(Connection.class); + Mockito.when(clientProvider.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection); - MySQLConnectionParam connectionParam = new MySQLConnectionParam(); - connectionParam.setUser("root"); - connectionParam.setPassword("123456"); - connection = DataSourceClientProvider.getInstance().getConnection(DbType.MYSQL, connectionParam); - - Assert.assertNotNull(connection); + MySQLConnectionParam connectionParam = new MySQLConnectionParam(); + connectionParam.setUser("root"); + connectionParam.setPassword("123456"); + connection = DataSourceClientProvider.getInstance().getConnection(DbType.MYSQL, connectionParam); + Assert.assertNotNull(connection); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java index b546e665eb..0caf7b96f7 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-oracle/src/test/java/org/apache/dolphinscheduler/plugin/datasource/oracle/param/OracleDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -48,13 +49,15 @@ public class OracleDataSourceProcessorTest { oracleDatasourceParamDTO.setPassword("123456"); oracleDatasourceParamDTO.setDatabase("default"); oracleDatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - OracleConnectionParam connectionParams = (OracleConnectionParam) oracleDatasourceProcessor - .createConnectionParams(oracleDatasourceParamDTO); - Assert.assertNotNull(connectionParams); - Assert.assertEquals("jdbc:oracle:thin:@localhost:3308", connectionParams.getAddress()); - Assert.assertEquals("jdbc:oracle:thin:@localhost:3308:default", connectionParams.getJdbcUrl()); + + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + OracleConnectionParam connectionParams = (OracleConnectionParam) oracleDatasourceProcessor + .createConnectionParams(oracleDatasourceParamDTO); + Assert.assertNotNull(connectionParams); + Assert.assertEquals("jdbc:oracle:thin:@localhost:3308", connectionParams.getAddress()); + Assert.assertEquals("jdbc:oracle:thin:@localhost:3308:default", connectionParams.getJdbcUrl()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-postgresql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/postgresql/param/PostgreSQLDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-postgresql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/postgresql/param/PostgreSQLDataSourceProcessorTest.java index 067f12cdca..e7dbc24b1f 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-postgresql/src/test/java/org/apache/dolphinscheduler/plugin/datasource/postgresql/param/PostgreSQLDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -46,13 +47,14 @@ public class PostgreSQLDataSourceProcessorTest { postgreSqlDatasourceParamDTO.setPort(3308); postgreSqlDatasourceParamDTO.setDatabase("default"); postgreSqlDatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - PostgreSQLConnectionParam connectionParams = (PostgreSQLConnectionParam) postgreSqlDatasourceProcessor - .createConnectionParams(postgreSqlDatasourceParamDTO); - Assert.assertEquals("jdbc:postgresql://localhost:3308", connectionParams.getAddress()); - Assert.assertEquals("jdbc:postgresql://localhost:3308/default", connectionParams.getJdbcUrl()); - Assert.assertEquals("root", connectionParams.getUser()); + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + PostgreSQLConnectionParam connectionParams = (PostgreSQLConnectionParam) postgreSqlDatasourceProcessor + .createConnectionParams(postgreSqlDatasourceParamDTO); + Assert.assertEquals("jdbc:postgresql://localhost:3308", connectionParams.getAddress()); + Assert.assertEquals("jdbc:postgresql://localhost:3308/default", connectionParams.getJdbcUrl()); + Assert.assertEquals("root", connectionParams.getUser()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-presto/src/test/java/org/apache/dolphinscheduler/plugin/datasource/presto/param/PrestoDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-presto/src/test/java/org/apache/dolphinscheduler/plugin/datasource/presto/param/PrestoDataSourceProcessorTest.java index 565d535265..7e28a23a5f 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-presto/src/test/java/org/apache/dolphinscheduler/plugin/datasource/presto/param/PrestoDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -46,12 +47,13 @@ public class PrestoDataSourceProcessorTest { prestoDatasourceParamDTO.setUserName("root"); prestoDatasourceParamDTO.setPassword("123456"); prestoDatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - PrestoConnectionParam connectionParams = (PrestoConnectionParam) prestoDatasourceProcessor - .createConnectionParams(prestoDatasourceParamDTO); - Assert.assertEquals("jdbc:presto://localhost:1234", connectionParams.getAddress()); - Assert.assertEquals("jdbc:presto://localhost:1234/default", connectionParams.getJdbcUrl()); + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + PrestoConnectionParam connectionParams = (PrestoConnectionParam) prestoDatasourceProcessor + .createConnectionParams(prestoDatasourceParamDTO); + Assert.assertEquals("jdbc:presto://localhost:1234", connectionParams.getAddress()); + Assert.assertEquals("jdbc:presto://localhost:1234/default", connectionParams.getJdbcUrl()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-redshift/src/test/java/org/apache/dolphinscheduler/plugin/datasource/redshift/param/RedshiftDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-redshift/src/test/java/org/apache/dolphinscheduler/plugin/datasource/redshift/param/RedshiftDataSourceProcessorTest.java index f01e7b3f04..d5929112d9 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-redshift/src/test/java/org/apache/dolphinscheduler/plugin/datasource/redshift/param/RedshiftDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -46,12 +47,14 @@ public class RedshiftDataSourceProcessorTest { redshiftDatasourceParamDTO.setUserName("awsuser"); redshiftDatasourceParamDTO.setPassword("123456"); redshiftDatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - RedshiftConnectionParam connectionParams = (RedshiftConnectionParam) redshiftDatasourceProcessor - .createConnectionParams(redshiftDatasourceParamDTO); - Assert.assertEquals("jdbc:redshift://localhost:5439", connectionParams.getAddress()); - Assert.assertEquals("jdbc:redshift://localhost:5439/dev", connectionParams.getJdbcUrl()); + + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + RedshiftConnectionParam connectionParams = (RedshiftConnectionParam) redshiftDatasourceProcessor + .createConnectionParams(redshiftDatasourceParamDTO); + Assert.assertEquals("jdbc:redshift://localhost:5439", connectionParams.getAddress()); + Assert.assertEquals("jdbc:redshift://localhost:5439/dev", connectionParams.getJdbcUrl()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-spark/src/test/java/org/apache/dolphinscheduler/plugin/datasource/spark/param/SparkDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-spark/src/test/java/org/apache/dolphinscheduler/plugin/datasource/spark/param/SparkDataSourceProcessorTest.java index 2796c29e6f..d33901bad8 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-spark/src/test/java/org/apache/dolphinscheduler/plugin/datasource/spark/param/SparkDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -47,14 +48,17 @@ public class SparkDataSourceProcessorTest { sparkDatasourceParamDTO.setPort(1234); sparkDatasourceParamDTO.setDatabase("default"); sparkDatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - Mockito.mockStatic(CommonUtils.class); - Mockito.when(CommonUtils.getKerberosStartupState()).thenReturn(false); - SparkConnectionParam connectionParams = (SparkConnectionParam) sparkDatasourceProcessor - .createConnectionParams(sparkDatasourceParamDTO); - Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234", connectionParams.getAddress()); - Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234/default", connectionParams.getJdbcUrl()); + + try ( + MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class); + MockedStatic mockedStaticCommonUtils = Mockito.mockStatic(CommonUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + mockedStaticCommonUtils.when(CommonUtils::getKerberosStartupState).thenReturn(false); + SparkConnectionParam connectionParams = (SparkConnectionParam) sparkDatasourceProcessor + .createConnectionParams(sparkDatasourceParamDTO); + Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234", connectionParams.getAddress()); + Assert.assertEquals("jdbc:hive2://localhost1:1234,localhost2:1234/default", connectionParams.getJdbcUrl()); + } } @Test diff --git a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java b/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java index 517381cc41..be90f14af8 100644 --- a/dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-sqlserver/src/test/java/org/apache/dolphinscheduler/plugin/datasource/sqlserver/param/SQLServerDataSourceProcessorTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -47,13 +48,15 @@ public class SQLServerDataSourceProcessorTest { sqlServerDatasourceParamDTO.setHost("localhost"); sqlServerDatasourceParamDTO.setPort(1234); sqlServerDatasourceParamDTO.setOther(props); - Mockito.mockStatic(PasswordUtils.class); - Mockito.when(PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); - SQLServerConnectionParam connectionParams = (SQLServerConnectionParam) sqlServerDatasourceProcessor - .createConnectionParams(sqlServerDatasourceParamDTO); - Assert.assertEquals("jdbc:sqlserver://localhost:1234", connectionParams.getAddress()); - Assert.assertEquals("jdbc:sqlserver://localhost:1234;databaseName=default", connectionParams.getJdbcUrl()); - Assert.assertEquals("root", connectionParams.getUser()); + + try (MockedStatic mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { + mockedStaticPasswordUtils.when(() -> PasswordUtils.encodePassword(Mockito.anyString())).thenReturn("test"); + SQLServerConnectionParam connectionParams = (SQLServerConnectionParam) sqlServerDatasourceProcessor + .createConnectionParams(sqlServerDatasourceParamDTO); + Assert.assertEquals("jdbc:sqlserver://localhost:1234", connectionParams.getAddress()); + Assert.assertEquals("jdbc:sqlserver://localhost:1234;databaseName=default", connectionParams.getJdbcUrl()); + Assert.assertEquals("root", connectionParams.getUser()); + } } @Test diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java index 5919bcbdb0..4bd1004411 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java +++ b/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.process.ProcessService; -import java.util.Date; - +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -47,15 +47,19 @@ public class TaskAckProcessorTest { private TaskEvent taskResponseEvent; private Channel channel; + private MockedStatic mockedStaticSpringApplicationContext; + @Before public void before() { - Mockito.mockStatic(SpringApplicationContext.class); + mockedStaticSpringApplicationContext = Mockito.mockStatic(SpringApplicationContext.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); - Mockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService); + mockedStaticSpringApplicationContext.when(() -> SpringApplicationContext.getBean(ProcessService.class)) + .thenReturn(processService); taskExecuteRunningProcessor = new TaskExecuteRunningProcessor(); @@ -74,6 +78,11 @@ public class TaskAckProcessorTest { taskExecuteRunningMessage.setProcessInstanceId(1); } + @After + public void after() { + mockedStaticSpringApplicationContext.close(); + } + @Test public void testProcess() { } diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/utils/ProcessUtilsTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/utils/ProcessUtilsTest.java index a1c7b7e233..516e5836e0 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/utils/ProcessUtilsTest.java +++ b/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.Test; import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; @@ -51,24 +52,32 @@ public class ProcessUtilsTest { @Test public void getPidsStr() throws Exception { int processId = 1; - Mockito.mockStatic(OSUtils.class); - Mockito.when(OSUtils.exeCmd(anyString())).thenReturn(null); - String pidList = ProcessUtils.getPidsStr(processId); - Assert.assertEquals("", pidList); + + try (MockedStatic mockedStaticOSUtils = Mockito.mockStatic(OSUtils.class)) { + mockedStaticOSUtils.when(() -> OSUtils.exeCmd(anyString())).thenReturn(null); + String pidList = ProcessUtils.getPidsStr(processId); + Assert.assertEquals("", pidList); + } } @Test public void testGetKerberosInitCommand() { - Mockito.mockStatic(PropertyUtils.class); - Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) - .thenReturn(true); - Mockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)).thenReturn("/etc/krb5.conf"); - Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)).thenReturn("/etc/krb5.keytab"); - Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)).thenReturn("test@DS.COM"); - Assert.assertNotEquals("", ProcessUtils.getKerberosInitCommand()); - Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) - .thenReturn(false); - Assert.assertEquals("", ProcessUtils.getKerberosInitCommand()); + try (MockedStatic mockedStaticPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) { + mockedStaticPropertyUtils + .when(() -> PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) + .thenReturn(true); + mockedStaticPropertyUtils.when(() -> PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)) + .thenReturn("/etc/krb5.conf"); + mockedStaticPropertyUtils.when(() -> PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)) + .thenReturn("/etc/krb5.keytab"); + mockedStaticPropertyUtils.when(() -> PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)) + .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 @@ -80,17 +89,18 @@ public class ProcessUtilsTest { String executePath = "/ds-exec/1/1/1"; TaskExecutionStatus running = TaskExecutionStatus.RUNNING_EXECUTION; - Mockito.mockStatic(HadoopUtils.class); - HadoopUtils hadoop = HadoopUtils.getInstance(); + try (MockedStatic mockedStaticHadoopUtils = Mockito.mockStatic(HadoopUtils.class)) { + HadoopUtils hadoop = HadoopUtils.getInstance(); - try { - Mockito.when(hadoop.getApplicationStatus("application_1585532379175_228491")).thenReturn(running); - Mockito.when(hadoop.getApplicationStatus("application_1598885606600_3677")).thenReturn(running); - } catch (Exception e) { - e.printStackTrace(); - ProcessUtils.cancelApplication(appIds, logger, tenantCode, executePath); + try { + Mockito.when(hadoop.getApplicationStatus("application_1585532379175_228491")).thenReturn(running); + Mockito.when(hadoop.getApplicationStatus("application_1598885606600_3677")).thenReturn(running); + } catch (Exception e) { + e.printStackTrace(); + ProcessUtils.cancelApplication(appIds, logger, tenantCode, executePath); + } + + Assert.assertNotNull(appIds); } - - Assert.assertNotNull(appIds); } } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java index bcd27f569e..a239b758c0 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-jupyter/src/test/java/org/apache/dolphinscheduler/plugin/task/jupyter/JupyterTaskTest.java +++ b/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.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -108,10 +109,12 @@ public class JupyterTaskTest { public void jupyterTaskUsePipRequirements() throws Exception { String jupyterTaskParameters = buildJupyterTaskUsePipRequirementsCommand(); JupyterTask jupyterTask = prepareJupyterTaskForTest(jupyterTaskParameters); - Mockito.mockStatic(DateUtils.class); - when(DateUtils.getTimestampString()).thenReturn("123456789"); - jupyterTask.init(); - Assertions.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_PIP_REQUIREMENTS); + + try (MockedStatic mockedStaticDateUtils = Mockito.mockStatic(DateUtils.class)) { + mockedStaticDateUtils.when(DateUtils::getTimestampString).thenReturn("123456789"); + jupyterTask.init(); + Assertions.assertEquals(jupyterTask.buildCommand(), EXPECTED_JUPYTER_TASK_COMMAND_USE_PIP_REQUIREMENTS); + } } private JupyterTask prepareJupyterTaskForTest(final String jupyterTaskParameters) { diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java index 4a6f305f10..ed4e10ead4 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-zeppelin/src/test/java/org/apache/dolphinscheduler/plugin/task/zeppelin/ZeppelinTaskTest.java +++ b/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.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -169,28 +170,29 @@ public class ZeppelinTaskTest { public void testHandleWithNoteExecutionSuccessWithProductionSetting() throws Exception { String zeppelinParametersWithNoParagraphId = buildZeppelinTaskParametersWithProductionSetting(); 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(); - when(this.zClient.cloneNote(any(String.class), any(String.class))).thenReturn(MOCK_CLONE_NOTE_ID); - when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult); + try (MockedStatic mockedStaticDateUtils = Mockito.mockStatic(DateUtils.class)) { + when(taskExecutionContext.getTaskParams()).thenReturn(zeppelinParametersWithNoParagraphId); + this.zeppelinTask = spy(new ZeppelinTask(taskExecutionContext)); - this.zeppelinTask.init(); - this.zeppelinTask.handle(taskCallBack); + this.zClient = mock(ZeppelinClient.class); + this.noteResult = mock(NoteResult.class); - Mockito.verify(this.zClient).cloneNote( - 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) 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()); + doReturn(this.zClient).when(this.zeppelinTask).getZeppelinClient(); + when(this.zClient.cloneNote(any(String.class), any(String.class))).thenReturn(MOCK_CLONE_NOTE_ID); + when(this.zClient.executeNote(any(), any(Map.class))).thenReturn(this.noteResult); + this.zeppelinTask.init(); + when(DateUtils.getTimestampString()).thenReturn("123456789"); + this.zeppelinTask.handle(taskCallBack); + Mockito.verify(this.zClient).cloneNote( + 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) 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() {