|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import com.orientechnologies.orient.core.db.ODatabaseSession; |
| 5 | +import com.orientechnologies.orient.core.db.ODatabaseType; |
| 6 | +import com.orientechnologies.orient.core.db.OrientDB; |
| 7 | +import com.orientechnologies.orient.core.db.OrientDBConfig; |
| 8 | +import lombok.NonNull; |
| 9 | +import org.apache.commons.io.IOUtils; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
| 13 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 14 | +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; |
| 15 | +import org.testcontainers.containers.wait.strategy.WaitStrategy; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.net.URL; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import static java.net.HttpURLConnection.HTTP_OK; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author robfrank |
| 27 | + */ |
| 28 | +public class OrientDBContainer extends GenericContainer<OrientDBContainer> { |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(OrientDBContainer.class); |
| 30 | + |
| 31 | + private static final String DEFAULT_IMAGE_NAME = "orientdb"; |
| 32 | + private static final String DEFAULT_TAG = "3.0.24-tp3"; |
| 33 | + private static final String DOCKER_IMAGE_NAME = DEFAULT_IMAGE_NAME + ":" + DEFAULT_TAG; |
| 34 | + |
| 35 | + private static final String DEFAULT_USERNAME = "admin"; |
| 36 | + private static final String DEFAULT_PASSWORD = "admin"; |
| 37 | + private static final String DEFAULT_SERVER_PASSWORD = "root"; |
| 38 | + |
| 39 | + private static final String DEFAULT_DATABASE_NAME = "testcontainers"; |
| 40 | + |
| 41 | + private static final int DEFAULT_BINARY_PORT = 2424; |
| 42 | + private static final int DEFAULT_HTTP_PORT = 2480; |
| 43 | + |
| 44 | + private String databaseName; |
| 45 | + private String serverPassword; |
| 46 | + private Optional<String> scriptPath = Optional.empty(); |
| 47 | + |
| 48 | + private OrientDB orientDB; |
| 49 | + private ODatabaseSession session; |
| 50 | + |
| 51 | + public OrientDBContainer() { |
| 52 | + this(DOCKER_IMAGE_NAME); |
| 53 | + } |
| 54 | + |
| 55 | + public OrientDBContainer(@NonNull String dockerImageName) { |
| 56 | + super(dockerImageName); |
| 57 | + |
| 58 | + serverPassword = DEFAULT_SERVER_PASSWORD; |
| 59 | + databaseName = DEFAULT_DATABASE_NAME; |
| 60 | + |
| 61 | + WaitStrategy waitForHttp = new HttpWaitStrategy() |
| 62 | + .forPort(DEFAULT_HTTP_PORT) |
| 63 | + .forStatusCodeMatching(response -> response == HTTP_OK); |
| 64 | + |
| 65 | + waitStrategy = new WaitAllStrategy() |
| 66 | + .withStrategy(Wait.forListeningPort()) |
| 67 | + .withStrategy(waitForHttp) |
| 68 | + .withStartupTimeout(Duration.ofMinutes(2)); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void configure() { |
| 73 | + addExposedPorts(DEFAULT_BINARY_PORT, DEFAULT_HTTP_PORT); |
| 74 | + addEnv("ORIENTDB_ROOT_PASSWORD", serverPassword); |
| 75 | + } |
| 76 | + |
| 77 | + public String getDatabaseName() { |
| 78 | + return databaseName; |
| 79 | + } |
| 80 | + |
| 81 | + public String getTestQueryString() { |
| 82 | + return "SELECT FROM V"; |
| 83 | + } |
| 84 | + |
| 85 | + public OrientDBContainer withDatabaseName(final String databaseName) { |
| 86 | + this.databaseName = databaseName; |
| 87 | + return self(); |
| 88 | + } |
| 89 | + |
| 90 | + public OrientDBContainer withServerPassword(final String serverPassword) { |
| 91 | + this.serverPassword = serverPassword; |
| 92 | + return self(); |
| 93 | + } |
| 94 | + |
| 95 | + public OrientDBContainer withScriptPath(String scriptPath) { |
| 96 | + this.scriptPath = Optional.of(scriptPath); |
| 97 | + return self(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 102 | + orientDB = new OrientDB(getServerUrl(), "root", serverPassword, OrientDBConfig.defaultConfig()); |
| 103 | + } |
| 104 | + |
| 105 | + public OrientDB getOrientDB() { |
| 106 | + return orientDB; |
| 107 | + } |
| 108 | + |
| 109 | + public String getServerUrl() { |
| 110 | + return "remote:" + getContainerIpAddress() + ":" + getMappedPort(2424); |
| 111 | + } |
| 112 | + |
| 113 | + public String getDbUrl() { |
| 114 | + return getServerUrl() + "/" + databaseName; |
| 115 | + } |
| 116 | + |
| 117 | + public ODatabaseSession getSession() { |
| 118 | + return getSession(DEFAULT_USERNAME, DEFAULT_PASSWORD); |
| 119 | + } |
| 120 | + |
| 121 | + public synchronized ODatabaseSession getSession(String username, String password) { |
| 122 | + orientDB.createIfNotExists(databaseName, ODatabaseType.PLOCAL); |
| 123 | + |
| 124 | + if (session == null) { |
| 125 | + session = orientDB.open(databaseName, username, password); |
| 126 | + |
| 127 | + scriptPath.ifPresent(path -> loadScript(path, session)); |
| 128 | + } |
| 129 | + return session; |
| 130 | + } |
| 131 | + |
| 132 | + private void loadScript(String path, ODatabaseSession session) { |
| 133 | + try { |
| 134 | + URL resource = getClass().getClassLoader().getResource(path); |
| 135 | + |
| 136 | + if (resource == null) { |
| 137 | + LOGGER.warn("Could not load classpath init script: {}", scriptPath); |
| 138 | + throw new RuntimeException("Could not load classpath init script: " + scriptPath + ". Resource not found."); |
| 139 | + } |
| 140 | + |
| 141 | + String script = IOUtils.toString(resource, StandardCharsets.UTF_8); |
| 142 | + |
| 143 | + session.execute("sql", script); |
| 144 | + } catch (IOException e) { |
| 145 | + LOGGER.warn("Could not load classpath init script: {}", scriptPath); |
| 146 | + throw new RuntimeException("Could not load classpath init script: " + scriptPath, e); |
| 147 | + } catch (UnsupportedOperationException e) { |
| 148 | + LOGGER.error("Error while executing init script: {}", scriptPath, e); |
| 149 | + throw new RuntimeException("Error while executing init script: " + scriptPath, e); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments