Skip to content

Instantly share code, notes, and snippets.

@umit
Created August 31, 2018 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umit/94da39468d725821e3483183f61edfde to your computer and use it in GitHub Desktop.
Save umit/94da39468d725821e3483183f61edfde to your computer and use it in GitHub Desktop.
Elasticsearch Test Containers
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchConfigTest {
@ClassRule
public static final GenericContainer elasticsearchContainer = new FixedHostPortGenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.1.2")
.withFixedExposedPort(9200, 9200)
.withFixedExposedPort(9300, 9300)
.withExposedPorts(9200)
.waitingFor(Wait.forHttp("/")) // Wait until elastic start
.withEnv("xpack.security.enabled", "false")
.withEnv("http.host", "0.0.0.0")
.withEnv("transport.host", "0.0.0.0")
.withEnv("xpack.security.enabled", "false")
.withEnv("cluster.name", "trendyoldev");
@Autowired
private Client elasticsearchClient;
@Test
public void it_should_put_index() {
//given
String index = "products";
String type = "product";
String source = "{\"key\":\"value\"}";
//when
IndexResponse indexResponse = elasticsearchClient.prepareIndex(index, type, "1").setSource(source).execute().actionGet();
//then
assertThat(indexResponse.getId()).isEqualTo("1");
assertThat(indexResponse.getIndex()).isEqualTo(index);
assertThat(indexResponse.getType()).isEqualTo(type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment