dk.kosmisk:postgresql-test-jpa

An integration test helper designed for dk.kosmisk:postgresql-maven-plugin. This produces a test environment where code blocks can be processed in a EntityManager transaction. EclipseLink and Hibernate is supported

License

License

Categories

Categories

PostgreSQL Data Databases
GroupId

GroupId

dk.kosmisk
ArtifactId

ArtifactId

postgresql-test-jpa
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

dk.kosmisk:postgresql-test-jpa
An integration test helper designed for dk.kosmisk:postgresql-maven-plugin. This produces a test environment where code blocks can be processed in a EntityManager transaction. EclipseLink and Hibernate is supported
Project URL

Project URL

https://github.com/kosmisk-dk/postgresql-test-jpa/
Source Code Management

Source Code Management

https://github.com/kosmisk-dk/postgresql-test-jpa/tree/master

Download postgresql-test-jpa

How to add to project

<!-- https://jarcasting.com/artifacts/dk.kosmisk/postgresql-test-jpa/ -->
<dependency>
    <groupId>dk.kosmisk</groupId>
    <artifactId>postgresql-test-jpa</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/dk.kosmisk/postgresql-test-jpa/
implementation 'dk.kosmisk:postgresql-test-jpa:1.0.2'
// https://jarcasting.com/artifacts/dk.kosmisk/postgresql-test-jpa/
implementation ("dk.kosmisk:postgresql-test-jpa:1.0.2")
'dk.kosmisk:postgresql-test-jpa:jar:1.0.2'
<dependency org="dk.kosmisk" name="postgresql-test-jpa" rev="1.0.2">
  <artifact name="postgresql-test-jpa" type="jar" />
</dependency>
@Grapes(
@Grab(group='dk.kosmisk', module='postgresql-test-jpa', version='1.0.2')
)
libraryDependencies += "dk.kosmisk" % "postgresql-test-jpa" % "1.0.2"
[dk.kosmisk/postgresql-test-jpa "1.0.2"]

Dependencies

compile (4)

Group / Artifact Type Version
dk.kosmisk : postgresql-test-datasource jar 1.0.2
javax.xml.bind : jaxb-api jar 2.3.1
org.eclipse.persistence : eclipselink jar 2.7.5
org.hibernate : hibernate-core jar 5.4.8.Final

provided (1)

Group / Artifact Type Version
junit : junit jar 4.12

runtime (1)

Group / Artifact Type Version
org.glassfish.jaxb : jaxb-runtime jar 2.3.1

test (4)

Group / Artifact Type Version
org.eclipse.persistence : org.eclipse.persistence.jpa jar 2.7.5
org.hibernate : hibernate-entitymanager jar 5.4.8.Final
org.postgresql : postgresql jar 42.2.8
javax.validation : validation-api jar 2.0.1.Final

Project Modules

There are no modules declared in this project.

PostgreSQL Test JPA - An Integration Test Helper

Integration testing of entities

For testing entities in a hibernate/eclipselink environment against a PostgreSQL database.

The database could be set up by dk.kosmisk:postgresql-maven-plugin https://github.com/kosmisk-dk/postgresql-maven-plugin

The project provides a JPAIntegrationTestBase class that on construction takes a persistence unit, and a PostgreSQL DataSource. Then it constructs an environment where tests can be performed.

The JPAIntegrationTestBase exposes 2 jpa methods one that takes a "Consumer" and one that takes a "Function".

  • Both methods are given an EntityManager in a transaction.
  • Both can throw Exceptions.
  • The Exceptions thrown are (if needed) wrapped in a RuntimeException.
  • The "Function" variant of the jpa method returns the return value of the function.

It also exposes jpaFlush which flushes the EntityManager's cache

It implements both @Before and @After functions for dumping / restoring the database, so that modifications to the database aren't seen by other tests.

Invocations could look like this:

    SomeType obj = jpa(em -> {
        return em.find(someKey, SomeType.class);
    });

or

    jpa(em -> {
        em.persist(someObject);
    });

A typical use case is outlined below:

Typical Test Environment

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${some.version}</version>
        <configuration>
            <redirectTestOutputToFile>false</redirectTestOutputToFile>
            <systemPropertyVariables>
                <postgresql.testbase.port>${postgresql.testbase.port}</postgresql.testbase.port>
                <postgresql.dump.folder>${postgresql.dump.folder}</postgresql.dump.folder>
            </systemPropertyVariables>
            <argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>dk.kosmisk</groupId>
        <artifactId>postgresql-maven-plugin</artifactId>
        <version>${some.version}</version>
        <configuration>
            <!-- <version>LATEST</version> -->
        </configuration>
        <executions>
            <execution>
                <id>postgresql-test-database</id>
                <goals>
                    <goal>setup</goal>
                    <goal>startup</goal>
                    <goal>shutdown</goal>
                </goals>
                <configuration>
                    <name>testbase</name>
                </configuration>
            </execution>
        </executions>
    </plugin>

...

    <dependency>
        <groupId>dk.kosmisk</groupId>
        <artifactId>postgresql-test-jpa</artifactId>
        <version>${some.version}</version>
        <type>jar</type>
    </dependency>

Typical Test

    public class EntityTest extends dk.kosmisk.JPAIntegrationTestBase {

        public static final String PERSISTENCE_UNIT = "testPU";
        public static final String ECLIPSELINK_LOGLEVEL = "FINE";

        public EntityTest() {
            super(PERSISTENCE_UNIT, ECLIPSELINK_LOGLEVEL,
                  dk.kosmisk.PostgresITDataSource.builder()
                          .fromProperty("testbase")
                          .withFallback()
                          .build());
            DatabaseMigrator.migrate(datasource);
        }

        @Test
        public void testJpaPersist() throws Exception {
            System.out.println("testJpaPersist");
            jpa(em -> {
                DataEntity dataEntity = new DataEntity(1, "abc");
                em.persist(dataEntity);
            });
            try (Connection connection = dataSource.getConnection() ;
                 Statement stmt = connection.createStatement() ;
                 ResultSet resultSet = stmt.executeQuery("SELECT key, data FROM data")) {
                while (resultSet.next()) {
                    int key = resultSet.getInt(1);
                    String data = resultSet.getString(2);
                    System.out.println("key = " + key + "; data = " + data);
                    if(key == 1 && data.equals("abc"))
                        return;
                }
            }
            fail("Could not find persisted data in database");
        }
    }

META-INF/persistence.xml

Hibernate

    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
                 version="2.1">
        <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <exclude-unlisted-classes>false</exclude-unlisted-classes>
            <properties>
                <property name="javax.persistence.schema-generation.database.action" value="none"/>
            </properties>
        </persistence-unit>
    </persistence>

Eclipselink

    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
                 version="2.1">
        <persistence-unit name="testPU" transaction-type="JTA">
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <jta-data-source>jdbc/foo</jta-data-source> 
            <exclude-unlisted-classes>false</exclude-unlisted-classes>
            <properties>
                <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
                <property name="eclipselink.logging.level" value="INFO"/>
            </properties>
        </persistence-unit>
    </persistence>

Versions

Version
1.0.2
1.0.1
1.0.0