@Test public void testGetTables() throws Exception{ DataSourceProvider provider=Mockito.mock(DataSourceProvider.class); List<Table> tables=Lists.newArrayList(); Table t=new Table(); t.setDateColumn("time"); t.setTableName("T1"); tables.add(t); when(provider.getTables()).thenReturn(tables); DataSourceMetaRepo repo=Mockito.mock(DataSourceMetaRepo.class); Map<String,DataSourceConfiguration> repoMap=Maps.newHashMap(); DataSourceConfiguration conf=new DataSourceConfiguration(DataSourceTypeEnum.PULSAR,"pulsar"); conf.setEndPoint(Lists.newArrayList("aaaa,bbbb")); conf.setRealOnly(true); conf.setRefreshTime(new Date().getTime()); repoMap.put("pulsar", conf); when(repo.getDBMetaFromCache(Matchers.anyString())).thenReturn(provider); ReflectFieldUtil.setStaticField(DataSourceMetaRepo.class, "instance", repo); RDBMS db = Mockito.mock(RDBMS.class); 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); DBDataSource datasource = new DBDataSource(); datasource.setName(uttestdatasource1); datasource.setId(1L); datasource.setOwner(uttestuser); datasource.setType(DataSourceTypeEnum.DRUID.getType()); List<String> ts=datasourceService.getDataSourceTables(datasource); Assert.assertEquals(Lists.newArrayList("T1"), ts); }
@PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW')") public List<String> getDataSourceTables(DBDataSource datasource) { StringBuilder keyBuilder = new StringBuilder(); Joiner.on('.').appendTo(keyBuilder, datasource.getType(), datasource.getName()); String dbNameSpace = keyBuilder.toString(); DataSourceProvider dataSourceProvider = DataSourceMetaRepo .getInstance().getDBMetaFromCache(dbNameSpace); if (dataSourceProvider != null) { return FluentIterable.from(dataSourceProvider.getTables()) .transform(new Function<Table, String>() { public String apply(Table input) { return input.getTableName(); } }).toList(); } return null; }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW')") public List<String> getDataSourceTables(DBDataSource datasource) { StringBuilder keyBuilder = new StringBuilder(); Joiner.on('.').appendTo(keyBuilder, datasource.getType(), datasource.getName()); String dbNameSpace = keyBuilder.toString(); DataSourceProvider dataSourceProvider = DataSourceMetaRepo .getInstance().getDBMetaFromCache(dbNameSpace); if (dataSourceProvider != null) { return FluentIterable.from(dataSourceProvider.getTables()) .transform(new Function<Table, String>() { public String apply(Table input) { return input.getTableName(); } }).toList(); } return null; } }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW')") public List<String> getDataSourceTables(DBDataSource datasource) { StringBuilder keyBuilder = new StringBuilder(); Joiner.on('.').appendTo(keyBuilder, datasource.getType(), datasource.getName()); String dbNameSpace = keyBuilder.toString(); DataSourceProvider dataSourceProvider = DataSourceMetaRepo .getInstance().getDBMetaFromCache(dbNameSpace); if (dataSourceProvider != null) { return FluentIterable.from(dataSourceProvider.getTables()) .transform(new Function<Table, String>() { public String apply(Table input) { return input.getTableName(); } }).toList(); } return null; } DataSourceService(); }
DataSourceService { @PreAuthorize("hasAuthority(#datasourceName+'_MANAGE') or hasAuthority(#datasourceName+'_VIEW')") public List<String> getDataSourceTables(DBDataSource datasource) { StringBuilder keyBuilder = new StringBuilder(); Joiner.on('.').appendTo(keyBuilder, datasource.getType(), datasource.getName()); String dbNameSpace = keyBuilder.toString(); DataSourceProvider dataSourceProvider = DataSourceMetaRepo .getInstance().getDBMetaFromCache(dbNameSpace); if (dataSourceProvider != null) { return FluentIterable.from(dataSourceProvider.getTables()) .transform(new Function<Table, String>() { public String apply(Table input) { return input.getTableName(); } }).toList(); } return null; } 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(#datasourceName+'_VIEW')") public List<String> getDataSourceTables(DBDataSource datasource) { StringBuilder keyBuilder = new StringBuilder(); Joiner.on('.').appendTo(keyBuilder, datasource.getType(), datasource.getName()); String dbNameSpace = keyBuilder.toString(); DataSourceProvider dataSourceProvider = DataSourceMetaRepo .getInstance().getDBMetaFromCache(dbNameSpace); if (dataSourceProvider != null) { return FluentIterable.from(dataSourceProvider.getTables()) .transform(new Function<Table, String>() { public String apply(Table input) { return input.getTableName(); } }).toList(); } return null; } 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();  }