SGF-402 - Add SDG Annotation configuration support for Lucene Integration.
(cherry picked from commit 249e64586537d9be9331971b9e3b3d864b6cf694) Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
@@ -206,6 +206,11 @@ public class EntityDefinedRegionsConfiguration
|
||||
return importingClassMetadata.hasAnnotation(annotationName);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected AnnotationAttributes getAnnotationAttributes(Annotation annotation) {
|
||||
return AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(annotation));
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected AnnotationAttributes getAnnotationAttributes(AnnotationMetadata importingClassMetadata) {
|
||||
return getAnnotationAttributes(importingClassMetadata, getAnnotationTypeName());
|
||||
|
||||
@@ -21,6 +21,8 @@ import java.lang.annotation.Annotation;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.geode.cache.Region;
|
||||
import org.apache.geode.cache.lucene.LuceneIndex;
|
||||
import org.apache.geode.cache.query.Index;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
@@ -33,13 +35,15 @@ import org.springframework.data.gemfire.config.xml.GemfireConstants;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentProperty;
|
||||
import org.springframework.data.gemfire.mapping.annotation.Indexed;
|
||||
import org.springframework.data.gemfire.mapping.annotation.LuceneIndexed;
|
||||
import org.springframework.data.gemfire.search.lucene.LuceneIndexFactoryBean;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* The {@link IndexConfiguration} class is a Spring {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}
|
||||
* and extension of {@link EntityDefinedRegionsConfiguration} used in the {@link EnableIndexes} annotation
|
||||
* to dynamically create GemFire/Geode {@link org.apache.geode.cache.Region} Indexes based on
|
||||
* to dynamically create GemFire/Geode {@link org.apache.geode.cache.Region} {@link Index Indexes} based on
|
||||
* {@link GemfirePersistentEntity} {@link GemfirePersistentProperty} annotations.
|
||||
*
|
||||
* @author John Blum
|
||||
@@ -49,11 +53,13 @@ import org.springframework.util.StringUtils;
|
||||
* @see org.springframework.data.annotation.Id
|
||||
* @see org.springframework.data.gemfire.IndexFactoryBean
|
||||
* @see org.springframework.data.gemfire.IndexType
|
||||
* @see org.springframework.data.gemfire.search.lucene.LuceneIndexFactoryBean
|
||||
* @see org.springframework.data.gemfire.config.annotation.EntityDefinedRegionsConfiguration
|
||||
* @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity
|
||||
* @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty
|
||||
* @see Indexed
|
||||
* @see org.springframework.data.gemfire.mapping.annotation.Indexed
|
||||
* @see org.apache.geode.cache.Region
|
||||
* @see org.apache.geode.cache.lucene.LuceneIndex
|
||||
* @see org.apache.geode.cache.query.Index
|
||||
* @since 1.9.0
|
||||
*/
|
||||
@@ -105,9 +111,9 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
|
||||
*/
|
||||
@Override
|
||||
protected GemfirePersistentEntity<?> postProcess(AnnotationMetadata importingClassMetadata,
|
||||
final BeanDefinitionRegistry registry, GemfirePersistentEntity<?> persistentEntity) {
|
||||
BeanDefinitionRegistry registry, GemfirePersistentEntity<?> persistentEntity) {
|
||||
|
||||
final GemfirePersistentEntity<?> localPersistentEntity =
|
||||
GemfirePersistentEntity<?> localPersistentEntity =
|
||||
super.postProcess(importingClassMetadata, registry, persistentEntity);
|
||||
|
||||
if (isAnnotationPresent(importingClassMetadata, getEnableIndexesAnnotationTypeName())) {
|
||||
@@ -115,16 +121,17 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
|
||||
getAnnotationAttributes(importingClassMetadata, getEnableIndexesAnnotationTypeName());
|
||||
|
||||
localPersistentEntity.doWithProperties((PropertyHandler<GemfirePersistentProperty>) persistentProperty -> {
|
||||
Optional<Id> idAnnotation = persistentProperty.findAnnotation(Id.class);
|
||||
persistentProperty.findAnnotation(Id.class).ifPresent(idAnnotation ->
|
||||
registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity, persistentProperty,
|
||||
IndexType.KEY, idAnnotation, registry));
|
||||
|
||||
idAnnotation.ifPresent(id -> registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity,
|
||||
persistentProperty, IndexType.KEY, id, registry));
|
||||
persistentProperty.findAnnotation(Indexed.class).ifPresent(indexedAnnotation ->
|
||||
registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity, persistentProperty,
|
||||
indexedAnnotation.type(), indexedAnnotation, registry));
|
||||
|
||||
Optional<Indexed> indexedAnnotation = persistentProperty.findAnnotation(Indexed.class);
|
||||
|
||||
indexedAnnotation.ifPresent(
|
||||
indexed -> registerIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity,
|
||||
persistentProperty, indexed.type(), indexed, registry));
|
||||
persistentProperty.findAnnotation(LuceneIndexed.class).ifPresent( luceneIndexAnnotation ->
|
||||
registerLuceneIndexBeanDefinition(enableIndexesAttributes, localPersistentEntity,
|
||||
persistentProperty, luceneIndexAnnotation, registry));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -150,38 +157,87 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
|
||||
* @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity
|
||||
* @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
protected void registerIndexBeanDefinition(AnnotationAttributes enableIndexesAttributes,
|
||||
GemfirePersistentEntity<?> persistentEntity, GemfirePersistentProperty persistentProperty,
|
||||
IndexType indexType, Annotation indexAnnotation, BeanDefinitionRegistry registry) {
|
||||
|
||||
if (indexAnnotation != null) {
|
||||
AnnotationAttributes indexAnnotationAttributes = AnnotationAttributes.fromMap(
|
||||
AnnotationUtils.getAnnotationAttributes(indexAnnotation));
|
||||
Optional.ofNullable(indexAnnotation).ifPresent(localIndexAnnotation -> {
|
||||
AnnotationAttributes indexedAttributes = getAnnotationAttributes(localIndexAnnotation);
|
||||
|
||||
BeanDefinitionBuilder indexFactoryBeanBuilder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(IndexFactoryBean.class);
|
||||
|
||||
String indexName = resolveName(persistentEntity, persistentProperty,
|
||||
indexAnnotationAttributes, indexType);
|
||||
String indexName = resolveName(persistentEntity, persistentProperty, indexedAttributes, indexType);
|
||||
|
||||
indexFactoryBeanBuilder.addPropertyReference("cache", GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME);
|
||||
|
||||
indexFactoryBeanBuilder.addPropertyValue("define", resolveDefine(enableIndexesAttributes));
|
||||
|
||||
indexFactoryBeanBuilder.addPropertyValue("expression",
|
||||
resolveExpression(persistentEntity, persistentProperty, indexAnnotationAttributes));
|
||||
resolveExpression(persistentEntity, persistentProperty, indexedAttributes));
|
||||
|
||||
indexFactoryBeanBuilder.addPropertyValue("from",
|
||||
resolveFrom(persistentEntity, persistentProperty, indexAnnotationAttributes));
|
||||
resolveFrom(persistentEntity, persistentProperty, indexedAttributes));
|
||||
|
||||
indexFactoryBeanBuilder.addPropertyValue("name", indexName);
|
||||
|
||||
/*
|
||||
indexFactoryBeanBuilder.addPropertyValue("override",
|
||||
resolveOverride(persistentEntity, persistentProperty, indexedAttributes));
|
||||
*/
|
||||
|
||||
indexFactoryBeanBuilder.addPropertyValue("type",
|
||||
resolveType(persistentEntity, persistentProperty, indexAnnotationAttributes, indexType).toString());
|
||||
resolveType(persistentEntity, persistentProperty, indexedAttributes, indexType).toString());
|
||||
|
||||
registry.registerBeanDefinition(indexName, indexFactoryBeanBuilder.getBeanDefinition());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a {@link LuceneIndex} for the {@link GemfirePersistentProperty} on the {@link GemfirePersistentEntity}
|
||||
* using the {@link Annotation} meta-data to define the Index.
|
||||
*
|
||||
* @param enableIndexesAttributes {@link AnnotationAttributes} containing meta-data
|
||||
* for the {@link EnableIndexes} annotation.
|
||||
* @param persistentEntity {@link GemfirePersistentEntity} containing the {@link GemfirePersistentProperty}
|
||||
* to be indexed.
|
||||
* @param persistentProperty {@link GemfirePersistentProperty} for which the {@link LuceneIndex} will be created.
|
||||
* @param luceneIndexAnnotation {@link LuceneIndexed} {@link Annotation}.
|
||||
* @param registry {@link BeanDefinitionRegistry} used to register the {@link LuceneIndex} bean definition.
|
||||
* @see java.lang.annotation.Annotation
|
||||
* @see org.springframework.beans.factory.support.BeanDefinitionBuilder
|
||||
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry
|
||||
* @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity
|
||||
* @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty
|
||||
* @see org.springframework.data.gemfire.mapping.annotation.LuceneIndexed
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
protected void registerLuceneIndexBeanDefinition(AnnotationAttributes enableIndexesAttributes,
|
||||
GemfirePersistentEntity<?> persistentEntity, GemfirePersistentProperty persistentProperty,
|
||||
Annotation luceneIndexAnnotation, BeanDefinitionRegistry registry) {
|
||||
|
||||
Optional.ofNullable(luceneIndexAnnotation).ifPresent(localLuceneIndexAnnotation -> {
|
||||
AnnotationAttributes luceneIndexAttributes =
|
||||
AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(localLuceneIndexAnnotation));
|
||||
|
||||
BeanDefinitionBuilder luceneIndexFactoryBeanBuilder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(LuceneIndexFactoryBean.class);
|
||||
|
||||
String indexName = luceneIndexAttributes.getString("name");
|
||||
|
||||
boolean destroy = (luceneIndexAttributes.containsKey("destroy")
|
||||
&& luceneIndexAttributes.getBoolean("destroy"));
|
||||
|
||||
luceneIndexFactoryBeanBuilder.addPropertyValue("destroy", destroy);
|
||||
|
||||
luceneIndexFactoryBeanBuilder.addPropertyValue("fields", persistentProperty.getName());
|
||||
|
||||
luceneIndexFactoryBeanBuilder.addPropertyValue("indexName", indexName);
|
||||
|
||||
luceneIndexFactoryBeanBuilder.addPropertyValue("regionPath", persistentEntity.getRegionName());
|
||||
|
||||
registry.registerBeanDefinition(indexName, luceneIndexFactoryBeanBuilder.getBeanDefinition());
|
||||
});
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
@@ -193,10 +249,10 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
|
||||
/* (non-Javadoc) */
|
||||
@SuppressWarnings("unused")
|
||||
private String resolveExpression(GemfirePersistentEntity<?> persistentEntity,
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes) {
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes) {
|
||||
|
||||
String expression = (indexAnnotationAttributes.containsKey("expression")
|
||||
? indexAnnotationAttributes.getString("expression") : null);
|
||||
String expression = (indexedAttributes.containsKey("expression")
|
||||
? indexedAttributes.getString("expression") : null);
|
||||
|
||||
return (StringUtils.hasText(expression) ? expression : persistentProperty.getName());
|
||||
}
|
||||
@@ -204,21 +260,20 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
|
||||
/* (non-Javadoc) */
|
||||
@SuppressWarnings("unused")
|
||||
private String resolveFrom(GemfirePersistentEntity<?> persistentEntity,
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes) {
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes) {
|
||||
|
||||
String from = (indexAnnotationAttributes.containsKey("from")
|
||||
? indexAnnotationAttributes.getString("from") : null);
|
||||
String from = (indexedAttributes.containsKey("from")
|
||||
? indexedAttributes.getString("from") : null);
|
||||
|
||||
return (StringUtils.hasText(from) ? from : persistentEntity.getRegionName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
private String resolveName(GemfirePersistentEntity<?> persistentEntity,
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes,
|
||||
IndexType indexType) {
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes, IndexType indexType) {
|
||||
|
||||
String indexName = (indexAnnotationAttributes.containsKey("name")
|
||||
? indexAnnotationAttributes.getString("name") : null);
|
||||
String indexName = (indexedAttributes.containsKey("name")
|
||||
? indexedAttributes.getString("name") : null);
|
||||
|
||||
return (StringUtils.hasText(indexName) ? indexName
|
||||
: generateIndexName(persistentEntity, persistentProperty, indexType));
|
||||
@@ -233,15 +288,25 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
|
||||
StringUtils.capitalize(indexType.name().toLowerCase()));
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
/*
|
||||
@SuppressWarnings("unused")
|
||||
private boolean resolveOverride(GemfirePersistentEntity persistentEntity,
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes) {
|
||||
|
||||
return (indexedAttributes.containsKey("override")
|
||||
&& indexedAttributes.getBoolean("override"));
|
||||
}
|
||||
*/
|
||||
|
||||
/* (non-Javadoc) */
|
||||
@SuppressWarnings("unused")
|
||||
private IndexType resolveType(GemfirePersistentEntity<?> persistentEntity,
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexAnnotationAttributes,
|
||||
IndexType indexType) {
|
||||
GemfirePersistentProperty persistentProperty, AnnotationAttributes indexedAttributes, IndexType indexType) {
|
||||
|
||||
IndexType resolvedIndexType = (indexAnnotationAttributes.containsKey("type")
|
||||
? indexAnnotationAttributes.getEnum("type") : null);
|
||||
IndexType resolvedIndexType = (indexedAttributes.containsKey("type")
|
||||
? indexedAttributes.getEnum("type") : null);
|
||||
|
||||
return (resolvedIndexType != null ? resolvedIndexType : indexType);
|
||||
return Optional.ofNullable(resolvedIndexType).orElse(indexType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2016 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.data.gemfire.mapping.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.apache.geode.cache.lucene.LuceneIndex;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentProperty;
|
||||
|
||||
/**
|
||||
* The {@link LuceneIndexed} annotation is used to index a {@link GemfirePersistentEntity}
|
||||
* {@link GemfirePersistentProperty}, creating a GemFire/Geode {@link LuceneIndex}
|
||||
* on a GemFire/Geode {@link org.apache.geode.cache.Region}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.core.annotation.AliasFor
|
||||
* @see org.springframework.data.gemfire.IndexType
|
||||
* @see org.apache.geode.cache.Region
|
||||
* @see org.apache.geode.cache.lucene.LuceneIndex
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Target({ ElementType.FIELD, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
@Documented
|
||||
@SuppressWarnings("unused")
|
||||
public @interface LuceneIndexed {
|
||||
|
||||
/**
|
||||
* {@link String Name} of the {@link LuceneIndex}.
|
||||
*
|
||||
* @return a {@link String} containing the name of the {@link LuceneIndex}
|
||||
*/
|
||||
@AliasFor(attribute = "name")
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* {@link String Name} of the {@link LuceneIndex}.
|
||||
*
|
||||
* @return a {@link String} containing the name of the {@link LuceneIndex}
|
||||
*/
|
||||
@AliasFor(attribute = "value")
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* Determine whether the {@link LuceneIndex} should be destroy when the application shutsdown.
|
||||
*
|
||||
* Default is {@literal false}.
|
||||
*/
|
||||
boolean destroy() default false;
|
||||
|
||||
}
|
||||
@@ -20,19 +20,25 @@ package org.springframework.data.gemfire.config.annotation;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.geode.cache.Cache;
|
||||
import org.apache.geode.cache.RegionAttributes;
|
||||
import org.apache.geode.cache.RegionFactory;
|
||||
import org.apache.geode.cache.RegionShortcut;
|
||||
import org.apache.geode.cache.lucene.LuceneIndex;
|
||||
import org.apache.geode.cache.lucene.LuceneService;
|
||||
import org.apache.geode.cache.query.Index;
|
||||
import org.apache.geode.cache.query.QueryService;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Matchers;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -65,9 +71,7 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (applicationContext != null) {
|
||||
applicationContext.close();
|
||||
}
|
||||
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
@@ -79,6 +83,15 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
assertThat(index.getType()).isEqualTo(indexType.getGemfireIndexType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected void assertLuceneIndex(LuceneIndex index, String name, String regionPath, String... fields) {
|
||||
assertThat(index).isNotNull();
|
||||
assertThat(index.getName()).isEqualTo(name);
|
||||
assertThat(index.getRegionPath()).isEqualTo(regionPath);
|
||||
assertThat(index.getFieldNames()).contains(fields);
|
||||
assertThat(index.getFieldNames()).hasSize(fields.length);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
|
||||
ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(annotatedClasses);
|
||||
@@ -87,17 +100,28 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pesistentEntityIndexesCreatedSuccessfully() {
|
||||
public void persistentEntityIndexesCreatedSuccessfully() {
|
||||
applicationContext = newApplicationContext(IndexedPersistentEntityConfiguration.class);
|
||||
|
||||
assertLuceneIndexes(applicationContext);
|
||||
assertOqlIndexes(applicationContext);
|
||||
}
|
||||
|
||||
private void assertLuceneIndexes(ConfigurableApplicationContext applicationContext) {
|
||||
LuceneIndex luceneIndex = applicationContext.getBean("TitleLuceneIdx", LuceneIndex.class);
|
||||
|
||||
assertLuceneIndex(luceneIndex, "TitleLuceneIdx", "Customers", "title");
|
||||
}
|
||||
|
||||
protected void assertOqlIndexes(ConfigurableApplicationContext applicationContext) {
|
||||
Index customersIdIdx = applicationContext.getBean("CustomersIdKeyIdx", Index.class);
|
||||
|
||||
assertIndex(customersIdIdx, "CustomersIdKeyIdx", "id", "Customers", IndexType.KEY);
|
||||
|
||||
Index customersFirstNameIdx = applicationContext.getBean("CustomersFirstNameFunctionalIdx", Index.class);
|
||||
|
||||
assertIndex(customersFirstNameIdx, "CustomersFirstNameFunctionalIdx", "first_name", "/LoyalCustomers",
|
||||
IndexType.FUNCTIONAL);
|
||||
assertIndex(customersFirstNameIdx, "CustomersFirstNameFunctionalIdx", "first_name",
|
||||
"/LoyalCustomers", IndexType.FUNCTIONAL);
|
||||
|
||||
Index lastNameIdx = applicationContext.getBean("LastNameIdx", Index.class);
|
||||
|
||||
@@ -116,20 +140,18 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
|
||||
@Configuration
|
||||
@SuppressWarnings("unused")
|
||||
static class CacheConfiguration {
|
||||
static class GemFireConfiguration {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("unchecked")
|
||||
Cache gemfireCache() throws Exception {
|
||||
Cache mockCache = mock(Cache.class);
|
||||
return mockQueryService(mockRegionFactory(mock(Cache.class)));
|
||||
}
|
||||
|
||||
Cache mockQueryService(Cache mockCache) throws Exception {
|
||||
QueryService mockQueryService = mock(QueryService.class);
|
||||
RegionFactory mockRegionFactory = mock(RegionFactory.class);
|
||||
|
||||
when(mockCache.getQueryService()).thenReturn(mockQueryService);
|
||||
when(mockCache.createRegionFactory()).thenReturn(mockRegionFactory);
|
||||
when(mockCache.createRegionFactory(any(RegionAttributes.class))).thenReturn(mockRegionFactory);
|
||||
when(mockCache.createRegionFactory(any(RegionShortcut.class))).thenReturn(mockRegionFactory);
|
||||
when(mockCache.createRegionFactory(anyString())).thenReturn(mockRegionFactory);
|
||||
|
||||
when(mockQueryService.createHashIndex(anyString(), anyString(), anyString()))
|
||||
.thenAnswer(new HashIndexAnswer());
|
||||
@@ -142,6 +164,46 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
|
||||
return mockCache;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Cache mockRegionFactory(Cache mockCache) {
|
||||
RegionFactory mockRegionFactory = mock(RegionFactory.class);
|
||||
|
||||
when(mockCache.createRegionFactory()).thenReturn(mockRegionFactory);
|
||||
when(mockCache.createRegionFactory(any(RegionAttributes.class))).thenReturn(mockRegionFactory);
|
||||
when(mockCache.createRegionFactory(any(RegionShortcut.class))).thenReturn(mockRegionFactory);
|
||||
when(mockCache.createRegionFactory(anyString())).thenReturn(mockRegionFactory);
|
||||
|
||||
return mockCache;
|
||||
}
|
||||
|
||||
@Bean
|
||||
LuceneService luceneService() {
|
||||
LuceneService mockLuceneService = mock(LuceneService.class);
|
||||
|
||||
doAnswer(invocation -> {
|
||||
LuceneIndex mockLuceneIndex = mock(LuceneIndex.class);
|
||||
|
||||
String indexName = invocation.getArgumentAt(0, String.class);
|
||||
String regionPath = invocation.getArgumentAt(1, String.class);
|
||||
|
||||
when(mockLuceneIndex.getName()).thenReturn(indexName);
|
||||
when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath);
|
||||
when(mockLuceneIndex.getFieldNames()).thenReturn(resolveFieldNames(invocation));
|
||||
when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex);
|
||||
|
||||
return mockLuceneIndex;
|
||||
}).when(mockLuceneService).createIndex(anyString(), anyString(), Matchers.<String[]>anyVararg());
|
||||
|
||||
return mockLuceneService;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
String[] resolveFieldNames(InvocationOnMock invocation) {
|
||||
String[] fieldNames = new String[invocation.getArguments().length - 2];
|
||||
System.arraycopy(invocation.getArguments(), 2, fieldNames, 0, fieldNames.length);
|
||||
return fieldNames;
|
||||
}
|
||||
}
|
||||
|
||||
static abstract class AbstractIndexAnswer implements Answer<Index> {
|
||||
@@ -195,7 +257,7 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
|
||||
ClientRegionEntity.class, CollocatedPartitionRegionEntity.class, GenericRegionEntity.class,
|
||||
LocalRegionEntity.class, ReplicateRegionEntity.class }))
|
||||
static class IndexedPersistentEntityConfiguration extends CacheConfiguration {
|
||||
static class IndexedPersistentEntityConfiguration extends GemFireConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@@ -203,7 +265,7 @@ public class EnableIndexesConfigurationUnitTests {
|
||||
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
|
||||
ClientRegionEntity.class, CollocatedPartitionRegionEntity.class, GenericRegionEntity.class,
|
||||
LocalRegionEntity.class, ReplicateRegionEntity.class }))
|
||||
static class NoIndexesCreatedForIndexedPersistentEntityConfiguration extends CacheConfiguration {
|
||||
static class NoIndexesCreatedForIndexedPersistentEntityConfiguration extends GemFireConfiguration {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.springframework.data.gemfire.config.annotation.test.entities;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.gemfire.IndexType;
|
||||
import org.springframework.data.gemfire.mapping.annotation.Indexed;
|
||||
import org.springframework.data.gemfire.mapping.annotation.LuceneIndexed;
|
||||
import org.springframework.data.gemfire.mapping.annotation.PartitionRegion;
|
||||
|
||||
/**
|
||||
@@ -35,6 +36,7 @@ import org.springframework.data.gemfire.mapping.annotation.PartitionRegion;
|
||||
@PartitionRegion.FixedPartition(name = "two", numBuckets = 21)
|
||||
}
|
||||
)
|
||||
@SuppressWarnings("unused")
|
||||
public class PartitionRegionEntity {
|
||||
|
||||
@Id
|
||||
@@ -46,4 +48,7 @@ public class PartitionRegionEntity {
|
||||
@Indexed(name = "LastNameIdx")
|
||||
private String lastName;
|
||||
|
||||
@LuceneIndexed("TitleLuceneIdx")
|
||||
private String title;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user