From 7f19de653ead4d8c56425bfa170777dbb48d890f Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 19 Jul 2017 14:43:19 -0700 Subject: [PATCH] SGF-644 - Adapt to additional changes in the Pivotal GemFire Index and Lucene APIs based on the Apache Geode 1.2.0 baseline. --- .../EnableIndexingConfigurationUnitTests.java | 83 ++++++++++---- .../config/xml/LuceneNamespaceUnitTests.java | 105 ++++++++++-------- 2 files changed, 116 insertions(+), 72 deletions(-) diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexingConfigurationUnitTests.java b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexingConfigurationUnitTests.java index 866e1159..3b258f54 100644 --- a/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexingConfigurationUnitTests.java +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableIndexingConfigurationUnitTests.java @@ -25,6 +25,10 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -35,15 +39,16 @@ 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.LuceneIndexFactory; import org.apache.geode.cache.lucene.LuceneService; import org.apache.geode.cache.query.Index; import org.apache.geode.cache.query.IndexExistsException; import org.apache.geode.cache.query.IndexNameConflictException; import org.apache.geode.cache.query.QueryService; import org.apache.geode.internal.concurrent.ConcurrentHashSet; +import org.apache.lucene.analysis.Analyzer; 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; @@ -83,6 +88,23 @@ public class EnableIndexingConfigurationUnitTests { indexes.clear(); } + private static String[] asArray(List list) { + return list.toArray(new String[list.size()]); + } + + private static String[] toStringArray(Object[] array) { + + String[] stringArray = new String[array.length]; + + int index = 0; + + for (Object element : array) { + stringArray[index++] = String.valueOf(element); + } + + return stringArray; + } + /* (non-Javadoc) */ private static Index findIndexByName(String indexName) { @@ -100,8 +122,8 @@ public class EnableIndexingConfigurationUnitTests { assertThat(index).isNotNull(); assertThat(index.getName()).isEqualTo(name); assertThat(index.getRegionPath()).isEqualTo(regionPath); - assertThat(index.getFieldNames()).contains(fields); assertThat(index.getFieldNames()).hasSize(fields.length); + assertThat(index.getFieldNames()).contains(fields); } /* (non-Javadoc) */ @@ -242,35 +264,48 @@ public class EnableIndexingConfigurationUnitTests { } @Bean + @SuppressWarnings("unchecked") LuceneService luceneService() { - LuceneService mockLuceneService = mock(LuceneService.class); - doAnswer(invocation -> { + when(mockLuceneService.createIndexFactory()).thenAnswer(invocation -> { + LuceneIndexFactory mockLuceneIndexFactory = mock(LuceneIndexFactory.class); - LuceneIndex mockLuceneIndex = mock(LuceneIndex.class); + List fieldNames = new ArrayList<>(); - String indexName = invocation.getArgument(0); - String regionPath = invocation.getArgument(1); + when(mockLuceneIndexFactory.setFields((String[]) any())).thenAnswer(setFieldsInvocation -> { + Collections.addAll(fieldNames, toStringArray(setFieldsInvocation.getArguments())); + return mockLuceneIndexFactory; + }); - 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); + Map fieldAnalyzers = new HashMap<>(); - return mockLuceneIndex; + when(mockLuceneIndexFactory.setFields(any(Map.class))).thenAnswer(setFieldsInvocation -> { + fieldAnalyzers.putAll(setFieldsInvocation.getArgument(0)); + return mockLuceneIndexFactory; + }); - }).when(mockLuceneService).createIndex(anyString(), anyString(), Matchers.anyVararg()); + doAnswer(createInvocation -> { + LuceneIndex mockLuceneIndex = mock(LuceneIndex.class); + + String indexName = createInvocation.getArgument(0); + String regionPath = createInvocation.getArgument(1); + + when(mockLuceneIndex.getName()).thenReturn(indexName); + when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath); + when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(fieldAnalyzers); + when(mockLuceneIndex.getFieldNames()).thenReturn(asArray(fieldNames)); + + when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex); + + return mockLuceneIndex; + }).when(mockLuceneIndexFactory).create(anyString(), anyString()); + + return mockLuceneIndexFactory; + }); 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 { @@ -302,7 +337,7 @@ public class EnableIndexingConfigurationUnitTests { abstract IndexType getType(); private void validateIndexDefinition(String name, String expression, String fromClause, IndexType type) - throws IndexExistsException { + throws IndexExistsException { for (Index index : indexes) { if (index.getIndexedExpression().equalsIgnoreCase(expression) @@ -311,7 +346,7 @@ public class EnableIndexingConfigurationUnitTests { throw new IndexExistsException(String.format( "Index [%1$s] has the same definition as existing Index [%2$s]", - name, index.getName())); + name, index.getName())); } } @@ -374,7 +409,7 @@ public class EnableIndexingConfigurationUnitTests { ClientRegionEntity.class, CollocatedPartitionRegionEntity.class, GenericRegionEntity.class, LocalRegionEntity.class, ReplicateRegionEntity.class })) static class IndexAnnotatedEntityPropertyIsIgnoredWithExistingIndexHavingSameDefinitionConfiguration - extends GemFireConfiguration { + extends GemFireConfiguration { @Bean @SuppressWarnings("unused") @@ -398,7 +433,7 @@ public class EnableIndexingConfigurationUnitTests { ClientRegionEntity.class, CollocatedPartitionRegionEntity.class, GenericRegionEntity.class, LocalRegionEntity.class, ReplicateRegionEntity.class })) static class IndexAnnotatedEntityPropertyIsIgnoredWithExistingIndexHavingSameNameConfiguration - extends GemFireConfiguration { + extends GemFireConfiguration { @Bean @SuppressWarnings("unused") diff --git a/src/test/java/org/springframework/data/gemfire/config/xml/LuceneNamespaceUnitTests.java b/src/test/java/org/springframework/data/gemfire/config/xml/LuceneNamespaceUnitTests.java index df7637f3..b952c64e 100644 --- a/src/test/java/org/springframework/data/gemfire/config/xml/LuceneNamespaceUnitTests.java +++ b/src/test/java/org/springframework/data/gemfire/config/xml/LuceneNamespaceUnitTests.java @@ -21,7 +21,6 @@ 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.Matchers.isA; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -29,17 +28,20 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import org.apache.geode.cache.GemFireCache; import org.apache.geode.cache.lucene.LuceneIndex; +import org.apache.geode.cache.lucene.LuceneIndexFactory; import org.apache.geode.cache.lucene.LuceneService; import org.apache.lucene.analysis.Analyzer; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.beans.BeansException; import org.springframework.beans.factory.FactoryBean; @@ -57,6 +59,10 @@ import org.springframework.test.context.junit4.SpringRunner; * @author John Blum * @see org.junit.Test * @see org.junit.runner.RunWith + * @see org.apache.geode.cache.lucene.LuceneIndex + * @see org.apache.geode.cache.lucene.LuceneService + * @see org.springframework.data.gemfire.config.xml.LuceneIndexParser + * @see org.springframework.data.gemfire.config.xml.LuceneServiceParser * @see org.springframework.test.context.ContextConfiguration * @see org.springframework.test.context.junit4.SpringRunner * @since 1.1.0 @@ -87,6 +93,21 @@ public class LuceneNamespaceUnitTests { @Qualifier("IndexFour") private LuceneIndex luceneIndexFour; + private static String[] asArray(List list) { + return list.toArray(new String[list.size()]); + } + + private static String[] toStringArray(Object[] array) { + String[] stringArray = new String[array.length]; + int index = 0; + + for (Object element : array) { + stringArray[index++] = String.valueOf(element); + } + + return stringArray; + } + protected void assertLuceneIndex(LuceneIndex index, String name, String regionPath) { assertThat(index).isNotNull(); assertThat(index.getName()).isEqualTo(name); @@ -94,12 +115,12 @@ public class LuceneNamespaceUnitTests { } protected void assertLuceneIndexWithFieldAnalyzers(LuceneIndex index, String name, String regionPath, - String... keys) { + String... keys) { assertLuceneIndex(index, name, regionPath); - assertThat(index.getFieldNames()).isEmpty(); assertThat(index.getFieldAnalyzers()).hasSize(keys.length); assertThat(index.getFieldAnalyzers()).containsKeys(keys); + assertThat(index.getFieldNames()).isEmpty(); } protected void assertLuceneIndexWithFields(LuceneIndex index, String name, String regionPath, String... fieldNames) { @@ -109,17 +130,10 @@ public class LuceneNamespaceUnitTests { } @Test - @SuppressWarnings({ "deprecation", "unchecked" }) public void luceneServiceConfigurationAndInteractionsAreCorrect() { assertThat(this.luceneService).isNotNull(); - - verify(this.luceneService, times(1)) - .createIndex(eq("IndexOne"), eq("/Example"), eq("fieldOne"), eq("fieldTwo")); - - verify(this.luceneService, times(1)) - .createIndex(eq("IndexTwo"), eq("/AnotherExample"), isA(Map.class)); - - verify(this.luceneService, never()).destroyIndex(any(LuceneIndex.class)); + verify(this.luceneService, times(4)).createIndexFactory(); + verify(this.luceneService, never()).destroyIndex(anyString(), anyString()); } @Test @@ -157,7 +171,6 @@ public class LuceneNamespaceUnitTests { public static class MockLuceneServiceFactoryBean implements FactoryBean, InitializingBean { - @SuppressWarnings("all") private GemFireCache gemfireCache; private LuceneService luceneService; @@ -173,59 +186,55 @@ public class LuceneNamespaceUnitTests { return Optional.ofNullable(this.luceneService).orElseGet(() -> { this.luceneService = mock(LuceneService.class); - Answer mockLuceneIndex = newMockLuceneIndex(this.luceneService); + when(this.luceneService.createIndexFactory()).thenAnswer(invocation -> { + LuceneIndexFactory mockLuceneIndexFactory = mock(LuceneIndexFactory.class); - doAnswer(mockLuceneIndex).when(this.luceneService) - .createIndex(anyString(), anyString(), (String[]) any()); + List fieldNames = new ArrayList<>(); - doAnswer(mockLuceneIndex).when(this.luceneService) - .createIndex(anyString(), anyString(), isA(Map.class)); + when(mockLuceneIndexFactory.setFields((String[]) any())).thenAnswer(setFieldsInvocation -> { + Collections.addAll(fieldNames, toStringArray(setFieldsInvocation.getArguments())); + return mockLuceneIndexFactory; + }); + + Map fieldAnalyzers = new HashMap<>(); + + when(mockLuceneIndexFactory.setFields(any(Map.class))).thenAnswer(setFieldsInvocation -> { + fieldAnalyzers.putAll(setFieldsInvocation.getArgument(0)); + return mockLuceneIndexFactory; + }); + + Answer mockLuceneIndex = + mockLuceneIndex(this.luceneService, fieldAnalyzers, fieldNames); + + doAnswer(mockLuceneIndex).when(mockLuceneIndexFactory).create(anyString(), anyString()); + + return mockLuceneIndexFactory; + }); return this.luceneService; }); } @SuppressWarnings("unchecked") - private Answer newMockLuceneIndex(LuceneService mockLuceneService) { - return (invocationOnMock) -> { - String indexName = invocationOnMock.getArgument(0); - String regionPath = invocationOnMock.getArgument(1); + private Answer mockLuceneIndex(LuceneService mockLuceneService, + Map fieldAnalyzers, List fieldNames) { + + return invocation -> { + String indexName = invocation.getArgument(0); + String regionPath = invocation.getArgument(1); LuceneIndex mockLuceneIndex = mock(LuceneIndex.class, indexName); when(mockLuceneIndex.getName()).thenReturn(indexName); when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath); - - if (invocationOnMock.getArguments().length > 2) { - Object fields = invocationOnMock.getArgument(2); - - if (fields instanceof Map) { - when(mockLuceneIndex.getFieldAnalyzers()).thenReturn((Map) fields); - when(mockLuceneIndex.getFieldNames()).thenReturn(EMPTY_STRING_ARRAY); - } - else { - when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(Collections.emptyMap()); - when(mockLuceneIndex.getFieldNames()).thenReturn(extractFields(invocationOnMock)); - } - } - + when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(fieldAnalyzers); + when(mockLuceneIndex.getFieldNames()).thenReturn(asArray(fieldNames)); when(mockLuceneService.getIndex(eq(indexName), eq(regionPath))).thenReturn(mockLuceneIndex); return mockLuceneIndex; }; } - private String[] asStringArray(Object fields) { - return (fields instanceof String[] ? (String[]) fields : String.valueOf(fields).split(", ")); - } - - @SuppressWarnings("all") - private String[] extractFields(InvocationOnMock invocationOnMock) { - String[] fields = new String[invocationOnMock.getArguments().length - 2]; - System.arraycopy(invocationOnMock.getArguments(), 2, fields, 0, fields.length); - return fields; - } - @Override public Class getObjectType() { return Optional.ofNullable(this.luceneService).>map(LuceneService::getClass)