CreateMappingCommand extends SingleGfshCommand { @Override public boolean updateConfigForGroup(String group, CacheConfig cacheConfig, Object element) { if (element == null) { return false; } Object[] arguments = (Object[]) element; RegionMapping regionMapping = (RegionMapping) arguments[0]; boolean synchronous = (Boolean) arguments[1]; String regionName = regionMapping.getRegionName(); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); RegionConfig regionConfig = findRegionConfig(cacheConfig, regionName); if (regionConfig == null) { return false; } RegionAttributesType attributes = getRegionAttribute(regionConfig); if (MappingCommandUtils.isAccessor(attributes)) { alterProxyRegion(queueName, attributes, synchronous); } else { addMappingToRegion(regionMapping, regionConfig); if (!synchronous) { createAsyncQueue(cacheConfig, attributes, queueName); } alterNonProxyRegion(queueName, attributes, synchronous); } return true; }  @CliCommand(value = CREATE_MAPPING, help = CREATE_MAPPING__HELP) @CliMetaData( interceptor = "org.apache.geode.connectors.jdbc.internal.cli.CreateMappingCommand$Interceptor", relatedTopic = {CliStrings.DEFAULT_TOPIC_GEODE}) @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.MANAGE) ResultModel createMapping(
      @CliOption(key = CREATE_MAPPING__REGION_NAME, mandatory = true,
          help = CREATE_MAPPING__REGION_NAME__HELP) String regionName,
      @CliOption(key = CREATE_MAPPING__DATA_SOURCE_NAME, mandatory = true,
          help = CREATE_MAPPING__DATA_SOURCE_NAME__HELP) String dataSourceName,
      @CliOption(key = CREATE_MAPPING__TABLE_NAME,
          help = CREATE_MAPPING__TABLE_NAME__HELP) String table,
      @CliOption(key = CREATE_MAPPING__PDX_NAME, mandatory = true,
          help = CREATE_MAPPING__PDX_NAME__HELP) String pdxName,
      @CliOption(key = CREATE_MAPPING__PDX_CLASS_FILE,
          help = CREATE_MAPPING__PDX_CLASS_FILE__HELP) String pdxClassFile,
      @CliOption(key = CREATE_MAPPING__SYNCHRONOUS_NAME,
          help = CREATE_MAPPING__SYNCHRONOUS_NAME__HELP,
          specifiedDefaultValue = "true", unspecifiedDefaultValue = "false") boolean synchronous,
      @CliOption(key = CREATE_MAPPING__ID_NAME, help = CREATE_MAPPING__ID_NAME__HELP) String id,
      @CliOption(key = CREATE_MAPPING__CATALOG_NAME,
          help = CREATE_MAPPING__CATALOG_NAME__HELP) String catalog,
      @CliOption(key = CREATE_MAPPING__SCHEMA_NAME,
          help = CREATE_MAPPING__SCHEMA_NAME__HELP) String schema,
      @CliOption(key = CliStrings.IFNOTEXISTS,
          specifiedDefaultValue = "true", unspecifiedDefaultValue = "false",
          help = CREATE_MAPPING__IFNOTEXISTS__HELP) boolean ifNotExists,
      @CliOption(key = {CliStrings.GROUP, CliStrings.GROUPS},
          optionContext = ConverterHint.MEMBERGROUP,
          help = CREATE_MAPPING__GROUPS_NAME__HELP) String[] groups); @Override boolean updateConfigForGroup(String group, CacheConfig cacheConfig, Object element); @CliAvailabilityIndicator({CREATE_MAPPING}) @SuppressWarnings("unused") boolean commandAvailable(); static final String CREATE_MAPPING__IFNOTEXISTS__HELP; }
