-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from xenit-eu/swagger-ui-loads-petstore
Add autoconfig to load contentgrid-spring-swagger-ui controller
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../com/contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../contentgrid/spring/boot/autoconfigure/swagger/ui/SwaggerUIRestAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...wagger-ui/src/main/java/com/contentgrid/spring/swagger/ui/SwaggerUIRestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |