DescribeDataSourceCommand extends GfshCommand { @CliCommand(value = DESCRIBE_DATA_SOURCE, help = DESCRIBE_DATA_SOURCE__HELP) @CliMetaData @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.READ) public ResultModel describeDataSource(@CliOption(key = "name", mandatory = true, help = "Name of the data source to describe") String dataSourceName) { ResultModel resultModel = new ResultModel(); resultModel.setHeader(EXPERIMENTAL); TabularResultModel tabularData = resultModel.addTable(DATA_SOURCE_PROPERTIES_SECTION); InternalConfigurationPersistenceService ccService = getConfigurationPersistenceService(); if (ccService == null) { return ResultModel.createError("Cluster configuration service must be enabled."); } CacheConfig cacheConfig = ccService.getCacheConfig(null); if (cacheConfig == null) { return ResultModel.createError(String.format("Data source: %s not found", dataSourceName)); } List<JndiBindingsType.JndiBinding> jndiBindings = cacheConfig.getJndiBindings(); JndiBindingsType.JndiBinding binding = jndiBindings.stream() .filter(b -> b.getJndiName().equals(dataSourceName)).findFirst().orElse(null); if (binding == null) { return ResultModel.createError(String.format("Data source: %s not found", dataSourceName)); } boolean pooled; String type = binding.getType(); if (DATASOURCE_TYPE.SIMPLE.getType().equals(type)) { pooled = false; } else if (DATASOURCE_TYPE.POOLED.getType().equals(type)) { pooled = true; } else { return ResultModel.createError(String.format("Unknown data source type: %s", type)); } addTableRow(tabularData, CreateDataSourceCommand.NAME, binding.getJndiName()); addTableRow(tabularData, CreateDataSourceCommand.URL, binding.getConnectionUrl()); addTableRow(tabularData, CreateDataSourceCommand.USERNAME, binding.getUserName()); addTableRow(tabularData, CreateDataSourceCommand.POOLED, Boolean.toString(pooled)); if (pooled) { addTableRow(tabularData, CreateDataSourceCommand.POOLED_DATA_SOURCE_FACTORY_CLASS, binding.getConnPooledDatasourceClass()); for (JndiBindingsType.JndiBinding.ConfigProperty confProp : binding.getConfigProperties()) { addTableRow(tabularData, confProp.getName(), confProp.getValue()); } } InfoResultModel regionsUsingSection = resultModel.addInfo(REGIONS_USING_DATA_SOURCE_SECTION); List<String> regionsUsing = getRegionsThatUseDataSource(cacheConfig, dataSourceName); regionsUsingSection.setHeader("Regions Using Data Source:"); if (regionsUsing.isEmpty()) { regionsUsingSection.addLine("no regions are using " + dataSourceName); } else { regionsUsingSection.setContent(regionsUsing); } return resultModel; }  @CliCommand(value = DESCRIBE_DATA_SOURCE, help = DESCRIBE_DATA_SOURCE__HELP) @CliMetaData @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.READ) ResultModel describeDataSource(@CliOption(key = "name", mandatory = true,
      help = "Name of the data source to describe") String dataSourceName); @CliAvailabilityIndicator({DESCRIBE_DATA_SOURCE}) boolean commandAvailable();  }
@Test public void describeDataSourceWithNoClusterConfigurationServerFails() { doReturn(null).when(command).getConfigurationPersistenceService(); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); assertThat(result.getStatus()).isEqualTo(Status.ERROR); assertThat(result.toString()).contains("Cluster configuration service must be enabled."); }
@Test public void describeDataSourceWithNoClusterConfigFails() { doReturn(null).when(clusterConfigService).getCacheConfig(any()); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); assertThat(result.getStatus()).isEqualTo(Status.ERROR); assertThat(result.toString()).contains("Data source: " + DATA_SOURCE_NAME + " not found"); }
@Test public void describeDataSourceWithWrongNameFails() { ResultModel result = command.describeDataSource("bogusName"); assertThat(result.getStatus()).isEqualTo(Status.ERROR); assertThat(result.toString()).contains("Data source: bogusName not found"); }
@Test public void describeDataSourceWithUnsupportedTypeFails() { binding.setType(DATASOURCE_TYPE.MANAGED.getType()); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); assertThat(result.getStatus()).isEqualTo(Status.ERROR); assertThat(result.toString()).contains("Unknown data source type: ManagedDataSource"); }
@Test public void describeDataSourceWithSimpleTypeReturnsPooledFalse() { binding.setType(DATASOURCE_TYPE.SIMPLE.getType()); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(3)).isEqualTo(Arrays.asList("pooled", "false")); }
@Test public void describeDataSourceWithPooledTypeReturnsPooledTrue() { binding.setType(DATASOURCE_TYPE.POOLED.getType()); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(3)).isEqualTo(Arrays.asList("pooled", "true")); }
@Test public void describeDataSourceTypeReturnsName() { ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(0)).isEqualTo(Arrays.asList("name", DATA_SOURCE_NAME)); }
@Test public void describeDataSourceWithUrlReturnsUrl() { binding.setConnectionUrl("myUrl"); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(1)).isEqualTo(Arrays.asList("url", "myUrl")); }
@Test public void describeDataSourceWithUsernameReturnsUsername() { binding.setUserName("myUserName"); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(2)).isEqualTo(Arrays.asList("username", "myUserName")); }
@Test public void describeDataSourceWithPooledDataSourceFactoryClassShowsItInTheResult() { binding.setType(DATASOURCE_TYPE.POOLED.getType()); binding.setConnPooledDatasourceClass("myPooledDataSourceFactoryClass"); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(4)).isEqualTo( Arrays.asList("pooled-data-source-factory-class", "myPooledDataSourceFactoryClass")); }
@Test public void describeDataSourceWithPasswordDoesNotShowPasswordInResult() { binding.setType(DATASOURCE_TYPE.POOLED.getType()); binding.setPassword("myPassword"); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); assertThat(result.toString()).doesNotContain("myPassword"); }
@Test public void describeDataSourceWithPoolPropertiesDoesNotShowsItInTheResult() { binding.setType(DATASOURCE_TYPE.SIMPLE.getType()); List<ConfigProperty> configProperties = binding.getConfigProperties(); configProperties.add(new ConfigProperty("name1", "value1")); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); assertThat(result.toString()).doesNotContain("name1"); assertThat(result.toString()).doesNotContain("value1"); }
@Test public void describeDataSourceWithPoolPropertiesShowsItInTheResult() { binding.setType(DATASOURCE_TYPE.POOLED.getType()); List<ConfigProperty> configProperties = binding.getConfigProperties(); configProperties.add(new ConfigProperty("name1", "value1")); configProperties.add(new ConfigProperty("name2", "value2")); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); TabularResultModel section = result.getTableSection(DescribeDataSourceCommand.DATA_SOURCE_PROPERTIES_SECTION); assertThat(section.getValuesInRow(5)).isEqualTo(Arrays.asList("name1", "value1")); assertThat(section.getValuesInRow(6)).isEqualTo(Arrays.asList("name2", "value2")); }
@Test public void describeDataSourceWithNoRegionsUsingItReturnsResultWithNoRegionsUsingIt() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(RegionMapping.class))); regionConfigs.add(regionConfig); ResultModel result = command.describeDataSource(DATA_SOURCE_NAME); InfoResultModel regionsUsingSection = (InfoResultModel) result .getSection(DescribeDataSourceCommand.REGIONS_USING_DATA_SOURCE_SECTION); assertThat(regionsUsingSection.getContent()) .isEqualTo(Arrays.asList("no regions are using " + DATA_SOURCE_NAME)); }
@Test public void describeDataSourceWithRegionsUsingItReturnsResultWithRegionNames() { 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.describeDataSource(DATA_SOURCE_NAME); InfoResultModel regionsUsingSection = (InfoResultModel) result .getSection(DescribeDataSourceCommand.REGIONS_USING_DATA_SOURCE_SECTION); assertThat(regionsUsingSection.getContent()) .isEqualTo(Arrays.asList("regionName1", "regionName3")); }