@SuppressWarnings("unchecked") @Test public void deleteDataSourceTest() throws Exception { final DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setOwner(uttestuser); datasourceCondition.setName(uttestdatasource1); datasourceCondition.setType("druid"); datasourceCondition.setEndpoint("http: datasourceCondition.setCreateTime(new Date()); final DBDataSource datasourceCondition2 = new DBDataSource(); datasourceCondition2.setOwner(uttestuser); datasourceCondition2.setName(uttestdatasource2); datasourceCondition2.setType("druid"); datasourceCondition2.setEndpoint("http: datasourceCondition2.setCreateTime(new Date()); RDBMS db = Mockito.mock(RDBMS.class); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("name", uttestdatasource1)), Matchers.eq(-1),Matchers.any(DBDataSourceMapper.class))) .thenReturn(Lists.<DBDataSource>newArrayList(datasourceCondition)); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("name", uttestdatasource2)), Matchers.eq(-1),Matchers.any(DBDataSourceMapper.class))) .thenReturn(Lists.<DBDataSource>newArrayList(datasourceCondition2)); when(db.update(Mockito.anyString(), Matchers.anyMap())) .thenReturn(1); when(db.delete(Mockito.anyString(), Matchers.eq(ImmutableMap.of("name",uttestdatasource1)))) .thenReturn(1); when(db.delete(Mockito.anyString(), Matchers.eq(ImmutableMap.of("name",uttestdatasource2)))) .thenReturn(1); DataSourceService datasourceService = new DataSourceService(); BaseDBService<?> ds=(BaseDBService<?>)ReflectFieldUtil.getField(datasourceService, "datasourceService"); BaseDBService<?> rs=(BaseDBService<?>)ReflectFieldUtil.getField(datasourceService, "rightGroupService"); ReflectFieldUtil.setField(BaseDBService.class,ds, "db", db); ReflectFieldUtil.setField(BaseDBService.class,rs, "db", db); long id = datasourceService.deleteDataSource(uttestdatasource1); Assert.assertTrue(id > 0); datasourceService.deleteDataSources(Lists.newArrayList( uttestdatasource1,uttestdatasource2)); Assert.assertTrue(id > 0); }
@PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") public int deleteDataSource(String datasourceName) { checkNotNull(datasourceName); if(isStaticDataSource(datasourceName)) return 0; DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setName(datasourceName); List<DBDataSource> list=datasourceService.get(datasourceCondition); if(list.size()>0){ rightGroupService.deleteRightsFromGroupByPrefix(datasourceName + "_"); int result = datasourceService.deleteBatch(datasourceCondition); if (result >= 0) { DynamicDataSourceConfigurationManager.disableDataSource(list.get(0)); PermissionControlCache.getInstance().expireSessionsAll(); } return result; } return 0; }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") public int deleteDataSource(String datasourceName) { checkNotNull(datasourceName); if(isStaticDataSource(datasourceName)) return 0; DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setName(datasourceName); List<DBDataSource> list=datasourceService.get(datasourceCondition); if(list.size()>0){ rightGroupService.deleteRightsFromGroupByPrefix(datasourceName + "_"); int result = datasourceService.deleteBatch(datasourceCondition); if (result >= 0) { DynamicDataSourceConfigurationManager.disableDataSource(list.get(0)); PermissionControlCache.getInstance().expireSessionsAll(); } return result; } return 0; } }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") public int deleteDataSource(String datasourceName) { checkNotNull(datasourceName); if(isStaticDataSource(datasourceName)) return 0; DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setName(datasourceName); List<DBDataSource> list=datasourceService.get(datasourceCondition); if(list.size()>0){ rightGroupService.deleteRightsFromGroupByPrefix(datasourceName + "_"); int result = datasourceService.deleteBatch(datasourceCondition); if (result >= 0) { DynamicDataSourceConfigurationManager.disableDataSource(list.get(0)); PermissionControlCache.getInstance().expireSessionsAll(); } return result; } return 0; } DataSourceService(); }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") public int deleteDataSource(String datasourceName) { checkNotNull(datasourceName); if(isStaticDataSource(datasourceName)) return 0; DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setName(datasourceName); List<DBDataSource> list=datasourceService.get(datasourceCondition); if(list.size()>0){ rightGroupService.deleteRightsFromGroupByPrefix(datasourceName + "_"); int result = datasourceService.deleteBatch(datasourceCondition); if (result >= 0) { DynamicDataSourceConfigurationManager.disableDataSource(list.get(0)); PermissionControlCache.getInstance().expireSessionsAll(); } return result; } return 0; } DataSourceService(); @PreAuthorize("hasAuthority('ADD_DATASOURCE')") long addDataSource(DBDataSource datasource); @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") int deleteDataSource(String datasourceName); @PreFilter("hasAuthority(filterObject+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") int deleteDataSources(List<String> datasourceNames); @PreAuthorize("hasAuthority(#datasource.name+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") int updateDataSource(DBDataSource datasource); @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW') or hasAuthority('SYS_MANAGE_DATASOURCE') or hasAuthority('SYS_VIEW_DATASOURCE')") DBDataSource getDataSourceByName(String datasourceName); @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW')") List<String> getDataSourceTables(DBDataSource datasource); List<String> getAllDataSourcesForOwner(String userName); @PreFilter("hasAuthority(filterObject+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE') or hasAuthority('SYS_VIEW_DATASOURCE')") List<DBDataSource> getDataSourceByNames(List<String> names); @PostFilter("hasAuthority(filterObject.name+'_MANAGE') or hasAuthority(filterObject.name+'_VIEW') or hasAuthority('SYS_MANAGE_DATASOURCE') or hasAuthority('SYS_VIEW_DATASOURCE')") Set<DBDataSource> getAllUserViewedDatasource(); @PostFilter("hasAuthority(filterObject.name+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") Set<DBDataSource> getAllUserManagedDatasource(); }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") public int deleteDataSource(String datasourceName) { checkNotNull(datasourceName); if(isStaticDataSource(datasourceName)) return 0; DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setName(datasourceName); List<DBDataSource> list=datasourceService.get(datasourceCondition); if(list.size()>0){ rightGroupService.deleteRightsFromGroupByPrefix(datasourceName + "_"); int result = datasourceService.deleteBatch(datasourceCondition); if (result >= 0) { DynamicDataSourceConfigurationManager.disableDataSource(list.get(0)); PermissionControlCache.getInstance().expireSessionsAll(); } return result; } return 0; } DataSourceService(); @PreAuthorize("hasAuthority('ADD_DATASOURCE')") long addDataSource(DBDataSource datasource); @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") int deleteDataSource(String datasourceName); @PreFilter("hasAuthority(filterObject+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") int deleteDataSources(List<String> datasourceNames); @PreAuthorize("hasAuthority(#datasource.name+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") int updateDataSource(DBDataSource datasource); @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW') or hasAuthority('SYS_MANAGE_DATASOURCE') or hasAuthority('SYS_VIEW_DATASOURCE')") DBDataSource getDataSourceByName(String datasourceName); @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW')") List<String> getDataSourceTables(DBDataSource datasource); List<String> getAllDataSourcesForOwner(String userName); @PreFilter("hasAuthority(filterObject+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE') or hasAuthority('SYS_VIEW_DATASOURCE')") List<DBDataSource> getDataSourceByNames(List<String> names); @PostFilter("hasAuthority(filterObject.name+'_MANAGE') or hasAuthority(filterObject.name+'_VIEW') or hasAuthority('SYS_MANAGE_DATASOURCE') or hasAuthority('SYS_VIEW_DATASOURCE')") Set<DBDataSource> getAllUserViewedDatasource(); @PostFilter("hasAuthority(filterObject.name+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") Set<DBDataSource> getAllUserManagedDatasource();  }