|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * Copyright 2019-2023 the original author or authors. |
| 7 | + * * * * * |
| 8 | + * * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * * * * * you may not use this file except in compliance with the License. |
| 10 | + * * * * * You may obtain a copy of the License at |
| 11 | + * * * * * |
| 12 | + * * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * * * * * |
| 14 | + * * * * * Unless required by applicable law or agreed to in writing, software |
| 15 | + * * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * * * * * See the License for the specific language governing permissions and |
| 18 | + * * * * * limitations under the License. |
| 19 | + * * * * |
| 20 | + * * * |
| 21 | + * * |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +package test.org.springdoc.api.v30.app209; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +import io.swagger.v3.oas.models.OpenAPI; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 34 | +import org.springdoc.core.customizers.OpenApiBuilderCustomizer; |
| 35 | +import org.springdoc.core.customizers.ServerBaseUrlCustomizer; |
| 36 | +import org.springdoc.core.properties.SpringDocConfigProperties; |
| 37 | +import org.springdoc.core.providers.JavadocProvider; |
| 38 | +import org.springdoc.core.service.OpenAPIService; |
| 39 | +import org.springdoc.core.service.SecurityService; |
| 40 | +import org.springdoc.core.utils.Constants; |
| 41 | +import org.springdoc.core.utils.PropertyResolverUtils; |
| 42 | +import test.org.springdoc.api.AbstractCommonTest; |
| 43 | + |
| 44 | +import org.springframework.beans.factory.annotation.Autowired; |
| 45 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 46 | +import org.springframework.boot.test.context.SpringBootTest; |
| 47 | +import org.springframework.context.annotation.Bean; |
| 48 | +import org.springframework.http.HttpHeaders; |
| 49 | +import org.springframework.test.web.servlet.MvcResult; |
| 50 | + |
| 51 | +import static org.hamcrest.Matchers.is; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 53 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 54 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 55 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 56 | + |
| 57 | +@SpringBootTest(properties = { |
| 58 | + "springdoc.pre-loading-enabled=true", |
| 59 | + "springdoc.pre-loading-locales=ja" |
| 60 | +}) |
| 61 | +public class SpringDocApp209Test extends AbstractCommonTest { |
| 62 | + public static String className; |
| 63 | + |
| 64 | + static { |
| 65 | + System.setProperty("user.country", "JP"); |
| 66 | + System.setProperty("user.language", "ja"); |
| 67 | + } |
| 68 | + |
| 69 | + @Autowired |
| 70 | + private OpenAPIServiceMock openAPIService; |
| 71 | + |
| 72 | + @SpringBootApplication |
| 73 | + static class SpringDocTestApp { |
| 74 | + @Bean("openAPIService") |
| 75 | + public OpenAPIServiceMock openAPIService(Optional<OpenAPI> openAPI, SecurityService securityParser, SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers, Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers, Optional<JavadocProvider> javadocProvider) { |
| 76 | + return new OpenAPIServiceMock(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static class OpenAPIServiceMock extends OpenAPIService { |
| 81 | + private int numberOfTimesCalculatePathWasCalled; |
| 82 | + |
| 83 | + public OpenAPIServiceMock(Optional<OpenAPI> openAPI, SecurityService securityParser, SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers, Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers, Optional<JavadocProvider> javadocProvider) { |
| 84 | + super(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void setCachedOpenAPI(OpenAPI cachedOpenAPI, Locale locale) { |
| 89 | + numberOfTimesCalculatePathWasCalled++; |
| 90 | + super.setCachedOpenAPI(cachedOpenAPI, locale); |
| 91 | + } |
| 92 | + |
| 93 | + public int getNumberOfTimesCalculatePathWasCalled() { |
| 94 | + return numberOfTimesCalculatePathWasCalled; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void shouldOnlyByCalledOnce() throws Exception { |
| 100 | + assertEquals(1, openAPIService.getNumberOfTimesCalculatePathWasCalled()); |
| 101 | + |
| 102 | + className = getClass().getSimpleName(); |
| 103 | + String testNumber = className.replaceAll("[^0-9]", ""); |
| 104 | + MvcResult mockMvcResult = mockMvc |
| 105 | + .perform(get(Constants.DEFAULT_API_DOCS_URL).header(HttpHeaders.ACCEPT_LANGUAGE, "ja")) |
| 106 | + .andExpect(status().isOk()) |
| 107 | + .andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn(); |
| 108 | + String result = mockMvcResult.getResponse().getContentAsString(); |
| 109 | + String expected = getContent("results/3.0.1/app" + testNumber + ".json"); |
| 110 | + JSONAssert.assertEquals(expected, result, true); |
| 111 | + |
| 112 | + assertEquals(1, openAPIService.getNumberOfTimesCalculatePathWasCalled()); |
| 113 | + } |
| 114 | +} |
0 commit comments