From ff03c242abce667114b2deec12a553e30c7c3b57 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 1 Apr 2020 16:23:17 -0700 Subject: [PATCH] DATAGEODE-244 - Add RegionResolver implementation resolving a single, configured Region object. --- .../support/SingleRegionRegionResolver.java | 75 ++++++++++++++ .../SingleRegionRegionResolverUnitTests.java | 97 +++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 spring-data-geode/src/main/java/org/springframework/data/gemfire/support/SingleRegionRegionResolver.java create mode 100644 spring-data-geode/src/test/java/org/springframework/data/gemfire/support/SingleRegionRegionResolverUnitTests.java diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/support/SingleRegionRegionResolver.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/support/SingleRegionRegionResolver.java new file mode 100644 index 00000000..464c9dcc --- /dev/null +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/support/SingleRegionRegionResolver.java @@ -0,0 +1,75 @@ +/* + * 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.support; + +import org.apache.geode.cache.Region; + +import org.springframework.data.gemfire.RegionResolver; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +/** + * {@link RegionResolver} implementation resolving a single, configured {@link Region} object. + * + * @author John Blum + * @see org.apache.geode.cache.Region + * @see org.springframework.data.gemfire.RegionResolver + * @since 2.3.0 + */ +@SuppressWarnings("rawtypes") +public class SingleRegionRegionResolver implements RegionResolver { + + private final Region region; + + /** + * Constructs a new instance of {@link SingleRegionRegionResolver} with the given {@link Region}. + * + * @param region {@link Region} returned in the resolution process; must not be {@literal null}. + * @throws IllegalArgumentException if {@link Region} is {@literal null}. + * @see org.apache.geode.cache.Region + */ + public SingleRegionRegionResolver(@NonNull Region region) { + + Assert.notNull(region, "Region must not be null"); + + this.region = region; + } + + /** + * Returns a reference to the configured {@link Region}. + * + * @param {@link Class type} of the {@link Region} key. + * @param {@link Class type} of the {@link Region} value. + * @return a reference to the configured {@link Region}. + * @see org.apache.geode.cache.Region + */ + @SuppressWarnings("unchecked") + protected @NonNull Region getRegion() { + return this.region; + } + + /** + * @inheritDoc + */ + @Override + public @Nullable Region resolve(@Nullable String regionName) { + + Region region = getRegion(); + + return region.getName().equals(regionName) ? region : null; + } +} diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/support/SingleRegionRegionResolverUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/support/SingleRegionRegionResolverUnitTests.java new file mode 100644 index 00000000..45ed15d3 --- /dev/null +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/support/SingleRegionRegionResolverUnitTests.java @@ -0,0 +1,97 @@ +/* + * 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.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.Region; + +/** + * Unit Tests for {@link SingleRegionRegionResolver}. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.mockito.junit.MockitoJUnitRunner + * @see org.apache.geode.cache.Region + * @see org.springframework.data.gemfire.support.SingleRegionRegionResolver + * @since 2.3.0 + */ +@RunWith(MockitoJUnitRunner.class) +@SuppressWarnings("rawtypes") +public class SingleRegionRegionResolverUnitTests { + + @Mock + private Region mockRegion; + + @Test + public void constructSingleRegionRegionResolverWithRegion() { + + SingleRegionRegionResolver regionResolver = new SingleRegionRegionResolver(this.mockRegion); + + assertThat(regionResolver).isNotNull(); + assertThat(regionResolver.getRegion()).isSameAs(this.mockRegion); + } + + @Test(expected = IllegalArgumentException.class) + public void constructSingleRegionRegionResolverWithNullThrowsIllegalArgumentException() { + + try { + new SingleRegionRegionResolver(null); + } + catch (IllegalArgumentException expected) { + + assertThat(expected).hasMessage("Region must not be null"); + assertThat(expected).hasNoCause(); + + throw expected; + } + } + + @Test + public void resolveReturnsConfiguredRegionMatchingName() { + + when(this.mockRegion.getName()).thenReturn("MockRegion"); + + assertThat(new SingleRegionRegionResolver(this.mockRegion).resolve("MockRegion")) + .isEqualTo(this.mockRegion); + + verify(this.mockRegion, times(1)).getName(); + } + + @Test + public void resolveReturnsNullWhenRegionNameDoesNotMatch() { + + when(this.mockRegion.getName()).thenReturn("MockRegion"); + + SingleRegionRegionResolver regionResolver = new SingleRegionRegionResolver(this.mockRegion); + + assertThat(regionResolver.resolve("TestRegion")).isNull(); + assertThat(regionResolver.resolve(" ")).isNull(); + assertThat(regionResolver.resolve("")).isNull(); + assertThat(regionResolver.resolve(null)).isNull(); + + verify(this.mockRegion, times(4)).getName(); + } +}