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 5b8c4ffc..3eeb4677 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 @@ -21,7 +21,9 @@ import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; import org.apache.geode.cache.client.Pool; +import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -76,4 +78,23 @@ public interface PoolResolver { */ @Nullable Pool resolve(@Nullable String poolName); + /** + * Requires a {@link Pool} object with the given {@link String name} to exist. + * + * @param poolName {@link String name} of the required {@link Pool} to resolve. + * @return the required {@link Pool} with the given {@link String name} or throw an {@link IllegalStateException} + * if a {@link Pool} with {@link String name} does not exist! + * @throws IllegalStateException if a {@link Pool} with the given {@link String name} does not exist. + * @see org.apache.geode.cache.client.Pool + * @see #resolve(String) + */ + default @NonNull Pool require(@NonNull String poolName) { + + Pool pool = resolve(poolName); + + Assert.state(pool != null, + () -> String.format("Pool with name [%s] not found", poolName)); + + return pool; + } } 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 0b4e1db6..850d0050 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 @@ -17,6 +17,7 @@ package org.springframework.data.gemfire.client; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -48,6 +49,7 @@ import org.apache.geode.cache.client.Pool; * @since 2.3.0 */ @RunWith(MockitoJUnitRunner.class) +@SuppressWarnings("rawtypes") public class PoolResolverUnitTests { @Mock @@ -125,6 +127,39 @@ public class PoolResolverUnitTests { verify(mockRegion, times(1)).getAttributes(); } + @Test + public void requireExistingPoolReturnsPool() { + + Pool mockPool = mock(Pool.class); + + when(this.testPoolResolver.resolve(anyString())).thenReturn(mockPool); + when(this.testPoolResolver.require(anyString())).thenCallRealMethod(); + + assertThat(this.testPoolResolver.require("TestPool")).isEqualTo(mockPool); + + verify(this.testPoolResolver, times(1)).resolve(eq("TestPool")); + } + + @Test(expected = IllegalStateException.class) + public void requireNonExistingPoolThrowsIllegalStateException() { + + when(this.testPoolResolver.require(anyString())).thenCallRealMethod(); + + try { + this.testPoolResolver.require("MockPool"); + } + catch (IllegalStateException expected) { + + assertThat(expected).hasMessage("Pool with name [MockPool] not found"); + assertThat(expected).hasNoCause(); + + throw expected; + } + finally { + verify(this.testPoolResolver, times(1)).resolve(eq("MockPool")); + } + } + private static abstract class TestPoolResolver implements PoolResolver { } }