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

This commit is contained in:
rickchengx 2022-10-11 16:13:05 +08:00 committed by GitHub
parent 875682d267
commit 05099bd8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 248 additions and 182 deletions

View File

@ -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<CommonUtils> 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<DataSourceClientProvider> 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());
}
}
}
}

View File

@ -41,13 +41,15 @@ public class CommonUtilsTest {
try (
MockedStatic<CommonUtils> mockedCommonUtils = Mockito.mockStatic(CommonUtils.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();
Assert.assertFalse(kerberosStartupState);
Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
mockedPropertyUtils.when(() -> PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
.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);
kerberosStartupState = CommonUtils.getKerberosStartupState();
Assert.assertFalse(kerberosStartupState);
@ -61,15 +63,16 @@ public class CommonUtilsTest {
MockedStatic<UserGroupInformation> mockedUserGroupInformation =
Mockito.mockStatic(UserGroupInformation.class)) {
try {
Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
mockedPropertyUtils.when(() -> PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
.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);
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");
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");
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");
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<Boolean>) 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"));

View File

@ -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<PasswordUtils> 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

View File

@ -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<PasswordUtils> 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

View File

@ -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<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class);
MockedStatic<CommonUtils> 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

View File

@ -39,7 +39,8 @@ public class JDBCDataSourceProviderTest {
MockedStatic<JDBCDataSourceProvider> 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<JDBCDataSourceProvider> 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));

View File

@ -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<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class);
MockedStatic<CommonUtils> 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<PropertyUtils> mockedStaticPropertyUtils = Mockito.mockStatic(PropertyUtils.class);
MockedStatic<DataSourceClientProvider> 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

View File

@ -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<PasswordUtils> 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

View File

@ -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<PasswordUtils> 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

View File

@ -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<PasswordUtils> 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

View File

@ -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<PasswordUtils> 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

View File

@ -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<PasswordUtils> mockedStaticPasswordUtils = Mockito.mockStatic(PasswordUtils.class);
MockedStatic<CommonUtils> 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

View File

@ -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<PasswordUtils> 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

View File

@ -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<SpringApplicationContext> 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() {
}

View File

@ -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<OSUtils> 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<PropertyUtils> 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<HadoopUtils> 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);
}
}

View File

@ -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<DateUtils> 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) {

View File

@ -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<DateUtils> 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<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());
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<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() {