Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/databases/orientdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Testcontainers module for [OrientDB](https://hub.docker.com/_/orientdb/)
You can start an OrientDB container instance from any Java application by using:

<!--codeinclude-->
[Container creation](../../../modules/orientdb/src/test/java/org/testcontainers/containers/OrientDBContainerTest.java) inside_block:container
[Container creation](../../../modules/orientdb/src/test/java/org/testcontainers/orientdb/OrientDBContainerTest.java) inside_block:container
<!--/codeinclude-->

## Adding this module to your project dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
* <li>Database: 2424</li>
* <li>Studio: 2480</li>
* </ul>
*
* @deprecated use {@link org.testcontainers.orientdb.OrientDBContainer} instead.
*/
@Deprecated
public class OrientDBContainer extends GenericContainer<OrientDBContainer> {

private static final Logger LOGGER = LoggerFactory.getLogger(OrientDBContainer.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.testcontainers.orientdb;

import com.github.dockerjava.api.command.InspectContainerResponse;
import lombok.NonNull;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.DockerImageName;

import java.io.IOException;

/**
* Testcontainers implementation for OrientDB.
* <p>
* Supported image: {@code orientdb}
* <p>
* Exposed ports:
* <ul>
* <li>Database: 2424</li>
* <li>Studio: 2480</li>
* </ul>
*/
public class OrientDBContainer extends GenericContainer<OrientDBContainer> {

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("orientdb");

private static final String DEFAULT_USERNAME = "admin";

private static final String DEFAULT_PASSWORD = "admin";

private static final String DEFAULT_SERVER_USER = "root";

private static final String DEFAULT_SERVER_PASSWORD = "root";

private static final String DEFAULT_DATABASE_NAME = "testcontainers";

private static final int DEFAULT_BINARY_PORT = 2424;

private static final int DEFAULT_HTTP_PORT = 2480;

private String databaseName;

private String serverPassword;

private Transferable scriptPath;

public OrientDBContainer(@NonNull String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

public OrientDBContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

this.serverPassword = DEFAULT_SERVER_PASSWORD;
this.databaseName = DEFAULT_DATABASE_NAME;

waitingFor(Wait.forLogMessage(".*OrientDB Studio available.*", 1));
addExposedPorts(DEFAULT_BINARY_PORT, DEFAULT_HTTP_PORT);
}

@Override
protected void configure() {
addEnv("ORIENTDB_ROOT_PASSWORD", serverPassword);
}

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
try {
String createDb = String.format(
"CREATE DATABASE remote:localhost/%s %s %s plocal; CONNECT remote:localhost/%s %s %s; CREATE USER %s IDENTIFIED BY %s ROLE admin;",
this.databaseName,
DEFAULT_SERVER_USER,
this.serverPassword,
this.databaseName,
DEFAULT_SERVER_USER,
this.serverPassword,
DEFAULT_USERNAME,
DEFAULT_PASSWORD
);
execInContainer("/orientdb/bin/console.sh", createDb);

if (this.scriptPath != null) {
copyFileToContainer(this.scriptPath, "/opt/testcontainers/script.osql");
String loadScript = String.format(
"CONNECT remote:localhost/%s %s %s; LOAD SCRIPT /opt/testcontainers/script.osql",
this.databaseName,
DEFAULT_SERVER_USER,
this.serverPassword
);
execInContainer("/orientdb/bin/console.sh", loadScript);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

public String getDatabaseName() {
return databaseName;
}

public OrientDBContainer withDatabaseName(final String databaseName) {
this.databaseName = databaseName;
return self();
}

public OrientDBContainer withServerPassword(final String serverPassword) {
this.serverPassword = serverPassword;
return self();
}

public OrientDBContainer withScriptPath(Transferable scriptPath) {
this.scriptPath = scriptPath;
return self();
}

public String getServerUrl() {
return "remote:" + getHost() + ":" + getMappedPort(2424);
}

public String getDbUrl() {
return getServerUrl() + "/" + this.databaseName;
}

public String getServerUser() {
return DEFAULT_SERVER_USER;
}

public String getServerPassword() {
return this.serverPassword;
}

public String getUsername() {
return DEFAULT_USERNAME;
}

public String getPassword() {
return DEFAULT_PASSWORD;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.testcontainers.containers;
package org.testcontainers.orientdb;

import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import org.junit.jupiter.api.Test;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
Expand All @@ -12,26 +14,24 @@ class OrientDBContainerTest {
private static final DockerImageName ORIENTDB_IMAGE = DockerImageName.parse("orientdb:3.2.0-tp3");

@Test
void shouldReturnTheSameSession() {
void shouldInitializeWithCommands() {
try ( // container {
OrientDBContainer orientdb = new OrientDBContainer("orientdb:3.2.0-tp3")
// }
) {
orientdb.start();

final ODatabaseSession session = orientdb.getSession();
final ODatabaseSession session2 = orientdb.getSession();

assertThat(session).isSameAs(session2);
}
}

@Test
void shouldInitializeWithCommands() {
try (OrientDBContainer orientdb = new OrientDBContainer(ORIENTDB_IMAGE)) {
orientdb.start();

final ODatabaseSession session = orientdb.getSession();
OrientDB orientDB = new OrientDB(
orientdb.getServerUrl(),
orientdb.getServerUser(),
orientdb.getServerPassword(),
OrientDBConfig.defaultConfig()
);
ODatabaseSession session = orientDB.open(
orientdb.getDatabaseName(),
orientdb.getUsername(),
orientdb.getPassword()
);

session.command("CREATE CLASS Person EXTENDS V");
session.command("INSERT INTO Person set name='john'");
Expand All @@ -52,7 +52,17 @@ void shouldQueryWithGremlin() {
) {
orientdb.start();

final ODatabaseSession session = orientdb.getSession("admin", "admin");
OrientDB orientDB = new OrientDB(
orientdb.getServerUrl(),
orientdb.getServerUser(),
orientdb.getServerPassword(),
OrientDBConfig.defaultConfig()
);
ODatabaseSession session = orientDB.open(
orientdb.getDatabaseName(),
orientdb.getUsername(),
orientdb.getPassword()
);

session.command("CREATE CLASS Person EXTENDS V");
session.command("INSERT INTO Person set name='john'");
Expand All @@ -66,15 +76,25 @@ void shouldQueryWithGremlin() {
void shouldInitializeDatabaseFromScript() {
try (
OrientDBContainer orientdb = new OrientDBContainer(ORIENTDB_IMAGE)
.withScriptPath("initscript.osql")
.withScriptPath(MountableFile.forClasspathResource("initscript.osql"))
.withDatabaseName("persons")
) {
orientdb.start();

assertThat(orientdb.getDbUrl())
.isEqualTo("remote:" + orientdb.getHost() + ":" + orientdb.getMappedPort(2424) + "/persons");

final ODatabaseSession session = orientdb.getSession();
OrientDB orientDB = new OrientDB(
orientdb.getServerUrl(),
orientdb.getServerUser(),
orientdb.getServerPassword(),
OrientDBConfig.defaultConfig()
);
ODatabaseSession session = orientDB.open(
orientdb.getDatabaseName(),
orientdb.getUsername(),
orientdb.getPassword()
);

assertThat(session.query("SELECT FROM Person").stream()).hasSize(4);
}
Expand Down
Loading