diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/session/SessionExpirationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/session/SessionExpirationIntegrationTests.java new file mode 100644 index 00000000..f00f4549 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/session/SessionExpirationIntegrationTests.java @@ -0,0 +1,148 @@ +/* + * Copyright 2018 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 + * + * http://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.geode.boot.autoconfigure.session; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.TimeUnit; + +import javax.annotation.Resource; + +import org.apache.geode.cache.Region; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Bean; +import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; +import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration; +import org.springframework.session.Session; +import org.springframework.session.SessionRepository; +import org.springframework.session.data.gemfire.GemFireOperationsSessionRepository; +import org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration; +import org.springframework.session.events.AbstractSessionEvent; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration tests to assert the property configuration of the Session state management provider + * with respect to Session expiration timeout. + * + * @author John Blum + * @see org.junit.Test + * @see org.apache.geode.cache.Region + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport + * @see org.springframework.session.Session + * @see org.springframework.session.SessionRepository + * @see org.springframework.session.data.gemfire.GemFireOperationsSessionRepository + * @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration + * @see org.springframework.session.events.AbstractSessionEvent + * @since 1.0.0 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(properties = { + "spring.session.data.gemfire.cache.client.region.shortcut=LOCAL", + "spring.session.timeout=1", +}, webEnvironment = SpringBootTest.WebEnvironment.NONE) +@SuppressWarnings("unused") +public class SessionExpirationIntegrationTests extends IntegrationTestsSupport { + + @Resource(name = GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME) + private Region sessionRegion; + + @Autowired + private SessionEventListener sessionEventListener; + + @Autowired + private SessionRepository sessionRepository; + + @Before + public void setup() { + + assertThat(this.sessionEventListener).isNotNull(); + assertThat(this.sessionRegion).isNotNull(); + assertThat(this.sessionRegion).isEmpty(); + assertThat(this.sessionRegion.getName()).isEqualTo(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME); + assertThat(this.sessionRepository).isInstanceOf(GemFireOperationsSessionRepository.class); + } + + @Test + public void sessionExpirationIsCorrect() { + + Session session = this.sessionRepository.createSession(); + + assertThat(session).isNotNull(); + assertThat(session.getId()).isNotBlank(); + assertThat(session.isExpired()).isFalse(); + + this.sessionRepository.save(session); + + Session savedSession = this.sessionRepository.findById(session.getId()); + + assertThat(savedSession).isEqualTo(session); + + // Clear any existing Session event + this.sessionEventListener.getSessionEvent(); + + AbstractSessionEvent sessionEvent = + this.sessionEventListener.waitForSessionEvent(TimeUnit.SECONDS.toMillis(2)); + + assertThat(sessionEvent).isNotNull(); + assertThat(sessionEvent.getSessionId()).isEqualTo(savedSession.getId()); + assertThat(this.sessionRepository.findById(savedSession.getId())).isNull(); + } + + static class SessionEventListener implements ApplicationListener { + + private volatile AbstractSessionEvent sessionEvent; + + @SuppressWarnings("unchecked") + public T getSessionEvent() { + + T sessionEvent = (T) this.sessionEvent; + + this.sessionEvent = null; + + return sessionEvent; + } + + @SuppressWarnings("all") + public void onApplicationEvent(AbstractSessionEvent event) { + this.sessionEvent = event; + } + + public T waitForSessionEvent(long duration) { + + waitOn(() -> SessionEventListener.this.sessionEvent != null, duration); + + return getSessionEvent(); + } + } + + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class) + static class TestConfiguration { + + @Bean + SessionEventListener sessionEventListener() { + return new SessionEventListener(); + } + } +}