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 new file mode 100644 index 00000000..eda5a0ce --- /dev/null +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolver.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.gemfire.client.support; + +import org.apache.geode.cache.client.Pool; + +import org.springframework.data.gemfire.client.PoolResolver; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +/** + * {@link PoolResolver} implementation that resolves a single, configured {@link Pool}. + * + * @author John Blum + * @see org.apache.geode.cache.client.Pool + * @see org.springframework.data.gemfire.client.PoolResolver + * @since 2.3.0 + */ +public class SinglePoolPoolResolver implements PoolResolver { + + private final Pool pool; + + /** + * Constructs an instance of {@link SinglePoolPoolResolver} initialized with the given {@link Pool} + * returned during resolution. + * + * @param pool {@link Pool} object resolved by this {@link PoolResolver}. + * @throws IllegalArgumentException if {@link Pool} is {@literal null}. + * @see org.apache.geode.cache.client.Pool + */ + public SinglePoolPoolResolver(@NonNull Pool pool) { + + Assert.notNull(pool, "Pool must not be null"); + + this.pool = pool; + } + + /** + * Returns a reference to the configured, "resolvable" {@link Pool}. + * + * @return a reference to the configured, "resolvable" {@link Pool}. + * @see org.apache.geode.cache.client.Pool + */ + protected @NonNull Pool getPool() { + return this.pool; + } + + /** + * Returns the configured {@link Pool} iff the given {@link String poolName} matches + * the configured {@link Pool} {@link Pool#getName() name}. + * + * @param poolName {@link String name} of the {@link Pool} to resolve. + * @return the configured {@link Pool} if the configured {@link Pool} {@link Pool#getName() name} + * and the given {@link String poolName} match. + * @see org.apache.geode.cache.client.Pool#getName() + */ + @Nullable @Override + public Pool resolve(@Nullable String poolName) { + + Pool pool = getPool(); + + return pool.getName().equals(poolName) ? pool : null; + } +} 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 new file mode 100644 index 00000000..9fb4273f --- /dev/null +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/client/support/SinglePoolPoolResolverUnitTests.java @@ -0,0 +1,112 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.gemfire.client.support; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import org.apache.geode.cache.client.Pool; + +import org.springframework.data.gemfire.client.PoolResolver; + +/** + * Unit Tests for {@link SinglePoolPoolResolver}. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mock + * @see org.mockito.Mockito + * @see org.mockito.junit.MockitoJUnitRunner + * @see org.apache.geode.cache.client.Pool + * @see org.springframework.data.gemfire.client.support.SinglePoolPoolResolver + * @since 2.3.0 + */ +@RunWith(MockitoJUnitRunner.class) +public class SinglePoolPoolResolverUnitTests { + + @Mock + private Pool mockPool; + + @Test + public void constructSinglePoolPoolResolver() { + + SinglePoolPoolResolver poolResolver = new SinglePoolPoolResolver(this.mockPool); + + assertThat(poolResolver).isNotNull(); + assertThat(poolResolver.getPool()).isEqualTo(this.mockPool); + } + + @SuppressWarnings("all") + @Test(expected = IllegalArgumentException.class) + public void constructSinglePoolPoolResolverWithNullPoolThrowsIllegalArgumentException() { + + try { + new SinglePoolPoolResolver(null); + } + catch (IllegalArgumentException expected) { + + assertThat(expected).hasMessage("Pool must not be null"); + assertThat(expected).hasNoCause(); + + throw expected; + } + } + + @Test + public void resolvesReturnsPoolWhenNamesMatch() { + + PoolResolver poolResolver = new SinglePoolPoolResolver(this.mockPool); + + when(this.mockPool.getName()).thenReturn("TestPool"); + + assertThat(poolResolver.resolve("TestPool")).isEqualTo(this.mockPool); + + verify(this.mockPool, times(1)).getName(); + } + + @Test + public void resolveReturnsNullWhenNamesDoNotMatch() { + + PoolResolver poolResolver = new SinglePoolPoolResolver(this.mockPool); + + when(this.mockPool.getName()).thenReturn("TestPool"); + + assertThat(poolResolver.resolve("MockPool")).isNull(); + assertThat(poolResolver.resolve(" ")).isNull(); + assertThat(poolResolver.resolve("")).isNull(); + + verify(this.mockPool, times(3)).getName(); + } + + @Test + public void resolveIsNullSafe() { + + PoolResolver poolResolver = new SinglePoolPoolResolver(this.mockPool); + + when(this.mockPool.getName()).thenReturn("TestPool"); + + assertThat(poolResolver.resolve((String) null)).isNull(); + + verify(this.mockPool, times(1)).getName(); + } +}