Add Spring Boot auto-configuration support for Spring Session using Apache Geode or Pivotal GemFire as the Session state management provider.

Resolves gh-3.
This commit is contained in:
John Blum
2018-08-24 00:44:01 -07:00
parent d659271613
commit ce5519f06c
8 changed files with 538 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
/*
* 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 org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionAttributes;
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.ApplicationContext;
import org.springframework.data.gemfire.GemfireAccessor;
import org.springframework.data.gemfire.GemfireOperations;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.data.gemfire.util.RegionUtils;
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.test.context.junit4.SpringRunner;
/**
* Integration Tests for auto-configuration of Spring Session using either Apache Geode or Pivotal GemFire
* as the {@link Session} state management provider.
*
* This test asserts that the Spring Boot auto-configuration properly configures Spring Session
* with either Apache Geode or Pivotal GemFire
*
* @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.context.ApplicationContext
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @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.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("unused")
public class AutoConfiguredSessionCachingIntegrationTests {
@Autowired
private ApplicationContext applicationContext;
@Before
public void setup() {
assertThat(this.applicationContext).isNotNull();
assertThat(this.applicationContext.getBean(GemFireHttpSessionConfiguration.class)).isNotNull();
}
@Test
@SuppressWarnings("unchecked")
public void sessionRegionExists() {
assertThat(this.applicationContext.containsBean(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME))
.isTrue();
Region<Object, Session> sessionRegion =
this.applicationContext.getBean(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME, Region.class);
assertThat(sessionRegion).isNotNull();
assertThat(sessionRegion.getName()).isEqualTo(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME);
assertThat(sessionRegion.getFullPath())
.isEqualTo(RegionUtils.toRegionPath(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME));
RegionAttributes<Object, Session> sessionRegionAttributes = sessionRegion.getAttributes();
assertThat(sessionRegionAttributes).isNotNull();
assertThat(sessionRegionAttributes.getDataPolicy()).isEqualTo(DataPolicy.EMPTY);
}
@Test
public void sessionRepositoryExists() {
assertThat(this.applicationContext.containsBean("sessionRepository")).isTrue();
SessionRepository<?> sessionRepository =
this.applicationContext.getBean("sessionRepository", SessionRepository.class);
assertThat(sessionRepository).isInstanceOf(GemFireOperationsSessionRepository.class);
GemfireOperations gemfireOperations = ((GemFireOperationsSessionRepository) sessionRepository).getTemplate();
Region<?, ?> sessionRegion =
this.applicationContext.getBean(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME, Region.class);
GemfireTemplate sessionRegionTemplate =
this.applicationContext.getBean("sessionRegionTemplate", GemfireTemplate.class);
assertThat(gemfireOperations).isInstanceOf(GemfireAccessor.class);
assertThat(gemfireOperations).isSameAs(sessionRegionTemplate);
assertThat(((GemfireAccessor) gemfireOperations).getRegion()).isEqualTo(sessionRegion);
}
@EnableGemFireMockObjects
@SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class)
static class TestConfiguration { }
}

View File

@@ -0,0 +1,242 @@
/*
* 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 static org.mockito.Mockito.mock;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.junit.Test;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertySource;
import org.springframework.data.gemfire.tests.integration.SpringApplicationContextIntegrationTestsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.session.Session;
import org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration;
import org.springframework.session.data.gemfire.config.annotation.web.http.support.SpringSessionGemFireConfigurer;
import org.springframework.session.data.gemfire.serialization.SessionSerializer;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
* Integration Test for the auto-configuration of Spring Session using either Apache Geode or Pivotal GemFire
* as the {@link Session} state management provider.
*
* This test asserts that the Spring Boot auto-configuration can be customized using either {@link Properties}
* or a {@link SpringSessionGemFireConfigurer}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.boot.SpringBootConfiguration
* @see org.springframework.boot.autoconfigure.EnableAutoConfiguration
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.context.annotation.Bean
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.core.env.PropertySource
* @see org.springframework.data.gemfire.tests.integration.SpringApplicationContextIntegrationTestsSupport
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.session.Session
* @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration
* @see org.springframework.session.data.gemfire.config.annotation.web.http.support.SpringSessionGemFireConfigurer
* @since 1.0.0
*/
@SuppressWarnings("unused")
public class CustomConfiguredSessionCachingIntegrationTests extends SpringApplicationContextIntegrationTestsSupport {
private static final String SPRING_SESSION_DATA_GEMFIRE_PROPERTY = "spring.session.data.gemfire";
@SuppressWarnings("unchecked")
private <T> T invoke(Object obj, String methodName) {
return (T) Optional.ofNullable(obj)
.map(Object::getClass)
.map(type -> ReflectionUtils.findMethod(type, methodName))
.map(this::makeAccessible)
.map(method -> ReflectionUtils.invokeMethod(method, obj))
.orElseThrow(() -> newIllegalArgumentException("Method [%s] not found on Object of type [%s]",
methodName, ObjectUtils.nullSafeClassName(obj)));
}
private Method makeAccessible(Method method) {
ReflectionUtils.makeAccessible(method);
return method;
}
private PropertySource newSpringSessionGemFireProperties() {
return new MockPropertySource("TestSpringSessionGemFireProperties")
.withProperty(springSessionPropertyName("cache.client.region.shortcut"), "LOCAL")
.withProperty(springSessionPropertyName("session.attributes.indexable"), "one, two")
.withProperty(springSessionPropertyName("session.expiration.max-inactive-interval-seconds"), "600")
.withProperty(springSessionPropertyName("cache.client.pool.name"), "MockPool")
.withProperty(springSessionPropertyName("session.region.name"), "MockRegion")
.withProperty(springSessionPropertyName("cache.server.region.shortcut"), "REPLICATE")
.withProperty(springSessionPropertyName("session.serializer.bean-name"), "MockSessionSerializer");
}
@Override
protected ConfigurableApplicationContext processBeforeRefresh(ConfigurableApplicationContext applicationContext) {
applicationContext.getEnvironment().getPropertySources().addFirst(newSpringSessionGemFireProperties());
return applicationContext;
}
private String springSessionPropertyName(String propertyNameSuffix) {
return String.format("%1$s.%2$s", SPRING_SESSION_DATA_GEMFIRE_PROPERTY, propertyNameSuffix);
}
@Test
public void springSessionConfigurationCustomizedWithConfigurer() {
newApplicationContext(TestConfiguration.class, SpringSessionGemFireConfigurerTestConfiguration.class);
GemFireHttpSessionConfiguration sessionConfiguration = getBean(GemFireHttpSessionConfiguration.class);
assertThat(sessionConfiguration).isNotNull();
assertThat(this.<ClientRegionShortcut>invoke(sessionConfiguration, "getClientRegionShortcut"))
.isEqualTo(ClientRegionShortcut.CACHING_PROXY);
assertThat(this.<String[]>invoke(sessionConfiguration, "getIndexableSessionAttributes"))
.contains("two", "four");
assertThat(this.<Integer>invoke(sessionConfiguration, "getMaxInactiveIntervalInSeconds"))
.isEqualTo(900);
assertThat(this.<String>invoke(sessionConfiguration, "getPoolName")).isEqualTo("TestPool");
assertThat(this.<RegionShortcut>invoke(sessionConfiguration, "getServerRegionShortcut"))
.isEqualTo(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW);
assertThat(this.<String>invoke(sessionConfiguration, "getSessionRegionName"))
.isEqualTo("TestRegion");
assertThat(this.<String>invoke(sessionConfiguration, "getSessionSerializerBeanName"))
.isEqualTo("TestSessionSerializer");
}
@Test
public void springSessionConfigurationCustomizedWithProperties() {
newApplicationContext(TestConfiguration.class);
GemFireHttpSessionConfiguration sessionConfiguration = getBean(GemFireHttpSessionConfiguration.class);
assertThat(sessionConfiguration).isNotNull();
assertThat(this.<ClientRegionShortcut>invoke(sessionConfiguration, "getClientRegionShortcut"))
.isEqualTo(ClientRegionShortcut.LOCAL);
assertThat(this.<String[]>invoke(sessionConfiguration, "getIndexableSessionAttributes"))
.contains("one", "two");
assertThat(this.<Integer>invoke(sessionConfiguration, "getMaxInactiveIntervalInSeconds"))
.isEqualTo(600);
assertThat(this.<String>invoke(sessionConfiguration, "getPoolName")).isEqualTo("MockPool");
assertThat(this.<RegionShortcut>invoke(sessionConfiguration, "getServerRegionShortcut"))
.isEqualTo(RegionShortcut.REPLICATE);
assertThat(this.<String>invoke(sessionConfiguration, "getSessionRegionName"))
.isEqualTo("MockRegion");
assertThat(this.<String>invoke(sessionConfiguration, "getSessionSerializerBeanName"))
.isEqualTo("MockSessionSerializer");
}
@SpringBootConfiguration
@EnableGemFireMockObjects
@EnableAutoConfiguration(exclude = ContinuousQueryAutoConfiguration.class)
//@SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class)
static class TestConfiguration {
@Bean("MockSessionSerializer")
@SuppressWarnings("unchecked")
SessionSerializer<Session, ?, ?> mockSessionSerializer() {
return mock(SessionSerializer.class);
}
}
@Configuration
static class SpringSessionGemFireConfigurerTestConfiguration {
@Bean
SpringSessionGemFireConfigurer customSpringSessionGemFireConfiguration() {
return new SpringSessionGemFireConfigurer() {
@Override
public ClientRegionShortcut getClientRegionShortcut() {
return ClientRegionShortcut.CACHING_PROXY;
}
@Override
public String[] getIndexableSessionAttributes() {
return new String[] { "two", "four" };
}
@Override
public int getMaxInactiveIntervalInSeconds() {
return Long.valueOf(TimeUnit.MINUTES.toSeconds(15L)).intValue();
}
@Override
public String getPoolName() {
return "TestPool";
}
@Override
public String getRegionName() {
return "TestRegion";
}
@Override
public RegionShortcut getServerRegionShortcut() {
return RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW;
}
@Override
public String getSessionSerializerBeanName() {
return "TestSessionSerializer";
}
};
}
@Bean("TestSessionSerializer")
@SuppressWarnings("unchecked")
SessionSerializer<Session, ?, ?> testSessionSerializer() {
return mock(SessionSerializer.class);
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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 static org.mockito.Mockito.mock;
import org.apache.geode.cache.GemFireCache;
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.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.data.gemfire.GemFireOperationsSessionRepository;
import org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration Tests for auto-configuration of Spring Session using a custom Spring Session {@link Session} state
* management provider, asserting that neither Apache Geode nor Pivotal GemFire is configured.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.GemFireCache
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.context.ApplicationContext
* @see org.springframework.context.annotation.Bean
* @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects
* @see org.springframework.session.Session
* @see org.springframework.session.SessionRepository
* @see org.springframework.session.config.annotation.web.http.EnableSpringHttpSession
* @see org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration
* @see org.springframework.session.data.gemfire.GemFireOperationsSessionRepository
* @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration
* @see org.springframework.test.context.junit4.SpringRunner
* @since 1.0.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SuppressWarnings("unused")
public class ManuallyConfiguredSessionCachingIntegrationTests {
@Autowired
private ApplicationContext applicationContext;
@Before
public void setup() {
assertThat(this.applicationContext).isNotNull();
assertThat(this.applicationContext.getBean(SpringHttpSessionConfiguration.class)).isNotNull();
}
@Test
public void gemfireSessionRegionAndSessionRepositoryAreNotPresent() {
assertThat(this.applicationContext.containsBean("gemfireCache")).isTrue();
assertThat(this.applicationContext.containsBean("sessionRepository")).isTrue();
assertThat(this.applicationContext.containsBean(GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME))
.isFalse();
GemFireCache gemfireCache = this.applicationContext.getBean(GemFireCache.class);
assertThat(gemfireCache).isNotNull();
assertThat(gemfireCache.rootRegions()).isEmpty();
SessionRepository<?> sessionRepository = this.applicationContext.getBean(SessionRepository.class);
assertThat(sessionRepository).isNotNull();
assertThat(sessionRepository).isNotInstanceOf(GemFireOperationsSessionRepository.class);
}
@EnableGemFireMockObjects
@EnableSpringHttpSession
@SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class)
static class TestConfiguration {
@Bean
SessionRepository<?> sessionRepository() {
return mock(SessionRepository.class);
}
}
}