Skip to content

Commit 83e2321

Browse files
committed
Add a test-case for builder customizer
1 parent e7f6bfe commit 83e2321

File tree

3 files changed

+223
-24
lines changed

3 files changed

+223
-24
lines changed

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java

+23-24
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,31 @@
4747
@AutoConfigureMockMvc
4848
public abstract class AbstractSpringDocTest {
4949

50-
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpringDocTest.class);
50+
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpringDocTest.class);
5151

52-
public static String className;
52+
public static String className;
5353

54-
@Autowired
55-
protected MockMvc mockMvc;
54+
@Autowired
55+
protected MockMvc mockMvc;
5656

57-
public static String getContent(String fileName) throws Exception {
58-
try {
59-
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
60-
byte[] fileBytes = Files.readAllBytes(path);
61-
return new String(fileBytes, StandardCharsets.UTF_8);
62-
}
63-
catch (Exception e) {
64-
throw new RuntimeException("Failed to read file: " + fileName, e);
65-
}
66-
}
57+
public static String getContent(String fileName) throws Exception {
58+
try {
59+
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
60+
byte[] fileBytes = Files.readAllBytes(path);
61+
return new String(fileBytes, StandardCharsets.UTF_8);
62+
} catch (Exception e) {
63+
throw new RuntimeException("Failed to read file: " + fileName, e);
64+
}
65+
}
6766

68-
@Test
69-
public void testApp() throws Exception {
70-
className = getClass().getSimpleName();
71-
String testNumber = className.replaceAll("[^0-9]", "");
72-
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
73-
.andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn();
74-
String result = mockMvcResult.getResponse().getContentAsString();
75-
String expected = getContent("results/app" + testNumber + ".json");
76-
assertEquals(expected, result, true);
77-
}
67+
@Test
68+
public void testApp() throws Exception {
69+
className = getClass().getSimpleName();
70+
String testNumber = className.replaceAll("[^0-9]", "");
71+
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk()).andExpect(jsonPath("$.openapi", is("3.0.1")))
72+
.andReturn();
73+
String result = mockMvcResult.getResponse().getContentAsString();
74+
String expected = getContent("results/app" + testNumber + ".json");
75+
assertEquals(expected, result, true);
76+
}
7877
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
package test.org.springdoc.api.app92;
19+
20+
import io.swagger.v3.oas.annotations.Operation;
21+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
22+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
23+
import io.swagger.v3.oas.annotations.tags.Tag;
24+
import org.apache.commons.lang3.RandomStringUtils;
25+
import org.springdoc.api.ActuatorProvider;
26+
import org.springdoc.api.OpenApiResource;
27+
import org.springdoc.core.AbstractRequestBuilder;
28+
import org.springdoc.core.GenericResponseBuilder;
29+
import org.springdoc.core.OpenAPIBuilder;
30+
import org.springdoc.core.OperationBuilder;
31+
import org.springdoc.core.SpringDocConfigProperties;
32+
import org.springdoc.core.customizers.OpenApiBuilderCustomiser;
33+
import org.springdoc.core.customizers.OpenApiCustomiser;
34+
import org.springframework.beans.BeansException;
35+
import org.springframework.beans.factory.annotation.Qualifier;
36+
import org.springframework.boot.autoconfigure.SpringBootApplication;
37+
import org.springframework.context.ApplicationContext;
38+
import org.springframework.context.ApplicationContextAware;
39+
import org.springframework.context.annotation.Bean;
40+
import org.springframework.http.MediaType;
41+
import org.springframework.http.ResponseEntity;
42+
import org.springframework.test.context.TestPropertySource;
43+
import org.springframework.web.bind.annotation.GetMapping;
44+
import org.springframework.web.bind.annotation.RequestMethod;
45+
import org.springframework.web.bind.annotation.ResponseBody;
46+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
47+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
48+
import test.org.springdoc.api.AbstractSpringDocTest;
49+
import test.org.springdoc.api.app91.Greeting;
50+
51+
import java.util.Collections;
52+
import java.util.List;
53+
import java.util.Optional;
54+
55+
import static org.springdoc.core.Constants.DEFAULT_GROUP_NAME;
56+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
57+
58+
@TestPropertySource(properties = "springdoc.default-produces-media-type=application/json")
59+
public class SpringDocApp92Test extends AbstractSpringDocTest {
60+
61+
@SpringBootApplication
62+
static class SpringDocTestApp implements ApplicationContextAware {
63+
64+
private ApplicationContext applicationContext;
65+
66+
@Bean
67+
public GreetingController greetingController() {
68+
return new GreetingController();
69+
}
70+
71+
@Bean
72+
public OpenApiBuilderCustomiser customOpenAPI() {
73+
return openApiBuilder -> openApiBuilder.addMappings(Collections.singletonMap("greetingController", new GreetingController()));
74+
}
75+
76+
@Bean
77+
public RequestMappingHandlerMapping defaultTestHandlerMapping(GreetingController greetingController) throws NoSuchMethodException {
78+
RequestMappingHandlerMapping result = new RequestMappingHandlerMapping();
79+
RequestMappingInfo requestMappingInfo =
80+
RequestMappingInfo.paths("/test").methods(RequestMethod.GET).produces(MediaType.APPLICATION_JSON_VALUE).build();
81+
82+
result.setApplicationContext(this.applicationContext);
83+
result.registerMapping(requestMappingInfo, "greetingController", GreetingController.class.getDeclaredMethod("sayHello2"));
84+
//result.handlerme
85+
return result;
86+
}
87+
88+
@Bean(name = "mvcOpenApiResource")
89+
public OpenApiResource openApiResource(OpenAPIBuilder openAPIBuilder, AbstractRequestBuilder requestBuilder, GenericResponseBuilder responseBuilder,
90+
OperationBuilder operationParser,
91+
@Qualifier("defaultTestHandlerMapping") RequestMappingHandlerMapping requestMappingHandlerMapping,
92+
Optional<ActuatorProvider> servletContextProvider, SpringDocConfigProperties springDocConfigProperties,
93+
Optional<List<OpenApiCustomiser>> openApiCustomisers) {
94+
return new OpenApiResource(DEFAULT_GROUP_NAME, openAPIBuilder, requestBuilder, responseBuilder, operationParser, requestMappingHandlerMapping,
95+
servletContextProvider, openApiCustomisers, springDocConfigProperties);
96+
}
97+
98+
@Override
99+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
100+
this.applicationContext = applicationContext;
101+
}
102+
}
103+
104+
@ResponseBody
105+
@Tag(name = "Demo", description = "The Demo API")
106+
public static class GreetingController {
107+
108+
@GetMapping(produces = APPLICATION_JSON_VALUE)
109+
@Operation(summary = "This API will return a random greeting.")
110+
public ResponseEntity<Greeting> sayHello() {
111+
return ResponseEntity.ok(new Greeting(RandomStringUtils.randomAlphanumeric(10)));
112+
}
113+
114+
@GetMapping("/test")
115+
@ApiResponses(value = { @ApiResponse(responseCode = "201", description = "item created"),
116+
@ApiResponse(responseCode = "400", description = "invalid input, object invalid"),
117+
@ApiResponse(responseCode = "409", description = "an existing item already exists") })
118+
public ResponseEntity<Greeting> sayHello2() {
119+
return ResponseEntity.ok(new Greeting(RandomStringUtils.randomAlphanumeric(10)));
120+
}
121+
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"tags": [
14+
{
15+
"name": "Demo",
16+
"description": "The Demo API"
17+
}
18+
],
19+
"paths": {
20+
"/test": {
21+
"get": {
22+
"tags": [
23+
"Demo"
24+
],
25+
"operationId": "sayHello2",
26+
"responses": {
27+
"400": {
28+
"description": "invalid input, object invalid",
29+
"content": {
30+
"application/json": {
31+
"schema": {
32+
"$ref": "#/components/schemas/Greeting"
33+
}
34+
}
35+
}
36+
},
37+
"201": {
38+
"description": "item created",
39+
"content": {
40+
"application/json": {
41+
"schema": {
42+
"$ref": "#/components/schemas/Greeting"
43+
}
44+
}
45+
}
46+
},
47+
"409": {
48+
"description": "an existing item already exists",
49+
"content": {
50+
"application/json": {
51+
"schema": {
52+
"$ref": "#/components/schemas/Greeting"
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}
60+
},
61+
"components": {
62+
"schemas": {
63+
"Greeting": {
64+
"title": "Greeting",
65+
"type": "object",
66+
"properties": {
67+
"payload": {
68+
"type": "string",
69+
"description": "The greeting value",
70+
"example": "sdfsdfs"
71+
}
72+
},
73+
"description": "An object containing a greeting message"
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)