Fix bugs in mock Index logic regarding (mock) Region references.

This commit is contained in:
John Blum
2021-09-10 17:26:14 -07:00
parent 70cf67cf45
commit 20e401e94f
2 changed files with 52 additions and 2 deletions

View File

@@ -163,6 +163,7 @@ import org.apache.geode.pdx.PdxSerializer;
import org.apache.lucene.analysis.Analyzer;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.data.gemfire.IndexType;
import org.springframework.data.gemfire.RegionShortcutWrapper;
import org.springframework.data.gemfire.client.ClientRegionShortcutWrapper;
@@ -2318,13 +2319,34 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
when(mockIndex.getFromClause()).thenReturn(fromClause);
when(mockIndex.getIndexedExpression()).thenReturn(expression);
when(mockIndex.getProjectionAttributes()).thenReturn(expression);
when(mockIndex.getRegion()).thenAnswer(invocation -> regions.get(fromClause));
when(mockIndex.getStatistics()).thenReturn(mockIndexStaticts);
when(mockIndex.getType()).thenReturn(indexType.getGemfireIndexType());
doAnswer(invocation -> {
String regionName = fromClauseToRegionPath(fromClause);
return regions.get(regionName);
}).when(mockIndex).getRegion();
return mockIndex;
}
private static String fromClauseToRegionPath(String fromClause) {
String regionName = String.valueOf(fromClause);
int indexOfDot = regionName.indexOf(".");
int indexOfSpace = regionName.indexOf(" ");
regionName = regionName.startsWith(Region.SEPARATOR) ? regionName : GemfireUtils.toRegionPath(regionName);
regionName = indexOfSpace > -1 ? regionName.substring(0, indexOfSpace) : regionName;
regionName = indexOfDot > -1 ? regionName.substring(0, indexOfDot) : regionName;
return regionName;
}
private static IndexStatistics mockIndexStatistics(String name) {
return mock(IndexStatistics.class, mockObjectIdentifier(name));
}

View File

@@ -40,10 +40,13 @@ import org.apache.geode.cache.lucene.LuceneIndex;
import org.apache.geode.cache.lucene.LuceneIndexFactory;
import org.apache.geode.cache.lucene.LuceneSerializer;
import org.apache.geode.cache.lucene.LuceneService;
import org.apache.geode.cache.query.Index;
import org.apache.geode.cache.server.ClientSubscriptionConfig;
import org.apache.lucene.analysis.Analyzer;
import org.springframework.data.gemfire.IndexType;
/**
* Unit Tests for {@link GemFireMockObjectsSupport}.
*
@@ -139,7 +142,32 @@ public class GemFireMockObjectsSupportUnitTests {
}
@Test
public void mockLuceneFunctionality() {
public void mockIndexIsCorrect() {
RegionService mockRegionService = mock(RegionService.class);
RegionAttributes<?, ?> mockRegionAttributes = mock(RegionAttributes.class);
Region<?, ?> mockRegion =
GemFireMockObjectsSupport.mockRegion(mockRegionService, "MockIndexRegion", mockRegionAttributes);
assertThat(mockRegion).isNotNull();
Index mockIndex = GemFireMockObjectsSupport
.mockIndex("MockIndex", "*", "/MockIndexRegion alias", IndexType.FUNCTIONAL);
assertThat(mockIndex).isNotNull();
assertThat(mockIndex.getName()).isEqualTo("MockIndex");
assertThat(mockIndex.getRegion()).isNotNull();
assertThat(mockIndex.getRegion()).isEqualTo(mockRegion);
assertThat(mockIndex.getRegion().getName()).isEqualTo("MockIndexRegion");
assertThat(mockIndex.getFromClause()).isEqualTo("/MockIndexRegion alias");
assertThat(mockIndex.getIndexedExpression()).isEqualTo("*");
assertThat(mockIndex.getType()).isEqualTo(IndexType.FUNCTIONAL.getGemfireIndexType());
}
@Test
public void mockLuceneFunctionalityIsCorrect() {
Cache mockCache = mock(Cache.class);