|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.arcadedb.remote.RemoteDatabase; |
| 4 | +import com.arcadedb.remote.RemoteServer; |
| 5 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 6 | +import lombok.Getter; |
| 7 | +import lombok.NonNull; |
| 8 | +import org.apache.commons.io.IOUtils; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 12 | +import org.testcontainers.utility.DockerImageName; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URL; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +/** |
| 20 | + * Testcontainers implementation for ArcadeDB. |
| 21 | + * <p> |
| 22 | + * Supported image: {@code arcadedb} with JDK version up to 17 |
| 23 | + * <p> |
| 24 | + * Exposed ports: |
| 25 | + * <ul> |
| 26 | + * <li>Database: 2424</li> |
| 27 | + * <li>Remote database and Studio: 2480</li> |
| 28 | + * </ul> |
| 29 | + */ |
| 30 | +public class ArcadeDBContainer extends GenericContainer<ArcadeDBContainer> { |
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(ArcadeDBContainer.class); |
| 33 | + |
| 34 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("arcadedata/arcadedb"); |
| 35 | + |
| 36 | + private static final String DEFAULT_SERVER_PASSWORD = "playwithdata"; |
| 37 | + |
| 38 | + private static final String DEFAULT_DATABASE_NAME = "testcontainers"; |
| 39 | + |
| 40 | + private static final int DEFAULT_BINARY_PORT = 2424; |
| 41 | + |
| 42 | + private static final int DEFAULT_HTTP_PORT = 2480; |
| 43 | + |
| 44 | + @Getter |
| 45 | + private String databaseName; |
| 46 | + |
| 47 | + private String serverPassword; |
| 48 | + |
| 49 | + private int serverPort; |
| 50 | + |
| 51 | + private Optional<String> scriptPath = Optional.empty(); |
| 52 | + |
| 53 | + private RemoteServer remoteServer; |
| 54 | + private RemoteDatabase database; |
| 55 | + |
| 56 | + public ArcadeDBContainer(@NonNull String dockerImageName) { |
| 57 | + this(DockerImageName.parse(dockerImageName)); |
| 58 | + } |
| 59 | + |
| 60 | + public ArcadeDBContainer(final DockerImageName dockerImageName) { |
| 61 | + super(dockerImageName); |
| 62 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 63 | + |
| 64 | + serverPort = DEFAULT_HTTP_PORT; |
| 65 | + serverPassword = DEFAULT_SERVER_PASSWORD; |
| 66 | + databaseName = DEFAULT_DATABASE_NAME; |
| 67 | + |
| 68 | + waitStrategy = Wait.forLogMessage(".*ArcadeDB Server started.*", 1); |
| 69 | + |
| 70 | + addExposedPorts(DEFAULT_BINARY_PORT, DEFAULT_HTTP_PORT); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void configure() { |
| 75 | + final String javaOpts = String.format("-Darcadedb.server.rootPassword=%s", serverPassword); |
| 76 | + |
| 77 | + addEnv("JAVA_OPTS", javaOpts); |
| 78 | + } |
| 79 | + |
| 80 | + public synchronized RemoteDatabase getDatabase() { |
| 81 | + |
| 82 | + final String host = getHost(); |
| 83 | + final Integer port = getMappedPort(serverPort); |
| 84 | + if (remoteServer == null) { |
| 85 | + try { |
| 86 | + remoteServer = new RemoteServer(host, port, "root", serverPassword); |
| 87 | + } catch (Exception e) { |
| 88 | + final String msg = String.format("Could not connect to server %s:%d with user 'root' due to %s", |
| 89 | + host, port, e.getMessage()); |
| 90 | + LOGGER.error(msg, e); |
| 91 | + throw new IllegalStateException(msg, e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (!remoteServer.exists(getDatabaseName())) { |
| 96 | + remoteServer.create(getDatabaseName()); |
| 97 | + } |
| 98 | + |
| 99 | + if (database != null && database.isOpen()) { |
| 100 | + return database; |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + database = new RemoteDatabase(host, port, getDatabaseName(), "root", serverPassword); |
| 105 | + scriptPath.ifPresent(path -> loadScript(path, database)); |
| 106 | + return database; |
| 107 | + } catch (Exception e) { |
| 108 | + final String msg = String.format("Could not connect to database %s on server %s:%d due to %s", |
| 109 | + getDatabaseName(), host, port, e.getMessage()); |
| 110 | + LOGGER.error(msg, e); |
| 111 | + throw new IllegalStateException(msg, e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public ArcadeDBContainer withDatabaseName(final String databaseName) { |
| 116 | + this.databaseName = databaseName; |
| 117 | + return self(); |
| 118 | + } |
| 119 | + |
| 120 | + public ArcadeDBContainer withServerPassword(final String serverPassword) { |
| 121 | + this.serverPassword = serverPassword; |
| 122 | + return self(); |
| 123 | + } |
| 124 | + |
| 125 | + public ArcadeDBContainer withServerPort(final int serverPort) { |
| 126 | + this.serverPort = serverPort; |
| 127 | + return self(); |
| 128 | + } |
| 129 | + |
| 130 | + public ArcadeDBContainer withScriptPath(String scriptPath) { |
| 131 | + this.scriptPath = Optional.of(scriptPath); |
| 132 | + return self(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 137 | + final String host = getHost(); |
| 138 | + final Integer port = getMappedPort(serverPort); |
| 139 | + |
| 140 | + try { |
| 141 | + remoteServer = new RemoteServer(host, port, "root", serverPassword); |
| 142 | + } catch (Exception e) { |
| 143 | + final String msg = String.format("Could not connect to server %s:%d with user 'root' due to %s", |
| 144 | + host, port, e.getMessage()); |
| 145 | + LOGGER.error(msg, e); |
| 146 | + throw new IllegalStateException(msg, e); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void loadScript(String path, RemoteDatabase db) { |
| 151 | + try { |
| 152 | + URL resource = getClass().getClassLoader().getResource(path); |
| 153 | + |
| 154 | + if (resource == null) { |
| 155 | + LOGGER.warn("Could not load classpath init script: {}", scriptPath); |
| 156 | + throw new RuntimeException( |
| 157 | + "Could not load classpath init script: " + scriptPath + ". Resource not found." |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + String script = IOUtils.toString(resource, StandardCharsets.UTF_8); |
| 162 | + |
| 163 | + db.command("sqlscript", script); |
| 164 | + } catch (IOException e) { |
| 165 | + LOGGER.warn("Could not load classpath init script: {}", scriptPath); |
| 166 | + throw new RuntimeException("Could not load classpath init script: " + scriptPath, e); |
| 167 | + } catch (UnsupportedOperationException e) { |
| 168 | + LOGGER.error("Error while executing init script: {}", scriptPath, e); |
| 169 | + throw new RuntimeException("Error while executing init script: " + scriptPath, e); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments