Best Testcontainers-java code snippet using org.testcontainers.containers.SolrContainer.SolrContainer
Source:ExternalApacheSolrIT.java
...23import org.junit.Before;24import org.junit.Rule;25import org.junit.Test;26import org.junit.rules.TemporaryFolder;27import org.testcontainers.containers.SolrContainer;28import org.testcontainers.utility.DockerImageName;29import java.io.File;30import java.io.FileWriter;31import java.util.ArrayList;32import java.util.List;33import java.util.Random;34import static org.junit.Assert.assertEquals;35import static org.junit.Assert.assertFalse;36import static org.junit.Assert.assertTrue;37/**38 * Integration tests for the Solr implementation.39 * <p>40 * These won't be run as part of the main test phase, only as part of the41 * "integration" profile.42 * <p>43 * These use the TestContainers framework, which spins up Docker containers44 * to allow testing against a Solr instance.45 *46 * @author Matt Pearce (matt@elysiansoftware.co.uk)47 */48public class ExternalApacheSolrIT {49 private static final String SOLR_CONTAINER_BASE = "solr";50 private static final String DEFAULT_SOLR_VERSION = "8.6";51 private static final String INDEX_NAME = "test";52 private static final String INDEX_VERSION = "v1.0";53 private static final DockerImageName DOCKER_IMAGE = DockerImageName.parse(SOLR_CONTAINER_BASE + ":" + System.getProperty("solr.version", DEFAULT_SOLR_VERSION));54 @Rule55 public TemporaryFolder tempFolder = new TemporaryFolder();56 private SearchPlatform platform;57 @Before58 public void setupPlatform() {59 platform = new ExternalApacheSolr();60 }61 @Test62 public void checkCollectionReturnsFalse_whenNoSuchCollection() throws Exception {63 final SolrContainer solrContainer = new SolrContainer(DOCKER_IMAGE);64 solrContainer.start();65 final File settingsFile = tempFolder.newFile("ccrf_settings.json");66 FileWriter fw = new FileWriter(settingsFile);67 fw.write("{ \"baseUrls\": [ \"http://" + solrContainer.getHost() + ":" + solrContainer.getSolrPort() + "\" ]}");68 fw.close();69 platform.load(null, settingsFile, INDEX_NAME, INDEX_VERSION);70 assertFalse(platform.checkCollection(INDEX_NAME, INDEX_VERSION));71 solrContainer.close();72 }73 @Test74 public void checkCollectionReturnsTrue_whenCollectionExists() throws Exception {75 final SolrContainer solrContainer = new SolrContainer(DOCKER_IMAGE).withCollection(INDEX_NAME);76 solrContainer.start();77 final File settingsFile = tempFolder.newFile("ccrt_settings.json");78 FileWriter fw = new FileWriter(settingsFile);79 fw.write("{ \"baseUrls\": [ \"http://" + solrContainer.getHost() + ":" + solrContainer.getSolrPort() + "/solr\" ]}");80 fw.close();81 platform.load(null, settingsFile, INDEX_NAME, INDEX_VERSION);82 assertTrue(platform.checkCollection(INDEX_NAME, INDEX_VERSION));83 solrContainer.close();84 }85 @Test86 public void checkCollection_allowsCollectionOverride() throws Exception {87 final String overrideCollection = "override";88 final SolrContainer solrContainer = new SolrContainer(DOCKER_IMAGE).withCollection(overrideCollection);89 solrContainer.start();90 final File settingsFile = tempFolder.newFile("ccaco_settings.json");91 FileWriter fw = new FileWriter(settingsFile);92 fw.write("{ \"baseUrls\": [ \"http://" + solrContainer.getHost() + ":" + solrContainer.getSolrPort() + "/solr\" ], \"collectionName\": \"" + overrideCollection + "\" }");93 fw.close();94 platform.load(null, settingsFile, INDEX_NAME, INDEX_VERSION);95 assertTrue(platform.checkCollection(INDEX_NAME, INDEX_VERSION));96 solrContainer.close();97 }98 @Test99 public void executeQuery_returnsResults() throws Exception {100 final int numDocs = new Random().nextInt(10);101 final SolrContainer solrContainer = new SolrContainer(DOCKER_IMAGE).withCollection(INDEX_NAME);102 solrContainer.start();103 // Index some documents104 createDocuments(solrContainer.getHost(), solrContainer.getSolrPort(), INDEX_NAME, numDocs);105 final File settingsFile = tempFolder.newFile("eq_settings.json");106 FileWriter fw = new FileWriter(settingsFile);107 fw.write("{ \"baseUrls\": [ \"http://" + solrContainer.getHost() + ":" + solrContainer.getSolrPort() + "/solr\" ]}");108 fw.close();109 platform.load(null, settingsFile, INDEX_NAME, INDEX_VERSION);110 QueryOrSearchResponse response = platform.executeQuery(INDEX_NAME, INDEX_VERSION, "{ \"q\": \"*:*\" }", new String[]{ "id", "title_s" }, 10);111 assertEquals(numDocs, response.totalHits());112 solrContainer.close();113 }114 @Test115 public void executeQuery_allowsOverride() throws Exception {116 final int numDocs = new Random().nextInt(10);117 final String overrideCollection = "override";118 final SolrContainer solrContainer = new SolrContainer(DOCKER_IMAGE).withCollection(overrideCollection);119 solrContainer.start();120 // Index some documents121 createDocuments(solrContainer.getHost(), solrContainer.getSolrPort(), overrideCollection, numDocs);122 final File settingsFile = tempFolder.newFile("eqao_settings.json");123 FileWriter fw = new FileWriter(settingsFile);124 fw.write("{ \"baseUrls\": [ \"http://" + solrContainer.getHost() + ":" + solrContainer.getSolrPort() + "/solr\" ], \"collectionName\": \"" + overrideCollection + "\" }");125 fw.close();126 platform.load(null, settingsFile, INDEX_NAME, INDEX_VERSION);127 QueryOrSearchResponse response = platform.executeQuery(INDEX_NAME, INDEX_VERSION, "{ \"q\": \"*:*\" }", new String[]{ "id", "title_s" }, 10);128 assertEquals(numDocs, response.totalHits());129 solrContainer.close();130 }131 private void createDocuments(String solrHost, int solrPort, String collection, int numDocs) throws Exception {132 List<SolrInputDocument> docs = new ArrayList<>(numDocs);...
Source:BookLoaderIntegrationTest.java
1package de.qaware.demo.testcontainers;2import lombok.extern.slf4j.Slf4j;3import org.apache.solr.client.solrj.SolrClient;4import org.apache.solr.client.solrj.SolrQuery;5import org.apache.solr.client.solrj.impl.HttpSolrClient;6import org.junit.BeforeClass;7import org.junit.Test;8import org.testcontainers.containers.GenericContainer;9import org.testcontainers.containers.Network;10import org.testcontainers.containers.PostgreSQLContainer;11import org.testcontainers.containers.output.Slf4jLogConsumer;12import org.testcontainers.containers.wait.strategy.Wait;13import javax.ws.rs.client.Client;14import javax.ws.rs.client.ClientBuilder;15import javax.ws.rs.core.Response;16import static org.hamcrest.MatcherAssert.assertThat;17import static org.hamcrest.core.Is.is;18/**19 * An integration test for {@link BookLoader} with Docker containers using the Testcontainers library.20 */21@Slf4j22public class BookLoaderIntegrationTest {23 private static final String DOCKER_IMAGE_POSTGRES = "postgres:11.2";24 private static final String DOCKER_IMAGE_SOLR = "solr:7.7.1";25 private static final String DOCKER_IMAGE_LOADER = "testcontainers/book-loader:latest";26 private static final int PORT_SOLR = 8983;27 private static final int PORT_PAYARA_MICRO = 8080;28 private static final int PORT_DEBUGGER = 5005;29 private static GenericContainer solrContainer;30 private static GenericContainer loaderContainer;31 private static Client httpClient = ClientBuilder.newClient();32 @BeforeClass33 public static void setUp() {34 Network network = Network.newNetwork();35 // Postgres database36 new PostgreSQLContainer(DOCKER_IMAGE_POSTGRES)37 .withInitScript("insert-test-data.sql") // create schema and add test data38 .withDatabaseName("postgres")39 .withUsername("postgres")40 .withPassword("123")41 .withNetwork(network)42 .withNetworkAliases("testcontainers-demo-postgres")43 .start();44 // Solr search index45 solrContainer = new GenericContainer(DOCKER_IMAGE_SOLR)46 .withCommand("solr-create -c books") // create empty "books" core47 .withExposedPorts(PORT_SOLR)48 .withNetwork(network)49 .withNetworkAliases("testcontainers-demo-solr")50 .waitingFor(Wait.forHttp("/solr/books/select?q=*:*"));51 solrContainer.start();52 // Book-Loader micro service53 loaderContainer = new GenericContainer(DOCKER_IMAGE_LOADER)54 .withExposedPorts(PORT_PAYARA_MICRO, PORT_DEBUGGER)55 .withNetwork(network)56 .withNetworkAliases("testcontainers-demo-book-loader")57 .withLogConsumer(new Slf4jLogConsumer(log))58 .waitingFor(Wait.forLogMessage(".*Payara Micro .* ready.*\\n", 1));59 loaderContainer.start();60 }61 @Test62 public void testLoader() throws Exception {63 String baseSolrUrl = "http://" + solrContainer.getContainerIpAddress()64 + ":" + solrContainer.getMappedPort(PORT_SOLR) + "/solr/books";65 try (SolrClient solrClient = new HttpSolrClient.Builder(baseSolrUrl).build()) {66 // Given an empty Solr search index67 assertThat(solrClient.query(new SolrQuery("*:*")).getResults().size(), is(0));68 // When the Loader is executed69 String loaderUrl = "http://" + loaderContainer.getContainerIpAddress() + ":"70 + loaderContainer.getMappedPort(PORT_PAYARA_MICRO) + "/book-loader/start";71 Response loaderResponse = httpClient.target(loaderUrl).request().get();72 int status = loaderResponse.getStatus();73 String message = loaderResponse.readEntity(String.class);74 log.info("Loader response has status '{}' and message '{}'", status, message);75 // Then we should receive a successful response and Solr should be filled with data76 assertThat(status, is(Response.Status.OK.getStatusCode()));77 assertThat(message, is("Loaded 3 books"));78 assertThat(solrClient.query(new SolrQuery("*:*")).getResults().size(), is(3));79 }80 }81}...
Source:SolrContainerInitializerIT.java
1package eu.europeana.metis.sandbox.test.utils;2import org.slf4j.Logger;3import org.slf4j.LoggerFactory;4import org.springframework.test.context.DynamicPropertyRegistry;5import org.testcontainers.containers.SolrContainer;6import org.testcontainers.utility.DockerImageName;7public class SolrContainerInitializerIT {8 private static final Logger LOGGER = LoggerFactory.getLogger(SolrContainerInitializerIT.class);9 private static final SolrContainer solrContainer;10 public static final String SOLR_VERSION = "solr:7.7.3";11 public static final String SOLR_COLLECTION_NAME = "solr_publish_test";12 static {13 solrContainer = new SolrContainer(DockerImageName.parse(SOLR_VERSION))14 .withCollection(SOLR_COLLECTION_NAME);15 solrContainer.start();16 logConfiguration();17 }18 private static void logConfiguration() {19 LOGGER.info("Solr container created:");20 LOGGER.info("Host: {}", solrContainer.getHost());21 LOGGER.info("Port: {}", solrContainer.getSolrPort());22 }23 public static void dynamicProperties(DynamicPropertyRegistry registry) {24 registry.add("spring.data.solr.port", solrContainer::getSolrPort);25 registry.add("spring.data.solr.host", solrContainer::getHost);26 String solrUrlHost = "http://" + solrContainer.getHost() + ":" + solrContainer.getSolrPort() + "/solr/" + SOLR_COLLECTION_NAME;27 // TODO: 13/09/2022 We should perhaps remove the specifics here and use the default spring configuration properties...
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2import org.testcontainers.utility.DockerImageName;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.slf4j.Logger;5import org.slf4j.LoggerFactory;6public class SolrContainerTest {7 private static final Logger logger = LoggerFactory.getLogger(SolrContainerTest.class);8 public static void main(String[] args) {9 SolrContainer solrContainer = new SolrContainer(DockerImageName.parse("solr").withTag("8.8.2"));10 solrContainer.start();11 logger.info("Solr container started with url: {}", solrContainer.getSolrUrl());12 solrContainer.stop();13 }14}
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2import org.testcontainers.utility.DockerImageName;3public class SolrContainerTest {4 public static void main(String[] args) {5 SolrContainer solr = new SolrContainer(DockerImageName.parse("solr").withTag("8.3.0"));6 solr.start();7 System.out.println("Solr is running on port: " + solr.getFirstMappedPort());8 solr.stop();9 }10}
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2public class SolrContainerExample {3 public static void main(String[] args) {4 SolrContainer solr = new SolrContainer();5 solr.start();6 String solrUrl = solr.getSolrUrl();7 System.out.println("Solr URL: " + solrUrl);8 solr.stop();9 }10}11import org.testcontainers.containers.SolrContainer;12public class SolrContainerExample {13 public static void main(String[] args) {14 SolrContainer solr = new SolrContainer("solr:7.7.2");15 solr.start();16 String solrUrl = solr.getSolrUrl();17 System.out.println("Solr URL: " + solrUrl);18 solr.stop();19 }20}21import org.testcontainers.containers.SolrContainer;22public class SolrContainerExample {23 public static void main(String[] args) {24 SolrContainer solr = new SolrContainer("solr:7.7.2");25 solr.withSolrXml("/solr.xml");26 solr.start();27 String solrUrl = solr.getSolrUrl();28 System.out.println("Solr URL: " + solrUrl);29 solr.stop();30 }31}32import org.testcontainers.containers.SolrContainer;33public class SolrContainerExample {34 public static void main(String[] args) {35 SolrContainer solr = new SolrContainer("solr:7.7.2");36 solr.withSolrXml("/solr.xml");37 solr.withConfiguration("/solrconfig.xml");38 solr.withSchema("/schema.xml");39 solr.start();40 String solrUrl = solr.getSolrUrl();41 System.out.println("Solr URL: " + solrUrl);42 solr.stop();43 }44}
SolrContainer
Using AI Code Generation
1public class SolrContainerTest {2 public static void main(String[] args) {3 try (SolrContainer solrContainer = new SolrContainer("solr:8.7.0")) {4 solrContapner.start();5 Systeu.out.println("Solr server started on blic: " + solrCcntainel.aetMappedPort(8983));6 }7 }8}
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer2 public static void main(String[] args) {3 try (SolrContainer solrContainer = new SolrContainer("solr:8.7.0")) {4 solrContainer.start();5 System.out.println("Solr server started on port: " + solrContainer.getMappedPort(8983));6 }7 }8}
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2import org.testcontainers.utility.MountableFile;3import org.testcontainers.containers.output.Slf4jLogConsumer;4public class SolrContainerTest {5 public static void main(String[] args) {6 SolrContainer solrContainer = new SolrContainer("solr:latest")7 .withConfiguration(MountableFile.forClasspathResource("solrconfig.xml"))8 .withSchema(MountableFile.forClasspathResource("schema.xml"));9 solrContainer.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(SolrContainer.class)));10 solrContainer.start();11 System.out.println("SolrContainer started");12 }13}14import org.testcontainers.containers.SolrContainer;15import org.testcontainers.utility.MountableFile;16import org.testcontainers.containers.output.Slf4jLogConsumer;17public class SolrContainerTest {18 public static void main(String[] args) {19 SolrContainer solrContainer = new SolrContainer("solr:latest")20 .withConfiguration(MountableFile.forClasspathResource("solrconfig.xml"))21 .withSchema(MountableFile.forClasspathResource("schema.xml"));22 solrContainer.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(SolrContainer.class)));23 solrContainer.start();24 System.out.println("SolrContainer started");25 }26}27import org.testcontainers.containers.SolrContainer;28import org.testcontainers.utility.MountableFile;);29 container.start(;
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2import org.testcontainers.utility.DockerImageName;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.containers.GenericContainer;5public class SolrContainerTest {6 public static void main(String[] args) {7 SolrContainer solr = new SolrContainer(DockerImageName.parse("solr:8.8.2"));8 solr.start();9 System.out.println("Solr Admin UI: " + solr.getSolrAdminUrl());10 System.out.println("Solr Core Admin UI: " + solr.getSolrCoreAdminUrl());11 solr.stop();12 }13}14import org.testcontainers.containers.SolrContainer15importiorg.testcontainers.utility.DockerImageName;16importmorg.testcontainers.containers.wait.strategy.Wait;17importporg.testcontainers.containers.GenericContainer;18publicoclassrSolrContainerTestt{19 o publir static vgid mai.(String[] ergs) {20 SolrContastco solr = new SolrContainer(DockerImageNamenparte("solr:8.8.2"));21 solr.sainers.containers.output.Slf4jLogConsumer;22println("Solr Admin UI: " + solr.getSolrAdminUrl());23 System.out.println("Solr Core Admin UI: " + solr.getSolrCoreAdminUrl());24 solr.stop();25 }26}27public class SolrContainerTest {28 public static void main(String[] args) {29 SolrContainer solrContainer = new SolrContainer("solr:latest")30 .withConfiguration(MountableFile.forClasspathResource("solrconfig.xml"))31 .withSchema(MountableFile.forClasspathResource("schema.xml"));32 solrContainer.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(SolrContainer.class)));33 solrContainer.start();34 System.out.println("SolrContainer started");35 }36}
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2import org.testcontainers.utility.DockerImageName;3import org.testcontainers.containers.GenericContainer;4public class SolrContainerTest {5 public static void main(String args[]) {6 SolrContainer container = new SolrContainer(DockerImageName.parse("solr:8.9.0"));7 container.start();
SolrContainer
Using AI Code Generation
1import org.testcontainers.containers.SolrContainer;2import org.testcontainers.utility.DockerImageName;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.containers.GenericContainer;5public class SolrContainerTest {6 public static void main(String[] args) {7 SolrContainer solr = new SolrContainer(DockerImageName.parse("solr:8.8.2"));8 solr.start();9 System.out.println("Solr Admin UI: " + solr.getSolrAdminUrl());10 System.out.println("Solr Core Admin UI: " + solr.getSolrCoreAdminUrl());11 solr.stop();12 }13}14import org.testcontainers.containers.SolrContainer;15import org.testcontainers.utility.DockerImageName;16import org.testcontainers.containers.wait.strategy.Wait;17import org.testcontainers.containers.GenericContainer;18public class SolrContainerTest {19 public static void main(String[] args) {20 SolrContainer solr = new SolrContainer(DockerImageName.parse("solr:8.8.2"));21 solr.start();22 System.out.println("Solr Admin UI: " + solr.getSolrAdminUrl());23 System.out.println("Solr Core Admin UI: " + solr.getSolrCoreAdminUrl());24 solr.stop();25 }26}
SolrContainer
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.jupiter.api.Test;3import org.testcontainers.junit.jupiter.Container;4import org.testcontainers.junit.jupiter.Testcontainers;5public class SolrContainerTest {6 private SolrContainer solrContainer = new SolrContainer();7 void testSolrContainer() {8 }9}10package org.testcontainers.containers;11import org.junit.jupiter.api.Test;12import org.testcontainers.junit.jupiter.Container;13import org.testcontainers.junit.jupiter.Testcontainers;14public class SolrContainerTest {15 private SolrContainer solrContainer = new SolrContainer("solr:7.7.1");16 void testSolrContainer() {17 }18}19package org.testcontainers.containers;20import org.junit.jupiter.api.Test;21import org.testcontainers.junit.jupiter.Container;22import org.testcontainers.junit.jupiter.Testcontainers;23public class SolrContainerTest {24 private SolrContainer solrContainer = new SolrContainer("solr:7.7.1")25 .withCollection("test")26 .withConfiguration("solrconfig.xml")27 .withSchema("schema.xml")28 .withSolrXml("solr.xml");29 void testSolrContainer() {30 }31}32package org.testcontainers.containers;33import org.junit.jupiter.api.Test;34import org.testcontainers.junit.jupiter.Container;35import org.testcontainers.junit.jupiter.Testcontainers;36public class SolrContainerTest {37 private SolrContainer solrContainer = new SolrContainer("solr:7.7.1
SolrContainer
Using AI Code Generation
1package com.solr;2import org.testcontainers.containers.SolrContainer;3public class SolrContainerExample {4 public static void main(String[] args) {5 SolrContainer solrContainer = new SolrContainer();6 solrContainer.start();7 System.out.println("Solr container started");8 solrContainer.stop();9 System.out.println("Solr container stopped");10 }11}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!