Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quarkus-quickstarts/rest-jsonb Posting a Fruit object without programmatically marshall a Fruit to JSon #205

Open
agoncal opened this issue May 28, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@agoncal
Copy link

commented May 28, 2019

In the rest-json quickstart, the FruitResourceTest.testAdd test case posts a JSon representation of a fruit

given()
    .body("{\"name\": \"Pear\", \"description\": \"Winter fruit\"}")

It would be good to have a test that passes the Fruit object itself

Fruit pear = new Fruit("Pear", "Winter fruit");
given()
    .body(pear)

If write the previous code, the test fails because of an XML exception (which is a bit annoying for a JSon quickstart

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "org.acme.rest.json.Fruit" as an element because it is missing an @XmlRootElement annotation]

If we add the @XmlRootElement annotation to Fruit and change all the MediaType to XML instead of JSon it works (but hey, again, for a JSON Quickstart this is not great)

Using JSON-B I can cheat and marshall the Fruit object programmatically to a JSon String

Jsonb jsonb = JsonbBuilder.create();
Fruit pear = new Fruit("Pear", "Winter fruit");
String pearJson = jsonb.toJson(pear);
given()
        .body(pearJson)
        .header("Content-Type", MediaType.APPLICATION_JSON)

Is there an easy way to just pass the Fruit object and leave Quarkus do the marhalling Object -> JSon without programmatically using JSON-B and being able to just write :

Fruit pear = new Fruit("Pear", "Winter fruit");
given()
    .body(pear)
@cescoffier

This comment has been minimized.

Copy link
Member

commented Jun 6, 2019

@agoncal I believe we need to provide some kind of provider to RestAssured:
https://github.com/rest-assured/rest-assured/wiki/usage#using-an-explicit-serializer

That's being said... I never tried :-)

@agoncal

This comment has been minimized.

Copy link
Author

commented Jun 6, 2019

I tried to manually setup a JSonB provider like this but didn't work out.

@cescoffier

This comment has been minimized.

Copy link
Member

commented Jun 6, 2019

Bonsoir @agoncal ,

Not amazing, but seems to work:

    @BeforeAll
    static void giveMeAMapper() {
        final Jsonb jsonb = JsonbBuilder.create();
        ObjectMapper mapper = new ObjectMapper() {
            @Override
            public Object deserialize(ObjectMapperDeserializationContext context) {
                return jsonb.fromJson(context.getDataToDeserialize().asString(), context.getType());
            }

            @Override
            public Object serialize(ObjectMapperSerializationContext context) {
                return jsonb.toJson(context.getObjectToSerialize());
            }
        };
        RestAssured.objectMapper(mapper);
    }

  
    @Test
    public void testAdd() {
        Fruit pear = new Fruit("Pear", "Winter fruit");

        given()
            .body(pear)
            .header("Content-Type", MediaType.APPLICATION_JSON)
        .when()
            .post("/fruits")
        .then()
            .statusCode(200)
            .body("$.size()", is(3),
                    "name", containsInAnyOrder("Apple", "Pineapple", "Pear"),
                    "description", containsInAnyOrder("Winter fruit", "Tropical fruit", "Winter fruit"));

        given()
            .body("{\"name\": \"Pear\", \"description\": \"Winter fruit\"}")
            .header("Content-Type", MediaType.APPLICATION_JSON)
        .when()
            .delete("/fruits")
        .then()
            .statusCode(200)
            .body("$.size()", is(2),
                    "name", containsInAnyOrder("Apple", "Pineapple"),
                    "description", containsInAnyOrder("Winter fruit", "Tropical fruit"));
    }

I need to check how it works with complex type on the deserialization side.

agoncal added a commit to agoncal/agoncal-sample-quarkus that referenced this issue Jun 8, 2019

@agoncal

This comment has been minimized.

Copy link
Author

commented Jun 8, 2019

@cescoffier nice trick. Works on my sample too.

Couldn't you register the JSonB mapper in the QuarkusTest (so we don't have to do it) ?

@cescoffier

This comment has been minimized.

Copy link
Member

commented Jun 11, 2019

@agoncal definitely. As we are setting the RestAssured port we can do that at the same time. But, I'd like to do proper testing first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.