From 8f0ae519397d310429249f42df2e84462206a240 Mon Sep 17 00:00:00 2001 From: Ivannikov Kirill Date: Wed, 2 Sep 2015 13:05:26 +0500 Subject: [PATCH] preIndex and postIndex listener test --- .../index/shard/IndexShardTests.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java b/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java index 656d251b1bf..c06480b31d7 100644 --- a/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java +++ b/core/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java @@ -18,7 +18,10 @@ */ package org.elasticsearch.index.shard; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.Term; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.Constants; import org.apache.lucene.util.IOUtils; @@ -37,6 +40,7 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.logging.ESLogger; @@ -48,6 +52,12 @@ import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.ShardLock; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine; +import org.elasticsearch.index.indexing.IndexingOperationListener; +import org.elasticsearch.index.indexing.ShardIndexingService; +import org.elasticsearch.index.mapper.Mapping; +import org.elasticsearch.index.mapper.ParseContext; +import org.elasticsearch.index.mapper.ParsedDocument; +import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettingsService; import org.elasticsearch.index.store.Store; @@ -63,6 +73,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ExecutionException; @@ -584,4 +596,62 @@ public class IndexShardTests extends ESSingleNodeTestCase { assertTrue(xContent.contains(expectedSubSequence)); } + private ParsedDocument testParsedDocument(String uid, String id, String type, String routing, long timestamp, long ttl, ParseContext.Document document, BytesReference source, Mapping mappingUpdate) { + Field uidField = new Field("_uid", uid, UidFieldMapper.Defaults.FIELD_TYPE); + Field versionField = new NumericDocValuesField("_version", 0); + document.add(uidField); + document.add(versionField); + return new ParsedDocument(uidField, versionField, id, type, routing, timestamp, ttl, Arrays.asList(document), source, mappingUpdate); + } + + public void testPreIndex() throws IOException { + createIndex("testpreindex"); + ensureGreen(); + IndicesService indicesService = getInstanceFromNode(IndicesService.class); + IndexService test = indicesService.indexService("testpreindex"); + IndexShard shard = test.shard(0); + ShardIndexingService shardIndexingService = shard.indexingService(); + + final HashMap listenerInfo = new HashMap<>(); + listenerInfo.put("preIndexCalled", false); + + shardIndexingService.addListener(new IndexingOperationListener() { + @Override + public Engine.Index preIndex(Engine.Index index) { + listenerInfo.put("preIndexCalled", true); + return super.preIndex(index); + } + }); + + ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, new ParseContext.Document(), new BytesArray(new byte[]{1}), null); + Engine.Index index = new Engine.Index(new Term("_uid", "1"), doc); + shard.index(index); + assertTrue(listenerInfo.get("preIndexCalled")); + } + + public void testPostIndex() throws IOException { + createIndex("testpostindex"); + ensureGreen(); + IndicesService indicesService = getInstanceFromNode(IndicesService.class); + IndexService test = indicesService.indexService("testpostindex"); + IndexShard shard = test.shard(0); + ShardIndexingService shardIndexingService = shard.indexingService(); + + final HashMap listenerInfo = new HashMap<>(); + listenerInfo.put("postIndexCalled", false); + + shardIndexingService.addListener(new IndexingOperationListener() { + @Override + public void postIndex(Engine.Index index) { + listenerInfo.put("postIndexCalled", true); + super.postIndex(index); + } + }); + + ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, new ParseContext.Document(), new BytesArray(new byte[]{1}), null); + Engine.Index index = new Engine.Index(new Term("_uid", "1"), doc); + shard.index(index); + assertTrue(listenerInfo.get("postIndexCalled")); + } + }