Skip to content

Commit

Permalink
Improve MongoDB tests on Java records
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu authored and holly-cummins committed Feb 8, 2024
1 parent 4ddba5b commit d9412b5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import io.quarkus.mongodb.panache.common.ProjectionFor;

@ProjectionFor(PersonWithRecord.class)
@ProjectionFor(PersonRecord.class)
public record PersonName(String firstName, String lastName) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.quarkus.it.mongodb.panache.record;

public record PersonRecord(String firstName, String lastName, Status status) {
}
Original file line number Diff line number Diff line change
@@ -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<PersonRecord> {
}
Original file line number Diff line number Diff line change
@@ -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<PersonName> 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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d9412b5

Please sign in to comment.