@Test public void updateClusterConfigWithNoRegionsDoesNotThrowException() { when(cacheConfig.getRegions()).thenReturn(Collections.emptyList()); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); }
@Test public void updateClusterConfigWithOneMatchingRegionAddsMappingToRegion() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); assertThat(listCacheElements.size()).isEqualTo(1); assertThat(listCacheElements).contains(mapping); }
@Test public void updateClusterConfigWithOneNonMatchingRegionDoesNotAddMapping() { List<RegionConfig> list = new ArrayList<>(); RegionConfig nonMatchingRegion = mock(RegionConfig.class); when(nonMatchingRegion.getName()).thenReturn("nonMatchingRegion"); List<CacheElement> listCacheElements = new ArrayList<>(); when(nonMatchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(nonMatchingRegion); when(cacheConfig.getRegions()).thenReturn(list); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); assertThat(listCacheElements).isEmpty(); }
@Test public void updateClusterConfigWithOneMatchingRegionCreatesAsyncEventQueue() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); assertThat(queueList.size()).isEqualTo(1); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); AsyncEventQueue createdQueue = queueList.get(0); assertThat(createdQueue.getId()).isEqualTo(queueName); assertThat(createdQueue.isParallel()).isFalse(); assertThat(createdQueue.getAsyncEventListener().getClassName()) .isEqualTo(JdbcAsyncWriter.class.getName()); }
@Test public void updateClusterConfigWithOneMatchingPartitionedRegionCreatesParallelAsyncEventQueue() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); when(matchingRegionAttributes.getDataPolicy()) .thenReturn(RegionAttributesDataPolicy.PARTITION); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); assertThat(queueList.get(0).isParallel()).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingPartitionedRegionRefidCreatesParallelAsyncEventQueue() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); when(matchingRegionAttributes.getDataPolicy()).thenReturn(null); when(matchingRegionAttributes.getRefid()).thenReturn(RegionShortcut.PARTITION.name()); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); assertThat(queueList.get(0).isParallel()).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingRegionAndNullQueuesAddsAsyncEventQueueIdToRegion() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn(null); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); verify(matchingRegionAttributes).setAsyncEventQueueIds(argument.capture()); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); assertThat(argument.getValue()).isEqualTo(queueName); }
@Test public void updateClusterConfigWithOneMatchingRegionAndEmptyQueuesAddsAsyncEventQueueIdToRegion() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn(""); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); verify(matchingRegionAttributes).setAsyncEventQueueIds(argument.capture()); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); assertThat(argument.getValue()).isEqualTo(queueName); }
@Test public void updateClusterConfigWithOneMatchingRegionAndExistingQueuesAddsAsyncEventQueueIdToRegion() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn("q1,q2"); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); verify(matchingRegionAttributes).setAsyncEventQueueIds(argument.capture()); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); assertThat(argument.getValue()).isEqualTo("q1,q2," + queueName); }
@Test public void updateClusterConfigWithOneMatchingRegionAndQueuesContainingDuplicateDoesNotModifyAsyncEventQueueIdOnRegion() { List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); String queueName = MappingCommandUtils.createAsyncEventQueueName(regionName); String existingQueues = "q1," + queueName + ",q2"; when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn(existingQueues); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); verify(matchingRegionAttributes, never()).setAsyncEventQueueIds(any()); }
@Test public void updateClusterConfigWithSynchronousAndOneMatchingRegionAndExistingQueuesDoesNotAddsAsyncEventQueueIdToRegion() { arguments[1] = true; List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); when(matchingRegionAttributes.getAsyncEventQueueIds()).thenReturn("q1,q2"); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); verify(matchingRegionAttributes, never()).setAsyncEventQueueIds(any()); }
@Test public void updateClusterConfigWithSynchronousAndOneMatchingRegionDoesNotCreateAsyncEventQueue() { arguments[1] = true; List<RegionConfig> list = new ArrayList<>(); List<CacheElement> listCacheElements = new ArrayList<>(); when(matchingRegion.getCustomRegionElements()).thenReturn(listCacheElements); list.add(matchingRegion); when(cacheConfig.getRegions()).thenReturn(list); List<CacheConfig.AsyncEventQueue> queueList = new ArrayList<>(); when(cacheConfig.getAsyncEventQueues()).thenReturn(queueList); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); assertThat(queueList).isEmpty(); }