Elasticsearch provides a jar file, which can be added to any java IDE and can be used to test the code which is related to Elasticsearch. A range of tests can be performed by using the framework provided by Elasticsearch. In this chapter, we will discuss these tests in detail −
- Unit testing
- Integration testing
- Randomized testing
Prerequisites
To start with testing, you need to add the Elasticsearch testing dependency to your program. You can use maven for this purpose and can add the following in pom.xml.
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.1.0</version> </dependency>
EsSetup has been initialized to start and stop Elasticsearch node and also to create indices.
EsSetup esSetup = new EsSetup();
esSetup.execute() function with createIndex will create the indices, you need to specify the settings, type and data.
Unit Testing
Unit test is carried out by using JUnit and Elasticsearch test framework. Node and indices can be created using Elasticsearch classes and in test method can be used to perform the testing. ESTestCase and ESTokenStreamTestCase classes are used for this testing.
Integration Testing
Integration testing uses multiple nodes in a cluster. ESIntegTestCase class is used for this testing. There are various methods which make the job of preparing a test case easier.
S.No | Method & Description |
---|---|
1 |
refresh() All the indices in a cluster are refreshed |
2 |
ensureGreen() Ensures a green health cluster state |
3 |
ensureYellow() Ensures a yellow health cluster state |
4 |
createIndex(name) Create index with the name passed to this method |
5 |
flush() All indices in cluster are flushed |
6 |
flushAndRefresh() flush() and refresh() |
7 |
indexExists(name) Verifies the existence of specified index |
8 |
clusterService() Returns the cluster service java class |
9 |
cluster() Returns the test cluster class |
Test Cluster Methods
S.No | Method & Description |
---|---|
1 |
ensureAtLeastNumNodes(n) Ensures minimum number of nodes up in a cluster is more than or equal to specified number. |
2 |
ensureAtMostNumNodes(n) Ensures maximum number of nodes up in a cluster is less than or equal to specified number. |
3 |
stopRandomNode() To stop a random node in a cluster |
4 |
stopCurrentMasterNode() To stop the master node |
5 |
stopRandomNonMaster() To stop a random node in a cluster, which is not a master node. |
6 |
buildNode() Create a new node |
7 |
startNode(settings) Start a new node |
8 |
nodeSettings() Override this method for changing node settings. |
Accessing Clients
A client is used to access different nodes in a cluster and carry out some action. ESIntegTestCase.client() method is used for getting a random client. Elasticsearch offers other methods also to access client and those methods can be accessed using ESIntegTestCase.internalCluster() method.
S.No | Method & Description |
---|---|
1 |
iterator() This helps you to access all the available clients. |
2 |
masterClient() This returns a client, which is communicating with master node. |
3 |
nonMasterClient() This returns a client, which is not communicating with master node. |
4 |
clientNodeClient() This returns a client currently up on client node. |
Randomized Testing
This testing is used to test the user’s code with every possible data, so that there will be no failure in future with any type of data. Random data is the best option to carry out this testing.
Generating Random Data
In this testing, the Random class is instantiated by the instance provided by RandomizedTest and offers many methods for getting different types of data.
Method | Return value |
---|---|
getRandom() | Instance of random class |
randomBoolean() | Random boolean |
randomByte() | Random byte |
randomShort() | Random short |
randomInt() | Random integer |
randomLong() | Random long |
randomFloat() | Random float |
randomDouble() | Random double |
randomLocale() | Random locale |
randomTimeZone() | Random time zone |
randomFrom() | Random element from array |
Assertions
ElasticsearchAssertions and ElasticsearchGeoAssertions classes contain assertions, which are used for performing some common checks at the time of testing. For example, observe the code given here −
SearchResponse seearchResponse = client().prepareSearch(); assertHitCount(searchResponse, 6); assertFirstHit(searchResponse, hasId("6")); assertSearchHits(searchResponse, "1", "2", "3", "4",”5”,”6”);