diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolver.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolver.java index eda5a0ce..ba1d81de 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolver.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolver.java @@ -15,6 +15,7 @@ */ package org.springframework.data.gemfire.client.support; +import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; import org.springframework.data.gemfire.client.PoolResolver; @@ -32,6 +33,25 @@ import org.springframework.util.Assert; */ public class SinglePoolPoolResolver implements PoolResolver { + /** + * Factory method used to construct a new instance of {@link SinglePoolPoolResolver} from an instance of + * {@link ClientCache} using the {@link ClientCache#getDefaultPool()} DEFAULT} {@link Pool}. + * + * @param clientCache {@link ClientCache} instance used to resolve the {@link ClientCache#getDefaultPool() DEFAULT} + * {@link Pool}. + * @return a new {@link SinglePoolPoolResolver} initialized with the {@literal DEFAULT} {@link Pool}. + * @throws IllegalArgumentException if the {@link ClientCache} or the {@link ClientCache#getDefaultPool()} DEFAULT} + * {@link Pool} is {@literal null}. + * @see org.apache.geode.cache.client.ClientCache + * @see org.apache.geode.cache.client.ClientCache#getDefaultPool() + */ + public static SinglePoolPoolResolver from(@NonNull ClientCache clientCache) { + + Assert.notNull(clientCache, "ClientCache must not be null"); + + return new SinglePoolPoolResolver(clientCache.getDefaultPool()); + } + private final Pool pool; /** diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolverUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolverUnitTests.java index 9fb4273f..67b7ac4b 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolverUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolverUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.data.gemfire.client.support; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -25,6 +26,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; import org.springframework.data.gemfire.client.PoolResolver; @@ -72,6 +74,57 @@ public class SinglePoolPoolResolverUnitTests { } } + @Test + public void fromClientCacheWithDefaultPool() { + + ClientCache mockClientCache = mock(ClientCache.class); + + when(mockClientCache.getDefaultPool()).thenReturn(this.mockPool); + + SinglePoolPoolResolver poolResolver = SinglePoolPoolResolver.from(mockClientCache); + + assertThat(poolResolver).isNotNull(); + assertThat(poolResolver.getPool()).isEqualTo(this.mockPool); + + verify(mockClientCache, times(1)).getDefaultPool(); + } + + @Test(expected = IllegalArgumentException.class) + public void fromClientCacheWithNoDefaultPoolThrowsIllegalArgumentException() { + + ClientCache mockClientCache = mock(ClientCache.class); + + try { + SinglePoolPoolResolver.from(mockClientCache); + } + catch (IllegalArgumentException expected) { + + assertThat(expected).hasMessage("Pool must not be null"); + assertThat(expected).hasNoCause(); + + throw expected; + } + finally { + verify(mockClientCache, times(1)).getDefaultPool(); + } + } + + @SuppressWarnings("all") + @Test(expected = IllegalArgumentException.class) + public void fromNullClientCacheThrowsIllegalArgumentException() { + + try { + SinglePoolPoolResolver.from(null); + } + catch (IllegalArgumentException expected) { + + assertThat(expected).hasMessage("ClientCache must not be null"); + assertThat(expected).hasNoCause(); + + throw expected; + } + } + @Test public void resolvesReturnsPoolWhenNamesMatch() {