Allow operations to produce different output
Update the actuator @Enpoint` infrastructure code so that operations may inject enums that indicate the type of output to produce. A new `Producible` interface can be implemented by any enum that indicates the mime-type that an enum value produces. The new `OperationArgumentResolver` provides a general strategy for resolving operation arguments with `ProducibleOperationArgumentResolver` providing support for `Producible` enums. Existing injection support has been refactored to use the new resolver. See gh-25738
This commit is contained in:
committed by
Phillip Webb
parent
663fd8ce5e
commit
1ec49cee8b
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,11 +39,24 @@ class InvocationContextTests {
|
||||
private final Map<String, Object> arguments = Collections.singletonMap("test", "value");
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void createWhenApiVersionIsNullUsesLatestVersion() {
|
||||
InvocationContext context = new InvocationContext(null, this.securityContext, this.arguments);
|
||||
assertThat(context.getApiVersion()).isEqualTo(ApiVersion.LATEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCreatedWithoutApiVersionThenGetApiVersionReturnsLatestVersion() {
|
||||
InvocationContext context = new InvocationContext(this.securityContext, this.arguments);
|
||||
assertThat(context.getApiVersion()).isEqualTo(ApiVersion.LATEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCreatedWithoutApiVersionThenResolveApiVersionReturnsLatestVersion() {
|
||||
InvocationContext context = new InvocationContext(this.securityContext, this.arguments);
|
||||
assertThat(context.resolveArgument(ApiVersion.class)).isEqualTo(ApiVersion.LATEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenSecurityContextIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new InvocationContext(null, this.arguments))
|
||||
@@ -57,17 +70,25 @@ class InvocationContextTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void getApiVersionReturnsApiVersion() {
|
||||
InvocationContext context = new InvocationContext(ApiVersion.V2, this.securityContext, this.arguments);
|
||||
assertThat(context.getApiVersion()).isEqualTo(ApiVersion.V2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void getSecurityContextReturnsSecurityContext() {
|
||||
InvocationContext context = new InvocationContext(this.securityContext, this.arguments);
|
||||
assertThat(context.getSecurityContext()).isEqualTo(this.securityContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveSecurityContextReturnsSecurityContext() {
|
||||
InvocationContext context = new InvocationContext(this.securityContext, this.arguments);
|
||||
assertThat(context.resolveArgument(SecurityContext.class)).isEqualTo(this.securityContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getArgumentsReturnsArguments() {
|
||||
InvocationContext context = new InvocationContext(this.securityContext, this.arguments);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -82,6 +82,12 @@ class ApiVersionTests {
|
||||
assertThat(version).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromHttpHeadersWhenAcceptsEverythingReturnsLatest() {
|
||||
ApiVersion version = ApiVersion.fromHttpHeaders(acceptHeader("*/*"));
|
||||
assertThat(version).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
private Map<String, List<String>> acceptHeader(String... types) {
|
||||
List<String> value = Arrays.asList(types);
|
||||
return value.isEmpty() ? Collections.emptyMap() : Collections.singletonMap("Accept", value);
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.endpoint.http;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ProducibleOperationArgumentResolver}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class ProducibleOperationArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
void whenAcceptHeaderIsEmptyThenHighestOrdinalIsReturned() {
|
||||
assertThat(resolve(acceptHeader())).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenEverythingIsAcceptableThenHighestOrdinalIsReturned() {
|
||||
assertThat(resolve(acceptHeader("*/*"))).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenNothingIsAcceptableThenNullIsReturned() {
|
||||
assertThat(resolve(acceptHeader("image/png"))).isEqualTo(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSingleValueIsAcceptableThenMatchingEnumValueIsReturned() {
|
||||
assertThat(new ProducibleOperationArgumentResolver(acceptHeader(ActuatorMediaType.V2_JSON))
|
||||
.resolve(ApiVersion.class)).isEqualTo(ApiVersion.V2);
|
||||
assertThat(new ProducibleOperationArgumentResolver(acceptHeader(ActuatorMediaType.V3_JSON))
|
||||
.resolve(ApiVersion.class)).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultipleValuesAreAcceptableThenHighestOrdinalIsReturned() {
|
||||
assertThat(resolve(acceptHeader(ActuatorMediaType.V2_JSON, ActuatorMediaType.V3_JSON)))
|
||||
.isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultipleValuesAreAcceptableAsSingleHeaderThenHighestOrdinalIsReturned() {
|
||||
assertThat(resolve(acceptHeader(ActuatorMediaType.V2_JSON + "," + ActuatorMediaType.V3_JSON)))
|
||||
.isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
private Map<String, List<String>> acceptHeader(String... types) {
|
||||
List<String> value = Arrays.asList(types);
|
||||
return value.isEmpty() ? Collections.emptyMap() : Collections.singletonMap("Accept", value);
|
||||
}
|
||||
|
||||
private ApiVersion resolve(Map<String, List<String>> httpHeaders) {
|
||||
return new ProducibleOperationArgumentResolver(httpHeaders).resolve(ApiVersion.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.invoker.cache;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -28,6 +29,7 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.InvocationContext;
|
||||
import org.springframework.boot.actuate.endpoint.OperationArgumentResolver;
|
||||
import org.springframework.boot.actuate.endpoint.SecurityContext;
|
||||
import org.springframework.boot.actuate.endpoint.http.ApiVersion;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.MissingParametersException;
|
||||
@@ -200,10 +202,10 @@ class CachingOperationInvokerTests {
|
||||
OperationInvoker target = mock(OperationInvoker.class);
|
||||
Object expectedV2 = new Object();
|
||||
Object expectedV3 = new Object();
|
||||
InvocationContext contextV2 = new InvocationContext(ApiVersion.V2, mock(SecurityContext.class),
|
||||
Collections.emptyMap());
|
||||
InvocationContext contextV3 = new InvocationContext(ApiVersion.V3, mock(SecurityContext.class),
|
||||
Collections.emptyMap());
|
||||
InvocationContext contextV2 = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(),
|
||||
Arrays.asList(new ApiVersionArgumentResolver(ApiVersion.V2)));
|
||||
InvocationContext contextV3 = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(),
|
||||
Arrays.asList(new ApiVersionArgumentResolver(ApiVersion.V3)));
|
||||
given(target.invoke(contextV2)).willReturn(expectedV2);
|
||||
given(target.invoke(contextV3)).willReturn(expectedV3);
|
||||
CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
|
||||
@@ -240,4 +242,25 @@ class CachingOperationInvokerTests {
|
||||
|
||||
}
|
||||
|
||||
private static final class ApiVersionArgumentResolver implements OperationArgumentResolver {
|
||||
|
||||
private final ApiVersion apiVersion;
|
||||
|
||||
private ApiVersionArgumentResolver(ApiVersion apiVersion) {
|
||||
this.apiVersion = apiVersion;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T resolve(Class<T> type) {
|
||||
return (T) this.apiVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canResolve(Class<?> type) {
|
||||
return ApiVersion.class.equals(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user