diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index afa13b7414..b2b42512aa 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -211,12 +211,20 @@ public class DataSourceService extends BaseService{ String parameter = dataSource.getConnectionParams(); BaseDataSource datasourceForm = DataSourceFactory.getDatasource(dataSource.getType(), parameter); + DbConnectType connectType = null; + String hostSeperator = Constants.DOUBLE_SLASH; + if(DbType.ORACLE.equals(dataSource.getType())){ + connectType = ((OracleDataSource) datasourceForm).getConnectType(); + if(DbConnectType.ORACLE_SID.equals(connectType)){ + hostSeperator = Constants.AT_SIGN; + } + } String database = datasourceForm.getDatabase(); // jdbc connection params String other = datasourceForm.getOther(); String address = datasourceForm.getAddress(); - String[] hostsPorts = getHostsAndPort(address); + String[] hostsPorts = getHostsAndPort(address,hostSeperator); // ip host String host = hostsPorts[0]; // prot @@ -252,6 +260,10 @@ public class DataSourceService extends BaseService{ map.put(NAME, dataSourceName); map.put(NOTE, desc); map.put(TYPE, dataSourceType); + if (connectType != null) { + map.put(Constants.ORACLE_DB_CONNECT_TYPE, connectType); + } + map.put(HOST, host); map.put(PORT, port); map.put(PRINCIPAL, datasourceForm.getPrincipal()); @@ -478,13 +490,10 @@ public class DataSourceService extends BaseService{ String password, DbConnectType connectType, String other) { String address = buildAddress(type, host, port, connectType); - - String jdbcUrl; - if (Constants.ORACLE.equals(type.name()) - && connectType == DbConnectType.ORACLE_SID) { - jdbcUrl = address + ":" + database; - } else { - jdbcUrl = address + "/" + database; + Map parameterMap = new LinkedHashMap(6); + String jdbcUrl = address + "/" + database; + if (Constants.ORACLE.equals(type.name())) { + parameterMap.put(Constants.ORACLE_DB_CONNECT_TYPE, connectType); } if (CommonUtils.getKerberosStartupState() && @@ -505,7 +514,6 @@ public class DataSourceService extends BaseService{ separator = ";"; } - Map parameterMap = new LinkedHashMap(6); parameterMap.put(Constants.ADDRESS, address); parameterMap.put(Constants.DATABASE, database); parameterMap.put(Constants.JDBC_URL, jdbcUrl); @@ -675,12 +683,23 @@ public class DataSourceService extends BaseService{ /** * get host and port by address * - * @param address + * @param address address * @return sting array: [host,port] */ private String[] getHostsAndPort(String address) { + return getHostsAndPort(address,Constants.DOUBLE_SLASH); + } + + /** + * get host and port by address + * + * @param address address + * @param separator separator + * @return sting array: [host,port] + */ + private String[] getHostsAndPort(String address,String separator) { String[] result = new String[2]; - String[] tmpArray = address.split(Constants.DOUBLE_SLASH); + String[] tmpArray = address.split(separator); String hostsAndPorts = tmpArray[tmpArray.length - 1]; StringBuilder hosts = new StringBuilder(); String[] hostPortArray = hostsAndPorts.split(Constants.COMMA); 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 e47103f9b6..d0714ffe8f 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 @@ -16,38 +16,85 @@ */ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.ApiApplicationServer; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.DbType; import org.apache.dolphinscheduler.common.enums.UserType; +import org.apache.dolphinscheduler.dao.entity.DataSource; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.modules.junit4.PowerMockRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import java.util.ArrayList; +import java.util.List; import java.util.Map; -@RunWith(SpringRunner.class) -@SpringBootTest(classes = ApiApplicationServer.class) +@RunWith(PowerMockRunner.class) +@PowerMockIgnore({"sun.security.*", "javax.net.*"}) public class DataSourceServiceTest { private static final Logger logger = LoggerFactory.getLogger(DataSourceServiceTest.class); - @Autowired + @InjectMocks private DataSourceService dataSourceService; + @Mock + private DataSourceMapper dataSourceMapper; @Test - public void queryDataSourceList(){ + public void queryDataSourceListTest(){ User loginUser = new User(); - loginUser.setId(27); loginUser.setUserType(UserType.GENERAL_USER); Map map = dataSourceService.queryDataSourceList(loginUser, DbType.MYSQL.ordinal()); Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); } + + @Test + public void verifyDataSourceNameTest(){ + User loginUser = new User(); + loginUser.setUserType(UserType.GENERAL_USER); + String dataSourceName = "dataSource1"; + PowerMockito.when(dataSourceMapper.queryDataSourceByName(dataSourceName)).thenReturn(getDataSourceList()); + Result result = dataSourceService.verifyDataSourceName(loginUser, dataSourceName); + Assert.assertEquals(Status.DATASOURCE_EXIST.getMsg(),result.getMsg()); + } + + @Test + public void queryDataSourceTest(){ + PowerMockito.when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(null); + Map result = dataSourceService.queryDataSource(Mockito.anyInt()); + Assert.assertEquals(((Status)result.get(Constants.STATUS)).getCode(),Status.RESOURCE_NOT_EXIST.getCode()); + + PowerMockito.when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(getOracleDataSource()); + result = dataSourceService.queryDataSource(Mockito.anyInt()); + Assert.assertEquals(((Status)result.get(Constants.STATUS)).getCode(),Status.SUCCESS.getCode()); + } + + + private List getDataSourceList(){ + + List dataSources = new ArrayList<>(); + dataSources.add(getOracleDataSource()); + return dataSources; + } + + private DataSource getOracleDataSource(){ + DataSource dataSource = new DataSource(); + dataSource.setName("test"); + dataSource.setNote("Note"); + dataSource.setType(DbType.ORACLE); + dataSource.setConnectionParams("{\"connectType\":\"ORACLE_SID\",\"address\":\"jdbc:oracle:thin:@192.168.xx.xx:49161\",\"database\":\"XE\",\"jdbcUrl\":\"jdbc:oracle:thin:@192.168.xx.xx:49161/XE\",\"user\":\"system\",\"password\":\"oracle\"}"); + + return dataSource; + } } \ No newline at end of file diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java index 686dce3dd6..df04e9a9d7 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java @@ -234,6 +234,10 @@ public final class Constants { * EQUAL SIGN */ public static final String EQUAL_SIGN = "="; + /** + * AT SIGN + */ + public static final String AT_SIGN = "@"; public static final String WORKER_MAX_CPULOAD_AVG = "worker.max.cpuload.avg"; @@ -916,6 +920,7 @@ public final class Constants { public static final String JDBC_URL = "jdbcUrl"; public static final String PRINCIPAL = "principal"; public static final String OTHER = "other"; + public static final String ORACLE_DB_CONNECT_TYPE = "connectType"; /** diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/OracleDataSource.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/OracleDataSource.java index 7666ca7d83..78fed24b72 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/OracleDataSource.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/datasource/OracleDataSource.java @@ -19,22 +19,20 @@ package org.apache.dolphinscheduler.dao.datasource; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.DbConnectType; import org.apache.dolphinscheduler.common.enums.DbType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * data source of Oracle */ public class OracleDataSource extends BaseDataSource { - private DbConnectType type; + private DbConnectType connectType; - public DbConnectType getType() { - return type; + public DbConnectType getConnectType() { + return connectType; } - public void setType(DbConnectType type) { - this.type = type; + public void setConnectType(DbConnectType connectType) { + this.connectType = connectType; } /**