From a4be012f92a158a8f31f11fd003d7f69d6c0a094 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Wed, 6 Dec 2023 11:02:37 +0100 Subject: [PATCH] Move JSON-related JPA tests to their own module --- .../jpa-postgresql-withjson/README.md | 27 ++ .../jpa-postgresql-withjson/pom.xml | 257 ++++++++++++++++++ .../it/jpa/postgresql/EntityWithJson.java | 0 .../JPAFunctionalityTestEndpoint.java | 75 +++++ .../otherpu/EntityWithJsonOtherPU.java | 0 .../postgresql/otherpu/JsonFormatMapper.java | 0 .../src/main/resources/application.properties | 14 + .../JPAFunctionalityInGraalITCase.java | 11 + .../jpa/postgresql/JPAFunctionalityTest.java | 21 ++ integration-tests/jpa-postgresql/pom.xml | 17 -- .../JPAFunctionalityTestEndpoint.java | 52 ---- .../src/main/resources/application.properties | 3 - .../JPAFunctionalityInGraalITCase.java | 2 +- .../jpa/postgresql/JPAFunctionalityTest.java | 5 - integration-tests/pom.xml | 1 + 15 files changed, 407 insertions(+), 78 deletions(-) create mode 100644 integration-tests/jpa-postgresql-withjson/README.md create mode 100644 integration-tests/jpa-postgresql-withjson/pom.xml rename integration-tests/{jpa-postgresql => jpa-postgresql-withjson}/src/main/java/io/quarkus/it/jpa/postgresql/EntityWithJson.java (100%) create mode 100644 integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java rename integration-tests/{jpa-postgresql => jpa-postgresql-withjson}/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/EntityWithJsonOtherPU.java (100%) rename integration-tests/{jpa-postgresql => jpa-postgresql-withjson}/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/JsonFormatMapper.java (100%) create mode 100644 integration-tests/jpa-postgresql-withjson/src/main/resources/application.properties create mode 100644 integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java create mode 100644 integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java diff --git a/integration-tests/jpa-postgresql-withjson/README.md b/integration-tests/jpa-postgresql-withjson/README.md new file mode 100644 index 0000000000000..ce110096b9f0f --- /dev/null +++ b/integration-tests/jpa-postgresql-withjson/README.md @@ -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://...`. diff --git a/integration-tests/jpa-postgresql-withjson/pom.xml b/integration-tests/jpa-postgresql-withjson/pom.xml new file mode 100644 index 0000000000000..1f54f7afa1bab --- /dev/null +++ b/integration-tests/jpa-postgresql-withjson/pom.xml @@ -0,0 +1,257 @@ + + + + quarkus-integration-tests-parent + io.quarkus + 999-SNAPSHOT + + 4.0.0 + + quarkus-integration-test-jpa-postgresql-withjson + Quarkus - Integration Tests - JPA - PostgreSQL with JSON + Module that contains JPA related tests running with the PostgreSQL database + + + jdbc:postgresql://localhost:5431/hibernate_orm_test + + + + + io.quarkus + quarkus-hibernate-orm + + + io.quarkus + quarkus-jdbc-postgresql + + + io.quarkus + quarkus-resteasy-reactive + + + io.quarkus + quarkus-jackson + + + + + io.quarkus + quarkus-junit5 + test + + + io.quarkus + quarkus-junit5-internal + test + + + io.rest-assured + rest-assured + test + + + org.assertj + assertj-core + test + + + + + io.quarkus + quarkus-hibernate-orm-deployment + ${project.version} + pom + test + + + * + * + + + + + io.quarkus + quarkus-jdbc-postgresql-deployment + ${project.version} + pom + test + + + * + * + + + + + io.quarkus + quarkus-resteasy-reactive-deployment + ${project.version} + pom + test + + + * + * + + + + + io.quarkus + quarkus-jackson-deployment + ${project.version} + pom + test + + + * + * + + + + + + + + + src/main/resources + true + + + + + maven-surefire-plugin + + true + + + + maven-failsafe-plugin + + true + + + + io.quarkus + quarkus-maven-plugin + + + + build + + + + + + + + + + test-postgresql + + + test-containers + + + + + + maven-surefire-plugin + + false + + + + maven-failsafe-plugin + + false + + + + + + + + docker-postgresql + + + start-containers + + + + + + io.fabric8 + docker-maven-plugin + + + + ${postgres.image} + postgresql + + + hibernate_orm_test + hibernate_orm_test + hibernate_orm_test + + + 5431:5432 + + + + + (?s)ready to accept connections.*ready to accept connections + + + + + + true + + + + docker-start + compile + + stop + start + + + + docker-stop + post-integration-test + + stop + + + + + + org.codehaus.mojo + exec-maven-plugin + + + docker-prune + generate-resources + + exec + + + ${docker-prune.location} + + + + + + + + + + + diff --git a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/EntityWithJson.java b/integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/EntityWithJson.java similarity index 100% rename from integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/EntityWithJson.java rename to integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/EntityWithJson.java diff --git a/integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java new file mode 100644 index 0000000000000..328b81be59a1b --- /dev/null +++ b/integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java @@ -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 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"; + } + +} diff --git a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/EntityWithJsonOtherPU.java b/integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/EntityWithJsonOtherPU.java similarity index 100% rename from integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/EntityWithJsonOtherPU.java rename to integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/EntityWithJsonOtherPU.java diff --git a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/JsonFormatMapper.java b/integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/JsonFormatMapper.java similarity index 100% rename from integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/JsonFormatMapper.java rename to integration-tests/jpa-postgresql-withjson/src/main/java/io/quarkus/it/jpa/postgresql/otherpu/JsonFormatMapper.java diff --git a/integration-tests/jpa-postgresql-withjson/src/main/resources/application.properties b/integration-tests/jpa-postgresql-withjson/src/main/resources/application.properties new file mode 100644 index 0000000000000..90370fd628984 --- /dev/null +++ b/integration-tests/jpa-postgresql-withjson/src/main/resources/application.properties @@ -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= +quarkus.hibernate-orm."other".packages=io.quarkus.it.jpa.postgresql.otherpu + +#Necessary for assertions in JPAFunctionalityInGraalITCase: +quarkus.native.enable-reports=true \ No newline at end of file diff --git a/integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java b/integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java new file mode 100644 index 0000000000000..4f5076a3f1b12 --- /dev/null +++ b/integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java @@ -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 { + +} diff --git a/integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java b/integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java new file mode 100644 index 0000000000000..a3b753a414251 --- /dev/null +++ b/integration-tests/jpa-postgresql-withjson/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java @@ -0,0 +1,21 @@ +package io.quarkus.it.jpa.postgresql; + +import static org.hamcrest.Matchers.is; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; + +/** + * Test various JPA operations with JSON running in Quarkus + */ +@QuarkusTest +public class JPAFunctionalityTest { + + @Test + public void json() { + RestAssured.when().get("/jpa/testfunctionality").then().body(is("OK")); + } + +} diff --git a/integration-tests/jpa-postgresql/pom.xml b/integration-tests/jpa-postgresql/pom.xml index 236e61604bbe9..dc23312144d2b 100644 --- a/integration-tests/jpa-postgresql/pom.xml +++ b/integration-tests/jpa-postgresql/pom.xml @@ -30,10 +30,6 @@ io.quarkus quarkus-resteasy-reactive - - io.quarkus - quarkus-jackson - @@ -97,19 +93,6 @@ - - io.quarkus - quarkus-jackson-deployment - ${project.version} - pom - test - - - * - * - - - diff --git a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java index 6b9b3df9d6433..5e1fd9d9cecb6 100644 --- a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java @@ -1,6 +1,5 @@ package io.quarkus.it.jpa.postgresql; -import java.time.LocalDate; import java.util.List; import java.util.UUID; @@ -15,8 +14,6 @@ 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; /** @@ -29,10 +26,6 @@ public class JPAFunctionalityTestEndpoint { @Inject EntityManager em; - @Inject - @PersistenceUnit("other") - EntityManager otherEm; - @GET @Path("base") public String base() { @@ -124,49 +117,4 @@ public String uuid() { return "OK"; } - @GET - @Path("json") - 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 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"; - } - } diff --git a/integration-tests/jpa-postgresql/src/main/resources/application.properties b/integration-tests/jpa-postgresql/src/main/resources/application.properties index 90370fd628984..61a3265d67803 100644 --- a/integration-tests/jpa-postgresql/src/main/resources/application.properties +++ b/integration-tests/jpa-postgresql/src/main/resources/application.properties @@ -6,9 +6,6 @@ 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= -quarkus.hibernate-orm."other".packages=io.quarkus.it.jpa.postgresql.otherpu #Necessary for assertions in JPAFunctionalityInGraalITCase: quarkus.native.enable-reports=true \ No newline at end of file diff --git a/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java b/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java index 158945098ab53..31f5089d394f2 100644 --- a/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java +++ b/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityInGraalITCase.java @@ -13,7 +13,7 @@ public class JPAFunctionalityInGraalITCase extends JPAFunctionalityTest { @Test - public void verifyJdkXmlParsersHavebeenEcludedFromNative() { + public void verifyJdkXmlParsersHaveBeenExcludedFromNative() { final ClassInclusionReport report = ClassInclusionReport.load(); //The following classes should be included in this applications; //if not, that would be a sign that this test has become too weak diff --git a/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java b/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java index 0ccf51d52d76a..862a316ca9957 100644 --- a/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java +++ b/integration-tests/jpa-postgresql/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java @@ -23,9 +23,4 @@ public void uuid() { RestAssured.when().get("/jpa/testfunctionality/uuid").then().body(is("OK")); } - @Test - public void json() { - RestAssured.when().get("/jpa/testfunctionality/json").then().body(is("OK")); - } - } diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index 3e9da6012ddb9..57fa1b2085bfe 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -227,6 +227,7 @@ jpa-db2 jpa-derby jpa-postgresql + jpa-postgresql-withjson jpa-postgresql-withxml jpa-mariadb jpa-h2