DATAGEDOE-317 - Add from(:ClientCache) factory method to construct a SinglePoolPoolResolver from the DEFAULT Pool.

This commit is contained in:
John Blum
2020-03-25 16:27:02 -07:00
parent 9636b1572f
commit e867d155bc
2 changed files with 73 additions and 0 deletions

View File

@@ -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;
/**

View File

@@ -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() {