From 54fe8a3c7533ba89007b7f307f4e23ed776f702b Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 14 Jun 2019 20:52:44 -0700 Subject: [PATCH] Add additional Unit Tests. Resolves gh-38. --- ...actGemFireOperationsSessionRepository.java | 49 ++++++-- ...mFireOperationsSessionRepositoryTests.java | 109 +++++++++++++++++- 2 files changed, 150 insertions(+), 8 deletions(-) diff --git a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepository.java b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepository.java index 093a08f..0aa3d8a 100644 --- a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepository.java +++ b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepository.java @@ -207,8 +207,10 @@ public abstract class AbstractGemFireOperationsSessionRepository * @see org.apache.geode.cache.Region * @see #newSessionEventHandler() * @see #newSessionIdInterestRegistrar() + * @see #isRegionRegisterInterestAllowed(Region) */ - private Region initializeSessionsRegion(@Nullable Region sessionsRegion) { + private @Nullable Region initializeSessionsRegion( + @Nullable Region sessionsRegion) { Optional.ofNullable(sessionsRegion) .map(Region::getAttributesMutator) @@ -227,11 +229,27 @@ public abstract class AbstractGemFireOperationsSessionRepository return sessionsRegion; } - boolean isNonLocalClientRegion(Region region) { + /** + * Determines whether the given {@link Region} is a client, non-local {@link Region}. + * + * @param region {@link Region} to evaluate. + * @return a boolean indicating whether the given {@link Region} is a client, non-local {@link Region}. + * @see org.apache.geode.cache.Region + */ + boolean isNonLocalClientRegion(@Nullable Region region) { return GemFireUtils.isNonLocalClientRegion(region); } - boolean isRegionPoolSubscriptionsEnabled(Region region) { + /** + * Determines whether the given client {@link Region Region's} configured {@link Pool} has subscription enabled. + * + * @param region {@link Region} to evaluate. + * @return a boolean value indicating whether the client {@link Region Region's} configured {@link Pool} + * has subscription enabled. + * @see org.apache.geode.cache.client.Pool#getSubscriptionEnabled() + * @see org.apache.geode.cache.Region + */ + boolean isRegionPoolSubscriptionEnabled(@Nullable Region region) { return Boolean.TRUE.equals(Optional.ofNullable(region) .map(Region::getAttributes) @@ -241,11 +259,29 @@ public abstract class AbstractGemFireOperationsSessionRepository .orElse(DEFAULT_CLIENT_SUBSCRIPTIONS_ENABLED)); } - boolean isRegionRegisterInterestAllowed(Region region) { - return isNonLocalClientRegion(region) && isRegionPoolSubscriptionsEnabled(region); + /** + * Determines whether the interest registration for the given {@link Region} is allowed. + * + * @param region {@link Region} to evaluate. + * @return a boolean value indicating whether interest registration for the given {@link Region} is allowed. + * @see org.apache.geode.cache.Region#registerInterest(Object) + * @see org.apache.geode.cache.Region + * @see #isNonLocalClientRegion(Region) + * @see #isRegionPoolSubscriptionEnabled(Region) + */ + boolean isRegionRegisterInterestAllowed(@Nullable Region region) { + return isNonLocalClientRegion(region) && isRegionPoolSubscriptionEnabled(region); } - protected Pool resolvePool(String name) { + /** + * Resolves the {@link Pool} with the given {@link String name) from the {@link PoolManager}. + * + * @param name {@link String) containing the name of the {@link Pool} to resovle. + * @return the resolved {@link Pool} for the given {@ling String name}. + * @see org.apache.geode.cache.client.PoolManager#find(String) + * @see org.apache.geode.cache.client.Pool + */ + protected @Nullable Pool resolvePool(String name) { return PoolManager.find(name); } @@ -744,7 +780,6 @@ public abstract class AbstractGemFireOperationsSessionRepository * or return a copy of the given {@link Session} as a {@link GemFireSession}. * @see #copy(Session) */ - @SuppressWarnings("unchecked") public static GemFireSession from(@NonNull Session session) { return session instanceof GemFireSession ? (GemFireSession) session : copy(session); } diff --git a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepositoryTests.java b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepositoryTests.java index 2407e63..72e5073 100644 --- a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepositoryTests.java +++ b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/AbstractGemFireOperationsSessionRepositoryTests.java @@ -84,6 +84,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.gemfire.GemfireOperations; import org.springframework.data.gemfire.GemfireTemplate; import org.springframework.data.gemfire.util.RegionUtils; +import org.springframework.lang.Nullable; import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.Session; import org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration; @@ -106,13 +107,22 @@ import org.slf4j.Logger; * Unit tests for {@link AbstractGemFireOperationsSessionRepository}. * * @author John Blum + * @see java.time.Duration + * @see java.time.Instant + * @see java.util.UUID + * @see java.util.concurrent.TimeUnit + * @see java.util.function.Function * @see org.junit.Test * @see org.junit.runner.RunWith * @see org.mockito.Mock * @see org.mockito.Mockito * @see org.mockito.junit.MockitoJUnitRunner * @see org.mockito.Spy + * @see org.apache.geode.Delta * @see org.apache.geode.cache.Region + * @see org.apache.geode.cache.RegionAttributes + * @see org.apache.geode.cache.client.ClientCache + * @see org.apache.geode.cache.client.Pool * @see org.springframework.data.gemfire.GemfireOperations * @see org.springframework.data.gemfire.GemfireTemplate * @see org.springframework.session.FindByIndexNameSessionRepository @@ -306,6 +316,103 @@ public class AbstractGemFireOperationsSessionRepositoryTests { } } + @Test + @SuppressWarnings("unchecked") + public void isRegionPoolSubscriptionEnabledReturnsTrue() { + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + + when(mockRegionAttributes.getPoolName()).thenReturn("Dead"); + when(this.mockRegion.getAttributes()).thenReturn(mockRegionAttributes); + when(this.mockPool.getSubscriptionEnabled()).thenReturn(true); + doReturn(this.mockPool).when(this.sessionRepository).resolvePool(anyString()); + + assertThat(this.sessionRepository.isRegionPoolSubscriptionEnabled(this.mockRegion)).isTrue(); + + verify(this.mockRegion, times(1)).getAttributes(); + verify(mockRegionAttributes, times(1)).getPoolName(); + verify(this.sessionRepository, times(1)).resolvePool(eq("Dead")); + } + + @Test + public void isRegionPoolSubscriptionEnabledReturnsFalseForNullRegion() { + + assertThat(this.sessionRepository.isRegionPoolSubscriptionEnabled(null)).isFalse(); + + verify(this.sessionRepository, never()).resolvePool(anyString()); + } + + @Test + @SuppressWarnings("unchecked") + public void isRegionPoolSubscriptionEnabledReturnsFalseForUnresolvablePool() { + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + + when(mockRegionAttributes.getPoolName()).thenReturn("Swimming"); + when(this.mockRegion.getAttributes()).thenReturn(mockRegionAttributes); + doReturn(null).when(this.sessionRepository).resolvePool(anyString()); + + assertThat(this.sessionRepository.isRegionPoolSubscriptionEnabled(this.mockRegion)).isFalse(); + + verify(this.mockRegion, times(1)).getAttributes(); + verify(mockRegionAttributes, times(1)).getPoolName(); + verify(this.sessionRepository, times(1)).resolvePool(eq("Swimming")); + } + + @Test + @SuppressWarnings("unchecked") + public void isRegionPoolSubscriptionEnabledReturnsFalseWhenPoolSubscriptionIsDisabled() { + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + + when(mockRegionAttributes.getPoolName()).thenReturn("Swimming"); + when(this.mockRegion.getAttributes()).thenReturn(mockRegionAttributes); + when(this.mockPool.getSubscriptionEnabled()).thenReturn(false); + doReturn(this.mockPool).when(this.sessionRepository).resolvePool(anyString()); + + assertThat(this.sessionRepository.isRegionPoolSubscriptionEnabled(this.mockRegion)).isFalse(); + + verify(this.mockRegion, times(1)).getAttributes(); + verify(mockRegionAttributes, times(1)).getPoolName(); + verify(this.sessionRepository, times(1)).resolvePool(eq("Swimming")); + verify(this.mockPool, times(1)).getSubscriptionEnabled(); + } + + @Test + public void isRegionRegisterInterestAllowedCallsIsNonLocalClientRegionAndIsRegionPoolSubscriptionEnabledReturnsTrue() { + + doReturn(true).when(this.sessionRepository).isNonLocalClientRegion(any(Region.class)); + doReturn(true).when(this.sessionRepository).isRegionPoolSubscriptionEnabled(any(Region.class)); + + assertThat(this.sessionRepository.isRegionRegisterInterestAllowed(this.mockRegion)).isTrue(); + + verify(this.sessionRepository, times(1)).isNonLocalClientRegion(eq(this.mockRegion)); + verify(this.sessionRepository, times(1)).isRegionPoolSubscriptionEnabled(eq(this.mockRegion)); + } + + @Test + public void isRegionRegisterInterestAllowedCallsIsNonLocalClientRegionAndIsRegionPoolSubscriptionEnabledReturnsFalse() { + + doReturn(true).when(this.sessionRepository).isNonLocalClientRegion(any(Region.class)); + doReturn(false).when(this.sessionRepository).isRegionPoolSubscriptionEnabled(any(Region.class)); + + assertThat(this.sessionRepository.isRegionRegisterInterestAllowed(this.mockRegion)).isFalse(); + + verify(this.sessionRepository, times(1)).isNonLocalClientRegion(eq(this.mockRegion)); + verify(this.sessionRepository, times(1)).isRegionPoolSubscriptionEnabled(eq(this.mockRegion)); + } + + @Test + public void isRegionRegisterInterestAllowedCallsIsNonLocalClientRegionAndIsRegionPoolSubscriptionEnabledReturnsFalseAgain() { + + doReturn(false).when(this.sessionRepository).isNonLocalClientRegion(any(Region.class)); + + assertThat(this.sessionRepository.isRegionRegisterInterestAllowed(this.mockRegion)).isFalse(); + + verify(this.sessionRepository, times(1)).isNonLocalClientRegion(eq(this.mockRegion)); + verify(this.sessionRepository, never()).isRegionPoolSubscriptionEnabled(eq(this.mockRegion)); + } + @Test public void setAndGetApplicationEventPublisher() { @@ -3639,7 +3746,7 @@ public class AbstractGemFireOperationsSessionRepositoryTests { } @Override - protected Pool resolvePool(String name) { + protected @Nullable Pool resolvePool(String name) { return mockPool; } }