OpenApiBundle implements ConfiguredBundle<T> { @Override public void run(T configuration, Environment environment) throws Exception { OpenApiConfiguration openApiConfiguration = getOpenApiConfiguration(configuration); if (openApiConfiguration == null) { LOG.error("Cannot instantiate OpenApiBundle. Missing OpenApiConfiguration."); throw new IllegalStateException("You need to provide an instance of OpenApiConfiguration"); } if (!openApiConfiguration.isEnabled()) { LOG.info("OpenAPI/Swagger is disabled."); return; } new AssetsBundle("/swagger-static", "/swagger-static", null, "swagger-assets") .run(configuration, environment); SwaggerConfiguration oasConfiguration = openApiConfiguration.build(); new JaxrsOpenApiContextBuilder<>().openApiConfiguration(oasConfiguration).buildContext(true); LOG.info("Registering OpenAPI and Swagger resources."); environment.jersey().register(new OpenApiResource().openApiConfiguration(oasConfiguration)); environment.jersey().register(new SwaggerSerializers()); environment.jersey().register(new SwaggerResource()); }  @Override void initialize(Bootstrap<?> bootstrap); @Override void run(T configuration, Environment environment); abstract OpenApiConfiguration getOpenApiConfiguration(T configuration);  }
@Test void testRun() throws Exception { var environment = mock(Environment.class); var jersey = mock(JerseyEnvironment.class); when(environment.jersey()).thenReturn(jersey); when(environment.servlets()).thenReturn(SERVLET); OpenApiBundle<ThunderConfiguration> bundle = new OpenApiBundle<ThunderConfiguration>() { @Override public OpenApiConfiguration getOpenApiConfiguration(ThunderConfiguration configuration) { return new OpenApiConfiguration(); } }; ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); bundle.run(CONFIG, environment); verify(jersey, times(3)).register(captor.capture()); List<Object> values = captor.getAllValues(); assertAll("Assert all objects were registered to Jersey", () -> assertEquals(1, values.stream().filter(v -> v instanceof OpenApiResource).count()), () -> assertEquals(1, values.stream().filter(v -> v instanceof SwaggerSerializers).count()), () -> assertEquals(1, values.stream().filter(v -> v instanceof SwaggerResource).count())); }
@Test void testRunDisabled() throws Exception { var environment = mock(Environment.class); var jersey = mock(JerseyEnvironment.class); when(environment.jersey()).thenReturn(jersey); when(environment.servlets()).thenReturn(SERVLET); OpenApiConfiguration mockedConfiguration = mock(OpenApiConfiguration.class); when(mockedConfiguration.isEnabled()).thenReturn(false); OpenApiBundle<ThunderConfiguration> bundle = new OpenApiBundle<ThunderConfiguration>() { @Override public OpenApiConfiguration getOpenApiConfiguration(ThunderConfiguration configuration) { return mockedConfiguration; } }; ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); bundle.run(CONFIG, environment); verify(jersey, never()).register(captor.capture()); List<Object> values = captor.getAllValues(); assertEquals(0, values.size()); }
@Test void testRunNullConfiguration() { var environment = mock(Environment.class); var jersey = mock(JerseyEnvironment.class); when(environment.jersey()).thenReturn(jersey); when(environment.servlets()).thenReturn(SERVLET); OpenApiBundle<ThunderConfiguration> bundle = new OpenApiBundle<ThunderConfiguration>() { @Override public OpenApiConfiguration getOpenApiConfiguration(ThunderConfiguration configuration) { return null; } }; assertThrows(IllegalStateException.class, () -> bundle.run(CONFIG, environment)); }