From 70be636bf0dedb061658f62f6749c2b54e99ba67 Mon Sep 17 00:00:00 2001 From: cghislai Date: Tue, 2 Jan 2024 08:58:19 +0100 Subject: [PATCH] Add a test for proper handling of ResponseExceptionMapper providers on subresource interface method calls (github issue 366) Signed-off-by: cghislai --- .../rest/client/tck/SubResourceTest.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/tck/src/main/java/org/eclipse/microprofile/rest/client/tck/SubResourceTest.java b/tck/src/main/java/org/eclipse/microprofile/rest/client/tck/SubResourceTest.java index dab85fa6..2370ed77 100644 --- a/tck/src/main/java/org/eclipse/microprofile/rest/client/tck/SubResourceTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/rest/client/tck/SubResourceTest.java @@ -18,33 +18,40 @@ package org.eclipse.microprofile.rest.client.tck; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; - import java.net.URI; +import jakarta.ws.rs.WebApplicationException; import org.eclipse.microprofile.rest.client.RestClientBuilder; import org.eclipse.microprofile.rest.client.tck.interfaces.RootResource; import org.eclipse.microprofile.rest.client.tck.interfaces.SubResource; import org.eclipse.microprofile.rest.client.tck.providers.ReturnWithURLRequestFilter; +import org.eclipse.microprofile.rest.client.tck.providers.TestResponseExceptionMapper; +import org.eclipse.microprofile.rest.client.tck.providers.TestResponseExceptionMapperOverridePriority; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.testng.Arquillian; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import jakarta.ws.rs.core.Response; +import static org.testng.Assert.*; + public class SubResourceTest extends Arquillian { @Deployment public static Archive createDeployment() { return ShrinkWrap.create(WebArchive.class, SubResourceTest.class.getSimpleName() + ".war") - .addClasses(RootResource.class, SubResource.class) + .addClasses(RootResource.class, SubResource.class, TestResponseExceptionMapper.class) .addPackage(ReturnWithURLRequestFilter.class.getPackage()); } + @BeforeTest + public void resetHandlers() { + TestResponseExceptionMapper.reset(); + } + @Test public void canInvokeMethodOnSubResourceInterface() throws Exception { ReturnWithURLRequestFilter filter = new ReturnWithURLRequestFilter(); @@ -59,4 +66,31 @@ public void canInvokeMethodOnSubResourceInterface() throws Exception { assertTrue(responseStr.contains("GET ") && responseStr.contains("/root/sub"), "Did not invoke expected method/URI. Expected GET .../root/sub ; got " + responseStr); } + + + @Test + public void exceptionMappedOnSubResourceInterface() throws Exception { + ReturnWithURLRequestFilter filter = new ReturnWithURLRequestFilter(); + + RestClientBuilder builder = RestClientBuilder.newBuilder() + .register(filter) + .register(TestResponseExceptionMapper.class); + + RootResource client = builder.baseUri(new URI("http://localhost/stub")).build(RootResource.class); + SubResource subClient = client.sub(); + assertNotNull(subClient, "SubResource interface is null"); + + try { + Response response = subClient.getFromSub(); + fail("A " + WebApplicationException.class + " should have been thrown via the registered " + + TestResponseExceptionMapper.class); + } catch (WebApplicationException w) { + assertEquals(w.getMessage(), TestResponseExceptionMapper.MESSAGE, + "The message should be sourced from " + TestResponseExceptionMapper.class); + assertTrue(TestResponseExceptionMapper.isHandlesCalled(), + "The handles method should have been called on " + TestResponseExceptionMapper.class); + assertTrue(TestResponseExceptionMapper.isThrowableCalled(), + "The toThrowable method should have been called on " + TestResponseExceptionMapper.class); + } + } }