|
| 1 | +package org.testcontainers.orientdb; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import lombok.NonNull; |
| 5 | +import org.testcontainers.containers.GenericContainer; |
| 6 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 7 | +import org.testcontainers.images.builder.Transferable; |
| 8 | +import org.testcontainers.utility.DockerImageName; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Testcontainers implementation for OrientDB. |
| 14 | + * <p> |
| 15 | + * Supported image: {@code orientdb} |
| 16 | + * <p> |
| 17 | + * Exposed ports: |
| 18 | + * <ul> |
| 19 | + * <li>Database: 2424</li> |
| 20 | + * <li>Studio: 2480</li> |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +public class OrientDBContainer extends GenericContainer<OrientDBContainer> { |
| 24 | + |
| 25 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("orientdb"); |
| 26 | + |
| 27 | + private static final String DEFAULT_USERNAME = "admin"; |
| 28 | + |
| 29 | + private static final String DEFAULT_PASSWORD = "admin"; |
| 30 | + |
| 31 | + private static final String DEFAULT_SERVER_USER = "root"; |
| 32 | + |
| 33 | + private static final String DEFAULT_SERVER_PASSWORD = "root"; |
| 34 | + |
| 35 | + private static final String DEFAULT_DATABASE_NAME = "testcontainers"; |
| 36 | + |
| 37 | + private static final int DEFAULT_BINARY_PORT = 2424; |
| 38 | + |
| 39 | + private static final int DEFAULT_HTTP_PORT = 2480; |
| 40 | + |
| 41 | + private String databaseName; |
| 42 | + |
| 43 | + private String serverPassword; |
| 44 | + |
| 45 | + private Transferable scriptPath; |
| 46 | + |
| 47 | + public OrientDBContainer(@NonNull String dockerImageName) { |
| 48 | + this(DockerImageName.parse(dockerImageName)); |
| 49 | + } |
| 50 | + |
| 51 | + public OrientDBContainer(DockerImageName dockerImageName) { |
| 52 | + super(dockerImageName); |
| 53 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 54 | + |
| 55 | + this.serverPassword = DEFAULT_SERVER_PASSWORD; |
| 56 | + this.databaseName = DEFAULT_DATABASE_NAME; |
| 57 | + |
| 58 | + waitingFor(Wait.forLogMessage(".*OrientDB Studio available.*", 1)); |
| 59 | + addExposedPorts(DEFAULT_BINARY_PORT, DEFAULT_HTTP_PORT); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void configure() { |
| 64 | + addEnv("ORIENTDB_ROOT_PASSWORD", serverPassword); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 69 | + try { |
| 70 | + String createDb = String.format( |
| 71 | + "CREATE DATABASE remote:localhost/%s %s %s plocal; CONNECT remote:localhost/%s %s %s; CREATE USER %s IDENTIFIED BY %s ROLE admin;", |
| 72 | + this.databaseName, |
| 73 | + DEFAULT_SERVER_USER, |
| 74 | + this.serverPassword, |
| 75 | + this.databaseName, |
| 76 | + DEFAULT_SERVER_USER, |
| 77 | + this.serverPassword, |
| 78 | + DEFAULT_USERNAME, |
| 79 | + DEFAULT_PASSWORD |
| 80 | + ); |
| 81 | + execInContainer("/orientdb/bin/console.sh", createDb); |
| 82 | + |
| 83 | + if (this.scriptPath != null) { |
| 84 | + copyFileToContainer(this.scriptPath, "/opt/testcontainers/script.osql"); |
| 85 | + String loadScript = String.format( |
| 86 | + "CONNECT remote:localhost/%s %s %s; LOAD SCRIPT /opt/testcontainers/script.osql", |
| 87 | + this.databaseName, |
| 88 | + DEFAULT_SERVER_USER, |
| 89 | + this.serverPassword |
| 90 | + ); |
| 91 | + execInContainer("/orientdb/bin/console.sh", loadScript); |
| 92 | + } |
| 93 | + } catch (IOException | InterruptedException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public String getDatabaseName() { |
| 99 | + return databaseName; |
| 100 | + } |
| 101 | + |
| 102 | + public OrientDBContainer withDatabaseName(final String databaseName) { |
| 103 | + this.databaseName = databaseName; |
| 104 | + return self(); |
| 105 | + } |
| 106 | + |
| 107 | + public OrientDBContainer withServerPassword(final String serverPassword) { |
| 108 | + this.serverPassword = serverPassword; |
| 109 | + return self(); |
| 110 | + } |
| 111 | + |
| 112 | + public OrientDBContainer withScriptPath(Transferable scriptPath) { |
| 113 | + this.scriptPath = scriptPath; |
| 114 | + return self(); |
| 115 | + } |
| 116 | + |
| 117 | + public String getServerUrl() { |
| 118 | + return "remote:" + getHost() + ":" + getMappedPort(2424); |
| 119 | + } |
| 120 | + |
| 121 | + public String getDbUrl() { |
| 122 | + return getServerUrl() + "/" + this.databaseName; |
| 123 | + } |
| 124 | + |
| 125 | + public String getServerUser() { |
| 126 | + return DEFAULT_SERVER_USER; |
| 127 | + } |
| 128 | + |
| 129 | + public String getServerPassword() { |
| 130 | + return this.serverPassword; |
| 131 | + } |
| 132 | + |
| 133 | + public String getUsername() { |
| 134 | + return DEFAULT_USERNAME; |
| 135 | + } |
| 136 | + |
| 137 | + public String getPassword() { |
| 138 | + return DEFAULT_PASSWORD; |
| 139 | + } |
| 140 | +} |
0 commit comments