From 6c7786761e3e53b8f77e601680ac5dc9932922eb Mon Sep 17 00:00:00 2001 From: Fedor Dudinskiy Date: Wed, 20 Apr 2022 09:52:30 +0200 Subject: [PATCH] Coverage for subresources --- .../http/restclient/reactive/BookClient.java | 68 +++++++++++++++++++ .../reactive/PlainBookResource.java | 28 ++++++++ .../reactive/ReactiveClientBookResource.java | 30 ++++++++ .../src/main/resources/classic.properties | 1 + .../src/main/resources/modern.properties | 4 ++ .../reactive/ReactiveRestClientIT.java | 59 ++++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/BookClient.java diff --git a/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/BookClient.java b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/BookClient.java new file mode 100644 index 000000000..317ce1575 --- /dev/null +++ b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/BookClient.java @@ -0,0 +1,68 @@ +package io.quarkus.ts.http.restclient.reactive; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; + +import io.quarkus.ts.http.restclient.reactive.json.Book; +import io.smallrye.mutiny.Uni; + +@RegisterRestClient +@Path("/books") +@RegisterClientHeaders +@Consumes(MediaType.TEXT_PLAIN) +@Produces(MediaType.TEXT_PLAIN) +public interface BookClient { + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + Uni getBook(@QueryParam("title") String title, @QueryParam("author") String author); + + @Path("/author") + AuthorClient getAuthor(); + + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.TEXT_PLAIN) + interface AuthorClient { + @GET + @Path("/name") + Uni getName(@QueryParam("author") String author); + + @Path("profession") + ProfessionClient getProfession(); + } + + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.TEXT_PLAIN) + interface ProfessionClient { + @GET + @Path("/name") + Uni getName(); + + @Path("/wage") + WageClient getWage(); + } + + interface WageClient { + @GET + @Path("/amount") + String getAmount(); + + @Path("/currency") + CurrencyClient getCurrency(); + } + + interface CurrencyClient { + @GET + @Path("/name") + String getName(); + } + +} diff --git a/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/PlainBookResource.java b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/PlainBookResource.java index 24db5a1c9..98c3fd92b 100644 --- a/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/PlainBookResource.java +++ b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/PlainBookResource.java @@ -2,6 +2,7 @@ import java.util.Map; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -22,4 +23,31 @@ public Uni getByQueryMap(@QueryParam("param") Map params) .item(new Book(params.get("id"), params.get("author"))); } + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Uni getBook(@QueryParam("title") String title, @QueryParam("author") String author) { + return Uni.createFrom().item(new Book(title, author)); + } + + @GET + @Path("/author/name") + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.TEXT_PLAIN) + public Uni getAuthorName(@QueryParam("author") String author) { + return Uni.createFrom().item(author); + } + + @GET + @Path("/author/profession/title") + public Uni getProfession() { + return Uni.createFrom().item("writer"); + } + + @GET + @Path("/author/profession/wage/currency/name") + public Uni getCurrency() { + return Uni.createFrom().item("USD"); + } } diff --git a/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/ReactiveClientBookResource.java b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/ReactiveClientBookResource.java index 0dd5c927a..9682ce0fc 100644 --- a/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/ReactiveClientBookResource.java +++ b/http/rest-client-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/ReactiveClientBookResource.java @@ -5,6 +5,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.eclipse.microprofile.rest.client.inject.RestClient; @@ -21,6 +22,10 @@ public class ReactiveClientBookResource { @RestClient JsonRestInterface restInterface; + @Inject + @RestClient + BookClient bookInterface; + @GET @Path("/{id}/json") @Produces(MediaType.APPLICATION_JSON) @@ -34,4 +39,29 @@ public Uni getAsJson(@PathParam("id") String id) { public Uni getAsJsonByBeanParam(@PathParam("id") String id) { return restInterface.getWithBeanParam(new IdBeanParam(id)); } + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Uni getResource(@QueryParam("title") String title, @QueryParam("author") String author) { + return bookInterface.getBook(title, author); + } + + @GET + @Path("/author") + public Uni getSubResource(@QueryParam("author") String author) { + return bookInterface.getAuthor().getName(author); + } + + @GET + @Path("/profession") + public Uni getSubSubResource() { + return bookInterface.getAuthor().getProfession().getName(); + } + + @GET + @Path("/currency") + public String getLastResource() { + return bookInterface.getAuthor().getProfession().getWage().getCurrency().getName(); + } } diff --git a/http/rest-client-reactive/src/main/resources/classic.properties b/http/rest-client-reactive/src/main/resources/classic.properties index bdb2b562b..e45ad898c 100644 --- a/http/rest-client-reactive/src/main/resources/classic.properties +++ b/http/rest-client-reactive/src/main/resources/classic.properties @@ -1 +1,2 @@ io.quarkus.ts.http.restclient.reactive.json.JsonRestInterface/mp-rest/url=http://localhost:${quarkus.http.port} +io.quarkus.ts.http.restclient.reactive.BookClient/mp-rest/url=http://localhost:${quarkus.http.port} diff --git a/http/rest-client-reactive/src/main/resources/modern.properties b/http/rest-client-reactive/src/main/resources/modern.properties index e83aa792d..e1a15cd82 100644 --- a/http/rest-client-reactive/src/main/resources/modern.properties +++ b/http/rest-client-reactive/src/main/resources/modern.properties @@ -1,5 +1,9 @@ # new schema introduced in https://github.com/quarkusio/quarkus/pull/17220 . See classic.properties for old-style settings quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.json.JsonRestInterface".url=http://localhost:${quarkus.http.port} quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.files.FileClient".url=http://localhost:${quarkus.http.port} +quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.BookClient".url=http://localhost:${quarkus.http.port} +quarkus.rest-client."io.quarkus.ts.http.restclient.reactive.BookClient.AuthorClient".url=http://localhost:${quarkus.http.port} +quarkus.rest-client.logging.scope=request-response +quarkus.log.category."org.jboss.resteasy.reactive.client.logging".level=DEBUG quarkus.http.limits.max-body-size=3G quarkus.rest-client.read-timeout = 60000 diff --git a/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java b/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java index 18d8d4b8f..41300d235 100644 --- a/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java +++ b/http/rest-client-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/ReactiveRestClientIT.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; @@ -48,4 +49,62 @@ public void mapInQueryParam() { assertEquals("Hagakure", response.jsonPath().getString("title")); assertEquals("Tsuramoto", response.jsonPath().getString("author")); } + + @Test + public void resourceDirectly() { + Response response = app.given() + .when() + .get("/books/?title=Catch-22&author=Heller"); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + assertEquals("Catch-22", response.jsonPath().getString("title")); + assertEquals("Heller", response.jsonPath().getString("author")); + } + + @Test + public void resourceClient() { + Response response = app.given() + .when() + .get("/client/book/?title=Catch-22&author=Heller"); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + assertEquals("Catch-22", response.jsonPath().getString("title")); + assertEquals("Heller", response.jsonPath().getString("author")); + } + + @Test + public void subResourceDirectly() { + Response response = app.given() + .when() + .get("/books/author/name?author=Cimrman"); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + assertEquals("Cimrman", response.getBody().asString()); + } + + @Test + public void deepestLevelDirectly() { + Response response = app.given() + .when() + .get("/books/author/profession/wage/currency/name"); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + assertEquals("USD", response.getBody().asString()); + } + + @Test + @Disabled("https://github.com/quarkusio/quarkus/issues/25028") + public void subResource() { + Response response = app.given().get("/client/book/author/?author=Heller"); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + assertEquals("Heller", response.getBody().asString()); + + Response sub = app.given().get("/client/book/profession"); + assertEquals(HttpStatus.SC_OK, sub.statusCode()); + assertEquals("writer", sub.getBody().asString()); + } + + @Test + @Disabled("https://github.com/quarkusio/quarkus/issues/25028") + public void deepLevel() { + Response response = app.given().get("/client/book/currency"); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + assertEquals("Heller", response.getBody().asString()); + } }