From 6b8844917cc29ddfe8fa1064d9746ccf4767b803 Mon Sep 17 00:00:00 2001 From: Erin Schnabel Date: Fri, 5 Mar 2021 14:12:26 -0500 Subject: [PATCH] Tests for quarkus.rest.path --- .../deployment/ResteasyReactiveProcessor.java | 4 +- .../ResteasyReactiveServerConfig.java | 13 ++++-- .../server/test/path/HelloResource.java | 17 ++++++- .../test/path/RelativeRestPathTestCase.java | 29 ++++++++++++ .../path/RestApplicationPathTestCase.java | 45 +++++++++++++++++++ .../server/test/path/RestPathTestCase.java | 16 +++---- .../src/test/resources/empty.properties | 0 7 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RelativeRestPathTestCase.java create mode 100644 extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestApplicationPathTestCase.java create mode 100644 extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/resources/empty.properties diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java index 3abf4d6d562b3..307adbaee1ac4 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java @@ -580,10 +580,10 @@ public List scan(MethodInfo method, Map }); } - private String determineApplicationPath(IndexView index, String defaultPath) { + private String determineApplicationPath(IndexView index, Optional defaultPath) { Collection applicationPaths = index.getAnnotations(ResteasyReactiveDotNames.APPLICATION_PATH); if (applicationPaths.isEmpty()) { - return defaultPath; + return defaultPath.orElse("/"); } // currently we only examine the first class that is annotated with @ApplicationPath so best // fail if the user code has multiple such annotations instead of surprising the user diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveServerConfig.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveServerConfig.java index 5baba89e338c2..4c9721d275321 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveServerConfig.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveServerConfig.java @@ -1,5 +1,7 @@ package io.quarkus.resteasy.reactive.server.deployment; +import java.util.Optional; + import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigRoot; @@ -7,9 +9,12 @@ public class ResteasyReactiveServerConfig { /** - * Set this to override the default path for JAX-RS resources if there are no - * annotated application classes. + * Set this to define the application path that serves as the base URI for all + * JAX-RS resource URIs provided by {@code @Path} annotations when there are no + * {@code @ApplicationPath} annotations defined on {@code Application} classes. + *

+ * This value is always resolved relative to {@code quarkus.http.root-path}. */ - @ConfigItem(defaultValue = "/") - String path; + @ConfigItem + Optional path; } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/HelloResource.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/HelloResource.java index 5ff8b528d8cb6..c172e743f13df 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/HelloResource.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/HelloResource.java @@ -3,11 +3,26 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; -@Path("/hello") +/** + * Per spec: + * + * Paths are relative. For an annotated class the base URI is the application path, see ApplicationPath. + * For an annotated method the base URI is the effective URI of the containing class. For the purposes of + * absolutizing a path against the base URI , a leading '/' in a path is ignored and base URIs are treated + * as if they ended in '/'. + * + */ +@Path("hello") public class HelloResource { @GET public String hello() { return "hello"; } + + @GET + @Path("/nested") + public String nested() { + return "world hello"; + } } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RelativeRestPathTestCase.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RelativeRestPathTestCase.java new file mode 100644 index 0000000000000..8d7c22e65d5eb --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RelativeRestPathTestCase.java @@ -0,0 +1,29 @@ +package io.quarkus.resteasy.reactive.server.test.path; + +import org.hamcrest.Matchers; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class RelativeRestPathTestCase { + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .withConfigurationResource("empty.properties") + .overrideConfigKey("quarkus.rest.path", "foo") + .overrideConfigKey("quarkus.http.root-path", "/app") + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClass(HelloResource.class)); + + @Test + public void testRestPath() { + RestAssured.basePath = "/"; + // This is expected behavior (relative path appended to HTTP root path) + RestAssured.when().get("/app/foo/hello").then().body(Matchers.is("hello")); + RestAssured.when().get("/app/foo/hello/nested").then().body(Matchers.is("world hello")); + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestApplicationPathTestCase.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestApplicationPathTestCase.java new file mode 100644 index 0000000000000..2bdc84d39bd7b --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestApplicationPathTestCase.java @@ -0,0 +1,45 @@ +package io.quarkus.resteasy.reactive.server.test.path; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +import org.hamcrest.Matchers; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class RestApplicationPathTestCase { + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .withConfigurationResource("empty.properties") + .overrideConfigKey("quarkus.rest.path", "/foo") + .overrideConfigKey("quarkus.http.root-path", "/app") + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClasses(HelloResource.class, BarApp.class)); + + /** + * Using @ApplicationPath will overlay/replace `quarkus.rest.path`. + * Per spec: + * + * Identifies the application path that serves as the base URI for all resource + * URIs provided by Path. May only be applied to a subclass of Application. + * + * + * This path will also be relative to the configured HTTP root + */ + @ApplicationPath("/bar") + public static class BarApp extends Application { + } + + @Test + public void testRestPath() { + RestAssured.basePath = "/"; + RestAssured.when().get("/app/bar/hello").then().body(Matchers.is("hello")); + RestAssured.when().get("/app/bar/hello/nested").then().body(Matchers.is("world hello")); + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestPathTestCase.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestPathTestCase.java index dec30f56dd0e6..379f4c84644b5 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestPathTestCase.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestPathTestCase.java @@ -2,7 +2,6 @@ import org.hamcrest.Matchers; import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -14,15 +13,16 @@ public class RestPathTestCase { @RegisterExtension static QuarkusUnitTest test = new QuarkusUnitTest() - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(HelloResource.class) - .addAsResource(new StringAsset("quarkus.rest.path=/foo"), "application.properties")); + .withConfigurationResource("empty.properties") + .overrideConfigKey("quarkus.rest.path", "/foo") + .overrideConfigKey("quarkus.http.root-path", "/app") + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClass(HelloResource.class)); @Test public void testRestPath() { - RestAssured.get("/hello") - .then().statusCode(404); - RestAssured.get("/foo/hello") - .then().statusCode(200).body(Matchers.equalTo("hello")); - + RestAssured.basePath = "/"; + RestAssured.when().get("/app/foo/hello").then().body(Matchers.is("hello")); + RestAssured.when().get("/app/foo/hello/nested").then().body(Matchers.is("world hello")); } } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/resources/empty.properties b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/resources/empty.properties new file mode 100644 index 0000000000000..e69de29bb2d1d