| 1 | # Api controllers testing |
| 2 | |
| 3 | ## Decision |
| 4 | |
| 5 | We should change the way we're testing API controllers. |
| 6 | |
| 7 | ## Rationale |
| 8 | |
| 9 | Currently for (almost) every controller there are two test classes: |
| 10 | - one that test the behavior in an unit manner (calling the controller method, mocking collaborators) |
| 11 | - one that load a whole EDC runtime and test the behavior integrated with the rest of the services. |
| 12 | |
| 13 | This approach brings in a lot of problems: |
| 14 | - duplicated tests |
| 15 | - every time a new EDC service is added somewhere, the integration tests could fail because of missing service registrations. |
| 16 | |
| 17 | ## Approach |
| 18 | |
| 19 | The way to go would be to have a single test class for every controller that's a way between the unit and the integration approach. |
| 20 | In these tests we want to test the integration with the Jersey framework and the Controller behavior, to do that we will have |
| 21 | an abstract class that start an embedded Jetty instance with Jersey and deploys a controller. |
| 22 | ```java |
| 23 | public abstract class JerseyIntegrationTestBase { |
| 24 | private JettyService jetty; |
| 25 | protected final int port = getFreePort(); |
| 26 | protected Monitor monitor = mock(Monitor.class); |
| 27 | |
| 28 | protected abstract Object controller(); |
| 29 | |
| 30 | @BeforeEach |
| 31 | void setup() { |
| 32 | var config = new JettyConfiguration(null, null); |
| 33 | config.portMapping(new PortMapping("test", port, "/")); |
| 34 | jetty = new JettyService(config, monitor); |
| 35 | var jerseyService = new JerseyRestService(jetty, new JacksonTypeManager(), mock(JerseyConfiguration.class), monitor); |
| 36 | jetty.start(); |
| 37 | jerseyService.registerResource("test", controller()); |
| 38 | jerseyService.start(); |
| 39 | } |
| 40 | |
| 41 | @AfterEach |
| 42 | void teardown() { |
| 43 | jetty.shutdown(); |
| 44 | } |
| 45 | } |
| 46 | ``` |
| 47 | |
| 48 | This class could then be extended by the actual test class that will instantiate the controller with mocked collaborators, |
| 49 | and use `RestAssured` to call the controller methods. This way the integration with Jersey (paths, validations, serdes) |
| 50 | will be verified. |
| 51 | e.g. |
| 52 | ```java |
| 53 | @ApiTest |
| 54 | public class AssetApiControllerIntegrationTest extends JerseyIntegrationTestBase { |
| 55 | |
| 56 | private final AssetService service = mock(AssetService.class); |
| 57 | private final DataAddressResolver dataAddressResolver = mock(DataAddressResolver.class); |
| 58 | private final DtoTransformerRegistry transformerRegistry = mock(DtoTransformerRegistry.class); |
| 59 | |
| 60 | @Override |
| 61 | protected Object controller() { |
| 62 | return new AssetApiController(monitor, service, dataAddressResolver, transformerRegistry); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | void getAllAssets() { |
| 67 | when(service.query(any())).thenReturn(ServiceResult.success(Stream.of(Asset.Builder.newInstance().build()))); |
| 68 | when(transformerRegistry.transform(isA(Asset.class), eq(AssetResponseDto.class))) |
| 69 | .thenReturn(Result.success(AssetResponseDto.Builder.newInstance().build())); |
| 70 | when(transformerRegistry.transform(isA(QuerySpecDto.class), eq(QuerySpec.class))) |
| 71 | .thenReturn(Result.success(QuerySpec.Builder.newInstance().offset(10).build())); |
| 72 | |
| 73 | baseRequest() |
| 74 | .get("/assets") |
| 75 | .then() |
| 76 | .statusCode(200) |
| 77 | .contentType(JSON) |
| 78 | .body("size()", is(1)); |
| 79 | } |
| 80 | } |
| 81 | ``` |