|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * Copyright 2019-2020 the original author or authors. |
| 6 | + * * * * |
| 7 | + * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * * * * you may not use this file except in compliance with the License. |
| 9 | + * * * * You may obtain a copy of the License at |
| 10 | + * * * * |
| 11 | + * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * * * * |
| 13 | + * * * * Unless required by applicable law or agreed to in writing, software |
| 14 | + * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * * * * See the License for the specific language governing permissions and |
| 17 | + * * * * limitations under the License. |
| 18 | + * * * |
| 19 | + * * |
| 20 | + * |
| 21 | + * |
| 22 | + */ |
| 23 | +package test.org.springdoc.api.app81; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.RepeatedTest; |
| 26 | +import org.springdoc.webflux.api.OpenApiResource; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 29 | +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; |
| 30 | +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; |
| 31 | +import org.springframework.context.annotation.ComponentScan; |
| 32 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 33 | +import org.springframework.test.context.ActiveProfiles; |
| 34 | +import org.springframework.test.context.TestPropertySource; |
| 35 | +import org.springframework.web.method.HandlerMethod; |
| 36 | +import org.springframework.web.reactive.result.method.RequestMappingInfo; |
| 37 | +import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping; |
| 38 | + |
| 39 | +import java.net.URI; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.Comparator; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.concurrent.ThreadLocalRandom; |
| 45 | + |
| 46 | +import static org.mockito.Mockito.mock; |
| 47 | +import static org.mockito.Mockito.when; |
| 48 | +import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; |
| 49 | +import static org.springdoc.core.Constants.SPRINGDOC_CACHE_DISABLED; |
| 50 | +import static test.org.springdoc.api.AbstractSpringDocTest.getContent; |
| 51 | + |
| 52 | + |
| 53 | +/** |
| 54 | + * Tests deterministic creation of operationIds |
| 55 | + */ |
| 56 | +@AutoConfigureWebTestClient(timeout = "3600000") |
| 57 | +@WebFluxTest(properties = SPRINGDOC_CACHE_DISABLED + "=true") |
| 58 | +@ActiveProfiles("test") |
| 59 | +public class SpringDocApp81Test { |
| 60 | + |
| 61 | + @SpringBootApplication |
| 62 | + @ComponentScan(basePackages = {"org.springdoc", "test.org.springdoc.api.app81"}) |
| 63 | + static class SpringDocTestApp { |
| 64 | + } |
| 65 | + |
| 66 | + @Autowired |
| 67 | + OpenApiResource resource; |
| 68 | + |
| 69 | + @Autowired |
| 70 | + RequestMappingInfoHandlerMapping mappingInfoHandlerMapping; |
| 71 | + |
| 72 | + @RepeatedTest(10) |
| 73 | + public void shouldGenerateOperationIdsDeterministically() throws Exception { |
| 74 | + shuffleSpringHandlerMethods(); |
| 75 | + |
| 76 | + ServerHttpRequest request = mock(ServerHttpRequest.class); |
| 77 | + when(request.getURI()).thenReturn(URI.create("http://localhost")); |
| 78 | + |
| 79 | + String expected = getContent("results/app81.json"); |
| 80 | + String openApi = resource.openapiJson(request, "").block(); |
| 81 | + assertEquals(expected, openApi, true); |
| 82 | + } |
| 83 | + |
| 84 | + private void shuffleSpringHandlerMethods() { |
| 85 | + Map<RequestMappingInfo, HandlerMethod> handlerMethods = mappingInfoHandlerMapping.getHandlerMethods(); |
| 86 | + List<Map.Entry<RequestMappingInfo, HandlerMethod>> collect = new ArrayList<>(handlerMethods.entrySet()); |
| 87 | + collect.sort(Comparator.comparing(a -> ThreadLocalRandom.current().nextBoolean() ? -1 : 1)); |
| 88 | + |
| 89 | + collect.forEach(e -> mappingInfoHandlerMapping.unregisterMapping(e.getKey())); |
| 90 | + collect.forEach(e -> mappingInfoHandlerMapping.registerMapping(e.getKey(), e.getValue().getBean(), e.getValue().getMethod())); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments