From d9412b53d3afa4fc10beeac19bd46c679ea1d255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Fri, 8 Dec 2023 16:20:42 +0100 Subject: [PATCH] Improve MongoDB tests on Java records --- .../it/mongodb/panache/record/PersonName.java | 2 +- .../mongodb/panache/record/PersonRecord.java | 4 +++ .../record/PersonRecordRepository.java | 9 +++++ .../panache/record/PersonResource.java | 16 ++++----- .../panache/record/PersonWithRecord.java | 9 ----- .../record/MongodbPanacheRecordIT.java | 33 ------------------- .../record/MongodbPanacheRecordTest.java | 16 +++------ 7 files changed, 27 insertions(+), 62 deletions(-) create mode 100644 integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecord.java create mode 100644 integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecordRepository.java delete mode 100644 integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonWithRecord.java diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonName.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonName.java index 21baf3976b594a..f3425e5f529393 100644 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonName.java +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonName.java @@ -2,6 +2,6 @@ import io.quarkus.mongodb.panache.common.ProjectionFor; -@ProjectionFor(PersonWithRecord.class) +@ProjectionFor(PersonRecord.class) public record PersonName(String firstName, String lastName) { } diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecord.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecord.java new file mode 100644 index 00000000000000..91ab54030c1835 --- /dev/null +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecord.java @@ -0,0 +1,4 @@ +package io.quarkus.it.mongodb.panache.record; + +public record PersonRecord(String firstName, String lastName, Status status) { +} diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecordRepository.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecordRepository.java new file mode 100644 index 00000000000000..ceb019b521282a --- /dev/null +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonRecordRepository.java @@ -0,0 +1,9 @@ +package io.quarkus.it.mongodb.panache.record; + +import jakarta.enterprise.context.ApplicationScoped; + +import io.quarkus.mongodb.panache.PanacheMongoRepository; + +@ApplicationScoped +public class PersonRecordRepository implements PanacheMongoRepository { +} diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonResource.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonResource.java index 21b72c87cd5a81..cd49f9dec29934 100644 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonResource.java +++ b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonResource.java @@ -1,24 +1,24 @@ package io.quarkus.it.mongodb.panache.record; -import java.net.URI; import java.util.List; +import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; -import jakarta.ws.rs.core.Response; -@Path("/mongo/persons") +@Path("/persons/record") public class PersonResource { + @Inject + PersonRecordRepository personRecordRepository; + @GET public List getPersons() { - return PersonWithRecord.findAll().project(PersonName.class).list(); + return personRecordRepository.findAll().project(PersonName.class).list(); } @POST - public Response addPerson(PersonWithRecord person) { - person.persist(); - String id = person.id.toString(); - return Response.created(URI.create("/persons/entity/" + id)).build(); + public void addPerson(PersonRecord person) { + personRecordRepository.persist(person); } } diff --git a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonWithRecord.java b/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonWithRecord.java deleted file mode 100644 index 5aa5543d42269e..00000000000000 --- a/integration-tests/mongodb-panache/src/main/java/io/quarkus/it/mongodb/panache/record/PersonWithRecord.java +++ /dev/null @@ -1,9 +0,0 @@ -package io.quarkus.it.mongodb.panache.record; - -import io.quarkus.mongodb.panache.PanacheMongoEntity; - -public class PersonWithRecord extends PanacheMongoEntity { - public String firstname; - public String lastname; - public Status status = Status.ALIVE; -} diff --git a/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordIT.java b/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordIT.java index 1c3331c2ca996d..2c87986c3d7a5f 100644 --- a/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordIT.java +++ b/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordIT.java @@ -1,40 +1,7 @@ package io.quarkus.it.mongodb.panache.record; -import static io.restassured.RestAssured.given; -import static io.restassured.RestAssured.when; -import static org.hamcrest.CoreMatchers.is; - -import org.junit.jupiter.api.Test; - import io.quarkus.test.junit.QuarkusIntegrationTest; -import io.restassured.http.ContentType; @QuarkusIntegrationTest class MongodbPanacheRecordIT extends MongodbPanacheRecordTest { - - private static final String ROOT_URL = "/mongo/persons"; - - @Test - void testRecordInPanache() { - var person1 = new PersonWithRecord(); - person1.firstname = "Loïc"; - person1.lastname = "Mathieu"; - person1.status = Status.ALIVE; - var person2 = new PersonWithRecord(); - person1.firstname = "Zombie"; - person2.lastname = "Zombie"; - person2.status = Status.DEAD; - - given().body(person1).contentType(ContentType.JSON) - .when().post(ROOT_URL) - .then().statusCode(201); - given().body(person2).contentType(ContentType.JSON) - .when().post(ROOT_URL) - .then().statusCode(201); - - when().get(ROOT_URL) - .then() - .statusCode(200) - .body("size()", is(2)); - } } diff --git a/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordTest.java b/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordTest.java index 1d2920ec8551ba..b6b1c77d75a656 100644 --- a/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordTest.java +++ b/integration-tests/mongodb-panache/src/test/java/io/quarkus/it/mongodb/panache/record/MongodbPanacheRecordTest.java @@ -15,25 +15,19 @@ @QuarkusTestResource(MongoReplicaSetTestResource.class) class MongodbPanacheRecordTest { - private static final String ROOT_URL = "/mongo/persons"; + private static final String ROOT_URL = "/persons/record"; @Test void testRecordInPanache() { - var person1 = new PersonWithRecord(); - person1.firstname = "Loïc"; - person1.lastname = "Mathieu"; - person1.status = Status.ALIVE; - var person2 = new PersonWithRecord(); - person1.firstname = "Zombie"; - person2.lastname = "Zombie"; - person2.status = Status.DEAD; + var person1 = new PersonRecord("Loïc", "Mathieu", Status.ALIVE); + var person2 = new PersonRecord("Zombie", "Zombie", Status.DEAD); given().body(person1).contentType(ContentType.JSON) .when().post(ROOT_URL) - .then().statusCode(201); + .then().statusCode(204); given().body(person2).contentType(ContentType.JSON) .when().post(ROOT_URL) - .then().statusCode(201); + .then().statusCode(204); when().get(ROOT_URL) .then()