How to use ElasticsearchContainer method of org.testcontainers.elasticsearch.ElasticsearchContainer class

Best Testcontainers-java code snippet using org.testcontainers.elasticsearch.ElasticsearchContainer.ElasticsearchContainer

Source:ElasticsearchTestContainer.java Github

copy

Full Screen

...5import org.slf4j.LoggerFactory;6import org.springframework.beans.factory.DisposableBean;7import org.springframework.beans.factory.InitializingBean;8import org.testcontainers.containers.output.Slf4jLogConsumer;9import org.testcontainers.elasticsearch.ElasticsearchContainer;10import org.testcontainers.utility.DockerImageName;11/**12 * Base class for starting/stopping ElasticSearch during tests.13 */14public class ElasticsearchTestContainer implements InitializingBean, DisposableBean {15 private static final Logger log = LoggerFactory.getLogger(ElasticsearchTestContainer.class);16 private static final Integer CONTAINER_STARTUP_TIMEOUT_MINUTES = 10;17 private ElasticsearchContainer elasticsearchContainer;18 @Override19 public void destroy() {20 if (null != elasticsearchContainer && elasticsearchContainer.isRunning()) {21 elasticsearchContainer.close();22 }23 }24 @Override25 public void afterPropertiesSet() {26 if (null == elasticsearchContainer) {27 elasticsearchContainer =28 new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch").withTag("7.17.4"))29 .withStartupTimeout(Duration.of(CONTAINER_STARTUP_TIMEOUT_MINUTES, ChronoUnit.MINUTES))30 .withSharedMemorySize(256000000L)31 .withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m")32 .withEnv("xpack.security.enabled", "false")33 .withLogConsumer(new Slf4jLogConsumer(log))34 .withReuse(true);35 }36 if (!elasticsearchContainer.isRunning()) {37 elasticsearchContainer.start();38 }39 }40 public ElasticsearchContainer getElasticsearchContainer() {41 return elasticsearchContainer;42 }43}...

Full Screen

Full Screen

Source:AbstractContainerIntegrationTest.java Github

copy

Full Screen

