CreateIndexCommand extends GfshCommand { Set<String> getGroupsContainingRegion(ConfigurationPersistenceService cps, String regionName) { Set<String> foundGroups = new HashSet<>(); for (String group : cps.getGroups()) { CacheConfig cacheConfig = cps.getCacheConfig(group, true); if (cacheConfig.findRegionConfiguration(regionName) != null) { foundGroups.add(group); } } return foundGroups; }  @CliCommand(value = CliStrings.CREATE_INDEX, help = CliStrings.CREATE_INDEX__HELP) @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA}) @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.MANAGE, target = ResourcePermission.Target.QUERY) @SuppressWarnings("deprecation") ResultModel createIndex(@CliOption(key = CliStrings.CREATE_INDEX__NAME, mandatory = true,
      help = CliStrings.CREATE_INDEX__NAME__HELP) final String indexName,

      @CliOption(key = CliStrings.CREATE_INDEX__EXPRESSION, mandatory = true,
          help = CliStrings.CREATE_INDEX__EXPRESSION__HELP) final String indexedExpression,

      @CliOption(key = CliStrings.CREATE_INDEX__REGION, mandatory = true,
          optionContext = ConverterHint.REGION_PATH,
          help = CliStrings.CREATE_INDEX__REGION__HELP) String regionPath,

      @CliOption(key = {CliStrings.MEMBER, CliStrings.MEMBERS},
          optionContext = ConverterHint.MEMBERIDNAME,
          help = CliStrings.CREATE_INDEX__MEMBER__HELP) final String[] memberNameOrID,

      @CliOption(key = CliStrings.CREATE_INDEX__TYPE, unspecifiedDefaultValue = "range",
          optionContext = ConverterHint.INDEX_TYPE,
          help = CliStrings.CREATE_INDEX__TYPE__HELP) final org.apache.geode.cache.query.IndexType indexType,

      @CliOption(key = {CliStrings.GROUP, CliStrings.GROUPS},
          optionContext = ConverterHint.MEMBERGROUP,
          help = CliStrings.CREATE_INDEX__GROUP__HELP) String[] groups);  }
@Test public void commandWithGroup() { doReturn(ccService).when(command).getConfigurationPersistenceService(); doReturn(Sets.newHashSet("group1", "group2")).when(command).getGroupsContainingRegion(any(), any()); doReturn(Collections.singleton(mock(DistributedMember.class))).when(command).findMembers(any(), any()); CliFunctionResult result = new CliFunctionResult("member", true, "reason"); doReturn(Collections.singletonList(result)).when(command).executeAndGetFunctionResult(any(), any(), any()); gfshParser.executeAndAssertThat(command, "create index --name=index --expression=abc --region=" + SEPARATOR + "regionA --groups=group1,group2") .statusIsSuccess(); verify(ccService).updateCacheConfig(eq("group1"), any()); verify(ccService).updateCacheConfig(eq("group2"), any()); }
@Test public void commandWithWrongGroup() { doReturn(ccService).when(command).getConfigurationPersistenceService(); doReturn(Sets.newHashSet("group1", "group2")).when(command).getGroupsContainingRegion(any(), any()); doReturn(Collections.singleton(mock(DistributedMember.class))).when(command).findMembers(any(), any()); gfshParser.executeAndAssertThat(command, "create index --name=index --expression=abc --region=" + SEPARATOR + "regionA --groups=group1,group3") .statusIsError() .containsOutput("Region regionA does not exist in some of the groups"); verify(ccService, never()).updateCacheConfig(any(), any()); }
@Test public void regionBelongsToCluster() { doReturn(ccService).when(command).getConfigurationPersistenceService(); Region region = mock(Region.class); @SuppressWarnings("unchecked") ClusterManagementListResult<Region, RuntimeRegionInfo> listResult = mock(ClusterManagementListResult.class); when(cms.list(any(Region.class))).thenReturn(listResult); when(listResult.getConfigResult()).thenReturn(Collections.singletonList(region)); doReturn(Sets.newHashSet("cluster")).when(command).getGroupsContainingRegion(any(), any()); doReturn(Collections.emptySet()).when(command).findMembers(any(), any()); gfshParser.executeAndAssertThat(command, "create index --name=index --expression=abc --region=" + SEPARATOR + "regionA") .containsOutput("No Members Found"); verify(command).findMembers(new String[] {}, null); }
@Test public void getGroupsContainingRegion() throws Exception { when(ccService.getGroups()).thenReturn(new HashSet<>(Arrays.asList("group1", "group2"))); CacheConfig cacheConfig1 = new CacheConfig(); RegionConfig regionA = new RegionConfig("regionA", "REPLICATE"); RegionConfig regionB = new RegionConfig("regionB", "REPLICATE"); RegionConfig child = new RegionConfig("child", "REPLICATE"); regionA.getRegions().add(child); cacheConfig1.getRegions().add(regionA); cacheConfig1.getRegions().add(regionB); CacheConfig cacheConfig2 = new CacheConfig(); cacheConfig2.getRegions().add(regionB); when(ccService.getCacheConfig("group1", true)).thenReturn(cacheConfig1); when(ccService.getCacheConfig("group2", true)).thenReturn(cacheConfig2); assertThat(command.getGroupsContainingRegion(ccService, "regionA")) .containsExactly("group1"); assertThat(command.getGroupsContainingRegion(ccService, "regionB")) .containsExactlyInAnyOrder("group1", "group2"); assertThat(command.getGroupsContainingRegion(ccService, "regionA" + SEPARATOR + "child")) .containsExactly("group1"); assertThat(command.getGroupsContainingRegion(ccService, "notExist")) .isEmpty(); }