Skip to content

Commit

Permalink
[#466] JDBC: added tests for Oracle, MySQL, MSSQL (#472)
Browse files Browse the repository at this point in the history
Co-authored-by: Valery Kharseko <[email protected]>
  • Loading branch information
maximthomas and vharseko authored Feb 3, 2025
1 parent 6c4ab89 commit 2050033
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 17 deletions.
40 changes: 39 additions & 1 deletion opendj-server-legacy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
information: "Portions Copyright [year] [name of copyright owner]".
Copyright 2011-2016 ForgeRock AS.
Portions Copyright 2017-2024 3A Systems, LLC.
Portions Copyright 2017-2025 3A Systems, LLC.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

Expand Down Expand Up @@ -281,6 +281,44 @@
<version>1.20.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-free</artifactId>
<version>1.20.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.20.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mssqlserver</artifactId>
<version>1.20.4</version>
<scope>test</scope>
</dependency>

<!-- test JDBC drivers -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>23.6.0.24.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.8.1.jre8</version>
<scope>test</scope>
</dependency>
</dependencies>

<build><finalName>${project.groupId}.${project.artifactId}</finalName>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2025 3A Systems, LLC.
*/
package org.opends.server.backends.jdbc;

import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MSSQLServerContainer;
import org.testng.annotations.Test;

//docker run --rm --name mssql -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=Passw0rd -p 1433:1433 mcr.microsoft.com/mssql/server:2017-CU12

@Test
public class MsSqlTestCase extends TestCase {

@Override
protected JdbcDatabaseContainer<?> getContainer() {
return new MSSQLServerContainer<>("mcr.microsoft.com/mssql/server:2019-CU30-ubuntu-20.04")
.withExposedPorts(1433)
.acceptLicense()
.withPassword("Passw0rd");

}

@Override
protected String getContainerDockerCommand() {
return "run before test: docker run --rm --name mssql -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=Passw0rd -p 1433:1433 mcr.microsoft.com/mssql/server:2017-CU12";
}

@Override
protected String getBackendId() {
return MsSqlTestCase.class.getSimpleName();
}

@Override
protected String getJdbcUrl() {
return "jdbc:sqlserver://localhost:" + ((container==null)?"1433":container.getMappedPort(1433)) + ";encrypt=false;user=sa;password=Passw0rd;";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2025 3A Systems, LLC.
*/
package org.opends.server.backends.jdbc;

import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testng.annotations.Test;

//docker run --rm --name mysql -p 3306:3306 -e MYSQL_DATABASE=database_name -e MYSQL_ROOT_PASSWORD=password mysql:latest

@Test
public class MySqlTestCase extends TestCase {

@Override
protected JdbcDatabaseContainer<?> getContainer() {
return new MySQLContainer<>("mysql")
.withExposedPorts(3306)
.withUsername("root")
.withPassword("password")
.withDatabaseName("database_name");
}

@Override
protected String getContainerDockerCommand() {
return "run before test: docker run --rm --name mysql -p 3306:3306 -e MYSQL_DATABASE=database_name -e MYSQL_ROOT_PASSWORD=password mysql:latest";
}

@Override
protected String getBackendId() {
return MySqlTestCase.class.getSimpleName();
}

@Override
protected String getJdbcUrl() {
return "jdbc:mysql://root:password@localhost:" + ((container==null)?"3306":container.getMappedPort(3306)) + "/database_name";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2025 3A Systems, LLC.
*/
package org.opends.server.backends.jdbc;

import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.oracle.OracleContainer;
import org.testng.annotations.Test;

//docker run --rm --name oracle-db -p 1521:1521 -e APP_USER=opendj -e ORACLE_DATABASE=database_name -e APP_USER_PASSWORD=password gvenzl/oracle-free:23.4-slim-faststart

@Test
public class OracleTestCase extends TestCase {

@Override
protected JdbcDatabaseContainer<?> getContainer() {
return new OracleContainer("gvenzl/oracle-free:23.4-slim-faststart")
.withExposedPorts(1521)
.withUsername("opendj")
.withPassword("password")
.withDatabaseName("database_name");
}

@Override
protected String getContainerDockerCommand() {
return "run before test: docker run --rm --name oracle-db -p 1521:1521 -e APP_USER=opendj -e ORACLE_DATABASE=database_name -e APP_USER_PASSWORD=password gvenzl/oracle-free:23.4-slim-faststart";
}

@Override
protected String getBackendId() {
return OracleTestCase.class.getSimpleName();
}

@Override
protected String getJdbcUrl() {
return "jdbc:oracle:thin:opendj/password@localhost: " + ((container==null)?"1521":container.getMappedPort(1521)) + "/database_name";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2025 3A Systems, LLC.
*/
package org.opends.server.backends.jdbc;

import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testng.annotations.Test;

//docker run --rm -it -p 5432:5432 -e POSTGRES_PASSWORD=password --name postgres postgres

@Test
public class PgSqlTestCase extends TestCase {

@Override
protected JdbcDatabaseContainer<?> getContainer() {
return new PostgreSQLContainer<>("postgres:latest")
.withExposedPorts(5432)
.withUsername("postgres")
.withPassword("password")
.withDatabaseName("database_name");
}

@Override
protected String getContainerDockerCommand() {
return "run before test: docker run --rm -it -p 5432:5432 -e POSTGRES_DB=database_name -e POSTGRES_PASSWORD=password --name postgres postgres";
}

@Override
protected String getBackendId() {
return PgSqlTestCase.class.getSimpleName();
}

@Override
protected String getJdbcUrl() {
return "jdbc:postgresql://localhost:"+ ((container==null)?"5432":container.getMappedPort(5432))+"/database_name?user=postgres&password=password";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,41 @@
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2024 3A Systems, LLC.
* Copyright 2024-2025 3A Systems, LLC.
*/
package org.opends.server.backends.jdbc;

import org.forgerock.opendj.server.config.server.JDBCBackendCfg;
import org.opends.server.backends.pluggable.PluggableBackendImplTestCase;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.sql.Connection;
import java.sql.DriverManager;

import static org.forgerock.opendj.config.ConfigurationMock.mockCfg;
import static org.mockito.Mockito.when;

//docker run --rm -it -p 5432:5432 -e POSTGRES_PASSWORD=password --name postgres postgres

@Test
public class TestCase extends PluggableBackendImplTestCase<JDBCBackendCfg> {

PostgreSQLContainer container;
public abstract class TestCase extends PluggableBackendImplTestCase<JDBCBackendCfg> {

JdbcDatabaseContainer container;

@BeforeClass
@Override
public void setUp() throws Exception {
if(DockerClientFactory.instance().isDockerAvailable()) {
container = new PostgreSQLContainer<>("postgres:latest")
.withExposedPorts(5432)
.withUsername("postgres")
.withPassword("password")
.withDatabaseName("database_name");
container = getContainer();
container.start();
}
try(Connection con= DriverManager.getConnection(createBackendCfg().getDBDirectory())){
try(Connection ignored = DriverManager.getConnection(createBackendCfg().getDBDirectory())){

} catch (Exception e) {
throw new SkipException("run before test: docker run --rm -it -p 5432:5432 -e POSTGRES_DB=database_name -e POSTGRES_PASSWORD=password --name postgres postgres");
throw new SkipException(getContainerDockerCommand());
}
super.setUp();
}
Expand All @@ -63,8 +58,8 @@ protected Backend createBackend() {
@Override
protected JDBCBackendCfg createBackendCfg() {
JDBCBackendCfg backendCfg = mockCfg(JDBCBackendCfg.class);
when(backendCfg.getBackendId()).thenReturn("PsqlTestCase");
when(backendCfg.getDBDirectory()).thenReturn("jdbc:postgresql://localhost:"+ ((container==null)?"5432":container.getMappedPort(5432))+"/database_name?user=postgres&password=password");
when(backendCfg.getBackendId()).thenReturn(getBackendId());
when(backendCfg.getDBDirectory()).thenReturn(getJdbcUrl());
return backendCfg;
}

Expand All @@ -76,4 +71,12 @@ public void cleanUp() throws Exception {
container.close();
}
}

protected abstract JdbcDatabaseContainer<?> getContainer();

protected abstract String getContainerDockerCommand();

protected abstract String getBackendId();

protected abstract String getJdbcUrl();
}

0 comments on commit 2050033

Please sign in to comment.