DestroyMappingCommand extends SingleGfshCommand { @Override public boolean updateConfigForGroup(String group, CacheConfig cacheConfig, Object configObject) { String regionName = (String) configObject; RegionConfig regionConfig = findRegionConfig(cacheConfig, regionName); if (regionConfig == null) { return false; } boolean modified = false; modified |= removeJdbcMappingFromRegion(regionConfig); modified |= removeJdbcQueueFromCache(cacheConfig, regionName); RegionAttributesType attributes = getRegionAttribute(regionConfig); modified |= removeJdbcLoader(attributes); modified |= removeJdbcWriter(attributes); modified |= removeJdbcAsyncEventQueueId(attributes, regionName); return modified; }  @CliCommand(value = DESTROY_MAPPING, help = DESTROY_MAPPING__HELP) @CliMetaData(relatedTopic = CliStrings.DEFAULT_TOPIC_GEODE) @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.MANAGE) ResultModel destroyMapping(@CliOption(key = DESTROY_MAPPING__REGION_NAME, mandatory = true,
      help = DESTROY_MAPPING__REGION_NAME__HELP) String regionName,
      @CliOption(key = {CliStrings.GROUP, CliStrings.GROUPS},
          optionContext = ConverterHint.MEMBERGROUP,
          help = DESTROY_MAPPING__GROUPS_NAME__HELP) String[] groups); @Override boolean updateConfigForGroup(String group, CacheConfig cacheConfig, Object configObject); @CliAvailabilityIndicator({DESTROY_MAPPING}) boolean commandAvailable();  }
@Test public void updateClusterConfigWithNoRegionsDoesNotThrowException() { when(cacheConfig.getRegions()).thenReturn(Collections.emptyList()); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); assertThat(modified).isFalse(); }
@Test public void updateClusterConfigWithOneNonMatchingRegionDoesNotRemoveMapping() { List<RegionConfig> list = new ArrayList<>(); RegionConfig nonMatchingRegion = mock(RegionConfig.class); when(nonMatchingRegion.getName()).thenReturn("nonMatchingRegion"); List<CacheElement> listCacheElements = new ArrayList<>(); RegionMapping nonMatchingMapping = mock(RegionMapping.class); listCacheElements.add(nonMatchingMapping); when(nonMatchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(nonMatchingRegion); when(cacheConfig.getRegions()).thenReturn(list); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); assertThat(listCacheElements).isEqualTo(Arrays.asList(nonMatchingMapping)); assertThat(modified).isFalse(); }
@Test public void updateClusterConfigWithOneMatchingRegionDoesRemoveMapping() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); RegionMapping matchingMapping = mock(RegionMapping.class); listCacheElements.add(matchingMapping); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); assertThat(listCacheElements).isEmpty(); assertThat(modified).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingRegionAndJdbcAsyncQueueRemovesTheQueue() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); RegionMapping matchingMapping = mock(RegionMapping.class); listCacheElements.add(matchingMapping); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); AsyncEventQueue queue = mock(AsyncEventQueue.class); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); when(queue.getId()).thenReturn(queueName); List<AsyncEventQueue> queueList = new ArrayList<>(); queueList.add(queue); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); assertThat(queueList).isEmpty(); assertThat(modified).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingRegionAndJdbcWriterRemovesTheWriter() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); RegionMapping matchingMapping = mock(RegionMapping.class); listCacheElements.add(matchingMapping); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); DeclarableType cacheWriter = mock(DeclarableType.class); when(cacheWriter.getClassName()).thenReturn(JdbcWriter.class.getName()); when(matchingRegionAttributes.getCacheWriter()).thenReturn(cacheWriter); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); verify(matchingRegionAttributes, times(1)).setCacheWriter(null); assertThat(modified).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingRegionAndJdbcAsyncQueueIdRemovesTheId() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); RegionMapping matchingMapping = mock(RegionMapping.class); listCacheElements.add(matchingMapping); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn(queueName); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); verify(matchingRegionAttributes, times(1)).setAsyncEventQueueIds(""); assertThat(modified).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingRegionAndJdbcAsyncQueueIdsRemovesTheId() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); RegionMapping matchingMapping = mock(RegionMapping.class); listCacheElements.add(matchingMapping); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); when(matchingRegionAttributes.getAsyncEventQueueIds()) .thenReturn(queueName + "1," + queueName + "," + queueName + "2"); boolean modified = destroyRegionMappingCommand.updateConfigForGroup(null, cacheConfig, regionName); verify(matchingRegionAttributes, times(1)) .setAsyncEventQueueIds(queueName + "1," + queueName + "2"); assertThat(modified).isTrue(); }