Add generic GemFire findByIndexNameAndIndexValue support

Fixes gh-353
This commit is contained in:
John Blum
2016-02-03 23:04:20 -08:00
committed by Rob Winch
parent 7de11753a9
commit c2b407189e
13 changed files with 611 additions and 50 deletions

View File

@@ -1268,6 +1268,55 @@ public class AbstractGemFireOperationsSessionRepositoryTest {
verify(mockDataInput, times(1)).readUTF();
}
@Test
public void sessionAttributesEntrySetIteratesAttributeNameValues() {
GemFireSessionAttributes sessionAttributes = new GemFireSessionAttributes();
sessionAttributes.setAttribute("keyOne", "valueOne");
sessionAttributes.setAttribute("keyTwo", "valueTwo");
Set<Map.Entry<String, Object>> sessionAttributeEntries = sessionAttributes.entrySet();
assertThat(sessionAttributeEntries).isNotNull();
assertThat(sessionAttributeEntries.size()).isEqualTo(2);
Set<String> expectedNames = asSet("keyOne", "keyTwo");
Set<?> expectedValues = asSet("valueOne", "valueTwo");
for (Map.Entry<String, Object> entry : sessionAttributeEntries) {
expectedNames.remove(entry.getKey());
expectedValues.remove(entry.getValue());
}
assertThat(expectedNames.isEmpty()).isTrue();
assertThat(expectedValues.isEmpty()).isTrue();
sessionAttributes.setAttribute("keyThree", "valueThree");
assertThat(sessionAttributeEntries.size()).isEqualTo(3);
expectedNames = asSet("keyOne", "keyTwo");
expectedValues = asSet("valueOne", "valueTwo");
for (Map.Entry<String, Object> entry : sessionAttributeEntries) {
expectedNames.remove(entry.getKey());
expectedValues.remove(entry.getValue());
}
assertThat(expectedNames.isEmpty()).isTrue();
assertThat(expectedValues.isEmpty()).isTrue();
sessionAttributes.removeAttribute("keyOne");
sessionAttributes.removeAttribute("keyTwo");
assertThat(sessionAttributeEntries.size()).isEqualTo(1);
Map.Entry<String, ?> entry = sessionAttributeEntries.iterator().next();
assertThat(entry.getKey()).isEqualTo("keyThree");
assertThat(entry.getValue()).isEqualTo("valueThree");
}
@Test
public void sessionWithAttributesAreThreadSafe() throws Throwable {
TestFramework.runOnce(new ThreadSafeSessionTest());

View File

@@ -27,6 +27,9 @@ 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 static org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
import static org.springframework.session.data.gemfire.GemFireOperationsSessionRepository.FIND_SESSIONS_BY_INDEX_NAME_VALUE_QUERY;
import static org.springframework.session.data.gemfire.GemFireOperationsSessionRepository.FIND_SESSIONS_BY_PRINCIPAL_NAME_QUERY;
import static org.springframework.session.data.gemfire.GemFireOperationsSessionRepository.GemFireSession;
import java.util.Arrays;
@@ -47,7 +50,6 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.gemfire.GemfireAccessor;
import org.springframework.data.gemfire.GemfireOperations;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.events.AbstractSessionEvent;
import org.springframework.session.events.SessionDeletedEvent;
@@ -66,6 +68,7 @@ import com.gemstone.gemfire.cache.query.SelectResults;
* @see org.mockito.Mockito
* @see org.mockito.runners.MockitoJUnitRunner
* @see org.springframework.session.data.gemfire.GemFireOperationsSessionRepository
* @see com.gemstone.gemfire.cache.Region
* @since 1.1.0
*/
@RunWith(MockitoJUnitRunner.class)
@@ -110,6 +113,36 @@ public class GemFireOperationsSessionRepositoryTest {
verify(mockTemplate, times(1)).getRegion();
}
@Test
@SuppressWarnings("unchecked")
public void findByIndexNameValueFindsMatchingSession() {
ExpiringSession mockSession = mock(ExpiringSession.class, "MockSession");
when(mockSession.getId()).thenReturn("1");
SelectResults<Object> mockSelectResults = mock(SelectResults.class);
when(mockSelectResults.asList()).thenReturn(Collections.<Object>singletonList(mockSession));
String indexName = "vip";
String indexValue = "rwinch";
String expectedQql = String.format(FIND_SESSIONS_BY_INDEX_NAME_VALUE_QUERY,
sessionRepository.getFullyQualifiedRegionName(), indexName);
when(mockTemplate.find(eq(expectedQql), eq(indexValue))).thenReturn(mockSelectResults);
Map<String, ExpiringSession> sessions = sessionRepository.findByIndexNameAndIndexValue(indexName, indexValue);
assertThat(sessions).isNotNull();
assertThat(sessions.size()).isEqualTo(1);
assertThat(sessions.get("1")).isEqualTo(mockSession);
verify(mockTemplate, times(1)).find(eq(expectedQql), eq(indexValue));
verify(mockSelectResults, times(1)).asList();
verify(mockSession, times(1)).getId();
}
@Test
@SuppressWarnings("unchecked")
public void findByPrincipalNameFindsMatchingSessions() throws Exception {
@@ -127,12 +160,13 @@ public class GemFireOperationsSessionRepositoryTest {
String principalName = "jblum";
String expectedOql = String.format(GemFireOperationsSessionRepository.FIND_SESSIONS_BY_PRINCIPAL_NAME_QUERY,
String expectedOql = String.format(FIND_SESSIONS_BY_PRINCIPAL_NAME_QUERY,
sessionRepository.getFullyQualifiedRegionName());
when(mockTemplate.find(eq(expectedOql), eq(principalName))).thenReturn(mockSelectResults);
Map<String, ExpiringSession> sessions = sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, principalName);
Map<String, ExpiringSession> sessions = sessionRepository.findByIndexNameAndIndexValue(
PRINCIPAL_NAME_INDEX_NAME, principalName);
assertThat(sessions).isNotNull();
assertThat(sessions.size()).isEqualTo(3);
@@ -156,12 +190,13 @@ public class GemFireOperationsSessionRepositoryTest {
String principalName = "jblum";
String expectedOql = String.format(GemFireOperationsSessionRepository.FIND_SESSIONS_BY_PRINCIPAL_NAME_QUERY,
String expectedOql = String.format(FIND_SESSIONS_BY_PRINCIPAL_NAME_QUERY,
sessionRepository.getFullyQualifiedRegionName());
when(mockTemplate.find(eq(expectedOql), eq(principalName))).thenReturn(mockSelectResults);
Map<String, ExpiringSession> sessions = sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, principalName);
Map<String, ExpiringSession> sessions = sessionRepository.findByIndexNameAndIndexValue(
PRINCIPAL_NAME_INDEX_NAME, principalName);
assertThat(sessions).isNotNull();
assertThat(sessions.isEmpty()).isTrue();
@@ -170,6 +205,25 @@ public class GemFireOperationsSessionRepositoryTest {
verify(mockSelectResults, times(1)).asList();
}
@Test
public void prepareQueryReturnsPrincipalNameOql() {
String actualQql = sessionRepository.prepareQuery(PRINCIPAL_NAME_INDEX_NAME);
String expectedOql = String.format(FIND_SESSIONS_BY_PRINCIPAL_NAME_QUERY,
sessionRepository.getFullyQualifiedRegionName());
assertThat(actualQql).isEqualTo(expectedOql);
}
@Test
public void prepareQueryReturnsIndexNameValueOql() {
String attributeName = "testAttributeName";
String actualOql = sessionRepository.prepareQuery(attributeName);
String expectedOql = String.format(FIND_SESSIONS_BY_INDEX_NAME_VALUE_QUERY,
sessionRepository.getFullyQualifiedRegionName(), attributeName);
assertThat(actualOql).isEqualTo(expectedOql);
}
@Test
public void createProperlyInitializedSession() {
final long beforeOrAtCreationTime = System.currentTimeMillis();

View File

@@ -47,6 +47,9 @@ import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.GemfireOperations
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.session.data.gemfire.GemFireOperationsSessionRepository
* @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration
* @see com.gemstone.gemfire.cache.Cache
* @see com.gemstone.gemfire.cache.GemFireCache
@@ -58,6 +61,10 @@ public class GemFireHttpSessionConfigurationTest {
private GemFireHttpSessionConfiguration gemfireConfiguration;
protected <T> T[] toArray(T... array) {
return array;
}
@Before
public void setup() {
gemfireConfiguration = new GemFireHttpSessionConfiguration();
@@ -91,6 +98,29 @@ public class GemFireHttpSessionConfigurationTest {
GemFireHttpSessionConfiguration.DEFAULT_CLIENT_REGION_SHORTCUT);
}
@Test
public void setAndGetIndexableSessionAttributes() {
assertThat(gemfireConfiguration.getIndexableSessionAttributes()).isEqualTo(
GemFireHttpSessionConfiguration.DEFAULT_INDEXABLE_SESSION_ATTRIBUTES);
gemfireConfiguration.setIndexableSessionAttributes(toArray("one", "two", "three"));
assertThat(gemfireConfiguration.getIndexableSessionAttributes()).isEqualTo(toArray("one", "two", "three"));
assertThat(gemfireConfiguration.getIndexableSessionAttributesAsGemFireIndexExpression())
.isEqualTo("'one', 'two', 'three'");
gemfireConfiguration.setIndexableSessionAttributes(toArray("one"));
assertThat(gemfireConfiguration.getIndexableSessionAttributes()).isEqualTo(toArray("one"));
assertThat(gemfireConfiguration.getIndexableSessionAttributesAsGemFireIndexExpression()).isEqualTo("'one'");
gemfireConfiguration.setIndexableSessionAttributes(null);
assertThat(gemfireConfiguration.getIndexableSessionAttributes()).isEqualTo(
GemFireHttpSessionConfiguration.DEFAULT_INDEXABLE_SESSION_ATTRIBUTES);
assertThat(gemfireConfiguration.getIndexableSessionAttributesAsGemFireIndexExpression()).isEqualTo("*");
}
@Test
public void setAndGetMaxInactiveIntervalInSeconds() {
assertThat(gemfireConfiguration.getMaxInactiveIntervalInSeconds()).isEqualTo(
@@ -160,6 +190,7 @@ public class GemFireHttpSessionConfigurationTest {
Map<String, Object> annotationAttributes = new HashMap<String, Object>(4);
annotationAttributes.put("clientRegionShortcut", ClientRegionShortcut.CACHING_PROXY);
annotationAttributes.put("indexableSessionAttributes", toArray("one", "two", "three"));
annotationAttributes.put("maxInactiveIntervalInSeconds", 600);
annotationAttributes.put("serverRegionShortcut", RegionShortcut.REPLICATE);
annotationAttributes.put("regionName", "TEST");
@@ -170,6 +201,7 @@ public class GemFireHttpSessionConfigurationTest {
gemfireConfiguration.setImportMetadata(mockAnnotationMetadata);
assertThat(gemfireConfiguration.getClientRegionShortcut()).isEqualTo(ClientRegionShortcut.CACHING_PROXY);
assertThat(gemfireConfiguration.getIndexableSessionAttributes()).isEqualTo(toArray("one", "two", "three"));
assertThat(gemfireConfiguration.getMaxInactiveIntervalInSeconds()).isEqualTo(600);
assertThat(gemfireConfiguration.getServerRegionShortcut()).isEqualTo(RegionShortcut.REPLICATE);
assertThat(gemfireConfiguration.getSpringSessionGemFireRegionName()).isEqualTo("TEST");