ListDataSourceCommand extends GfshCommand { boolean isDataSourceUsedByRegion(CacheConfig cacheConfig, String dataSourceName) { return cacheConfig.getRegions().stream() .anyMatch(regionConfig -> hasJdbcMappingThatUsesDataSource(regionConfig, dataSourceName)); }  @CliCommand(value = LIST_DATA_SOURCE, help = LIST_DATA_SOURCE__HELP) @CliMetaData @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.READ) ResultModel listDataSources(); @CliAvailabilityIndicator({LIST_DATA_SOURCE}) boolean commandAvailable();  }
@Test public void isDataSourceUsedByRegionGivenNoRegionsReturnsFalse() { regionConfigs.clear(); boolean result = command.isDataSourceUsedByRegion(cacheConfig, ""); assertThat(result).isFalse(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithNoCustomRegionElementsReturnsFalse() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()).thenReturn(Collections.emptyList()); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, ""); assertThat(result).isFalse(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithNonRegionMappingElementReturnsFalse() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(CacheElement.class))); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, ""); assertThat(result).isFalse(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithRegionMappingForOtherDataSourceReturnsFalse() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(RegionMapping.class))); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, "bogusDataSource"); assertThat(result).isFalse(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithRegionMappingForDataSourceReturnsTrue() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getName()).thenReturn("regionName"); RegionMapping regionMapping = mock(RegionMapping.class); when(regionMapping.getDataSourceName()).thenReturn("dataSourceName"); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(regionMapping)); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, "dataSourceName"); assertThat(result).isTrue(); }
@Test public void isDataSourceUsedByRegionGivenMultipleRegionConfigsReturnsTrue() { RegionMapping regionMapping; { RegionConfig regionConfig1 = mock(RegionConfig.class, "regionConfig1"); when(regionConfig1.getName()).thenReturn("regionName1"); regionMapping = mock(RegionMapping.class, "regionMapping1"); when(regionMapping.getDataSourceName()).thenReturn("dataSourceName"); when(regionConfig1.getCustomRegionElements()) .thenReturn(Arrays.asList(regionMapping)); regionConfigs.add(regionConfig1); } { RegionConfig regionConfig2 = mock(RegionConfig.class, "regionConfig2"); when(regionConfig2.getName()).thenReturn("regionName2"); regionMapping = mock(RegionMapping.class, "regionMapping2"); when(regionMapping.getDataSourceName()).thenReturn("otherDataSourceName"); when(regionConfig2.getCustomRegionElements()) .thenReturn(Arrays.asList(regionMapping)); regionConfigs.add(regionConfig2); } { RegionConfig regionConfig3 = mock(RegionConfig.class, "regionConfig3"); when(regionConfig3.getName()).thenReturn("regionName3"); regionMapping = mock(RegionMapping.class, "regionMapping3"); when(regionMapping.getDataSourceName()).thenReturn("dataSourceName"); when(regionConfig3.getCustomRegionElements()) .thenReturn(Arrays.asList(regionMapping)); regionConfigs.add(regionConfig3); } boolean result = command.isDataSourceUsedByRegion(cacheConfig, "dataSourceName"); assertThat(result).isTrue(); }