@SuppressWarnings("unchecked") @Test public void addDataSourceTest() throws Exception { final DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setOwner(uttestuser); datasourceCondition.setName(uttestdatasource1); datasourceCondition.setType("druid"); datasourceCondition.setEndpoint("http: DBDataSource datasourceCondition2 = new DBDataSource(); datasourceCondition2.setOwner(uttestuser); datasourceCondition2.setName(uttestdatasource2); datasourceCondition2.setType("druid"); datasourceCondition2.setEndpoint("http: 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()); when(db.insert(Mockito.anyString(), Matchers.anyMap(), Matchers.any(GeneratedKeyHolder.class))) .thenReturn(1); final GeneratedKeyHolder keyHolder = PowerMockito.mock(GeneratedKeyHolder.class); PowerMockito.whenNew(GeneratedKeyHolder.class).withNoArguments() .thenReturn(keyHolder); when(keyHolder.getKey()).thenReturn(1L); 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.addDataSource(datasourceCondition); Assert.assertTrue(id > 0); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("name", uttestdatasource1)), Matchers.eq(-1),Matchers.any(DBDataSourceMapper.class))) .thenReturn(Lists.<DBDataSource>newArrayList(datasourceCondition)); Assert.assertTrue(uttestuser.equalsIgnoreCase(datasourceService .getDataSourceByName(uttestdatasource1).getOwner())); long id2 = datasourceService.addDataSource(datasourceCondition2); Assert.assertTrue(id2 > 0); when(db.query(Matchers.anyString(),Matchers.eq(ImmutableMap.of("owner", uttestuser)), Matchers.eq(-1),Matchers.any(DBDataSourceMapper.class))) .thenReturn(Lists.<DBDataSource>newArrayList(datasourceCondition,datasourceCondition2)); List<String> list = datasourceService .getAllDataSourcesForOwner(uttestuser); Assert.assertTrue(list.size() > 0); Assert.assertTrue(list.size()==2); }
@PreAuthorize("hasAuthority('ADD_DATASOURCE')") public long addDataSource(DBDataSource datasource) { checkNotNull(datasource); checkArgument( datasource.getName() != null && !"".equals(datasource.getName()), "datasource name could not be empty."); checkArgument( !isStaticDataSource(datasource.getName()), "datasource name["+datasource.getName()+"] is reserved."); checkArgument( datasource.getType() != null && !"".equals(datasource.getType()), "datasource type could not be empty."); checkArgument( datasource.getEndpoint() != null && !"".equals(datasource.getEndpoint()), "datasource endpoint could not be empty."); checkArgument(!StringUtils.isEmpty(datasource.getOwner()), "datasource owner must be specified."); if (datasource.getDisplayName() == null) datasource.setDisplayName(datasource.getName()); if (datasource.getCreateTime() == null) datasource.setCreateTime(new Date()); datasource.setName(datasource.getName()); DBDataSource dataSourceCondition = new DBDataSource(); dataSourceCondition.setName(datasource.getName()); List<DBDataSource> list = datasourceService.get(dataSourceCondition); checkState(list == null || list.size() == 0, "[%s] datasource name already exists.", datasource.getName()); long id = datasourceService.inser(datasource); checkState(id > 0, "insert datasource to db failed.datasourceName=%s,owner=", datasource.getName(), datasource.getOwner()); datasource.setId(id); DynamicDataSourceConfigurationManager.activateDataSource(datasource); PermissionControlCache.getInstance().expireSessions(datasource.getOwner()); return id; }
DataSourceService { @PreAuthorize("hasAuthority('ADD_DATASOURCE')") public long addDataSource(DBDataSource datasource) { checkNotNull(datasource); checkArgument( datasource.getName() != null && !"".equals(datasource.getName()), "datasource name could not be empty."); checkArgument( !isStaticDataSource(datasource.getName()), "datasource name["+datasource.getName()+"] is reserved."); checkArgument( datasource.getType() != null && !"".equals(datasource.getType()), "datasource type could not be empty."); checkArgument( datasource.getEndpoint() != null && !"".equals(datasource.getEndpoint()), "datasource endpoint could not be empty."); checkArgument(!StringUtils.isEmpty(datasource.getOwner()), "datasource owner must be specified."); if (datasource.getDisplayName() == null) datasource.setDisplayName(datasource.getName()); if (datasource.getCreateTime() == null) datasource.setCreateTime(new Date()); datasource.setName(datasource.getName()); DBDataSource dataSourceCondition = new DBDataSource(); dataSourceCondition.setName(datasource.getName()); List<DBDataSource> list = datasourceService.get(dataSourceCondition); checkState(list == null || list.size() == 0, "[%s] datasource name already exists.", datasource.getName()); long id = datasourceService.inser(datasource); checkState(id > 0, "insert datasource to db failed.datasourceName=%s,owner=", datasource.getName(), datasource.getOwner()); datasource.setId(id); DynamicDataSourceConfigurationManager.activateDataSource(datasource); PermissionControlCache.getInstance().expireSessions(datasource.getOwner()); return id; } }
DataSourceService { @PreAuthorize("hasAuthority('ADD_DATASOURCE')") public long addDataSource(DBDataSource datasource) { checkNotNull(datasource); checkArgument( datasource.getName() != null && !"".equals(datasource.getName()), "datasource name could not be empty."); checkArgument( !isStaticDataSource(datasource.getName()), "datasource name["+datasource.getName()+"] is reserved."); checkArgument( datasource.getType() != null && !"".equals(datasource.getType()), "datasource type could not be empty."); checkArgument( datasource.getEndpoint() != null && !"".equals(datasource.getEndpoint()), "datasource endpoint could not be empty."); checkArgument(!StringUtils.isEmpty(datasource.getOwner()), "datasource owner must be specified."); if (datasource.getDisplayName() == null) datasource.setDisplayName(datasource.getName()); if (datasource.getCreateTime() == null) datasource.setCreateTime(new Date()); datasource.setName(datasource.getName()); DBDataSource dataSourceCondition = new DBDataSource(); dataSourceCondition.setName(datasource.getName()); List<DBDataSource> list = datasourceService.get(dataSourceCondition); checkState(list == null || list.size() == 0, "[%s] datasource name already exists.", datasource.getName()); long id = datasourceService.inser(datasource); checkState(id > 0, "insert datasource to db failed.datasourceName=%s,owner=", datasource.getName(), datasource.getOwner()); datasource.setId(id); DynamicDataSourceConfigurationManager.activateDataSource(datasource); PermissionControlCache.getInstance().expireSessions(datasource.getOwner()); return id; } DataSourceService(); }
DataSourceService { @PreAuthorize("hasAuthority('ADD_DATASOURCE')") public long addDataSource(DBDataSource datasource) { checkNotNull(datasource); checkArgument( datasource.getName() != null && !"".equals(datasource.getName()), "datasource name could not be empty."); checkArgument( !isStaticDataSource(datasource.getName()), "datasource name["+datasource.getName()+"] is reserved."); checkArgument( datasource.getType() != null && !"".equals(datasource.getType()), "datasource type could not be empty."); checkArgument( datasource.getEndpoint() != null && !"".equals(datasource.getEndpoint()), "datasource endpoint could not be empty."); checkArgument(!StringUtils.isEmpty(datasource.getOwner()), "datasource owner must be specified."); if (datasource.getDisplayName() == null) datasource.setDisplayName(datasource.getName()); if (datasource.getCreateTime() == null) datasource.setCreateTime(new Date()); datasource.setName(datasource.getName()); DBDataSource dataSourceCondition = new DBDataSource(); dataSourceCondition.setName(datasource.getName()); List<DBDataSource> list = datasourceService.get(dataSourceCondition); checkState(list == null || list.size() == 0, "[%s] datasource name already exists.", datasource.getName()); long id = datasourceService.inser(datasource); checkState(id > 0, "insert datasource to db failed.datasourceName=%s,owner=", datasource.getName(), datasource.getOwner()); datasource.setId(id); DynamicDataSourceConfigurationManager.activateDataSource(datasource); PermissionControlCache.getInstance().expireSessions(datasource.getOwner()); return id; } 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('ADD_DATASOURCE')") public long addDataSource(DBDataSource datasource) { checkNotNull(datasource); checkArgument( datasource.getName() != null && !"".equals(datasource.getName()), "datasource name could not be empty."); checkArgument( !isStaticDataSource(datasource.getName()), "datasource name["+datasource.getName()+"] is reserved."); checkArgument( datasource.getType() != null && !"".equals(datasource.getType()), "datasource type could not be empty."); checkArgument( datasource.getEndpoint() != null && !"".equals(datasource.getEndpoint()), "datasource endpoint could not be empty."); checkArgument(!StringUtils.isEmpty(datasource.getOwner()), "datasource owner must be specified."); if (datasource.getDisplayName() == null) datasource.setDisplayName(datasource.getName()); if (datasource.getCreateTime() == null) datasource.setCreateTime(new Date()); datasource.setName(datasource.getName()); DBDataSource dataSourceCondition = new DBDataSource(); dataSourceCondition.setName(datasource.getName()); List<DBDataSource> list = datasourceService.get(dataSourceCondition); checkState(list == null || list.size() == 0, "[%s] datasource name already exists.", datasource.getName()); long id = datasourceService.inser(datasource); checkState(id > 0, "insert datasource to db failed.datasourceName=%s,owner=", datasource.getName(), datasource.getOwner()); datasource.setId(id); DynamicDataSourceConfigurationManager.activateDataSource(datasource); PermissionControlCache.getInstance().expireSessions(datasource.getOwner()); return id; } 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();  }