diff --git a/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/JerseyRestServiceTest.java b/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/JerseyRestServiceTest.java index 6122c3942aa..1d5d3b4c09e 100644 --- a/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/JerseyRestServiceTest.java +++ b/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/JerseyRestServiceTest.java @@ -48,9 +48,9 @@ public class JerseyRestServiceTest { private final int httpPort = getFreePort(); + private final Monitor monitor = mock(Monitor.class); private JerseyRestService jerseyRestService; private JettyService jettyService; - private final Monitor monitor = mock(Monitor.class); @AfterEach void teardown() { @@ -194,6 +194,47 @@ void verifySeparateFilters() { verifyNoMoreInteractions(fooRequestFilter); } + @Test + @DisplayName("Verifies that different filters fire for different controllers") + void verifySeparateFiltersForDifferentControllers() { + var port1 = getFreePort(); + startJetty( + PortMapping.getDefault(httpPort), + new PortMapping("foo", port1, "/foo") + ); + // mocking the ContextRequestFilter doesn't work here, Mockito apparently re-uses mocks for the same target class + var testControllerFilter = mock(BarRequestFilter.class); + var bazControllerFilter = mock(FooRequestFilter.class); + + jerseyRestService.registerResource("foo", new TestController()); + jerseyRestService.registerResource("foo", new BazController()); + jerseyRestService.registerDynamicResource("foo", TestController.class, testControllerFilter); + jerseyRestService.registerDynamicResource("foo", BazController.class, bazControllerFilter); + jerseyRestService.start(); + + //verify that the first request hits only the TestController filter + given() + .get("http://localhost:" + port1 + "/foo/test/resource") + .then() + .statusCode(200); + + verify(bazControllerFilter, never()).filter(any(ContainerRequestContext.class)); + verify(testControllerFilter).filter(any(ContainerRequestContext.class)); + verifyNoMoreInteractions(testControllerFilter); + + reset(bazControllerFilter, testControllerFilter); + + // verify that the second request only hits the BazController filter + given() + .get("http://localhost:" + port1 + "/foo/baz/resource") + .then() + .statusCode(200); + + verify(testControllerFilter, never()).filter(any()); + verify(bazControllerFilter).filter(any()); + verifyNoMoreInteractions(bazControllerFilter); + } + @Test @DisplayName("Verifies that registering two identical paths raises an exception") void verifyIdenticalPathsRaiseException() { @@ -247,6 +288,17 @@ public String foo() { } } + @Produces(MediaType.TEXT_PLAIN) + @Path("/baz") + public static class BazController { //needs to be public, otherwise it won't get picked up + + @GET + @Path("/resource") + public String foo() { + return "exists"; + } + } + //needs to be public, otherwise it won't get picked up public static class BarRequestFilter implements ContainerRequestFilter { diff --git a/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/feature/DynamicResourceFeatureTest.java b/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/feature/DynamicResourceFeatureTest.java index 0272a84acb7..979528d3e11 100644 --- a/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/feature/DynamicResourceFeatureTest.java +++ b/extensions/common/http/jersey-core/src/test/java/org/eclipse/edc/web/jersey/feature/DynamicResourceFeatureTest.java @@ -28,7 +28,6 @@ public class DynamicResourceFeatureTest { - @Test void configure() { var feature = new DynamicResourceFeature(Map.of(Target.class, List.of(new Feature()))); @@ -44,10 +43,8 @@ void configure() { } record Feature() { - } record Target() { - } }