...5import org.springframework.test.context.DynamicPropertyRegistry;6import org.springframework.test.context.DynamicPropertySource;7import org.testcontainers.containers.KafkaContainer;8import org.testcontainers.containers.Network;9import org.testcontainers.elasticsearch.ElasticsearchContainer;10import org.testcontainers.junit.jupiter.Testcontainers;11@Testcontainers12public abstract class AbstractContainerIntegrationTest {13 14 private static final KafkaContainer kafkaContainer = new KafkaContainer("5.5.1");15 private static final ElasticsearchContainer elasticsearchContainer = new PolicyElasticsearchContainer();16 17 @DynamicPropertySource18 private static void dynamicProperties(DynamicPropertyRegistry registry) {19 Network network = Network.SHARED;20 21 // Kafka22 kafkaContainer.withNetwork(network).withNetworkAliases("kafka")23 .withExternalZookeeper("zookeeper:2181")24 .withExposedPorts(9092, 9093);25 kafkaContainer.start();26 // elasticsearch27 elasticsearchContainer.withNetwork(network).withNetworkAliases("elasticsearch");28 elasticsearchContainer.start();29 ...

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.elasticsearch.action.search.SearchResponse;3import org.elasticsearch.client.Client;4import org.elasticsearch.client.transport.TransportClient;5import org.elasticsearch.common.settings.Settings;6import org.elasticsearch.common.transport.InetSocketTransportAddress;7import org.elasticsearch.index.query.QueryBuilders;8import org.elasticsearch.node.Node;9import org.elasticsearch.node.NodeBuilder;10import org.elasticsearch.transport.client.PreBuiltTransportClient;11import org.junit.Before;12import org.junit.Test;13import java.net.InetAddress;14import java.net.UnknownHostException;15import static org.elasticsearch.node.NodeBuilder.nodeBuilder;16import static org.junit.Assert.assertEquals;17public class ElasticsearchContainerTest {18 private Node node;19 private Client client;20 public void setUp() throws Exception {21 ElasticsearchContainer container = new ElasticsearchContainer();22 container.start();23 Settings settings = Settings.builder()24 .put("cluster.name", container.getClusterName()).build();25 client = new PreBuiltTransportClient(settings)26 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(container.getContainerIpAddress()), container.getFirstMappedPort()));27 }28 public void testNode() {29 client.prepareIndex("test", "type1", "1")30 .setSource("field1", "value1")31 .get();32 SearchResponse response = client.prepareSearch("test")33 .setQuery(QueryBuilders.matchQuery("field1", "value1"))34 .get();35 assertEquals(1, response.getHits().getTotalHits());36 }37}38package org.testcontainers.elasticsearch;39import org.elasticsearch.action.search.SearchResponse;40import org.elasticsearch.client.Client;41import org.elasticsearch.client.transport.TransportClient;42import org.elasticsearch.common.settings.Settings;43import org.elasticsearch.common.transport.InetSocketTransportAddress;44import org.elasticsearch.index.query.QueryBuilders;45import org.elasticsearch.node.Node;46import org.elasticsearch.node.NodeBuilder;47import org.elasticsearch.transport.client.PreBuiltTransportClient;48import org.junit.Before;49import org.junit.Test;50import java.net.InetAddress;51import java.net.UnknownHostException;52import static org.elasticsearch.node.NodeBuilder.nodeBuilder;53import static org.junit.Assert.assertEquals;54public class ElasticsearchContainerTest {55 private Node node;56 private Client client;57 public void setUp() throws Exception {58 ElasticsearchContainer container = new ElasticsearchContainer();59 container.start();60 Settings settings = Settings.builder()61 .put("cluster.name",

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.elasticsearch.action.search.SearchRequest;3import org.elasticsearch.action.search.SearchResponse;4import org.elasticsearch.client.RequestOptions;5import org.elasticsearch.client.RestHighLevelClient;6import org.elasticsearch.index.query.QueryBuilders;7import org.elasticsearch.search.builder.SearchSourceBuilder;8import org.junit.jupiter.api.Test;9import org.testcontainers.junit.jupiter.Container;10import org.testcontainers.junit.jupiter.Testcontainers;11import java.io.IOException;12import static org.junit.jupiter.api.Assertions.assertEquals;13public class ElasticsearchContainerTest {14 private ElasticsearchContainer container = new ElasticsearchContainer();15 public void testElasticsearchContainer() throws IOException {16 RestHighLevelClient client = container.getClient();17 SearchResponse searchResponse = client.search(new SearchRequest()18 .source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())), RequestOptions.DEFAULT);19 assertEquals(0L, searchResponse.getHits().getTotalHits().value);20 }21}22package org.testcontainers.elasticsearch;23import org.elasticsearch.action.search.SearchRequest;24import org.elasticsearch.action.search.SearchResponse;25import org.elasticsearch.client.RequestOptions;26import org.elasticsearch.client.RestHighLevelClient;27import org.elasticsearch.index.query.QueryBuilders;28import org.elasticsearch.search.builder.SearchSourceBuilder;29import org.junit.jupiter.api.Test;30import org.testcontainers.junit.jupiter.Container;31import org.testcontainers.junit.jupiter.Testcontainers;32import java.io.IOException;33import static org.junit.jupiter.api.Assertions.assertEquals;34public class ElasticsearchContainerTest {35 private ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:7.6.0");36 public void testElasticsearchContainer() throws IOException {37 RestHighLevelClient client = container.getClient();38 SearchResponse searchResponse = client.search(new SearchRequest()39 .source(new SearchSourceBuilder().query(QueryBuilders.match

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.elasticsearch.client.RequestOptions;3import org.elasticsearch.client.RestHighLevelClient;4import org.elasticsearch.client.indices.CreateIndexRequest;5import org.elasticsearch.client.indices.CreateIndexResponse;6import org.elasticsearch.client.indices.GetIndexRequest;7import org.elasticsearch.client.indices.GetIndexResponse;8import org.junit.jupiter.api.Test;9import org.slf4j.Logger;10import org.slf4j.LoggerFactory;11import org.testcontainers.elasticsearch.ElasticsearchContainer;12import org.testcontainers.junit.jupiter.Container;13import org.testcontainers.junit.jupiter.Testcontainers;14public class TestContainerElasticSearch {15 private static final Logger LOGGER = LoggerFactory.getLogger(TestContainerElasticSearch.class);16 public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();17 public void testElasticSearch() throws Exception {18 LOGGER.info("ElasticSearch URL: " + elasticsearch.getHttpHostAddress());19 try (RestHighLevelClient client = elasticsearch.createClient()) {20 CreateIndexRequest request = new CreateIndexRequest("test_index");21 CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);22 GetIndexRequest getIndexRequest = new GetIndexRequest("test_index");23 GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);24 }25 }26}

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.elasticsearch.client.Request;3import org.elasticsearch.client.RestClient;4import org.elasticsearch.client.RestHighLevelClient;5import org.junit.Test;6import org.testcontainers.elasticsearch.ElasticsearchContainer;7import java.io.IOException;8public class ElasticsearchContainerTest {9 public void testElasticsearchContainer() throws IOException {10 try (ElasticsearchContainer elasticsearch = new ElasticsearchContainer()) {11 elasticsearch.start();12 try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(elasticsearch.getHttpHostAddress()))) {13 client.info();14 }15 }16 }17}18package org.testcontainers.elasticsearch;19import org.elasticsearch.client.Request;20import org.elasticsearch.client.RestClient;21import org.elasticsearch.client.RestHighLevelClient;22import org.junit.Test;23import org.testcontainers.elasticsearch.ElasticsearchContainer;24import java.io.IOException;25public class ElasticsearchContainerTest {26 public void testElasticsearchContainer() throws IOException {27 try (ElasticsearchContainer elasticsearch = new ElasticsearchContainer()) {28 elasticsearch.start();29 try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(elasticsearch.getHttpHostAddress()))) {30 client.info();31 }32 }33 }34}35package org.testcontainers.elasticsearch;36import org.elasticsearch.client.Request;37import org.elasticsearch.client.RestClient;38import org.elasticsearch.client.RestHighLevelClient;39import org.junit.Test;40import org.testcontainers.elasticsearch.ElasticsearchContainer;41import java.io.IOException;42public class ElasticsearchContainerTest {43 public void testElasticsearchContainer() throws IOException {44 try (ElasticsearchContainer elasticsearch = new ElasticsearchContainer()) {45 elasticsearch.start();46 try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(elasticsearch.getHttpHostAddress()))) {47 client.info();48 }49 }50 }51}

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.elasticsearch.ElasticsearchContainer;2import org.testcontainers.utility.DockerImageName;3import org.testcontainers.utility.MountableFile;4public class Test {5 public static void main(String[] args) {6 ElasticsearchContainer container = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.6.2"));7 container.withCopyFileToContainer(MountableFile.forClasspathResource("elasticsearch.yml"), "/usr/share/elasticsearch/config/elasticsearch.yml");8 container.start();9 }10}11import org.testcontainers.elasticsearch.ElasticsearchContainer;12import org.testcontainers.utility.DockerImageName;13import org.testcontainers.utility.MountableFile;14public class Test {15 public static void main(String[] args) {16 ElasticsearchContainer container = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.6.2"));17 container.withCopyFileToContainer(MountableFile.forClasspathResource("elasticsearch.yml"), "/usr/share/elasticsearch/config/elasticsearch.yml");18 container.start();19 }20}21import org.testcontainers.elasticsearch.ElasticsearchContainer;22import org.testcontainers.utility.DockerImageName;23import org.testcontainers.utility.MountableFile;24public class Test {25 public static void main(String[] args) {26 ElasticsearchContainer container = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.6.2"));27 container.withCopyFileToContainer(MountableFile.forClasspathResource("elasticsearch.yml"), "/usr/share/elasticsearch/config/elasticsearch.yml");28 container.start();29 }30}31import org.testcontainers.elasticsearch.ElasticsearchContainer;32import org.testcontainers.utility.DockerImageName;33import org.testcontainers.utility.MountableFile;34public class Test {35 public static void main(String[] args) {36 ElasticsearchContainer container = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.6.2"));37 container.withCopyFileToContainer(MountableFile.forClasspathResource("elasticsearch.yml"),

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package com.soumyadeep;2import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;3import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;4import org.elasticsearch.action.admin.indices.get.GetIndexRequest;5import org.elasticsearch.action.admin.indices.get.GetIndexResponse;6import org.elasticsearch.client.RequestOptions;7import org.elasticsearch.client.RestHighLevelClient;8import org.elasticsearch.client.indices.CreateIndexRequest;9import org.elasticsearch.client.indices.CreateIndexResponse;10import org.elasticsearch.client.indices.GetIndexRequest;11import org.elasticsearch.client.indices.GetIndexResponse;12import org.elasticsearch.common.settings.Settings;13import org.elasticsearch.common.xcontent.XContentType;14import org.junit.jupiter.api.Test;15import org.testcontainers.elasticsearch.ElasticsearchContainer;16import static org.junit.jupiter.api.Assertions.assertTrue;17public class TestContainerElasticSearch {18 public void testCreateIndex() throws Exception {19 ElasticsearchContainer elasticsearch = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.8.1");20 elasticsearch.start();21 RestHighLevelClient client = new RestHighLevelClient(22 elasticsearch.getRestClientBuilder());23 String indexName = "test";24 CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);25 createIndexRequest.settings(Settings.builder()26 .put("index.number_of_shards", 1)27 .put("index.number_of_replicas", 0)28 );29 String mapping = "{\n" +30 " \"properties\": {\n" +31 " \"message\": {\n" +32 " }\n" +33 " }\n" +34 "}";35 createIndexRequest.mapping(mapping, XContentType.JSON);36 CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);37 assertTrue(createIndexResponse.isAcknowledged());38 GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);39 GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);40 assertTrue(getIndexResponse.getIndices().length == 1);41 assertTrue(getIndexResponse.getIndices()[0].equals(indexName));42 elasticsearch.stop();43 }44}

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1public class ElasticsearchContainerTest {2 private static final Logger logger = LoggerFactory.getLogger(ElasticsearchContainerTest.class);3 public void testElasticsearchContainer() throws IOException {4 try (ElasticsearchContainer container = new ElasticsearchContainer()) {5 container.start();6 logger.info("Elasticsearch container started with host: {} and port: {}", container.getHttpHostAddress(), container.getMappedPort(9200));7 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HttpHost.create(container.getHttpHostAddress())));8 logger.info("Elasticsearch client created");9 client.close();10 logger.info("Elasticsearch client closed");11 }12 }13}14public class ElasticsearchContainerTest {15 private static final Logger logger = LoggerFactory.getLogger(ElasticsearchContainerTest.class);16 public void testElasticsearchContainer() throws IOException {17 try (ElasticsearchContainer container = new ElasticsearchContainer()) {18 container.start();19 logger.info("Elasticsearch container started with host: {} and port: {}", container.getHttpHostAddress(), container.getMappedPort(9200));20 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HttpHost.create(container.getHttpHostAddress())));21 logger.info("Elasticsearch client created");22 client.close();23 logger.info("Elasticsearch client closed");24 }25 }26}27public class ElasticsearchContainerTest {28 private static final Logger logger = LoggerFactory.getLogger(ElasticsearchContainerTest.class);29 public void testElasticsearchContainer() throws IOException {30 try (ElasticsearchContainer container = new ElasticsearchContainer()) {31 container.start();32 logger.info("Elasticsearch container started with host: {} and port: {}", container.getHttpHostAddress(), container.getMappedPort(9200));33 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HttpHost.create(container.getHttpHostAddress())));34 logger.info("Elasticsearch client created");35 client.close();36 logger.info("Elasticsearch client closed");37 }38 }39}

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package com.containers.test;2import org.testcontainers.elasticsearch.ElasticsearchContainer;3public class TestElasticSearchContainer {4 public static void main(String[] args) {5 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:6.2.4");6 elasticsearchContainer.start();7 System.out.println("ElasticsearchContainer started");8 }9}

Full Screen

Full Screen

ElasticsearchContainer

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.junit.Test;3import org.testcontainers.containers.GenericContainer;4import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;5import org.testcontainers.utility.DockerImageName;6import java.time.Duration;7import java.util.concurrent.TimeUnit;8import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;9public class TestElasticsearchContainer {10 public void testElasticsearchContainer() {11 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(12 DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.14.0")13 .asCompatibleSubstituteFor("elasticsearch")14 );15 elasticsearchContainer.start();16 assertTrue("Elasticsearch is not running", elasticsearchContainer.isRunning());17 elasticsearchContainer.stop();18 }19 public void testElasticsearchContainerWithCustomImage() {20 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(21 DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.14.0")22 .asCompatibleSubstituteFor("elasticsearch")23 );24 elasticsearchContainer.start();25 assertTrue("Elasticsearch is not running", elasticsearchContainer.isRunning());26 elasticsearchContainer.stop();27 }28 public void testElasticsearchContainerWithCustomConfig() {29 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(30 DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.14.0")31 .asCompatibleSubstituteFor("elasticsearch")32 );33 elasticsearchContainer.withEnv("discovery.type", "single-node");34 elasticsearchContainer.start();35 assertTrue("Elasticsearch is not running", elasticsearchContainer.isRunning());36 elasticsearchContainer.stop();37 }38 public void testElasticsearchContainerWithCustomConfigAndPlugins() {39 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(40 DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.14.0")41 .asCompatibleSubstituteFor("elasticsearch")42 );43 elasticsearchContainer.withEnv("discovery.type", "single-node");44 elasticsearchContainer.withPlugin("analysis-icu");45 elasticsearchContainer.withPlugin("analysis-kuromoji");46 elasticsearchContainer.withPlugin("analysis-smartcn");47 elasticsearchContainer.withPlugin("analysis-stempel");

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful