Best Testcontainers-java code snippet using org.testcontainers.utility.AuditLogger
Source:AuditLoggingDockerClient.java
...3import com.github.dockerjava.api.command.*;4import lombok.experimental.Delegate;5import lombok.extern.slf4j.Slf4j;6import org.jetbrains.annotations.NotNull;7import org.testcontainers.utility.AuditLogger;8import java.lang.reflect.InvocationTargetException;9import java.lang.reflect.Proxy;10import java.util.function.BiConsumer;11/**12 * Wrapper for {@link DockerClient} to facilitate 'audit logging' of potentially destruction actions using13 * {@link org.testcontainers.utility.AuditLogger}.14 *15 */16@Slf4j17@SuppressWarnings("unchecked")18class AuditLoggingDockerClient implements DockerClient {19 @Delegate(excludes = InterceptedMethods.class)20 private final DockerClient wrappedClient;21 public AuditLoggingDockerClient(DockerClient wrappedClient) {22 this.wrappedClient = wrappedClient;23 }24 @Override25 public CreateContainerCmd createContainerCmd(@NotNull String image) {26 return wrappedCommand(27 CreateContainerCmd.class,28 wrappedClient.createContainerCmd(image),29 (cmd, res) -> AuditLogger.doLog("CREATE", image, res.getId(), cmd),30 (cmd, e) -> AuditLogger.doLog("CREATE", image, null, cmd, e)31 );32 }33 @Override34 public StartContainerCmd startContainerCmd(@NotNull String containerId) {35 return wrappedCommand(36 StartContainerCmd.class,37 wrappedClient.startContainerCmd(containerId),38 (cmd, res) -> AuditLogger.doLog("START", null, containerId, cmd),39 (cmd, e) -> AuditLogger.doLog("START", null, containerId, cmd, e)40 );41 }42 @Override43 public RemoveContainerCmd removeContainerCmd(@NotNull String containerId) {44 return wrappedCommand(45 RemoveContainerCmd.class,46 wrappedClient.removeContainerCmd(containerId),47 (cmd, res) -> AuditLogger.doLog("REMOVE", null, containerId, cmd),48 (cmd, e) -> AuditLogger.doLog("REMOVE", null, containerId, cmd, e)49 );50 }51 @Override52 public StopContainerCmd stopContainerCmd(@NotNull String containerId) {53 return wrappedCommand(54 StopContainerCmd.class,55 wrappedClient.stopContainerCmd(containerId),56 (cmd, res) -> AuditLogger.doLog("STOP", null, containerId, cmd),57 (cmd, e) -> AuditLogger.doLog("STOP", null, containerId, cmd, e)58 );59 }60 @Override61 public KillContainerCmd killContainerCmd(@NotNull String containerId) {62 return wrappedCommand(63 KillContainerCmd.class,64 wrappedClient.killContainerCmd(containerId),65 (cmd, res) -> AuditLogger.doLog("KILL", null, containerId, cmd),66 (cmd, e) -> AuditLogger.doLog("KILL", null, containerId, cmd, e)67 );68 }69 @Override70 public CreateNetworkCmd createNetworkCmd() {71 return wrappedCommand(72 CreateNetworkCmd.class,73 wrappedClient.createNetworkCmd(),74 (cmd, res) -> AuditLogger.doLog("CREATE_NETWORK", null, null, cmd),75 (cmd, e) -> AuditLogger.doLog("CREATE_NETWORK", null, null, cmd, e)76 );77 }78 @Override79 public RemoveNetworkCmd removeNetworkCmd(@NotNull String networkId) {80 return wrappedCommand(81 RemoveNetworkCmd.class,82 wrappedClient.removeNetworkCmd(networkId),83 (cmd, res) -> AuditLogger.doLog("REMOVE_NETWORK", null, null, cmd),84 (cmd, e) -> AuditLogger.doLog("REMOVE_NETWORK", null, null, cmd, e)85 );86 }87 private <T extends SyncDockerCmd<R>, R> T wrappedCommand(88 Class<T> clazz,89 T cmd,90 BiConsumer<T, R> successConsumer,91 BiConsumer<T, Exception> failureConsumer92 ) {93 return (T) Proxy.newProxyInstance(94 clazz.getClassLoader(),95 new Class<?>[] { clazz },96 (proxy, method, args) -> {97 if (method.getName().equals("exec")) {98 try {...
AuditLogger
Using AI Code Generation
1import org.junit.jupiter.api.Test;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.utility.AuditLogger;4public class AuditLoggerTest {5 public void testAuditLogger() {6 AuditLogger.disable();7 AuditLogger.enable();
AuditLogger
Using AI Code Generation
1import org.testcontainers.containers.PostgreSQLContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.utility.AuditLogger;4public class AuditLoggerExample {5 public static void main(String[] args) {6 PostgreSQLContainer postgres = new PostgreSQLContainer();7 postgres.start();8 AuditLogger.INSTANCE.log("PostgreSQL started");9 postgres.followOutput(new OutputFrame.OutputFrameVisitor() {10 public void accept(OutputFrame outputFrame) {11 AuditLogger.INSTANCE.log(outputFrame.getUtf8String());12 }13 });14 }15}16AuditLogger#log(String message, Object... args) method is used to log the message with the default log level, which is TRACE. It also
AuditLogger
Using AI Code Generation
1import org.testcontainers.containers.PostgreSQLContainer2import org.testcontainers.utility.AuditLogger3import org.testcontainers.utility.DockerImageName4def postgresContainer = new PostgreSQLContainer(DockerImageName.parse("postgres:12.3"))5postgresContainer.withLogConsumer(AuditLogger::log)6postgresContainer.start()
AuditLogger
Using AI Code Generation
1import org.testcontainers.utility.AuditLogger;2import org.testcontainers.utility.LogUtils;3public class TestContainerAuditLogger {4 public static void main(String[] args) {5 AuditLogger.INSTANCE.log("This is a test log");6 AuditLogger.INSTANCE.log("This is a test log with throwable", new RuntimeException("Test throwable"));7 LogUtils.logContainerStartupDetails("This is a test log with container id", "Container id");8 }9}10 at com.testcontainers.TestContainerAuditLogger.main(TestContainerAuditLogger.java:11)
AuditLogger
Using AI Code Generation
1try (final PostgreSQLContainer<?> container = new PostgreSQLContainer<>()) {2 container.start();3 final String jdbcUrl = container.getJdbcUrl();4 final String username = container.getUsername();5 final String password = container.getPassword();6 try (final AuditLogger auditLogger = new AuditLogger()) {7 final List<String> logs = auditLogger.getLogs();8 }9}10try (final PostgreSQLContainer<?> container = new PostgreSQLContainer<>()) {11 container.start();12 final String jdbcUrl = container.getJdbcUrl();13 final String username = container.getUsername();14 final String password = container.getPassword();15}16try (final PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageName.parse("postgres:13.2"))) {17 container.withDatabaseName("mydb")18 .withUsername("user")19 .withPassword("pass")20 .withExposedPorts(5432)21 .withEnv("POSTGRES_INITDB_ARGS", "-E UTF-8");22 container.start();23 final String jdbcUrl = container.getJdbcUrl();24 final String username = container.getUsername();25 final String password = container.getPassword();26}27The above code uses the latest version of PostgreSQL (13.2), the database name is mydb , username is user , password is pass , and the exposed port is 5432 . To learn more about configuring the container
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!!