Created
January 11, 2017 18:43
-
-
Save tamast/37424c443a13d73c698fef8a48e65536 to your computer and use it in GitHub Desktop.
Elasticsearch Embedded test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use this dependency in your pom.xml | |
// <dependency> | |
// <groupId>org.elasticsearch</groupId> | |
// <artifactId>elasticsearch</artifactId> | |
// <version>2.4.0</version> | |
// </dependency> | |
@Test | |
public void elasticsearchForTests() throws InterruptedException | |
{ | |
Settings.Builder settingsBuilder = | |
Settings.settingsBuilder(); | |
settingsBuilder.put("node.name", "test"); //Creates files, should be removed after test | |
settingsBuilder.put("path.home", "test"); | |
settingsBuilder.put("path.data", "test"); | |
settingsBuilder.put("http.enabled", true); //ES is available via HTTP | |
Settings settings = settingsBuilder.build(); | |
NodeBuilder.nodeBuilder() | |
.settings(settings) | |
.clusterName("test") | |
.data(true).local(true).node(); | |
Thread.sleep(1000); //have to wait for elastic start | |
//Call it | |
RestTemplate restTemplate = new RestTemplate(); | |
try { | |
Movie movie = restTemplate.getForObject("http://localhost:9200/movies/movie/1", Movie.class); | |
} | |
catch(HttpClientErrorException e) | |
{ | |
Assert.assertTrue(StringUtils.startsWithIgnoreCase(e.getResponseBodyAsString(),"{\"error\":{\"root_cause\":[{\"type\":\"index_not_found_exception\"")); | |
System.out.println("Response from ES: " + e.getResponseBodyAsString()); | |
} | |
} | |
public static class Movie | |
{ | |
private String error; | |
} |
settingsBuilder.put("index.store.type", "memory")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/alv-ch/alv-ch-batches/blob/master/commons/test/src/main/java/ch/alv/batches/commons/test/elasticsearch/EmbeddedElasticsearchNode.java
https://github.com/alv-ch/alv-ch-batches/blob/master/commons/test/src/main/java/ch/alv/batches/commons/test/TestApplicationWithEmbeddedElasticsearchNode.java
https://github.com/alv-ch/alv-ch-batches/blob/master/jobdesk/master/src/test/java/ch/alv/batches/master/to/jobdesk/MasterToJobdeskIntegrationTest.java