From e47901189bdcc1f948299903cfb81ac6a1a332f6 Mon Sep 17 00:00:00 2001 From: Syed Faraz Ali Date: Mon, 30 Oct 2023 22:16:58 -0700 Subject: [PATCH 1/2] Include JSONB example in hibernate quickstart Example to clarify Quarkus issue # 36730 Include JSONB example Example to clarify Quarkus issue # 36730 --- .../orm/panache/rest/entity/Person.java | 7 +++++ .../rest/entity/PeopleResourceTest.java | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+) 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..c167158154 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,10 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; @@ -113,4 +117,30 @@ void shouldDeleteNotBeExposed() { given().when().delete("/my-people/1") .then().statusCode(405); } + + @Test + void shouldUpdatePerson() { + + Person newPerson = new Person(); + newPerson.name = "Holly"; + newPerson.birthDate = LocalDate.of(2001, 11, 20); + + 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("Holly"))) + .and().body("jsonAddress", is( equalTo(jsonObject))); + + } + } From 494005879d1ef4357308a0f879b48461249a35e6 Mon Sep 17 00:00:00 2001 From: Syed Faraz Ali Date: Tue, 31 Oct 2023 22:08:26 -0700 Subject: [PATCH 2/2] Fixed formatting and test --- .../rest/entity/PeopleResourceTest.java | 62 ++++++++++--------- 1 file changed, 34 insertions(+), 28 deletions(-) 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 c167158154..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,7 +6,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import java.time.LocalDate; import java.util.HashMap; import java.util.Map; @@ -66,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 @@ -119,27 +118,34 @@ void shouldDeleteNotBeExposed() { } @Test - void shouldUpdatePerson() { - - Person newPerson = new Person(); - newPerson.name = "Holly"; - newPerson.birthDate = LocalDate.of(2001, 11, 20); - - 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("Holly"))) - .and().body("jsonAddress", is( equalTo(jsonObject))); + 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))); }