DATAGEODE-317 - Add require(..) method to PoolResolver.

This commit is contained in:
John Blum
2020-03-25 15:43:05 -07:00
parent 42ab3534e6
commit bf914c8a46
2 changed files with 56 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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 { }
}