diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolResolver.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolResolver.java index 3eeb4677..f25dfc85 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolResolver.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/PoolResolver.java @@ -19,8 +19,10 @@ import java.util.Optional; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; +import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; +import org.springframework.data.gemfire.util.CacheUtils; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -42,6 +44,21 @@ import org.springframework.util.StringUtils; @FunctionalInterface public interface PoolResolver { + String DEFAULT_POOL_NAME = CacheUtils.DEFAULT_POOL_NAME; + + /** + * Resolves the {@literal DEFAULT} {@link Pool} from the given {@link ClientCache} instance. + * + * @param clientCache {@link ClientCache} instance from which to resolve the {@literal DEFAULT} {@link Pool}. + * @return the configured {@literal DEFAULT} {@link Pool} from the given {@link ClientCache} instance. + * @see org.apache.geode.cache.client.ClientCache#getDefaultPool() + * @see org.apache.geode.cache.client.ClientCache + * @see org.apache.geode.cache.client.Pool + */ + default @Nullable Pool resolve(@Nullable ClientCache clientCache) { + return clientCache != null ? clientCache.getDefaultPool() : null; + } + /** * Resolves the {@link Pool} instance used by the given {@link Region}. * diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/PoolResolverUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/PoolResolverUnitTests.java index 850d0050..a9df0d00 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/PoolResolverUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/PoolResolverUnitTests.java @@ -34,6 +34,7 @@ import org.mockito.junit.MockitoJUnitRunner; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; +import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; /** @@ -58,10 +59,30 @@ public class PoolResolverUnitTests { @Before public void setup() { when(this.testPoolResolver.resolve(any(Region.class))).thenCallRealMethod(); + when(this.testPoolResolver.resolve(any(ClientCache.class))).thenCallRealMethod(); } @Test - public void resolvePoolFromRegionWithPoolReturnsPool() { + public void resolvePoolFromClientCacheHavingDefaultPoolReturnsDefaultPool() { + + ClientCache mockClientCache = mock(ClientCache.class); + + Pool mockDefaultPool = mock(Pool.class, "DEFAULT"); + + when(mockClientCache.getDefaultPool()).thenReturn(mockDefaultPool); + + assertThat(this.testPoolResolver.resolve(mockClientCache)).isEqualTo(mockDefaultPool); + + verify(mockClientCache, times(1)).getDefaultPool(); + } + + @Test + public void resolvePoolFromNullClientCacheIsNullSafe() { + assertThat(this.testPoolResolver.resolve((ClientCache) null)).isNull(); + } + + @Test + public void resolvePoolFromRegionWithConfiguredPoolNameReturnsPool() { Pool mockPool = mock(Pool.class); @@ -94,22 +115,30 @@ public class PoolResolverUnitTests { verify(mockRegion, times(1)).getAttributes(); verify(mockRegionAttributes, times(1)).getPoolName(); - verify(this.testPoolResolver, never()).resolve(eq(poolName)); } @Test public void resolvePoolWithRegionWithBlankPoolNameReturnsNull() { testResolvePoolFromRegionWithNoPoolReturnsNull(" "); + verify(this.testPoolResolver, never()).resolve(anyString()); } @Test public void resolvePoolWithRegionWithEmptyPoolNameReturnsNull() { testResolvePoolFromRegionWithNoPoolReturnsNull(""); + verify(this.testPoolResolver, never()).resolve(anyString()); } @Test public void resolvePoolWithRegionWithNullPoolNameReturnsNull() { testResolvePoolFromRegionWithNoPoolReturnsNull(null); + verify(this.testPoolResolver, never()).resolve(any(String.class)); + } + + @Test + public void resolvePoolWithRegionWithNonExistingPoolForNameReturnsNull() { + testResolvePoolFromRegionWithNoPoolReturnsNull("NonExistingPool"); + verify(this.testPoolResolver, times(1)).resolve(eq("NonExistingPool")); } @Test