DATAGEODE-323 - Add default 'resolve(:ClientCache)' method.

This commit is contained in:
John Blum
2020-04-01 12:38:35 -07:00
parent 75701480ea
commit 7d8b1c38f1
2 changed files with 48 additions and 2 deletions

View File

@@ -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}.
*

View File

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