diff --git a/contentgrid-spring-boot-autoconfigure/build.gradle b/contentgrid-spring-boot-autoconfigure/build.gradle index 6bee641c..87e97257 100644 --- a/contentgrid-spring-boot-autoconfigure/build.gradle +++ b/contentgrid-spring-boot-autoconfigure/build.gradle @@ -27,6 +27,7 @@ dependencies { compileOnly project(':contentgrid-spring-boot-actuators') compileOnly project(':contentgrid-spring-integration-events') compileOnly project(':contentgrid-spring-data-rest') + compileOnly project(':contentgrid-spring-swagger-ui') compileOnly 'org.springframework.security:spring-security-web' compileOnly 'org.springframework.security:spring-security-config' @@ -65,6 +66,8 @@ dependencies { testRuntimeOnly 'com.h2database:h2' testRuntimeOnly 'io.micrometer:micrometer-core' + testRuntimeOnly project(':contentgrid-spring-swagger-ui') + } tasks.named('test') { diff --git a/contentgrid-spring-boot-autoconfigure/src/main/java/com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfiguration.java b/contentgrid-spring-boot-autoconfigure/src/main/java/com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfiguration.java new file mode 100644 index 00000000..1bf05f9c --- /dev/null +++ b/contentgrid-spring-boot-autoconfigure/src/main/java/com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfiguration.java @@ -0,0 +1,15 @@ +package com.contentgrid.spring.boot.autoconfigure.swagger.ui; + +import com.contentgrid.spring.swagger.ui.SwaggerUIRestConfiguration; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Import; + +@AutoConfiguration +@ConditionalOnWebApplication +@ConditionalOnClass(SwaggerUIRestConfiguration.class) +@Import(SwaggerUIRestConfiguration.class) +public class SwaggerUIRestAutoConfiguration { + +} diff --git a/contentgrid-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/contentgrid-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index b9801cba..70121b87 100644 --- a/contentgrid-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/contentgrid-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -4,4 +4,5 @@ com.contentgrid.spring.boot.autoconfigure.integration.EventsAutoConfiguration com.contentgrid.spring.boot.autoconfigure.s3.S3RegionAutoConfiguration com.contentgrid.spring.boot.autoconfigure.flyway.FlywayPostgresAutoConfiguration com.contentgrid.spring.boot.autoconfigure.security.ActuatorEndpointsWebSecurityAutoConfiguration -com.contentgrid.spring.boot.autoconfigure.security.MultiTenantOAuth2ResourceServerAutoConfiguration \ No newline at end of file +com.contentgrid.spring.boot.autoconfigure.security.MultiTenantOAuth2ResourceServerAutoConfiguration +com.contentgrid.spring.boot.autoconfigure.swagger.ui.SwaggerUIRestAutoConfiguration \ No newline at end of file diff --git a/contentgrid-spring-boot-autoconfigure/src/test/java/com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfigurationTest.java b/contentgrid-spring-boot-autoconfigure/src/test/java/com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfigurationTest.java new file mode 100644 index 00000000..65cdb822 --- /dev/null +++ b/contentgrid-spring-boot-autoconfigure/src/test/java/com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfigurationTest.java @@ -0,0 +1,66 @@ +package com.contentgrid.spring.boot.autoconfigure.swagger.ui; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.contentgrid.spring.swagger.ui.SwaggerUIInitializerController; +import com.contentgrid.spring.swagger.ui.SwaggerUIRestConfiguration; +import org.hamcrest.core.StringContains; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.boot.test.context.FilteredClassLoader; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.test.web.servlet.client.MockMvcWebTestClient; +import org.springframework.web.context.WebApplicationContext; + +class SwaggerUIRestAutoConfigurationTest { + + WebApplicationContextRunner runner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + ServletWebServerFactoryAutoConfiguration.class, + DispatcherServletAutoConfiguration.class, + WebMvcAutoConfiguration.class, + SwaggerUIRestAutoConfiguration.class + )); + + + @Test + void autoConfigurationLoaded() { + runner.run(context -> { + assertThat(context).hasSingleBean(SwaggerUIRestAutoConfiguration.class); + assertThat(context).hasSingleBean(SwaggerUIRestConfiguration.class); + assertThat(context).hasSingleBean(SwaggerUIInitializerController.class); + + webTestClient(context) + .get().uri("/webjars/swagger-ui/swagger-initializer.js") + .exchange() + .expectBody(String.class) + .value(StringContains.containsString("/openapi.yml")); + }); + } + + @Test + void conditionalOnSwaggerUIClass() { + runner.withClassLoader(new FilteredClassLoader(SwaggerUIRestConfiguration.class)) + .run(context -> { + assertThat(context).doesNotHaveBean(SwaggerUIRestAutoConfiguration.class); + assertThat(context).doesNotHaveBean(SwaggerUIRestConfiguration.class); + assertThat(context).doesNotHaveBean(SwaggerUIInitializerController.class); + + // swagger-ui autoconfiguration not loaded, load default swagger-init petstore config + webTestClient(context) + .get().uri("/webjars/swagger-ui/swagger-initializer.js") + .exchange() + .expectBody(String.class) + .value(StringContains.containsString("https://petstore.swagger.io/v2/swagger.json")); + }); + } + + static WebTestClient webTestClient(WebApplicationContext context) { + return MockMvcWebTestClient.bindToApplicationContext(context).build(); + } + +} \ No newline at end of file diff --git a/contentgrid-spring-swagger-ui/src/main/java/com/contentgrid/spring/swagger/ui/SwaggerUIRestConfiguration.java b/contentgrid-spring-swagger-ui/src/main/java/com/contentgrid/spring/swagger/ui/SwaggerUIRestConfiguration.java new file mode 100644 index 00000000..a7c0bf8f --- /dev/null +++ b/contentgrid-spring-swagger-ui/src/main/java/com/contentgrid/spring/swagger/ui/SwaggerUIRestConfiguration.java @@ -0,0 +1,10 @@ +package com.contentgrid.spring.swagger.ui; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@Import(SwaggerUIInitializerController.class) +public class SwaggerUIRestConfiguration { + +}