forked from spring-projects/spring-boot
/
AbstractEndpointDocumentationTests.java
129 lines (114 loc) · 5.68 KB
/
AbstractEndpointDocumentationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2012-2020 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.autoconfigure.endpoint.web.documentation;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.restdocs.operation.preprocess.ContentModifyingOperationPreprocessor;
import org.springframework.restdocs.operation.preprocess.OperationPreprocessor;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.util.StringUtils;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
/**
* Abstract base class for tests that generate endpoint documentation using Spring REST
* Docs.
*
* @author Andy Wilkinson
*/
@TestPropertySource(properties = { "spring.jackson.serialization.indent_output=true",
"management.endpoints.web.exposure.include=*", "spring.jackson.default-property-inclusion=non_null" })
public abstract class AbstractEndpointDocumentationTests {
protected String describeEnumValues(Class<? extends Enum<?>> enumType) {
return StringUtils.collectionToDelimitedString(Stream.of(enumType.getEnumConstants())
.map((constant) -> "`" + constant.name() + "`").collect(Collectors.toList()), ", ");
}
protected OperationPreprocessor limit(String... keys) {
return limit((candidate) -> true, keys);
}
@SuppressWarnings("unchecked")
protected <T> OperationPreprocessor limit(Predicate<T> filter, String... keys) {
return new ContentModifyingOperationPreprocessor((content, mediaType) -> {
ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
try {
Map<String, Object> payload = objectMapper.readValue(content, Map.class);
Object target = payload;
Map<Object, Object> parent = null;
for (String key : keys) {
if (!(target instanceof Map)) {
throw new IllegalStateException();
}
parent = (Map<Object, Object>) target;
target = parent.get(key);
}
if (target instanceof Map) {
parent.put(keys[keys.length - 1], select((Map<String, Object>) target, filter));
}
else {
parent.put(keys[keys.length - 1], select((List<Object>) target, filter));
}
return objectMapper.writeValueAsBytes(payload);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
});
}
protected FieldDescriptor parentIdField() {
return fieldWithPath("contexts.*.parentId").description("Id of the parent application context, if any.")
.optional().type(JsonFieldType.STRING);
}
@SuppressWarnings("unchecked")
private <T> Map<String, Object> select(Map<String, Object> candidates, Predicate<T> filter) {
Map<String, Object> selected = new HashMap<>();
candidates.entrySet().stream().filter((candidate) -> filter.test((T) candidate)).limit(3)
.forEach((entry) -> selected.put(entry.getKey(), entry.getValue()));
return selected;
}
@SuppressWarnings("unchecked")
private <T> List<Object> select(List<Object> candidates, Predicate<T> filter) {
return candidates.stream().filter((candidate) -> filter.test((T) candidate)).limit(3)
.collect(Collectors.toList());
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration({ JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, EndpointAutoConfiguration.class,
WebEndpointAutoConfiguration.class, WebMvcEndpointManagementContextConfiguration.class,
WebFluxEndpointManagementContextConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
WebFluxAutoConfiguration.class, HttpHandlerAutoConfiguration.class })
static class BaseDocumentationConfiguration {
}
}