Skip to content

Commit

Permalink
chore: add tests for dynamic feature in JerserRestServiceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 18, 2024
1 parent aca1429 commit 5ae3845
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

public class DynamicResourceFeatureTest {


@Test
void configure() {
var feature = new DynamicResourceFeature(Map.of(Target.class, List.of(new Feature())));
Expand All @@ -44,10 +43,8 @@ void configure() {
}

record Feature() {

}

record Target() {

}
}

0 comments on commit 5ae3845

Please sign in to comment.