yangyichao-mango commented on a change in pull request #3561:
URL: 
https://github.com/apache/incubator-dolphinscheduler/pull/3561#discussion_r473942398



##########
File path: 
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java
##########
@@ -51,46 +55,206 @@
     private DataSourceService dataSourceService;
     @Mock
     private DataSourceMapper dataSourceMapper;
+    @Mock
+    private DataSourceUserMapper datasourceUserMapper;
+
+
+    @Test
+    public void createDataSourceTest() {
+        User loginUser = getAdminUser();
+
+        String dataSourceName = "dataSource01";
+        String dataSourceDesc = "test dataSource";
+        DbType dataSourceType = DbType.POSTGRESQL;
+        String parameter = dataSourceService.buildParameter(dataSourceType, 
"172.16.133.200", "5432", "dolphinscheduler", null, "postgres", "", null, null);
+
+        // data source exits
+        List<DataSource> dataSourceList = new ArrayList<>();
+        DataSource dataSource = new DataSource();
+        dataSource.setName(dataSourceName);
+        dataSourceList.add(dataSource);
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(dataSourceList);
+        Map<String, Object> dataSourceExitsResult = 
dataSourceService.createDataSource(loginUser, dataSourceName, dataSourceDesc, 
dataSourceType, parameter);
+        Assert.assertEquals(Status.DATASOURCE_EXIST, 
dataSourceExitsResult.get(Constants.STATUS));
+
+        // data source exits
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null);
+        when(dataSourceService.checkConnection(dataSourceType, 
parameter)).thenReturn(false);
+        Map<String, Object> connectFailedResult = 
dataSourceService.createDataSource(loginUser, dataSourceName, dataSourceDesc, 
dataSourceType, parameter);
+        Assert.assertEquals(Status.DATASOURCE_CONNECT_FAILED, 
connectFailedResult.get(Constants.STATUS));
+
+        // data source exits
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null);
+        when(dataSourceService.checkConnection(dataSourceType, 
parameter)).thenReturn(true);
+        when(DataSourceFactory.getDatasource(dataSourceType, 
parameter)).thenReturn(null);
+        Map<String, Object> notValidError = 
dataSourceService.createDataSource(loginUser, dataSourceName, dataSourceDesc, 
dataSourceType, parameter);
+        Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, 
notValidError.get(Constants.STATUS));
+
+        // success
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null);
+        when(dataSourceService.checkConnection(dataSourceType, 
parameter)).thenReturn(true);
+        when(DataSourceFactory.getDatasource(dataSourceType, 
parameter)).thenReturn(JSONUtils.parseObject(parameter, MySQLDataSource.class));
+        Map<String, Object> success = 
dataSourceService.createDataSource(loginUser, dataSourceName, dataSourceDesc, 
dataSourceType, parameter);
+        Assert.assertEquals(Status.SUCCESS, success.get(Constants.STATUS));
+    }
+
+    @Test
+    public void updateDataSourceTest() {
+        User loginUser = getAdminUser();
+
+        int dataSourceId = 12;
+        String dataSourceName = "dataSource01";
+        String dataSourceDesc = "test dataSource";
+        DbType dataSourceType = DbType.POSTGRESQL;
+        String parameter = dataSourceService.buildParameter(dataSourceType, 
"172.16.133.200", "5432", "dolphinscheduler", null, "postgres", "", null, null);
+
+        // data source not exits
+        when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null);
+        Map<String, Object> resourceNotExits = 
dataSourceService.updateDataSource(dataSourceId, loginUser, dataSourceName, 
dataSourceDesc, dataSourceType, parameter);
+        Assert.assertEquals(Status.RESOURCE_NOT_EXIST, 
resourceNotExits.get(Constants.STATUS));
+        // user no operation perm
+        DataSource dataSource = new DataSource();
+        dataSource.setUserId(0);
+        when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource);
+        Map<String, Object> userNoOperationPerm = 
dataSourceService.updateDataSource(dataSourceId, loginUser, dataSourceName, 
dataSourceDesc, dataSourceType, parameter);
+        Assert.assertEquals(Status.USER_NO_OPERATION_PERM, 
userNoOperationPerm.get(Constants.STATUS));
+
+        // data source name exits
+        dataSource.setUserId(-1);
+        List<DataSource> dataSourceList = new ArrayList<>();
+        dataSourceList.add(dataSource);
+        when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource);
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName)).thenReturn(dataSourceList);
+        Map<String, Object> dataSourceNameExist = 
dataSourceService.updateDataSource(dataSourceId, loginUser, dataSourceName, 
dataSourceDesc, dataSourceType, parameter);
+        Assert.assertEquals(Status.DATASOURCE_EXIST, 
dataSourceNameExist.get(Constants.STATUS));
+
+        // data source connect failed
+        when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource);
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName)).thenReturn(null);
+        when(dataSourceService.checkConnection(dataSourceType, 
parameter)).thenReturn(true);
+        Map<String, Object> connectFailed = 
dataSourceService.updateDataSource(dataSourceId, loginUser, dataSourceName, 
dataSourceDesc, dataSourceType, parameter);
+        Assert.assertEquals(Status.DATASOURCE_CONNECT_FAILED, 
connectFailed.get(Constants.STATUS));
+
+        //success
+        when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource);
+        
when(dataSourceMapper.queryDataSourceByName(dataSourceName)).thenReturn(null);
+        when(dataSourceService.checkConnection(dataSourceType, 
parameter)).thenReturn(false);
+        Map<String, Object> success = 
dataSourceService.updateDataSource(dataSourceId, loginUser, dataSourceName, 
dataSourceDesc, dataSourceType, parameter);
+        Assert.assertEquals(Status.SUCCESS, 
connectFailed.get(Constants.STATUS));
+
+    }
+
+    @Test
+    public void queryDataSourceListPagingTest() {
+        User loginUser = getAdminUser();
+        String searchVal = "";
+        int pageNo = 1;
+        int pageSize = 10;
+        Map<String, Object> success = 
dataSourceService.queryDataSourceListPaging(loginUser, searchVal, pageNo, 
pageSize);
+        Assert.assertEquals(Status.SUCCESS, success.get(Constants.STATUS));
+    }
 
     @Test
-    public void queryDataSourceListTest(){
+    public void connectionTest() {
+        int dataSourceId = -1;
+        when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null);
+        Assert.assertEquals(false, 
dataSourceService.connectionTest(dataSourceId));

Review comment:
       ```suggestion
           Assert.assertFalse(dataSourceService.connectionTest(dataSourceId));
   ```

##########
File path: 
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java
##########
@@ -101,31 +265,49 @@ private DataSource getOracleDataSource(){
     }
 
     @Test
-    public void buildParameter(){
-        String param = dataSourceService.buildParameter("","", DbType.ORACLE, 
"192.168.9.1","1521","im"
-                ,"","test","test", DbConnectType.ORACLE_SERVICE_NAME,"");
+    public void buildParameter() {
+        String param = dataSourceService.buildParameter(DbType.ORACLE, 
"192.168.9.1", "1521", "im"
+                , "", "test", "test", DbConnectType.ORACLE_SERVICE_NAME, "");
         String expected = 
"{\"connectType\":\"ORACLE_SERVICE_NAME\",\"type\":\"ORACLE_SERVICE_NAME\",\"address\":\"jdbc:oracle:thin:@//192.168.9.1:1521\",\"database\":\"im\",\"jdbcUrl\":\"jdbc:oracle:thin:@//192.168.9.1:1521/im\",\"user\":\"test\",\"password\":\"test\"}";
         Assert.assertEquals(expected, param);
     }
 
     @Test
-    public void buildParameterWithDecodePassword(){
-        PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE,"true");
-        String param = dataSourceService.buildParameter("name","desc", 
DbType.MYSQL, "192.168.9.1","1521","im"
-                ,"","test","123456", null,"");
+    public void buildParameterWithDecodePassword() {
+        PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE, "true");
+        String param = dataSourceService.buildParameter(DbType.MYSQL, 
"192.168.9.1", "1521", "im"
+                , "", "test", "123456", null, "");
         String expected = 
"{\"type\":null,\"address\":\"jdbc:mysql://192.168.9.1:1521\",\"database\":\"im\",\"jdbcUrl\":\"jdbc:mysql://192.168.9.1:1521/im\",\"user\":\"test\",\"password\":\"IUAjJCVeJipNVEl6TkRVMg==\"}";
         Assert.assertEquals(expected, param);
 
 
-        PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE,"false");
-        param = dataSourceService.buildParameter("name","desc", DbType.MYSQL, 
"192.168.9.1","1521","im"
-                ,"","test","123456", null,"");
+        PropertyUtils.setValue(Constants.DATASOURCE_ENCRYPTION_ENABLE, 
"false");
+        param = dataSourceService.buildParameter(DbType.MYSQL, "192.168.9.1", 
"1521", "im"
+                , "", "test", "123456", null, "");
         expected = 
"{\"type\":null,\"address\":\"jdbc:mysql://192.168.9.1:1521\",\"database\":\"im\",\"jdbcUrl\":\"jdbc:mysql://192.168.9.1:1521/im\",\"user\":\"test\",\"password\":\"123456\"}";
         Assert.assertEquals(expected, param);
     }
 
+    /**
+     * get Mock Admin User
+     *
+     * @return admin user
+     */
+    private User getAdminUser() {
+        User loginUser = new User();
+        loginUser.setId(-1);
+        loginUser.setUserName("admin");
+        loginUser.setUserType(UserType.GENERAL_USER);
+        return loginUser;
+    }
 
-
-
+    private void putMsg(Map<String, Object> result, Status status, Object... 
statusParams) {
+        result.put(Constants.STATUS, status);
+        if (statusParams != null && statusParams.length > 0) {
+            result.put(Constants.MSG, MessageFormat.format(status.getMsg(), 
statusParams));
+        } else {
+            result.put(Constants.MSG, status.getMsg());
+        }
+    }

Review comment:
       ```suggestion
   ```
   Please remove this unused block.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to