diff --git a/hibernate-orm-rest-data-panache-quickstart/src/main/java/org/acme/hibernate/orm/panache/rest/entity/Person.java b/hibernate-orm-rest-data-panache-quickstart/src/main/java/org/acme/hibernate/orm/panache/rest/entity/Person.java index 2fb7bc0c3c..59d76d6d31 100644 --- a/hibernate-orm-rest-data-panache-quickstart/src/main/java/org/acme/hibernate/orm/panache/rest/entity/Person.java +++ b/hibernate-orm-rest-data-panache-quickstart/src/main/java/org/acme/hibernate/orm/panache/rest/entity/Person.java @@ -1,6 +1,10 @@ package org.acme.hibernate.orm.panache.rest.entity; import java.time.LocalDate; +import java.util.Map; + +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; import jakarta.persistence.Entity; import jakarta.persistence.NamedQuery; @@ -12,4 +16,7 @@ public class Person extends PanacheEntity { public String name; public LocalDate birthDate; + + @JdbcTypeCode(SqlTypes.JSON) + public Map jsonAddress; } diff --git a/hibernate-orm-rest-data-panache-quickstart/src/test/java/org/acme/hibernate/orm/panache/rest/entity/PeopleResourceTest.java b/hibernate-orm-rest-data-panache-quickstart/src/test/java/org/acme/hibernate/orm/panache/rest/entity/PeopleResourceTest.java index e7491330ee..cc3624a82d 100644 --- a/hibernate-orm-rest-data-panache-quickstart/src/test/java/org/acme/hibernate/orm/panache/rest/entity/PeopleResourceTest.java +++ b/hibernate-orm-rest-data-panache-quickstart/src/test/java/org/acme/hibernate/orm/panache/rest/entity/PeopleResourceTest.java @@ -6,6 +6,9 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import java.util.HashMap; +import java.util.Map; + import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; @@ -62,12 +65,12 @@ void shouldListPersonsInDescending() { @Test void shouldListPersonsWithPageAndSize() { given().accept(ContentType.JSON) - .and().queryParam("page", 0) - .and().queryParam("size", 1) - .when().get("/my-people/all") - .then().statusCode(200) - .and().body("id", contains(1)) - .and().body("name", contains("John Johnson")); + .and().queryParam("page", 0) + .and().queryParam("size", 1) + .when().get("/my-people/all?sort=name") + .then().statusCode(200) + .and().body("id", contains(1)) + .and().body("name", contains("John Johnson")); } @Test @@ -113,4 +116,37 @@ void shouldDeleteNotBeExposed() { given().when().delete("/my-people/1") .then().statusCode(405); } + + @Test + void shouldUpdatePerson() { + given().accept(ContentType.JSON) + .when().get("/my-people/1") + .then().statusCode(200) + .and().body("id", is(equalTo(1))) + .and().body("name", is(equalTo("John Johnson"))); + + Person newPerson = new Person(); + newPerson.id = 1L; + newPerson.name = "John Johnson"; + + Map jsonObject = new HashMap<>(); + jsonObject.put("zipcode", 95014); + jsonObject.put("city", "cupertino"); + newPerson.jsonAddress = jsonObject; + + given().accept(ContentType.JSON) + .and().contentType(ContentType.JSON) + .and().body(newPerson) + .when().put("/my-people/1").then().statusCode(204); + + // verify after updating that the new json address fields were stored + given().accept(ContentType.JSON) + .when().get("/my-people/1") + .then().statusCode(200) + .and().body("id", is(equalTo(1))) + .and().body("name", is(equalTo("John Johnson"))) + .and().body("jsonAddress", is(equalTo(jsonObject))); + + } + }