-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue-42851: port resteasy fix for sub-resources from resteasy/restea…
…sy-microprofile PR #241
- Loading branch information
1 parent
dcd9928
commit 46843b3
Showing
8 changed files
with
441 additions
and
17 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...easy-client/deployment/src/test/java/io/quarkus/restclient/exception/SubResourceTest.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,130 @@ | ||
package io.quarkus.restclient.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.net.URL; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.restclient.exception.resource.ClientRootResource; | ||
import io.quarkus.restclient.exception.resource.ClientSubResource; | ||
import io.quarkus.restclient.exception.resource.ServerResource; | ||
import io.quarkus.restclient.exception.resource.TestException; | ||
import io.quarkus.restclient.exception.resource.TestExceptionMapper; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
|
||
/** | ||
* Tests client sub-resources | ||
* | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
public class SubResourceTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(SubResourceTest.class, | ||
ClientRootResource.class, ClientSubResource.class, ServerResource.class, | ||
TestExceptionMapper.class, TestException.class)) | ||
.overrideRuntimeConfigKey("quarkus.http.port", "0") | ||
.overrideRuntimeConfigKey("quarkus.http.test-port", "0"); | ||
|
||
@TestHTTPResource | ||
URL url; | ||
|
||
/** | ||
* Creates a REST client with an attached exception mapper. The exception mapper will throw a {@link TestException}. | ||
* This test invokes a call to the root resource. | ||
* | ||
* @throws Exception if a test error occurs | ||
*/ | ||
@Test | ||
public void rootResourceExceptionMapper() { | ||
try (final ClientRootResource rootResource = RestClientBuilder.newBuilder().baseUrl(url) | ||
.build(ClientRootResource.class)) { | ||
rootResource.fromRoot(); | ||
fail("fromRoot() should have thrown a TestException"); | ||
} catch (TestException expected) { | ||
assertEquals("RootResource failed on purpose", expected.getMessage()); | ||
} catch (Exception e) { | ||
failWithException(e, "fromRoot"); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a REST client with an attached exception mapper. The exception mapper will throw a {@link TestException}. | ||
* This test invokes a call to the sub-resource. The sub-resource then invokes an additional call which should also | ||
* result in a {@link TestException} thrown. | ||
* | ||
* @throws Exception if a test error occurs | ||
*/ | ||
@Test | ||
public void subResourceExceptionMapper() { | ||
try (final ClientRootResource rootResource = RestClientBuilder.newBuilder().baseUrl(url) | ||
.build(ClientRootResource.class)) { | ||
final ClientSubResource subResource = rootResource.subResource(); | ||
assertNotNull(subResource, "The SubResource should not be null"); | ||
subResource.fromSub(); | ||
fail("fromSub() should have thrown a TestException"); | ||
} catch (TestException expected) { | ||
assertEquals("SubResource failed on purpose", expected.getMessage()); | ||
} catch (Exception e) { | ||
failWithException(e, "fromSub"); | ||
} | ||
} | ||
|
||
/** | ||
* This test invokes a call to the sub-resource. The sub-resource then invokes an additional call which should | ||
* return the header value for {@code test-header}. | ||
* | ||
* @throws Exception if a test error occurs | ||
*/ | ||
@Test | ||
public void subResourceWithHeader() throws Exception { | ||
try (final ClientRootResource rootResource = RestClientBuilder.newBuilder().baseUrl(url) | ||
.build(ClientRootResource.class)) { | ||
final ClientSubResource subResource = rootResource.subResource(); | ||
assertNotNull(subResource, "The SubResource should not be null"); | ||
try (final Response response = subResource.withHeader()) { | ||
assertEquals(Response.Status.OK, response.getStatusInfo()); | ||
assertEquals("SubResourceHeader", response.readEntity(String.class)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This test invokes a call to the sub-resource. The sub-resource then invokes an additional call which should | ||
* return the header value for {@code test-global-header}. | ||
* | ||
* @throws Exception if a test error occurs | ||
*/ | ||
@Test | ||
public void subResourceWithGlobalHeader() throws Exception { | ||
try (final ClientRootResource rootResource = RestClientBuilder.newBuilder().baseUrl(url) | ||
.build(ClientRootResource.class)) { | ||
final ClientSubResource subResource = rootResource.subResource(); | ||
assertNotNull(subResource, "The SubResource should not be null"); | ||
try (final Response response = subResource.withGlobalHeader()) { | ||
assertEquals(Response.Status.OK, response.getStatusInfo()); | ||
assertEquals("GlobalSubResourceHeader", response.readEntity(String.class)); | ||
} | ||
} | ||
} | ||
|
||
private static void failWithException(final Exception e, final String methodName) { | ||
final StringWriter writer = new StringWriter(); | ||
writer.write(methodName); | ||
writer.write("() should have thrown an TestException. Instead got: "); | ||
writer.write(System.lineSeparator()); | ||
e.printStackTrace(new PrintWriter(writer)); | ||
fail(writer.toString()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...deployment/src/test/java/io/quarkus/restclient/exception/resource/ClientRootResource.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,40 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.restclient.exception.resource; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
@RegisterRestClient | ||
@Path("/root") | ||
public interface ClientRootResource extends AutoCloseable { | ||
|
||
@Path("/sub") | ||
ClientSubResource subResource(); | ||
|
||
@GET | ||
Response fromRoot() throws TestException; | ||
} |
49 changes: 49 additions & 0 deletions
49
.../deployment/src/test/java/io/quarkus/restclient/exception/resource/ClientSubResource.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,49 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.restclient.exception.resource; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
@ClientHeaderParam(name = "test-global-header", value = "GlobalSubResourceHeader") | ||
public interface ClientSubResource { | ||
|
||
@GET | ||
Response fromSub() throws TestException; | ||
|
||
@GET | ||
@ClientHeaderParam(name = "test-header", value = "SubResourceHeader") | ||
@Path("/header") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
Response withHeader(); | ||
|
||
@GET | ||
@Path("/global/header") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
Response withGlobalHeader(); | ||
} |
59 changes: 59 additions & 0 deletions
59
...ent/deployment/src/test/java/io/quarkus/restclient/exception/resource/ServerResource.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,59 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.restclient.exception.resource; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.HeaderParam; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
@Path("/root") | ||
public class ServerResource { | ||
|
||
@GET | ||
public Response fromRoot() { | ||
return Response.serverError().entity("RootResource failed on purpose").build(); | ||
} | ||
|
||
@GET | ||
@Path("/sub") | ||
public Response fromSub() { | ||
return Response.serverError().entity("SubResource failed on purpose").build(); | ||
} | ||
|
||
@GET | ||
@Path("/sub/header") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Response subHeader(@HeaderParam("test-header") final String value) { | ||
return Response.ok(value).build(); | ||
} | ||
|
||
@GET | ||
@Path("/sub/global/header") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Response subGlobalHeader(@HeaderParam("test-global-header") final String value) { | ||
return Response.ok(value).build(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ient/deployment/src/test/java/io/quarkus/restclient/exception/resource/TestException.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,30 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.restclient.exception.resource; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
public class TestException extends RuntimeException { | ||
|
||
public TestException(final String msg) { | ||
super(msg); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...eployment/src/test/java/io/quarkus/restclient/exception/resource/TestExceptionMapper.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,36 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2024 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.quarkus.restclient.exception.resource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; | ||
|
||
/** | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
@ApplicationScoped | ||
public class TestExceptionMapper implements ResponseExceptionMapper<TestException> { | ||
@Override | ||
public TestException toThrowable(final Response response) { | ||
return new TestException(response.readEntity(String.class)); | ||
} | ||
} |
Oops, something went wrong.