Skip to content
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

Move JSON-related JPA tests to their own module #37561

Closed
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
27 changes: 27 additions & 0 deletions integration-tests/jpa-postgresql-withjson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# JPA example with PostgreSQL and JSON

## Running the tests

By default, the tests of this module are disabled.

To run the tests in a standard JVM with PostgreSQL started as a Docker container, you can run the following command:

```
mvn clean install -Dtest-containers -Dstart-containers
```

Additionally, you can generate a native image and run the tests for this native image by adding `-Dnative`:

```
mvn clean install -Dtest-containers -Dstart-containers -Dnative
```

If you don't want to run PostgreSQL as a Docker container, you can start your own PostgreSQL server. It needs to listen on the default port and have a database called `hibernate_orm_test` accessible to the user `hibernate_orm_test` with the password `hibernate_orm_test`.

You can then run the tests as follows (either with `-Dnative` or not):

```
mvn clean install -Dtest-containers
```

If you have specific requirements, you can define a specific connection URL with `-Dpostgres.url=jdbc:postgresql://...`.
257 changes: 257 additions & 0 deletions integration-tests/jpa-postgresql-withjson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-integration-test-jpa-postgresql-withjson</artifactId>
<name>Quarkus - Integration Tests - JPA - PostgreSQL with JSON</name>
<description>Module that contains JPA related tests running with the PostgreSQL database</description>

<properties>
<postgres.url>jdbc:postgresql://localhost:5431/hibernate_orm_test</postgres.url>
</properties>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>test-postgresql</id>
<activation>
<property>
<name>test-containers</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>docker-postgresql</id>
<activation>
<property>
<name>start-containers</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>${postgres.image}</name>
<alias>postgresql</alias>
<run>
<env>
<POSTGRES_USER>hibernate_orm_test</POSTGRES_USER>
<POSTGRES_PASSWORD>hibernate_orm_test</POSTGRES_PASSWORD>
<POSTGRES_DB>hibernate_orm_test</POSTGRES_DB>
</env>
<ports>
<port>5431:5432</port>
</ports>
<wait>
<!-- For some reason, Postgres will tell us it's ready *twice*,
and that's the truth the second time only.
See https://github.com/fabric8io/docker-maven-plugin/issues/628
See also how the condition is configured in testcontainers:
https://github.com/testcontainers/testcontainers-java/blob/c64aab9fd5e3a452ee0faf793560327eb4da9841/modules/postgresql/src/main/java/org/testcontainers/containers/PostgreSQLContainer.java#L52-L55 -->
<time>20000</time>
<log>(?s)ready to accept connections.*ready to accept connections</log>
</wait>
</run>
</image>
</images>
<!--Stops all postgres images currently running, not just those we just started.
Useful to stop processes still running from a previously failed integration test run -->
<allContainers>true</allContainers>
</configuration>
<executions>
<execution>
<id>docker-start</id>
<phase>compile</phase>
<goals>
<goal>stop</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>docker-stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>docker-prune</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${docker-prune.location}</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.quarkus.it.jpa.postgresql;

import java.time.LocalDate;
import java.util.List;

import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.hibernate.orm.PersistenceUnit;
import io.quarkus.it.jpa.postgresql.otherpu.EntityWithJsonOtherPU;
import io.quarkus.narayana.jta.QuarkusTransaction;

/**
* Various tests covering JPA functionality. All tests should work in both standard JVM and in native mode.
*/
@Path("/jpa/testfunctionality")
@Produces(MediaType.TEXT_PLAIN)
public class JPAFunctionalityTestEndpoint {

@Inject
EntityManager em;

@Inject
@PersistenceUnit("other")
EntityManager otherEm;

@GET
public String json() {
QuarkusTransaction.requiringNew().run(() -> {
EntityWithJson entity = new EntityWithJson(
new EntityWithJson.ToBeSerializedWithDateTime(LocalDate.of(2023, 7, 28)));
em.persist(entity);
});

QuarkusTransaction.requiringNew().run(() -> {
List<EntityWithJson> entities = em
.createQuery("select e from EntityWithJson e", EntityWithJson.class)
.getResultList();
if (entities.isEmpty()) {
throw new AssertionError("No entities with json were found");
}
});

QuarkusTransaction.requiringNew().run(() -> {
em.createQuery("delete from EntityWithJson").executeUpdate();
});

Exception exception = null;
try {
QuarkusTransaction.requiringNew().run(() -> {
EntityWithJsonOtherPU otherPU = new EntityWithJsonOtherPU(
new EntityWithJsonOtherPU.ToBeSerializedWithDateTime(LocalDate.of(2023, 7, 28)));
otherEm.persist(otherPU);
});
} catch (Exception e) {
exception = e;
}

if (exception == null) {
throw new AssertionError(
"Default mapper cannot process date/time properties. So we were expecting transaction to fail, but it did not!");
}
if (!(exception instanceof UnsupportedOperationException)
|| !exception.getMessage().contains("I cannot convert anything to JSON")) {
throw new AssertionError("flush failed for a different reason than expected.", exception);
}

return "OK";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
quarkus.datasource.username=hibernate_orm_test
quarkus.datasource.password=hibernate_orm_test
quarkus.datasource.jdbc.url=${postgres.url}
quarkus.datasource.jdbc.max-size=8

quarkus.hibernate-orm.packages=io.quarkus.it.jpa.postgresql
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.database.generation.create-schemas=true
# Define non-default PU so that we can configure a custom JSON format mapper. The default PU is using the default mapper.
quarkus.hibernate-orm."other".datasource=<default>
quarkus.hibernate-orm."other".packages=io.quarkus.it.jpa.postgresql.otherpu

#Necessary for assertions in JPAFunctionalityInGraalITCase:
quarkus.native.enable-reports=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.it.jpa.postgresql;

import io.quarkus.test.junit.QuarkusIntegrationTest;

/**
* Test various JPA operations with JSON running in native mode
*/
@QuarkusIntegrationTest
public class JPAFunctionalityInGraalITCase extends JPAFunctionalityTest {

}
Loading
Loading