How to write a test for Elasticsearch custom plugin?

前端 未结 1 1674
北荒
北荒 2021-02-02 10:23

I create custom Elasticsearch plugin. Now I want to write a test for this plugin. My expectations were - that I could run embedded Elasticsearch

相关标签:
1条回答
  • 2021-02-02 10:34

    To write tests for you plugin you can use Elasticsearch Cluster Runner. For reference check how MinHash Plugin wrote test.

    UPDATE:

    I've changed CustomParserPluginTest class to use Elasticsearch Cluster Runner:

    import static org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs;
    import java.util.Map;
    import junit.framework.TestCase;
    import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.common.bytes.BytesArray;
    import org.elasticsearch.common.settings.ImmutableSettings;
    import org.elasticsearch.common.settings.ImmutableSettings.Builder;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.index.get.GetField;
    import org.junit.Assert;
    import org.elasticsearch.action.search.SearchResponse;
    import static org.hamcrest.core.Is.is;
    
    public class CustomParserPluginTest extends TestCase {
    
        private ElasticsearchClusterRunner runner;
    
        @Override
        protected void setUp() throws Exception {
            // create runner instance
            runner = new ElasticsearchClusterRunner();
            // create ES nodes
            runner.onBuild(new ElasticsearchClusterRunner.Builder() {
                @Override
                public void build(final int number, final Builder settingsBuilder) {
                }
            }).build(newConfigs().ramIndexStore().numOfNode(1));
    
            // wait for yellow status
            runner.ensureYellow();
        }
    
        @Override
        protected void tearDown() throws Exception {
            // close runner
            runner.close();
            // delete all files
            runner.clean();
        }
    
        public void test_jsonParsing() throws Exception {
            final String index = "test_index";
    
            runner.createIndex(index, ImmutableSettings.builder().build());
            runner.ensureYellow(index);
    
            final SearchResponse test = runner.client().prepareSearch(index).setSource(addQuery()).execute().actionGet();
        }
    
        private String addQuery() {
            return "{\"match_all\":{\"boost\":1.2}}";
        }
    }
    

    I've created es-plugin.properties(pluginrootdirectory\src\main\resources) file with following content which will force elasticsearch instance to load plugin:

    plugin=CustomQueryParserPlugin
    

    When you will run the this test you will see in the output that the newly created insance of elasticsearch loaded the plugin.

    [2015-04-29 19:22:10,783][INFO ][org.elasticsearch.node ] [Node 1] version[1.5 .0], pid[34360], build[5448160/2015-03-23T14:30:58Z] [2015-04-29 19:22:10,784][INFO ][org.elasticsearch.node ] [Node 1] initializin g ... [2015-04-29 19:22:10,795][INFO ][org.elasticsearch.plugins] [Node 1] loaded [custom_query], sites [] [2015-04-29 19:22:13,342][INFO ][org.elasticsearch.node ] [Node 1] initialized

    [2015-04-29 19:22:13,342][INFO ][org.elasticsearch.node ] [Node 1] starting .. .

    Hope this helps.

    0 讨论(0)
提交回复
热议问题