Skip to content

Issue 432 - Example using Spring Boot #540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

beargiles
Copy link

This is the preliminary implementation of an example implementation using Spring Boot and Test Containers.

It will successfully execute a SpringBootTest using a PostgreSQLContainer and proper initialization of all spring beans.

The current implementation uses a standard PostgreSQL docker image (from
http://hub.docker.com/_/postgres) so it does not support PL/Java-specific tests but
there is a separate effort to produce our own docker images that will have PL/Java pre-installed.

(The key limitation is that the pljava shared
library is not preinstalled.)

At this time there are no tests other than a 'happy path' test that shows that the test's autowired DataSource is pointing to the TestContainer instance.

+==============================================================================+
|  Database Server : Name         : PostgreSQL                                 |
|                  : Version      : 16.3                                       |
|                  : URL          : jdbc:postgresql://localhost:32834/test     |
+------------------+--------------+--------------------------------------------+
|  Driver          : Name         : PostgreSQL JDBC Driver                     |
|                  : Version      : 42.6.0                                     |
|                  : JDBC Version : 4.2                                        |
+------------------+--------------+--------------------------------------------+
|  Client          : User         : test                                       |
|                  : Connection   : com.zaxxer.hikari.pool.HikariProxyConnecti |
+------------------+--------------+--------------------------------------------+
|  Client Host     : User         : bgiles                                     |
|                  : Hostname     : eris.coyotesong.net                        |
|                  : OS Name      : Ubuntu 24.04.2 LTS                         |
|                  : OS Kernel    : 6.8.0-59-generic                           |
+==============================================================================+

This is the preliminary implementation of an example
implementation using Spring Boot and Test Containers.

It will successfully execute a `SpringBootTest` using
a PostgreSQLContainer and proper initialization of all
spring beans.

The current implementation uses a standard PostgreSQL
docker image (from
[http://hub.docker.com/_/postgres](https://hub.docker.com/_/postgres))
so it does not support PL/Java-specific tests but
there is a separate effort to produce our own docker
images that will have PL/Java pre-installed.

(The key limitation is that the pljava shared
library is not preinstalled.)

At this time there are no tests other than a 'happy path'
test that shows that the test's autowired `DataSource`
is pointing to the TestContainer instance.

```
+==============================================================================+
|  Database Server : Name         : PostgreSQL                                 |
|                  : Version      : 16.3                                       |
|                  : URL          : jdbc:postgresql://localhost:32834/test     |
+------------------+--------------+--------------------------------------------+
|  Driver          : Name         : PostgreSQL JDBC Driver                     |
|                  : Version      : 42.6.0                                     |
|                  : JDBC Version : 4.2                                        |
+------------------+--------------+--------------------------------------------+
|  Client          : User         : test                                       |
|                  : Connection   : com.zaxxer.hikari.pool.HikariProxyConnecti |
+------------------+--------------+--------------------------------------------+
|  Client Host     : User         : bgiles                                     |
|                  : Hostname     : eris.coyotesong.net                        |
|                  : OS Name      : Ubuntu 24.04.2 LTS                         |
|                  : OS Kernel    : 6.8.0-59-generic                           |
+==============================================================================+
```
@beargiles
Copy link
Author

Sorry for the huge PR but it had to cover a lot of territory... and it's still missing some things. (Esp. documentation!)

The README.md file has more details but the gist is that this introduces a new module with three child modules.

Test-framework

We can ignore that. It does lots of spring magic.

Backend

This is what is pushed to the server. It has one responsibility in addition to creating the jar file - it must create a test docker image.

At the moment I'm making some reasonable assumptions (PostgreSQL 17.3, PLJava version 1.6.9) and also reusing the existing 'pljava-examples-1.6.9.jar' instead of copying the locally built jar into the docker working directory. Those are easy to address later.

It would be nice if there was a clean way to reuse the DDR file.

Application

This is a minimal spring boot application that can use the standard tools provided by spring-test. All of the messy stuff is handled by the test-framework and to a lesser extent the docker image that has the pljava jars preinstalled.

A typical test looks like:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
        classes = {
                PersistenceTestConfiguration.class
        }
)
@ContextConfiguration
@ActiveProfiles("test")
@Testcontainers(disabledWithoutDocker = true)
public class HappyPathTest {
    private static final Logger LOG = LoggerFactory.getLogger(HappyPathTest.class);
    protected static final String LOCAL_IMAGE_NAME = "tada/pljava-examples:17.3-1.6.9-bookworm";

    @Container
    @ServiceConnection
    protected static PostgreSQLContainer<?> postgres = new AugmentedPostgreSQLContainer<>(LOCAL_IMAGE_NAME);

    private final DataSource dataSource;

    @Autowired
    public HappyPathTest(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    // ------------------------------
    // actual tests follow
    // ------------------------------

    @Test
    public void happyPathTest() throws SQLException, IOException {
        try (Connection conn = dataSource.getConnection();
             Statement stmt = conn.createStatement()) {

            try (ResultSet rs = stmt.executeQuery("SELECT javatest.randomInts(10)")) {
                while (rs.next()) {
                    LOG.info(rs.getString(1));
                }
            }
        }
    }
}

This looks nasty if you're unfamiliar with the spring annotations but the key thing here is that there's NO references to the actual TestContainer or database server beyond the initial stanza. Everything goes through the DataSource and the test container could be replaced with an external test server with no other changes than commenting out the @Container lines and adding a Spring bean that provides the required connection credentials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant