I am doing automated API testing with rest assured. The back-end has REST calls but everything is stored in elastic search. So far only POST and GET methods are implemented and I am not able to use DELETE as a method. I know that there is a way to delete the elastic search base through chrome extension Sense, but I am looking for a way to delete elastic search base inside the automated API tests in order to make my tests independent. For example in @BeforeTest load the elastic search base with data, execute the @Test after that and in the @After I want to delete the base and return it to it's default empty state in order to run the next test from scratch in an empty base. Any help would be appreciated.
0
votes
1 Answers
0
votes
You can check the soupmix/elasticsearch client automated test cases. We are using php to create a client and on test setup we create a index and populate the datas to this index.
protected function setUp()
{
$config =[
'db_name' => 'test',
'hosts' => ['127.0.0.1:9200'],
];
$client = ClientBuilder::create()->setHosts($config['hosts'])->build();
$this->client = new ElasticSearch($config, $client);
}
For Java, you can use following code block on your @BeforeTest part:
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http"
).build();
After that, we run some test cases as you can see on github page. You can do this in your java code. At the end of the test we drop the index again.
protected function tearDown()
{
$this->client->drop('test');
}
For Java, you need to do this steps with below code snippet in your @After part:
Response deleteIndex = restClient.performRequest(
"DELETE",
"/test",
Collections.<String, String>emptyMap());
restClient.close();
Like Java example, drop method, is in our php example, using directly HTTP DELETE method.
public function drop($collection)
{
$params = ['index' => $this->index];
try {
$this->conn->indices()->delete($params);
} catch (\Exception $e) {
// This ignore the error
return false;
}
return true;
}
We are using one index especially for all of test cases to be able to easily delete all the data at the end of the test.