Elasticsearch – Testing

Elasticsearch - Testing

Elasticsearch Testing 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.NoMethod & Description
1refresh()All the indices in a cluster are refreshed
2ensureGreen()Ensures a green health cluster state
3ensureYellow()Ensures a yellow health cluster state
4createIndex(name)Create index with the name passed to this method
5flush()All indices in cluster are flushed
6flushAndRefresh()flush() and refresh()
7indexExists(name)Verifies the existence of specified index
8clusterService()Returns the cluster service java class
9cluster()Returns the test cluster class

Test Cluster Methods

S.NoMethod & Description
1ensureAtLeastNumNodes(n)Ensures minimum number of nodes up in a cluster is more than or equal to specified number.
2ensureAtMostNumNodes(n)Ensures maximum number of nodes up in a cluster is less than or equal to specified number.
3stopRandomNode()To stop a random node in a cluster
4stopCurrentMasterNode()To stop the master node
5stopRandomNonMaster()To stop a random node in a cluster, which is not a master node.
6buildNode()Create a new node
7startNode(settings)Start a new node
8nodeSettings()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.NoMethod & Description
1iterator()This helps you to access all the available clients.
2masterClient()This returns a client, which is communicating with master node.
3nonMasterClient()This returns a client, which is not communicating with master node.
4clientNodeClient()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.

MethodReturn 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”);

Next Topic – Click Here

This Post Has One Comment

Leave a Reply