ListDataSourceCommand extends GfshCommand { @CliCommand(value = LIST_DATA_SOURCE, help = LIST_DATA_SOURCE__HELP) @CliMetaData @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.READ) public ResultModel listDataSources() { ResultModel resultModel = new ResultModel(); resultModel.setHeader(EXPERIMENTAL); TabularResultModel tabularData = resultModel.addTable(DATA_SOURCE_PROPERTIES_SECTION); tabularData.setColumnHeader("name", "pooled", "in use", "url"); ConfigurationPersistenceService ccService = getConfigurationPersistenceService(); if (ccService == null) { return ResultModel.createError("Cluster configuration service must be enabled."); } CacheConfig cacheConfig = ccService.getCacheConfig(null); if (cacheConfig == null) { return ResultModel.createInfo("No data sources found"); } cacheConfig.getJndiBindings().stream() .forEach(dataSource -> addDataSourceToResult(dataSource, cacheConfig, tabularData)); return resultModel; }  @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 listDataSourceWithNoClusterConfigurationServerFails() { doReturn(null).when(command).getConfigurationPersistenceService(); ResultModel result = command.listDataSources(); assertThat(result.getStatus()).isEqualTo(Status.ERROR); assertThat(result.toString()).contains("Cluster configuration service must be enabled."); }
@Test public void listDataSourceWithNoClusterConfigIsOkWithNoDataSources() { doReturn(null).when(clusterConfigService).getCacheConfig(any()); ResultModel result = command.listDataSources(); assertThat(result.getStatus()).isEqualTo(Status.OK); assertThat(result.toString()).contains("No data sources found"); }
@Test public void listDataSourceWithUnsupportedTypeIgnoresIt() { binding.setType(DATASOURCE_TYPE.MANAGED.getType()); ResultModel result = command.listDataSources(); assertThat(result.getStatus()).isEqualTo(Status.OK); assertThat(result.toString()).doesNotContain(DATA_SOURCE_NAME); }
@Test public void listDataSourceWithSimpleTypeReturnsPooledFalse() { binding.setType(DATASOURCE_TYPE.SIMPLE.getType()); ResultModel result = command.listDataSources(); TabularResultModel section = result.getTableSection(ListDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)) .isEqualTo(Arrays.asList(DATA_SOURCE_NAME, "false", "false", "myURL")); }
@Test public void listDataSourceWithPooledTypeReturnsPooledTrue() { binding.setType(DATASOURCE_TYPE.POOLED.getType()); ResultModel result = command.listDataSources(); TabularResultModel section = result.getTableSection(ListDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)) .isEqualTo(Arrays.asList(DATA_SOURCE_NAME, "true", "false", "myURL")); }
@Test public void listDataSourcesReturnsInfoOnSingleDataSource() { ResultModel result = command.listDataSources(); TabularResultModel section = result.getTableSection(ListDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)) .isEqualTo(Arrays.asList(DATA_SOURCE_NAME, "true", "false", "myURL")); }
@Test public void listDataSourcesReturnsInfoOnMultipleDataSources() { JndiBinding binding2 = new JndiBindingsType.JndiBinding(); binding2.setJndiName("myDataSource2"); binding2.setType(DATASOURCE_TYPE.SIMPLE.getType()); binding2.setConnectionUrl("myURL2"); bindings.add(binding2); ResultModel result = command.listDataSources(); TabularResultModel section = result.getTableSection(ListDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)) .isEqualTo(Arrays.asList(DATA_SOURCE_NAME, "true", "false", "myURL")); assertThat(section.getValuesInRow(1)) .isEqualTo(Arrays.asList("myDataSource2", "false", "false", "myURL2")); }
@Test public void listDataSourcesWithNoRegionsUsingItReturnsResultWithInUseFalse() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(RegionMapping.class))); regionConfigs.add(regionConfig); ResultModel result = command.listDataSources(); TabularResultModel section = result.getTableSection(ListDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)) .isEqualTo(Arrays.asList(DATA_SOURCE_NAME, "true", "false", "myURL")); }
@Test public void listDataSourcesWithRegionsUsingItReturnsResultWithInUseTrue() { RegionMapping regionMapping; { RegionConfig regionConfig1 = mock(RegionConfig.class, "regionConfig1"); when(regionConfig1.getName()).thenReturn("regionName1"); regionMapping = mock(RegionMapping.class, "regionMapping1"); when(regionMapping.getDataSourceName()).thenReturn(DATA_SOURCE_NAME); 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(DATA_SOURCE_NAME); when(regionConfig3.getCustomRegionElements()) .thenReturn(Arrays.asList(regionMapping)); regionConfigs.add(regionConfig3); } ResultModel result = command.listDataSources(); TabularResultModel section = result.getTableSection(ListDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)) .isEqualTo(Arrays.asList(DATA_SOURCE_NAME, "true", "true", "myURL")); }