Skip to content

Commit

Permalink
fix(jersey): bind controllers to the correct context (#3327)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Jul 25, 2023
1 parent deaa26a commit 90803b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void registerContext(String contextAlias, List<Object> controllers) {
// Register controller (JAX-RS resources) with Jersey. Instances instead of classes are used so extensions may inject them with dependencies and manage their lifecycle.
// In order to use instances with Jersey, the controller types must be registered along with an {@link AbstractBinder} that maps those types to the instances.
resourceConfig.registerClasses(controllers.stream().map(Object::getClass).collect(toSet()));
resourceConfig.registerInstances(new Binder());
resourceConfig.registerInstances(new Binder(controllers));
resourceConfig.registerInstances(new ObjectMapperProvider(typeManager.getMapper()));
resourceConfig.registerInstances(new EdcApiExceptionMapper());
resourceConfig.registerInstances(new UnexpectedExceptionMapper(monitor));
Expand All @@ -109,11 +109,17 @@ private void registerContext(String contextAlias, List<Object> controllers) {
/**
* Maps (JAX-RS resource) instances to types.
*/
private class Binder extends AbstractBinder {
private static class Binder extends AbstractBinder {

private final List<Object> controllers;

Binder(List<Object> controllers) {
this.controllers = controllers;
}

@Override
protected void configure() {
controllers.forEach((key, value) -> value.forEach(c -> bind(c).to((Class<? super Object>) c.getClass())));
controllers.forEach(c -> bind(c).to((Class<? super Object>) c.getClass()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.edc.web.jetty.JettyService;
import org.eclipse.edc.web.jetty.PortMapping;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -42,6 +41,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -50,11 +50,11 @@ public class JerseyRestServiceTest {
private final int httpPort = getFreePort();
private JerseyRestService jerseyRestService;
private JettyService jettyService;
private Monitor monitorMock;
private final Monitor monitor = mock(Monitor.class);

@BeforeEach
void setup() {
monitorMock = mock(Monitor.class);
@AfterEach
void teardown() {
jettyService.shutdown();
}

@Test
Expand All @@ -79,8 +79,10 @@ void verifyAnotherContextPath() {
PortMapping.getDefault(httpPort),
new PortMapping("path", anotherPort, "/path")
);
jerseyRestService.registerResource("path", new TestController());
jerseyRestService.registerResource(new TestController());
var pathController = spy(new TestController());
var defaultController = spy(new TestController());
jerseyRestService.registerResource("path", pathController);
jerseyRestService.registerResource(defaultController);
jerseyRestService.start();

given()
Expand All @@ -89,11 +91,17 @@ void verifyAnotherContextPath() {
.statusCode(200)
.body(is("exists"));

verify(pathController).foo();
verifyNoInteractions(defaultController);

given()
.get("http://localhost:" + httpPort + "/api/test/resource")
.then()
.statusCode(200)
.body(is("exists"));

verifyNoMoreInteractions(pathController);
verify(defaultController).foo();
}

@Test
Expand All @@ -106,7 +114,9 @@ void verifyIdenticalContextPats_throwsException() {

jerseyRestService.registerResource("path1", new TestController());
jerseyRestService.registerResource("path2", new TestController());
assertThatThrownBy(() -> jerseyRestService.start()).isInstanceOf(EdcException.class).hasRootCauseInstanceOf(IllegalStateException.class);

assertThatThrownBy(() -> jerseyRestService.start()).isInstanceOf(EdcException.class)
.hasRootCauseInstanceOf(IllegalStateException.class);
}

@Test
Expand Down Expand Up @@ -197,6 +207,7 @@ void verifyIdenticalPathsRaiseException() {

jerseyRestService.registerResource("another", new TestController());
jerseyRestService.registerResource("yet-another", new TestController());

assertThatThrownBy(() -> jerseyRestService.start()).isInstanceOf(EdcException.class)
.hasRootCauseInstanceOf(IllegalStateException.class);
}
Expand All @@ -211,20 +222,16 @@ void verifyInvalidContextAlias_shouldThrowException() {
);

jerseyRestService.registerResource("not-exists", new TestController());

assertThatThrownBy(() -> jerseyRestService.start()).isInstanceOf(EdcException.class)
.hasRootCauseInstanceOf(IllegalArgumentException.class);
}

@AfterEach
void teardown() {
jettyService.shutdown();
}

private void startJetty(PortMapping... mapping) {
JettyConfiguration config = new JettyConfiguration(null, null);
var config = new JettyConfiguration(null, null);
Arrays.stream(mapping).forEach(config::portMapping);
jettyService = new JettyService(config, monitorMock);
jerseyRestService = new JerseyRestService(jettyService, new TypeManager(), JerseyConfiguration.none(), monitorMock);
jettyService = new JettyService(config, monitor);
jerseyRestService = new JerseyRestService(jettyService, new TypeManager(), JerseyConfiguration.none(), monitor);
jettyService.start();
}

Expand Down

0 comments on commit 90803b3

Please sign in to comment.