From 6cdc6779e15fbd6fce3c4c5834be58ceba79861c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller?= Date: Fri, 7 Jun 2024 16:35:41 +0200 Subject: [PATCH] chore(core): add integration test for custom springwolf path Co-authored-by: Timon Back --- .../SpringwolfUiResourceConfigurer.java | 9 ++- .../core/controller/AsyncApiController.java | 6 +- ...ustomPathConfigurationIntegrationTest.java | 56 +++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 springwolf-examples/springwolf-sqs-example/src/test/java/io/github/springwolf/examples/sqs/CustomPathConfigurationIntegrationTest.java diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java index 8757a23c4..ccea00426 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/SpringwolfUiResourceConfigurer.java @@ -3,6 +3,7 @@ import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.web.WebProperties; import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties; import org.springframework.context.annotation.Configuration; @@ -15,6 +16,7 @@ import java.util.Arrays; import java.util.List; +@Slf4j @Configuration @RequiredArgsConstructor @Order(value = Ordered.HIGHEST_PRECEDENCE) // Highest so that all others will replace this configuration @@ -26,8 +28,11 @@ public class SpringwolfUiResourceConfigurer implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler( - springwolfConfigProperties.getPath().getBase() + "/**", webMvcProperties.getStaticPathPattern()) + String springwolfBasePath = springwolfConfigProperties.getPath().getBase(); + + log.debug("Serving Springwolf with base-path: {}", springwolfBasePath); + + registry.addResourceHandler(springwolfBasePath + "/**", webMvcProperties.getStaticPathPattern()) .addResourceLocations(buildStaticLocation()); } diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/controller/AsyncApiController.java b/springwolf-core/src/main/java/io/github/springwolf/core/controller/AsyncApiController.java index f1ac23f16..bbf6f986e 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/controller/AsyncApiController.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/controller/AsyncApiController.java @@ -22,8 +22,8 @@ public class AsyncApiController { path = { "${springwolf.paths.docs:/springwolf/docs}", "${springwolf.paths.docs:/springwolf/docs}.json", - "${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}", - "${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}.json" + "${springwolf.path.base:/springwolf}${springwolf.path.docs:/docs}", + "${springwolf.path.base:/springwolf}${springwolf.path.docs:/docs}.json" }, produces = MediaType.APPLICATION_JSON_VALUE) public String asyncApiJson() throws JsonProcessingException { @@ -36,7 +36,7 @@ public String asyncApiJson() throws JsonProcessingException { @GetMapping( path = { "${springwolf.paths.docs:/springwolf/docs}.yaml", - "${springwolf.path.base:/springwolf}/${springwolf.path.docs:/docs}.yaml" + "${springwolf.path.base:/springwolf}${springwolf.path.docs:/docs}.yaml" }, produces = "application/yaml") public String asyncApiYaml() throws JsonProcessingException { diff --git a/springwolf-examples/springwolf-sqs-example/src/test/java/io/github/springwolf/examples/sqs/CustomPathConfigurationIntegrationTest.java b/springwolf-examples/springwolf-sqs-example/src/test/java/io/github/springwolf/examples/sqs/CustomPathConfigurationIntegrationTest.java new file mode 100644 index 000000000..6dfafa0d6 --- /dev/null +++ b/springwolf-examples/springwolf-sqs-example/src/test/java/io/github/springwolf/examples/sqs/CustomPathConfigurationIntegrationTest.java @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: Apache-2.0 +package io.github.springwolf.examples.sqs; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.TestPropertySource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SpringBootTest( + classes = {SpringwolfSqsExampleApplication.class}, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ExtendWith({SqsTestContainerExtension.class}) +@TestPropertySource( + properties = {"springwolf.path.base=/my-custom/springwolf/endpoint/test", "springwolf.path.docs=/apispec"}) +class CustomPathConfigurationIntegrationTest { + + @DynamicPropertySource + static void setUpTestContainers(DynamicPropertyRegistry registry) { + SqsTestContainerExtension.overrideConfiguration(registry); + } + + @Autowired + private TestRestTemplate restTemplate; + + @Test + void getSpec() { + String url = "/my-custom/springwolf/endpoint/test/apispec"; + ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } + + @Test + void canPublish() { + String url = "/my-custom/springwolf/endpoint/test/sqs/publish"; + ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } + + @Test + void asyncapiDocsShouldReturnTheCorrectJsonResponse() { + String url = "/my-custom/springwolf/endpoint/test/asyncapi-ui.html"; + ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } +}