private boolean removeJdbcMappingFromRegion(RegionConfig regionConfig) { Iterator<CacheElement> iterator = regionConfig.getCustomRegionElements().iterator(); while (iterator.hasNext()) { CacheElement element = iterator.next(); if (element instanceof RegionMapping) { iterator.remove(); return true; } } return false; }
private boolean hasJdbcMappingThatUsesDataSource(RegionConfig regionConfig, String dataSourceName) { return regionConfig.getCustomRegionElements() .stream() .anyMatch(cacheElement -> isRegionMappingUsingDataSource(cacheElement, dataSourceName)); }
private boolean hasJdbcMappingThatUsesDataSource(RegionConfig regionConfig, String dataSourceName) { return regionConfig.getCustomRegionElements() .stream() .anyMatch(cacheElement -> isRegionMappingUsingDataSource(cacheElement, dataSourceName)); }
private void checkForExistingMapping(String regionName, RegionConfig regionConfig) throws PreconditionException { if (regionConfig.getCustomRegionElements().stream() .anyMatch(element -> element instanceof RegionMapping)) { throw new PreconditionException("A JDBC mapping for " + regionName + " already exists."); } }
@Test public void getRegionsThatUseDataSourceGivenRegionConfigWithNonRegionMappingElementReturnsEmptyList() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(CacheElement.class))); regionConfigs.add(regionConfig); List<String> result = command.getRegionsThatUseDataSource(cacheConfig, ""); assertThat(result).isEmpty(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithNonRegionMappingElementReturnsFalse() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(CacheElement.class))); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, ""); assertThat(result).isFalse(); }
@Test public void getRegionsThatUseDataSourceGivenRegionConfigWithRegionMappingForOtherDataSourceReturnsEmptyList() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(mock(RegionMapping.class))); regionConfigs.add(regionConfig); List<String> result = command.getRegionsThatUseDataSource(cacheConfig, "bogusDataSource"); assertThat(result).isEmpty(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithNoCustomRegionElementsReturnsFalse() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()).thenReturn(Collections.emptyList()); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, ""); assertThat(result).isFalse(); }
private void setupRegionConfigToUseDataSource(String dataSourceName) { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getName()).thenReturn("regionUsingDataSource"); List<RegionConfig> regionConfigList = new ArrayList<>(); regionConfigList.add(regionConfig); when(cacheConfig.getRegions()).thenReturn(regionConfigList); RegionMapping regionMapping = mock(RegionMapping.class); when(regionMapping.getDataSourceName()).thenReturn(dataSourceName); List<CacheElement> cacheElementList = new ArrayList<>(); cacheElementList.add(regionMapping); when(regionConfig.getCustomRegionElements()).thenReturn(cacheElementList); }
@Test public void getRegionsThatUseDataSourceGivenRegionConfigWithNoCustomRegionElementsReturnsEmptyList() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getCustomRegionElements()).thenReturn(Collections.emptyList()); regionConfigs.add(regionConfig); List<String> result = command.getRegionsThatUseDataSource(cacheConfig, ""); assertThat(result).isEmpty(); }
@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 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 getRegionsThatUseDataSourceGivenRegionConfigWithRegionMappingForDataSourceReturnsRegionName() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getName()).thenReturn("regionName"); RegionMapping regionMapping = mock(RegionMapping.class); when(regionMapping.getDataSourceName()).thenReturn("dataSourceName"); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(regionMapping)); regionConfigs.add(regionConfig); List<String> result = command.getRegionsThatUseDataSource(cacheConfig, "dataSourceName"); assertThat(result).isEqualTo(Collections.singletonList("regionName")); }
@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(); }
@Test public void isDataSourceUsedByRegionGivenRegionConfigWithRegionMappingForDataSourceReturnsTrue() { RegionConfig regionConfig = mock(RegionConfig.class); when(regionConfig.getName()).thenReturn("regionName"); RegionMapping regionMapping = mock(RegionMapping.class); when(regionMapping.getDataSourceName()).thenReturn("dataSourceName"); when(regionConfig.getCustomRegionElements()) .thenReturn(Collections.singletonList(regionMapping)); regionConfigs.add(regionConfig); boolean result = command.isDataSourceUsedByRegion(cacheConfig, "dataSourceName"); assertThat(result).isTrue(); }
@Test public void updateClusterConfigWithOneMatchingRegionCallsSetCacheLoader() { 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); verify(matchingRegionAttributes).setCacheLoader(any()); }
@Test public void unmarshallAnyElement() { String xml = "<region name=\"one\">\n" + " <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n" + " <custom:any xmlns:custom=\"http://geode.apache.org/schema/custom\" id=\"any\"/>" + " </region>"; RegionConfig config = service2.unMarshall(xml, RegionConfig.class); assertThat(config.getName()).isEqualTo("one"); assertThat(config.getCustomRegionElements()).hasSize(1); }
@Test public void updateClusterConfigWithSynchronousSetsTheCacheWriterOnTheMatchingRegion() { 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); createRegionMappingCommand.updateConfigForGroup(null, cacheConfig, arguments); verify(matchingRegionAttributes).setCacheWriter(any()); }
@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 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")); }