forked from quarkusio/quarkus
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make TestHttpEndpoint work for ResteasyReactive
Also add support for setting the root path using config. Fixes quarkusio#15001
- Loading branch information
1 parent
8a9f1c9
commit b6b6a98
Showing
9 changed files
with
168 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ain/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkus.resteasy.reactive.server.deployment; | ||
|
||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot(name = "rest") | ||
public class ResteasyReactiveServerConfig { | ||
|
||
/** | ||
* Set this to override the default path for JAX-RS resources if there are no | ||
* annotated application classes. | ||
*/ | ||
@ConfigItem(defaultValue = "/") | ||
String path; | ||
} |
13 changes: 13 additions & 0 deletions
13
...deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/HelloResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.quarkus.resteasy.reactive.server.test.path; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
@Path("/hello") | ||
public class HelloResource { | ||
|
||
@GET | ||
public String hello() { | ||
return "hello"; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...loyment/src/test/java/io/quarkus/resteasy/reactive/server/test/path/RestPathTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.quarkus.resteasy.reactive.server.test.path; | ||
|
||
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; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
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")); | ||
|
||
@Test | ||
public void testRestPath() { | ||
RestAssured.get("/hello") | ||
.then().statusCode(404); | ||
RestAssured.get("/foo/hello") | ||
.then().statusCode(200).body(Matchers.equalTo("hello")); | ||
|
||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...in/java/io/quarkus/resteasy/reactive/server/runtime/ResteasyReactiveTestHttpProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.quarkus.resteasy.reactive.server.runtime; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import javax.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import io.quarkus.runtime.test.TestHttpEndpointProvider; | ||
|
||
public class ResteasyReactiveTestHttpProvider implements TestHttpEndpointProvider { | ||
@Override | ||
public Function<Class<?>, String> endpointProvider() { | ||
return new Function<Class<?>, String>() { | ||
@Override | ||
public String apply(Class<?> aClass) { | ||
String value = getPath(aClass); | ||
if (value == null) { | ||
return null; | ||
} | ||
if (value.startsWith("/")) { | ||
value = value.substring(1); | ||
} | ||
//TODO: there is not really any way to handle @ApplicationPath, we could do something for @QuarkusTest apps but we can't for | ||
//native apps, so we just have to document the limitation | ||
String path = "/"; | ||
Optional<String> appPath = ConfigProvider.getConfig().getOptionalValue("quarkus.rest.path", String.class); | ||
if (appPath.isPresent()) { | ||
path = appPath.get(); | ||
} | ||
if (!path.endsWith("/")) { | ||
path = path + "/"; | ||
} | ||
value = path + value; | ||
return value; | ||
} | ||
}; | ||
} | ||
|
||
private String getPath(Class<?> aClass) { | ||
String value = null; | ||
for (Annotation annotation : aClass.getAnnotations()) { | ||
if (annotation.annotationType().getName().equals(Path.class.getName())) { | ||
try { | ||
value = (String) annotation.annotationType().getMethod("value").invoke(annotation); | ||
break; | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
if (value == null) { | ||
for (Class<?> i : aClass.getInterfaces()) { | ||
value = getPath(i); | ||
if (value != null) { | ||
break; | ||
} | ||
} | ||
} | ||
if (value == null) { | ||
if (aClass.getSuperclass() != Object.class) { | ||
value = getPath(aClass.getSuperclass()); | ||
} | ||
} | ||
return value; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ime/src/main/resources/META-INF/services/io.quarkus.runtime.test.TestHttpEndpointProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.resteasy.reactive.server.runtime.ResteasyReactiveTestHttpProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters