DATAGEODE-317 - Add PoolResolver implementation that resolves a single, configured Pool.

This commit is contained in:
John Blum
2020-03-25 15:44:11 -07:00
parent bf914c8a46
commit 9636b1572f
2 changed files with 190 additions and 0 deletions

View File

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

View File

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