DataSourceService { @PreAuthorize("hasAuthority(#datasource.name+'_MANAGE') or hasAuthority('SYS_MANAGE_DATASOURCE')") public int updateDataSource(DBDataSource datasource) { checkNotNull(datasource.getName()); checkArgument( !isStaticDataSource(datasource.getName()), "datasource ["+datasource.getName()+"] couldn't be updated."); DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setName(datasource.getName()); List<DBDataSource> list = datasourceService.get(datasourceCondition); checkState(list.size() == 1, "[%s] datasource is not exists.", datasource.getName()); datasourceCondition.setId(list.get(0).getId()); if (datasource.getDisplayName() != null) { datasourceCondition.setDisplayName(datasource.getDisplayName()); } if (datasource.getEndpoint() != null) { datasourceCondition.setEndpoint(datasource.getEndpoint()); } if (datasource.getProperties() != null) { datasourceCondition.setProperties(datasource.getProperties()); } DynamicDataSourceConfigurationManager.disableDataSource(list.get(0)); int response = datasourceService.updateById(datasourceCondition); DBDataSource fromDB = getDataSourceByName(datasource.getName()); DynamicDataSourceConfigurationManager.activateDataSource(fromDB); return response; } 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();  }
@SuppressWarnings("unchecked") @Test public void updateDataSourceTest() throws Exception { DBDataSource datasource = new DBDataSource(); datasource.setName(uttestdatasource1); final DBDataSource datasourceCondition = new DBDataSource(); datasourceCondition.setOwner(uttestuser); datasourceCondition.setName(uttestdatasource1); datasourceCondition.setType("druid"); datasourceCondition.setEndpoint("http: datasourceCondition.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.update(Mockito.anyString(), Matchers.anyMap())) .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); String newConfig = "testConfig"; datasource.setProperties(newConfig); long id = datasourceService.updateDataSource(datasource); Assert.assertTrue(id == 1); }