Skip to content

Elasticsearch annotations for unit tests and Java API samples

Notifications You must be signed in to change notification settings

diuis/elasticsearch-test

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 

Repository files navigation

elasticsearch-test, a framework that makes elasticsearch unit testing a breeze.

Here you will find simple Java annotations that simplifies unit testing with Elasticsearch

Versions

elasticsearch-test ElasticSearch
master (0.0.5) 0.20.0.RC1
0.0.4 0.19.8
0.0.3 0.19.7
0.0.2 0.19.4
0.0.1 0.19.4

How To

Maven dependency

Add a new dependency in your Maven POM file:

<dependency>
  <groupId>com.github.tlrx</groupId>
  <artifactId>elasticsearch-test</artifactId>
  <version>0.0.5</version>
</dependency>

JUnit Runner

Define the JUnit Runner to use in your test classes with the annotation @RunWith(ElasticsearchRunner.class) :

package com.github.tlrx.elasticsearch.samples.core;

@RunWith(ElasticsearchRunner.class)
public class VersionTest {
	...
}

Annotations

The library provides the following annotations:

Annotation
@ElasticsearchNode Instantiate an elasticsearch Node
@ElasticsearchClient Instantiate an elasticsearch Client
@ElasticsearchAdminClient Instantiate an elasticsearch AdminClient
@ElasticsearchTransportClient Instantiate an elasticsearch TransportClient
@ElasticsearchIndexes Used to create multiple index
@ElasticsearchIndex Creates an index
@ElasticsearchMapping Defines a mapping for an index and a document type
@ElasticsearchMappingField Defines field properties in a mapping
@ElasticsearchSetting Defines settings (at node or index level)
@ElasticsearchBulkRequest Used to import data
@ElasticsearchMappingMultiField Used to define a multi_field in the mapping properties
@ElasticsearchAnalysis Used to define analysis settings of an ElasticsearchIndex
@ElasticsearchAnalyzer Used to define an analyzer
@ElasticsearchFilter Used to define an analysis filter

@ElasticsearchNode

Used to instantiate an elasticsearch Node in a unit test class.

Simple node has default name “elasticsearch-test-node” and is part of default cluster name “elasticsearch-test-cluster”. Node is local and can hold data.

package com.github.tlrx.elasticsearch.samples.core;

@RunWith(ElasticsearchRunner.class)
public class VersionTest {

        @ElasticsearchNode
	Node node;
	
        @Test
	public void test(){
                // Use of node
	}
}

Example with custom cluster and node names, and local set to true and data set to false:

@ElasticsearchNode(name = "node3", clusterName = "fourth-cluster-name", local = true, data = false)
Node node3;

Example with custom settings:

@ElasticsearchNode(name = "node0", settings = {
	@ElasticsearchSetting(name = "http.enabled", value = "false"),
	@ElasticsearchSetting(name = "node.zone", value = "zone_one") })
Node node;

@ElasticsearchClient

Used to instantiate an elasticsearch Client from a Node. The nodeName parameter of the annotation is used to retrieve the node from which the client will be instantiated. If no nodeName is defined, a default node will be instantiated.

Example with default node:

@ElasticsearchClient
Client client;

Example with a predefined node:

@ElasticsearchNode(name = "node1")
Node node1;

@ElasticsearchClient(nodeName = "node1")
Client client1;

@ElasticsearchAdminClient

Same as ElasticsearchClient but instantiates an AdminClient.

@ElasticsearchAdminClient
AdminClient adminClient0;

@ElasticsearchNode(name = "node1")
Node node1;

@ElasticsearchAdminClient(nodeName = "node1")
AdminClient adminClient1;

@ElasticsearchTransportClient

Used to instantiate an elasticsearch TransportClient. By default the TransportClient will try to join localhost:9200.

Connect to a remote node with a TransportClient:

@ElasticsearchTransportClient(local = false, clusterName = "external",
					hostnames = {"host1", "host2"},
					ports= {9300, 9300})
	Client client;

@ElasticsearchIndex, @ElasticsearchIndexes

Used to creates one or many index before a test method. A node must be instantiated before using this annotation.

This example will create an index with default name test:

@ElasticsearchAdminClient
AdminClient adminClient;

@Test
@ElasticsearchIndex
public void test(){...}

This example will create an index “people”:

@Test
@ElasticsearchIndex(indexName = "people")
public void test(){
        ...
}

This example will create 2 indexs with settings:

@Test
@ElasticsearchIndexes(indexes = {
			@ElasticsearchIndex(indexName = "library", 
					forceCreate = true,
					settings = {
						@ElasticsearchSetting(name = "number_of_shards", value = "2"),
						@ElasticsearchSetting(name = "number_of_replicas", value = "1") }),
				@ElasticsearchIndex(indexName = "people") })
public void test(){
        ...
}

This example will create an index with settings:

@Test
@Test
@ElasticsearchIndex(indexName = "documents", settingsFile = "path/to/settings.json")
public void test() {
        ...
}

Load data from JSON file with @ElasticsearchBulkRequest

@Test
@ElasticsearchIndex(indexName = "documents", forceCreate = true)
@ElasticsearchBulkRequest(dataFile = "com/tlrx/elasticsearch/test/annotations/documents/bulk1.json")
public void test() {
   // Data from JSON file are indexed
}

@ElasticsearchMapping, @ElasticsearchMappingField and @ElasticsearchSetting

Used to define the mappings and settings of an index

This example will create 2 indexs, “people” and “library”, with a mapping for document type “book”:

@ElasticsearchNode
Node node;

@Test
@ElasticsearchIndexes(indexes = {
@ElasticsearchIndex(indexName = “people”, settings = {
@ElasticsearchSetting(name = “number_of_shards”, value = “2”),
@ElasticsearchSetting(name = “number_of_replicas”, value = “1”)
}),
@ElasticsearchIndex(indexName = “library”,
mappings = {
@ElasticsearchMapping(typeName = “book”,
properties = {
@ElasticsearchMappingField(name = “title”, store = Store.Yes, type = Types.String),
@ElasticsearchMappingField(name = “author”, store = Store.Yes, type = Types.String)
})
})
})
public void test(){

}

You can also look at the unit tests for some inspiration

About

Elasticsearch annotations for unit tests and Java API samples

Resources

Stars

Watchers

Forks

Packages

No packages published