Skip to content

Commit

Permalink
Merge pull request #170 from xenit-eu/swagger-ui-loads-petstore
Browse files Browse the repository at this point in the history
Add autoconfig to load contentgrid-spring-swagger-ui controller
  • Loading branch information
tgeens authored Jan 16, 2024
2 parents 1808ef7 + 8900fc1 commit 0bbed3a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions contentgrid-spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -65,6 +66,8 @@ dependencies {
testRuntimeOnly 'com.h2database:h2'
testRuntimeOnly 'io.micrometer:micrometer-core'

testRuntimeOnly project(':contentgrid-spring-swagger-ui')

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
com.contentgrid.spring.boot.autoconfigure.security.MultiTenantOAuth2ResourceServerAutoConfiguration
com.contentgrid.spring.boot.autoconfigure.swagger.ui.SwaggerUIRestAutoConfiguration
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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 {

}

0 comments on commit 0bbed3a

Please sign in to comment.