Create a bean for ResourceControllerAdvice. (#2285)

Adding test

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2023-06-13 19:47:05 -04:00
committed by GitHub
parent 538267a7e3
commit 5d757d81c1
3 changed files with 24 additions and 1 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.cloud.config.server.environment.EnvironmentController
import org.springframework.cloud.config.server.environment.EnvironmentEncryptorEnvironmentRepository;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.resource.ResourceController;
import org.springframework.cloud.config.server.resource.ResourceControllerAdvice;
import org.springframework.cloud.config.server.resource.ResourceRepository;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
@@ -95,6 +96,12 @@ public class ConfigServerMvcConfiguration implements WebMvcConfigurer {
return controller;
}
@Bean
@ConditionalOnBean(ResourceController.class)
public ResourceControllerAdvice resourceControllerAdvice() {
return new ResourceControllerAdvice();
}
private EnvironmentRepository encrypted(EnvironmentRepository envRepository, ConfigServerProperties server) {
EnvironmentEncryptorEnvironmentRepository encrypted = new EnvironmentEncryptorEnvironmentRepository(
envRepository, this.environmentEncryptors);

View File

@@ -27,7 +27,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
*/
@RestControllerAdvice(basePackages = { "org.springframework.cloud.config.server.resource" })
@Order
class ResourceControllerAdvice {
public class ResourceControllerAdvice {
@ExceptionHandler(NoSuchResourceException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)

View File

@@ -121,6 +121,17 @@ public class ResourceControllerIntegrationTests {
verify(this.resources).findOne("foo", "default", "master", "foo.txt");
}
@Test
public void resourceHttpDoesNotExist() throws Exception {
when(this.resources.findOne("foo", "default", "master", "doesNotExist.txt"))
.thenThrow(new NoSuchResourceException("Does not exist"));
ResponseEntity<String> response = new TestRestTemplate()
.getForEntity("http://localhost:" + port + "/foo/default/master/doesNotExist.txt", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
verify(this.resources).findOne("foo", "default", "master", "doesNotExist.txt");
}
@Test
public void resourceNoLabel() throws Exception {
when(this.repository.findOne("foo", "default", null, false))
@@ -178,6 +189,11 @@ public class ResourceControllerIntegrationTests {
return repository;
}
@Bean
public ResourceControllerAdvice resourceControllerAdvice() {
return new ResourceControllerAdvice();
}
@Bean
public EnvironmentController environmentController() {
return new EnvironmentController(environmentRepository());