From 5c647b8683d49f11f06cdf34d4e6552f284c3ee6 Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 28 Nov 2017 21:18:40 -0800 Subject: [PATCH] Fix test failures related to API changes in Spring Session core 2.0.0.RC2. --- ...FireHttpSessionEventsIntegrationTests.java | 5 +- .../GemFireOperationsSessionRepository.java | 9 ++-- ...mFireOperationsSessionRepositoryTests.java | 49 ++++++++++++++----- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/spring-session-data-geode/src/integration-test/java/org/springframework/session/data/gemfire/config/annotation/web/http/EnableGemFireHttpSessionEventsIntegrationTests.java b/spring-session-data-geode/src/integration-test/java/org/springframework/session/data/gemfire/config/annotation/web/http/EnableGemFireHttpSessionEventsIntegrationTests.java index 234b180..79c6e58 100644 --- a/spring-session-data-geode/src/integration-test/java/org/springframework/session/data/gemfire/config/annotation/web/http/EnableGemFireHttpSessionEventsIntegrationTests.java +++ b/spring-session-data-geode/src/integration-test/java/org/springframework/session/data/gemfire/config/annotation/web/http/EnableGemFireHttpSessionEventsIntegrationTests.java @@ -207,7 +207,7 @@ public class EnableGemFireHttpSessionEventsIntegrationTests extends AbstractGemF sessionEvent = this.sessionEventListener.getSessionEvent(); assertThat(sessionEvent).isInstanceOf(SessionDeletedEvent.class); - assertThat(sessionEvent.getSession()).isNull(); + assertThat(sessionEvent.getSession()).isEqualTo(expectedSession); assertThat(sessionEvent.getSessionId()).isEqualTo(expiredSession.getId()); assertThat(this.gemfireSessionRepository.findById(sessionEvent.getSessionId())).isNull(); } @@ -224,7 +224,8 @@ public class EnableGemFireHttpSessionEventsIntegrationTests extends AbstractGemF AbstractSessionEvent sessionEvent = this.sessionEventListener.getSessionEvent(); assertThat(sessionEvent).isInstanceOf(SessionDeletedEvent.class); - assertThat(sessionEvent.getSession()).isNull(); + assertThat(sessionEvent.getSession()).isNotNull(); + assertThat(sessionEvent.getSession().getId()).isEqualTo(expectedSessionId); assertThat(sessionEvent.getSessionId()).isEqualTo(expectedSessionId); } diff --git a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepository.java b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepository.java index b63ad09..f000c0a 100644 --- a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepository.java +++ b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepository.java @@ -114,11 +114,12 @@ public class GemFireOperationsSessionRepository extends AbstractGemFireOperation } /** - * Gets a copy of an existing, non-expired {@link Session} by ID. If the - * Session is expired, then it is deleted. + * Gets a copy of an existing, non-expired {@link Session} by ID. + * + * If the {@link Session} is expired, then the {@link Session }is deleted. * * @param sessionId a String indicating the ID of the Session to get. - * @return an existing {@link Session} by ID or null if not Session exists. + * @return an existing {@link Session} by ID or null if no {@link Session} exists. * @see AbstractGemFireOperationsSessionRepository.GemFireSession#from(Session) * @see org.springframework.session.Session * @see #deleteById(String) @@ -156,6 +157,6 @@ public class GemFireOperationsSessionRepository extends AbstractGemFireOperation * @see #handleDeleted(String, Session) */ public void deleteById(String sessionId) { - handleDeleted(sessionId, getTemplate().remove(sessionId)); + handleDeleted(sessionId, toSession(getTemplate().remove(sessionId), sessionId)); } } diff --git a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepositoryTests.java b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepositoryTests.java index b35f63d..8c7b035 100644 --- a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepositoryTests.java +++ b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/GemFireOperationsSessionRepositoryTests.java @@ -24,9 +24,11 @@ import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.same; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.time.Duration; import java.time.Instant; @@ -34,6 +36,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.After; import org.junit.Before; @@ -52,6 +55,7 @@ import org.springframework.data.gemfire.GemfireAccessor; import org.springframework.data.gemfire.GemfireOperations; import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.Session; +import org.springframework.session.data.gemfire.support.GemFireUtils; import org.springframework.session.events.AbstractSessionEvent; import org.springframework.session.events.SessionDeletedEvent; @@ -66,6 +70,8 @@ import org.springframework.session.events.SessionDeletedEvent; * @see org.mockito.Mockito * @see org.mockito.junit.MockitoJUnitRunner * @see org.apache.geode.cache.Region + * @see org.springframework.context.ApplicationEvent + * @see org.springframework.context.ApplicationEventPublisher * @see org.springframework.data.gemfire.GemfireAccessor * @see org.springframework.data.gemfire.GemfireOperations * @see org.springframework.session.FindByIndexNameSessionRepository @@ -87,6 +93,7 @@ public class GemFireOperationsSessionRepositoryTests { @Mock private GemfireOperationsAccessor mockTemplate; + // Subject Under Test (SUT) private GemFireOperationsSessionRepository sessionRepository; @Mock @@ -95,9 +102,9 @@ public class GemFireOperationsSessionRepositoryTests { @Before public void setup() throws Exception { - given(this.mockRegion.getAttributesMutator()).willReturn(this.mockAttributesMutator); - given(this.mockRegion.getFullPath()).willReturn("/Example"); - given(this.mockTemplate.getRegion()).willReturn(this.mockRegion); + when(this.mockRegion.getAttributesMutator()).thenReturn(this.mockAttributesMutator); + when(this.mockRegion.getFullPath()).thenReturn(GemFireUtils.toRegionPath("Example")); + when(this.mockTemplate.getRegion()).thenReturn(this.mockRegion); this.sessionRepository = new GemFireOperationsSessionRepository(this.mockTemplate); this.sessionRepository.setApplicationEventPublisher(this.mockApplicationEventPublisher); @@ -105,7 +112,7 @@ public class GemFireOperationsSessionRepositoryTests { this.sessionRepository.afterPropertiesSet(); assertThat(this.sessionRepository.getApplicationEventPublisher()).isSameAs(this.mockApplicationEventPublisher); - assertThat(this.sessionRepository.getFullyQualifiedRegionName()).isEqualTo("/Example"); + assertThat(this.sessionRepository.getFullyQualifiedRegionName()).isEqualTo(GemFireUtils.toRegionPath("Example")); assertThat(this.sessionRepository.getMaxInactiveIntervalInSeconds()).isEqualTo(MAX_INACTIVE_INTERVAL_IN_SECONDS); } @@ -270,12 +277,13 @@ public class GemFireOperationsSessionRepositoryTests { AbstractSessionEvent sessionEvent = (AbstractSessionEvent) applicationEvent; - assertThat(sessionEvent.getSource()) - .isSameAs(GemFireOperationsSessionRepositoryTests.this.sessionRepository); assertThat(sessionEvent.getSession()).isSameAs(mockSession); assertThat(sessionEvent.getSessionId()).isEqualTo(expectedSessionId); + assertThat(sessionEvent.getSource()) + .isSameAs(GemFireOperationsSessionRepositoryTests.this.sessionRepository); return null; + }).given(this.mockApplicationEventPublisher).publishEvent(any(ApplicationEvent.class)); assertThat(this.sessionRepository.findById(expectedSessionId)).isNull(); @@ -411,11 +419,16 @@ public class GemFireOperationsSessionRepositoryTests { @Test public void deleteRemovesNonExistingSessionAndHandlesDelete() { + AtomicBoolean called = new AtomicBoolean(false); + + Session mockSession = mock(Session.class); + String expectedSessionId = "1"; - given(this.mockTemplate.remove(anyString())).willReturn(null); + when(mockSession.getId()).thenReturn(expectedSessionId); + when(this.mockTemplate.remove(anyString())).thenReturn(mockSession); - willAnswer(invocation -> { + doAnswer(invocation -> { ApplicationEvent applicationEvent = invocation.getArgument(0); @@ -423,21 +436,31 @@ public class GemFireOperationsSessionRepositoryTests { AbstractSessionEvent sessionEvent = (AbstractSessionEvent) applicationEvent; - assertThat(sessionEvent.getSource()).isSameAs(GemFireOperationsSessionRepositoryTests.this.sessionRepository); - assertThat(sessionEvent.getSession()).isNull(); + Session session = sessionEvent.getSession(); + + assertThat(session).isNotNull(); + assertThat(session.getId()).isEqualTo(expectedSessionId); assertThat(sessionEvent.getSessionId()).isEqualTo(expectedSessionId); + assertThat(sessionEvent.getSource()) + .isSameAs(GemFireOperationsSessionRepositoryTests.this.sessionRepository); + + called.set(true); return null; - }).given(this.mockApplicationEventPublisher).publishEvent(isA(SessionDeletedEvent.class)); + + }).when(this.mockApplicationEventPublisher).publishEvent(isA(SessionDeletedEvent.class)); this.sessionRepository.deleteById(expectedSessionId); + assertThat(called.get()).isTrue(); + + verify(mockSession, times(2)).getId(); verify(this.mockTemplate, times(1)).remove(eq(expectedSessionId)); verify(this.mockApplicationEventPublisher, times(1)) .publishEvent(isA(SessionDeletedEvent.class)); } - protected abstract class GemfireOperationsAccessor extends GemfireAccessor - implements GemfireOperations { + protected abstract class GemfireOperationsAccessor extends GemfireAccessor implements GemfireOperations { + } }