|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * Copyright 2019-2024 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.app238; |
| 26 | + |
| 27 | +import java.util.Locale; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.springdoc.core.customizers.OpenApiLocaleCustomizer; |
| 31 | +import org.springdoc.core.utils.Constants; |
| 32 | +import test.org.springdoc.api.v30.AbstractSpringDocV30Test; |
| 33 | + |
| 34 | +import org.springframework.beans.factory.annotation.Autowired; |
| 35 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.context.support.ResourceBundleMessageSource; |
| 38 | +import org.springframework.http.HttpHeaders; |
| 39 | +import org.springframework.test.context.TestPropertySource; |
| 40 | +import org.springframework.test.web.servlet.MvcResult; |
| 41 | + |
| 42 | +import static org.hamcrest.Matchers.is; |
| 43 | +import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; |
| 44 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 45 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 46 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 47 | + |
| 48 | +@TestPropertySource(properties = "springdoc.allowed-locales=en-US,fr-CA") |
| 49 | +public class SpringDocApp238Test extends AbstractSpringDocV30Test { |
| 50 | + |
| 51 | + @Test |
| 52 | + @Override |
| 53 | + public void testApp() throws Exception { |
| 54 | + testApp(Locale.US); |
| 55 | + testApp(Locale.CANADA_FRENCH); |
| 56 | + // resolves to en-US as Chinese locale is not allowed in properties |
| 57 | + testApp(Locale.SIMPLIFIED_CHINESE); |
| 58 | + } |
| 59 | + |
| 60 | + private void testApp(Locale locale) throws Exception { |
| 61 | + className = getClass().getSimpleName(); |
| 62 | + String testNumber = className.replaceAll("[^0-9]", ""); |
| 63 | + MvcResult mockMvcResult = |
| 64 | + mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL).locale(locale).header(HttpHeaders.ACCEPT_LANGUAGE, locale.toLanguageTag())).andExpect(status().isOk()) |
| 65 | + .andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn(); |
| 66 | + String result = mockMvcResult.getResponse().getContentAsString(); |
| 67 | + String expected = getContent("results/3.0.1/app" + testNumber + "-" + locale.toLanguageTag() + ".json"); |
| 68 | + assertEquals(expected, result, true); |
| 69 | + } |
| 70 | + |
| 71 | + @SpringBootApplication |
| 72 | + static class SpringDocTestApp { |
| 73 | + |
| 74 | + @Autowired |
| 75 | + ResourceBundleMessageSource resourceBundleMessageSource; |
| 76 | + |
| 77 | + @Bean |
| 78 | + public OpenApiLocaleCustomizer openApiLocaleCustomizer() { |
| 79 | + return (openAPI, locale) |
| 80 | + -> openAPI.getInfo().title(resourceBundleMessageSource.getMessage("test", null, locale)); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments