5252
5353import org .apache .commons .collections4 .CollectionUtils ;
5454
55+ import java .nio .charset .StandardCharsets ;
5556import java .sql .Connection ;
5657import java .sql .SQLException ;
5758import java .util .ArrayList ;
5859import java .util .Collections ;
5960import java .util .HashMap ;
60- import java .util .HashSet ;
6161import java .util .List ;
6262import java .util .Map ;
63- import java .util .Set ;
63+ import java .util .Random ;
6464import java .util .concurrent .ExecutionException ;
6565
6666import org .junit .jupiter .api .Assertions ;
7373import org .mockito .junit .jupiter .MockitoExtension ;
7474import org .slf4j .Logger ;
7575import org .slf4j .LoggerFactory ;
76+ import org .springframework .dao .DuplicateKeyException ;
77+
78+ import com .baomidou .mybatisplus .core .metadata .IPage ;
7679
7780/**
7881 * data source service test
@@ -96,6 +99,15 @@ public class DataSourceServiceTest {
9699 @ Mock
97100 private ResourcePermissionCheckService resourcePermissionCheckService ;
98101
102+ @ Mock
103+ private IPage <DataSource > dataSourceList ;
104+
105+ private String randomStringWithLengthN (int n ) {
106+ byte [] bitArray = new byte [n ];
107+ new Random ().nextBytes (bitArray );
108+ return new String (bitArray , StandardCharsets .UTF_8 );
109+ }
110+
99111 private void passResourcePermissionCheckService () {
100112 when (resourcePermissionCheckService .operationPermissionCheck (Mockito .any (), Mockito .anyInt (),
101113 Mockito .anyString (), Mockito .any ())).thenReturn (true );
@@ -131,6 +143,7 @@ public void createDataSourceTest() throws ExecutionException {
131143 when (dataSourceMapper .queryDataSourceByName (dataSourceName .trim ())).thenReturn (dataSourceList );
132144 passResourcePermissionCheckService ();
133145
146+ // DATASOURCE_EXIST
134147 assertThrowsServiceException (Status .DATASOURCE_EXIST ,
135148 () -> dataSourceService .createDataSource (loginUser , postgreSqlDatasourceParam ));
136149
@@ -140,13 +153,24 @@ public void createDataSourceTest() throws ExecutionException {
140153
141154 when (dataSourceMapper .queryDataSourceByName (dataSourceName .trim ())).thenReturn (null );
142155
156+ // DESCRIPTION TOO LONG
157+ postgreSqlDatasourceParam .setNote (randomStringWithLengthN (512 ));
158+ assertThrowsServiceException (Status .DESCRIPTION_TOO_LONG_ERROR ,
159+ () -> dataSourceService .createDataSource (loginUser , postgreSqlDatasourceParam ));
160+ postgreSqlDatasourceParam .setNote (dataSourceDesc );
161+
143162 // SUCCESS
144163 assertDoesNotThrow (() -> dataSourceService .createDataSource (loginUser , postgreSqlDatasourceParam ));
164+
165+ // Duplicated Key Exception
166+ when (dataSourceMapper .insert (Mockito .any (DataSource .class ))).thenThrow (DuplicateKeyException .class );
167+ assertThrowsServiceException (Status .DATASOURCE_EXIST ,
168+ () -> dataSourceService .createDataSource (loginUser , postgreSqlDatasourceParam ));
145169 }
146170 }
147171
148172 @ Test
149- public void updateDataSourceTest () throws ExecutionException {
173+ public void updateDataSourceTest () {
150174 User loginUser = getAdminUser ();
151175
152176 int dataSourceId = 12 ;
@@ -200,32 +224,74 @@ public void updateDataSourceTest() throws ExecutionException {
200224 // DATASOURCE_CONNECT_FAILED
201225 when (dataSourceMapper .queryDataSourceByName (postgreSqlDatasourceParam .getName ())).thenReturn (null );
202226
227+ // DESCRIPTION TOO LONG
228+ postgreSqlDatasourceParam .setNote (randomStringWithLengthN (512 ));
229+ assertThrowsServiceException (Status .DESCRIPTION_TOO_LONG_ERROR ,
230+ () -> dataSourceService .updateDataSource (loginUser , postgreSqlDatasourceParam ));
231+ postgreSqlDatasourceParam .setNote (dataSourceDesc );
232+
203233 // SUCCESS
204234 assertDoesNotThrow (() -> dataSourceService .updateDataSource (loginUser , postgreSqlDatasourceParam ));
235+
236+ // Duplicated Key Exception
237+ when (dataSourceMapper .updateById (Mockito .any (DataSource .class ))).thenThrow (DuplicateKeyException .class );
238+ assertThrowsServiceException (Status .DATASOURCE_EXIST ,
239+ () -> dataSourceService .updateDataSource (loginUser , postgreSqlDatasourceParam ));
205240 }
206241 }
207242
208243 @ Test
209- public void queryDataSourceListPagingTest () {
210- User loginUser = getAdminUser ();
244+ public void testQueryDataSourceListPaging () {
245+
246+ User adminUser = getAdminUser ();
247+ User generalUser = getGeneralUser ();
211248 String searchVal = "" ;
212249 int pageNo = 1 ;
213250 int pageSize = 10 ;
214251
215252 PageInfo <DataSource > pageInfo =
216- dataSourceService .queryDataSourceListPaging (loginUser , searchVal , pageNo , pageSize );
253+ dataSourceService .queryDataSourceListPaging (adminUser , searchVal , pageNo , pageSize );
217254 Assertions .assertNotNull (pageInfo );
255+
256+ // test query datasource as general user with no datasource authed
257+ when (dataSourceList .getRecords ()).thenReturn (getSingleDataSourceList ());
258+ when (dataSourceMapper .selectPagingByIds (Mockito .any (), Mockito .any (), Mockito .any ()))
259+ .thenReturn (dataSourceList );
260+ assertDoesNotThrow (() -> dataSourceService .queryDataSourceListPaging (generalUser , searchVal , pageNo , pageSize ));
261+
262+ // test query datasource as general user with datasource authed
263+ when (resourcePermissionCheckService .userOwnedResourceIdsAcquisition (AuthorizationType .DATASOURCE ,
264+ generalUser .getId (), dataSourceServiceLogger )).thenReturn (Collections .singleton (1 ));
265+
266+ assertDoesNotThrow (() -> dataSourceService .queryDataSourceListPaging (generalUser , searchVal , pageNo , pageSize ));
218267 }
219268
220269 @ Test
221- public void connectionTest () {
270+ public void testConnectionTest () {
222271 int dataSourceId = -1 ;
223272 when (dataSourceMapper .selectById (dataSourceId )).thenReturn (null );
224273 assertThrowsServiceException (Status .RESOURCE_NOT_EXIST , () -> dataSourceService .connectionTest (dataSourceId ));
274+
275+ try (
276+ MockedStatic <DataSourceUtils > ignored =
277+ Mockito .mockStatic (DataSourceUtils .class )) {
278+ DataSource dataSource = getOracleDataSource (999 );
279+ when (dataSourceMapper .selectById (dataSource .getId ())).thenReturn (dataSource );
280+ DataSourceProcessor dataSourceProcessor = Mockito .mock (DataSourceProcessor .class );
281+
282+ when (DataSourceUtils .getDatasourceProcessor (Mockito .any ())).thenReturn (dataSourceProcessor );
283+ when (dataSourceProcessor .checkDataSourceConnectivity (Mockito .any ())).thenReturn (true );
284+ assertDoesNotThrow (() -> dataSourceService .connectionTest (dataSource .getId ()));
285+
286+ when (dataSourceProcessor .checkDataSourceConnectivity (Mockito .any ())).thenReturn (false );
287+ assertThrowsServiceException (Status .CONNECTION_TEST_FAILURE ,
288+ () -> dataSourceService .connectionTest (dataSource .getId ()));
289+ }
290+
225291 }
226292
227293 @ Test
228- public void deleteTest () {
294+ public void testDelete () {
229295 User loginUser = getAdminUser ();
230296 int dataSourceId = 1 ;
231297 // resource not exist
@@ -252,7 +318,7 @@ public void deleteTest() {
252318 }
253319
254320 @ Test
255- public void unauthDatasourceTest () {
321+ public void testUnAuthDatasource () {
256322 User loginUser = getAdminUser ();
257323 loginUser .setId (1 );
258324 loginUser .setUserType (UserType .ADMIN_USER );
@@ -279,7 +345,7 @@ public void unauthDatasourceTest() {
279345 }
280346
281347 @ Test
282- public void authedDatasourceTest () {
348+ public void testAuthedDatasource () {
283349 User loginUser = getAdminUser ();
284350 loginUser .setId (1 );
285351 loginUser .setUserType (UserType .ADMIN_USER );
@@ -300,19 +366,28 @@ public void authedDatasourceTest() {
300366 }
301367
302368 @ Test
303- public void queryDataSourceListTest () {
304- User loginUser = new User ();
305- loginUser .setUserType (UserType .GENERAL_USER );
306- Set <Integer > dataSourceIds = new HashSet <>();
307- dataSourceIds .add (1 );
369+ public void testQueryDataSourceList () {
370+ User adminUser = getAdminUser ();
371+ assertDoesNotThrow (() -> dataSourceService .queryDataSourceList (adminUser , DbType .MYSQL .ordinal ()));
372+
373+ User generalUser = getGeneralUser ();
374+
308375 when (resourcePermissionCheckService .userOwnedResourceIdsAcquisition (AuthorizationType .DATASOURCE ,
309- loginUser .getId (), dataSourceServiceLogger )).thenReturn (dataSourceIds );
376+ generalUser .getId (), dataSourceServiceLogger )).thenReturn (Collections .emptySet ());
377+ List <DataSource > emptyList = dataSourceService .queryDataSourceList (generalUser , DbType .MYSQL .ordinal ());
378+ Assertions .assertEquals (emptyList .size (), 0 );
379+
380+ when (resourcePermissionCheckService .userOwnedResourceIdsAcquisition (AuthorizationType .DATASOURCE ,
381+ generalUser .getId (), dataSourceServiceLogger )).thenReturn (Collections .singleton (1 ));
310382
311383 DataSource dataSource = new DataSource ();
384+ dataSource .setId (1 );
312385 dataSource .setType (DbType .MYSQL );
313- when (dataSourceMapper .selectBatchIds (dataSourceIds )).thenReturn (Collections .singletonList (dataSource ));
386+ when (dataSourceMapper .selectBatchIds (Collections .singleton (1 )))
387+ .thenReturn (Collections .singletonList (dataSource ));
388+
314389 List <DataSource > list =
315- dataSourceService .queryDataSourceList (loginUser , DbType .MYSQL .ordinal ());
390+ dataSourceService .queryDataSourceList (generalUser , DbType .MYSQL .ordinal ());
316391 Assertions .assertNotNull (list );
317392 }
318393
@@ -327,21 +402,28 @@ public void verifyDataSourceNameTest() {
327402 }
328403
329404 @ Test
330- public void queryDataSourceTest () {
331- when (dataSourceMapper .selectById (Mockito .anyInt ())).thenReturn (null );
405+ public void testQueryDataSource () {
406+ // datasource not exists
407+ when (dataSourceMapper .selectById (999 )).thenReturn (null );
332408 User loginUser = new User ();
333409 loginUser .setUserType (UserType .GENERAL_USER );
334410 loginUser .setId (2 );
335- try {
336- dataSourceService .queryDataSource (Mockito .anyInt (), loginUser );
337- } catch (Exception e ) {
338- Assertions .assertTrue (e .getMessage ().contains (Status .RESOURCE_NOT_EXIST .getMsg ()));
339- }
411+
412+ assertThrowsServiceException (Status .RESOURCE_NOT_EXIST ,
413+ () -> dataSourceService .queryDataSource (999 , loginUser ));
340414
341415 DataSource dataSource = getOracleDataSource (1 );
342- when (dataSourceMapper .selectById (Mockito . anyInt ())).thenReturn (dataSource );
416+ when (dataSourceMapper .selectById (dataSource . getId ())).thenReturn (dataSource );
343417 when (resourcePermissionCheckService .operationPermissionCheck (AuthorizationType .DATASOURCE ,
344418 loginUser .getId (), DATASOURCE , baseServiceLogger )).thenReturn (true );
419+
420+ // no perm
421+ when (resourcePermissionCheckService .resourcePermissionCheck (AuthorizationType .DATASOURCE ,
422+ new Object []{dataSource .getId ()}, loginUser .getId (), baseServiceLogger )).thenReturn (false );
423+ assertThrowsServiceException (Status .USER_NO_OPERATION_PERM ,
424+ () -> dataSourceService .queryDataSource (dataSource .getId (), loginUser ));
425+
426+ // success
345427 when (resourcePermissionCheckService .resourcePermissionCheck (AuthorizationType .DATASOURCE ,
346428 new Object []{dataSource .getId ()}, loginUser .getId (), baseServiceLogger )).thenReturn (true );
347429 BaseDataSourceParamDTO paramDTO = dataSourceService .queryDataSource (dataSource .getId (), loginUser );
@@ -472,8 +554,16 @@ public void buildParameterWithDecodePassword() {
472554 */
473555 private User getAdminUser () {
474556 User loginUser = new User ();
475- loginUser .setId (- 1 );
557+ loginUser .setId (1 );
476558 loginUser .setUserName ("admin" );
559+ loginUser .setUserType (UserType .ADMIN_USER );
560+ return loginUser ;
561+ }
562+
563+ private User getGeneralUser () {
564+ User loginUser = new User ();
565+ loginUser .setId (2 );
566+ loginUser .setUserName ("user" );
477567 loginUser .setUserType (UserType .GENERAL_USER );
478568 return loginUser ;
479569 }
0 commit comments