Best Testcontainers-java code snippet using org.testcontainers.containers.InfluxDBContainer
Source:InfluxdbIT.java
1package alpakka.influxdb;2import akka.Done;3import akka.actor.ActorSystem;4import org.junit.jupiter.api.*;5import org.slf4j.Logger;6import org.slf4j.LoggerFactory;7import org.testcontainers.containers.Container;8import org.testcontainers.containers.GenericContainer;9import org.testcontainers.junit.jupiter.Testcontainers;10import org.testcontainers.utility.DockerImageName;11import util.LogFileScanner;12import java.io.IOException;13import java.util.List;14import java.util.concurrent.CompletableFuture;15import java.util.concurrent.CompletionStage;16import java.util.concurrent.ExecutionException;17import java.util.concurrent.TimeUnit;18import java.util.stream.Collectors;19import java.util.stream.IntStream;20import static org.assertj.core.api.Assertions.assertThat;21@TestMethodOrder(MethodOrderer.OrderAnnotation.class)22@Testcontainers23public class InfluxdbIT {24 private static final Logger LOGGER = LoggerFactory.getLogger(InfluxdbIT.class);25 private static final Integer INFLUXDB_PORT = 8086;26 private static final ActorSystem actorSystem = ActorSystem.create("InfluxdbIT");27 public String searchAfterPattern;28 @org.testcontainers.junit.jupiter.Container29 public static GenericContainer influxDBContainer = new GenericContainer<>(DockerImageName.parse("influxdb"))30 .withExposedPorts(INFLUXDB_PORT);31 String influxURL = "http://localhost:" + influxDBContainer.getMappedPort(INFLUXDB_PORT);32 InfluxdbWriter influxDBWriter = new InfluxdbWriter(influxURL, "abcdefgh", "testorg", "testbucket", actorSystem);33 InfluxdbReader influxDBReader = new InfluxdbReader(influxURL, "abcdefgh", "testorg", "testbucket", actorSystem);34 @BeforeAll35 public static void setupBeforeClass() throws IOException, InterruptedException {36 // We use the new official docker image, which has the (now separate cli) installed37 // Doc: https://docs.influxdata.com/influxdb/v2.1/reference/release-notes/influxdb/38 LOGGER.info("InfluxDB container listening on port: {}. Running: {} ", influxDBContainer.getMappedPort(INFLUXDB_PORT), influxDBContainer.isRunning());39 Container.ExecResult result = influxDBContainer.execInContainer("influx", "setup", "-b", "testbucket", "-f", "-o", "testorg", "-t", "abcdefgh", "-u", "admin", "-p", "adminadmin");40 LOGGER.info("Result exit code: " + result.getExitCode());41 LOGGER.info("Result stdout: " + result.getStdout());42 browserClient();43 }44 @AfterAll45 public static void shutdownAfterClass() throws InterruptedException {46 LOGGER.info("Sleep to keep influxdb instance running...");47 Thread.sleep(10000000);48 }49 @BeforeEach50 public void setupBeforeTest(TestInfo testInfo) {51 searchAfterPattern = String.format("Starting test: %s", testInfo.getTestMethod().toString());52 LOGGER.info(searchAfterPattern);53 }54 @Test55 @Order(1)56 public void testWriteAndRead() {57 int maxClients = 5;58 int nPoints = 1000;59 List<CompletionStage<Done>> futList = IntStream.rangeClosed(1, maxClients).boxed().parallel()60 .map(i -> influxDBWriter.writeTestPoints(nPoints, "sensor" + i))61 .collect(Collectors.toList());62 assertThat(CompletableFuture.allOf(futList.toArray(new CompletableFuture[futList.size()]))).succeedsWithin(4 * maxClients, TimeUnit.SECONDS);63 assertThat(influxDBReader.getQuerySync("testMem").length()).isEqualTo(nPoints * maxClients);64 assertThat(influxDBReader.fluxQueryCount("testMem")).isEqualTo(nPoints * maxClients);65 assertThat(new LogFileScanner("logs/application.log").run(1, 2, searchAfterPattern, "ERROR").length()).isEqualTo(0);66 }67 @Test68 @Order(2)69 public void testWriteAndReadLineProtocol() throws ExecutionException, InterruptedException {70 int nPoints = 10;71 influxDBWriter.writeTestPointsFromLineProtocolSync();72 assertThat(influxDBReader.getQuerySync("testMemLP").length()).isEqualTo(nPoints);73 }74 @Test75 @Order(3)76 public void testWriteContinuously() throws ExecutionException, InterruptedException {77 influxDBReader.run();78 influxDBWriter.writeTestPointEverySecond("sensorPeriodic");79 }80 // login with admin/adminadmin81 private static void browserClient() throws IOException {82 String os = System.getProperty("os.name").toLowerCase();83 String influxURL = "http://localhost:" + influxDBContainer.getMappedPort(INFLUXDB_PORT);84 if (os.equals("mac os x")) {85 Runtime.getRuntime().exec("open " + influxURL);86 } else {87 LOGGER.info("Please open a browser at: {}", influxURL);88 }89 }90}...
Source:InfluxDBIntegrationTest.java
...12import org.springframework.context.ApplicationContextInitializer;13import org.springframework.context.ConfigurableApplicationContext;14import org.springframework.test.context.ContextConfiguration;15import org.springframework.test.context.junit4.SpringRunner;16import org.testcontainers.containers.InfluxDBContainer;17import org.testcontainers.junit.jupiter.Container;18import org.testcontainers.junit.jupiter.Testcontainers;19import org.testcontainers.utility.DockerImageName;20import static io.restassured.RestAssured.given;21@Testcontainers22@RunWith(SpringRunner.class)23@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)24@ContextConfiguration(initializers = {InfluxDBIntegrationTest.Initializer.class})25public class InfluxDBIntegrationTest {26 @LocalServerPort27 int localPort;28 @Container29 public static InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(DockerImageName.parse("influxdb:1.4.3"));30 static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {31 @SneakyThrows32 @Override33 public void initialize(ConfigurableApplicationContext configurableApplicationContext) {34 influxDBContainer.start();35 TestPropertyValues36 .of("influxdb.url=" + influxDBContainer.getUrl(),37 "influxdb.password=" + "password",38 "influxdb.username=" + "admin")39 .applyTo(configurableApplicationContext.getEnvironment());40 }41 }42 @Before43 public void setUp() {...
Source:JavaHelper.java
1package ch.hevs.pim.test;2import org.influxdb.InfluxDB;3import org.junit.ClassRule;4import org.testcontainers.containers.InfluxDBContainer;5import org.testcontainers.containers.output.Slf4jLogConsumer;6import org.slf4j.Logger;7import org.slf4j.LoggerFactory;8public class JavaHelper {9 @ClassRule10 public static InfluxDBContainer influxDbContainer = new InfluxDBContainer().withAuthEnabled(false);11 12 private static org.slf4j.Logger logger = LoggerFactory.getLogger(JavaHelper.class);13 14 public static InfluxDB getNewInfluxDB() {15 influxDbContainer.followOutput(new Slf4jLogConsumer(logger));16 return influxDbContainer.getNewInfluxDB();17 }18}
InfluxDBContainer
Using AI Code Generation
1import org.testcontainers.containers.InfluxDBContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.utility.DockerImageName;4import org.slf4j.Logger;5import org.slf4j.LoggerFactory;6public class InfluxDBContainerTest {7 private static final Logger LOGGER = LoggerFactory.getLogger(InfluxDBContainerTest.class);8 public static void main(String[] args) {9 InfluxDBContainer influxDBContainer = new InfluxDBContainer(10 DockerImageName.parse("influxdb:1.8.4").asCompatibleSubstituteFor("influxdb"));11 influxDBContainer.withLogConsumer(new Slf4jLogConsumer(LOGGER));12 influxDBContainer.start();13 LOGGER.info("InfluxDB container started with URL: {} and version: {}", influxDBContainer.getHttpUrl(), influxDBContainer.getVersion());14 LOGGER.info("InfluxDB container started with admin credentials: {} and password: {}", influxDBContainer.getUsername(), influxDBContainer.getPassword());15 }16}
InfluxDBContainer
Using AI Code Generation
1import org.testcontainers.containers.InfluxDBContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5public class 1 {6 private static final Logger LOGGER = LoggerFactory.getLogger(1.class);7 public static void main(String[] args) {8 LOGGER.info("Starting InfluxDB container");9 InfluxDBContainer influxDBContainer = new InfluxDBContainer("influxdb:1.8.3")10 .withLogConsumer(new Slf4jLogConsumer(LOGGER));11 influxDBContainer.start();12 LOGGER.info("InfluxDB container started");13 }14}152020-11-24 17:25:34.820 INFO 1: Starting InfluxDB container (1.java:10)
InfluxDBContainer
Using AI Code Generation
1package com.mycompany.app;2import org.testcontainers.containers.InfluxDBContainer;3{4 public static void main( String[] args )5 {6 InfluxDBContainer influxDBContainer = new InfluxDBContainer();7 influxDBContainer.start();8 System.out.println( "Hello World!" );9 }10}
InfluxDBContainer
Using AI Code Generation
1import org.testcontainers.containers.InfluxDBContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5public class 1 {6 private static final Logger log = LoggerFactory.getLogger(1.class);7 public static void main(String[] args) {8 InfluxDBContainer influxDBContainer = new InfluxDBContainer();9 influxDBContainer.withLogConsumer(new Slf4jLogConsumer(log));10 influxDBContainer.start();11 String influxDBUrl = influxDBContainer.getHttpUrl();12 String influxDBUsername = influxDBContainer.getUsername();13 String influxDBPassword = influxDBContainer.getPassword();14 influxDBContainer.stop();15 }16}
InfluxDBContainer
Using AI Code Generation
1import org.testcontainers.containers.InfluxDBContainer;2public class InfluxDBContainerExample {3 public static void main(String[] args) {4 InfluxDBContainer influxDBContainer = new InfluxDBContainer();5 influxDBContainer.start();6 String url = influxDBContainer.getHttpUrl();7 System.out.println("URL of the container is: " + url);8 influxDBContainer.stop();9 }10}
InfluxDBContainer
Using AI Code Generation
1import org.testcontainers.containers.InfluxDBContainer;2import org.testcontainers.utility.DockerImageName;3public class InfluxDBContainerExample {4 public static void main(String[] args) {5 DockerImageName influxDBImage = DockerImageName.parse("influxdb:2.0.4");6 try (InfluxDBContainer influxDBContainer = new InfluxDBContainer(influxDBImage)) {7 influxDBContainer.start();8 String influxDBUrl = influxDBContainer.getHttpUrl();9 String influxDBAdminUser = influxDBContainer.getUsername();10 String influxDBAdminPassword = influxDBContainer.getPassword();11 System.out.println("InfluxDB URL: " + influxDBUrl);12 System.out.println("InfluxDB Admin User: " + influxDBAdminUser);13 System.out.println("InfluxDB Admin Password: " + influxDBAdminPassword);14 }15 }16}17import org.testcontainers.containers.InfluxDBContainer;18import org.testcontainers.utility.DockerImageName;19public class InfluxDBContainerExample {20 public static void main(String[] args) {21 DockerImageName influxDBImage = DockerImageName.parse("influxdb:2.0.4");22 try (InfluxDBContainer influxDBContainer = new InfluxDBContainer(influxDBImage)) {23 influxDBContainer.start();24 String influxDBUrl = influxDBContainer.getHttpUrl();25 String influxDBAdminUser = influxDBContainer.getUsername();26 String influxDBAdminPassword = influxDBContainer.getPassword();27 System.out.println("InfluxDB URL: " + influxDBUrl);28 System.out.println("InfluxDB Admin User: " + influxDBAdminUser);29 System.out.println("InfluxDB Admin Password: " + influxDBAdminPassword);30 }31 }32}
InfluxDBContainer
Using AI Code Generation
1package com.influxdb.testcontainers;2import org.testcontainers.containers.InfluxDBContainer;3public class InfluxDBTest {4 public static void main(String[] args) {5 InfluxDBContainer container = new InfluxDBContainer("influxdb:1.8.0");6 container.start();7 System.out.println("InfluxDB container is started");8 System.out.println("InfluxDB container URL is: " + container.getHttpUrl());9 System.out.println("InfluxDB container admin username is: " + container.getUsername());10 System.out.println("InfluxDB container admin password is: " + container.getPassword());11 container.stop();12 System.out.println("InfluxDB container is stopped");13 }14}15InfluxDBContainer container = new InfluxDBContainer("influxdb:1.8.0");16container.start();17System.out.println("InfluxDB container is started");18System.out.println("InfluxDB container URL is: " + container.getHttpUrl());19System.out.println("InfluxDB container admin username is: " + container.getUsername());20System.out.println("InfluxDB container admin password is: " + container.getPassword());21container.stop();22System.out.println("InfluxDB container is stopped");
InfluxDBContainer
Using AI Code Generation
1public class InfluxDBContainerTest {2 public static void main(String[] args) {3 try (InfluxDBContainer influxDBContainer = new InfluxDBContainer()) {4 influxDBContainer.start();5 String influxDBContainerURL = influxDBContainer.getHttpUrl();6 System.out.println("URL of the InfluxDB container: " + influxDBContainerURL);7 }8 }9}10withCreateContainerCmdModifier(CreateContainerCmdModifier create
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!!