|
| 1 | +package io.github.bsayli.customerservice.common.openapi.autoreg; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import io.github.bsayli.customerservice.common.openapi.OpenApiSchemas; |
| 7 | +import io.github.bsayli.customerservice.common.openapi.introspector.ResponseTypeIntrospector; |
| 8 | +import io.swagger.v3.oas.models.Components; |
| 9 | +import io.swagger.v3.oas.models.OpenAPI; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Tag; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.springdoc.core.customizers.OpenApiCustomizer; |
| 18 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 19 | +import org.springframework.web.method.HandlerMethod; |
| 20 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 21 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 22 | + |
| 23 | +@Tag("unit") |
| 24 | +@DisplayName("Unit Test: AutoWrapperSchemaCustomizer") |
| 25 | +class AutoWrapperSchemaCustomizerTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + @DisplayName("Registers composed wrapper schemas for all discovered data refs") |
| 29 | + void registersSchemas_forDiscoveredRefs() throws Exception { |
| 30 | + var beanFactory = mock(ListableBeanFactory.class); |
| 31 | + var handlerMapping = mock(RequestMappingHandlerMapping.class); |
| 32 | + var introspector = mock(ResponseTypeIntrospector.class); |
| 33 | + |
| 34 | + var controller = new SampleController(); |
| 35 | + Method foo = SampleController.class.getMethod("foo"); |
| 36 | + Method bar = SampleController.class.getMethod("bar"); |
| 37 | + |
| 38 | + var handlerMap = new LinkedHashMap<RequestMappingInfo, HandlerMethod>(); |
| 39 | + handlerMap.put(mock(RequestMappingInfo.class), new HandlerMethod(controller, foo)); |
| 40 | + handlerMap.put(mock(RequestMappingInfo.class), new HandlerMethod(controller, bar)); |
| 41 | + |
| 42 | + when(handlerMapping.getHandlerMethods()).thenReturn(handlerMap); |
| 43 | + when(beanFactory.getBeansOfType(RequestMappingHandlerMapping.class)) |
| 44 | + .thenReturn(Map.of("rmh", handlerMapping)); |
| 45 | + |
| 46 | + when(introspector.extractDataRefName(any(Method.class))) |
| 47 | + .then( |
| 48 | + inv -> { |
| 49 | + Method m = inv.getArgument(0); |
| 50 | + return switch (m.getName()) { |
| 51 | + case "foo" -> Optional.of("FooRef"); |
| 52 | + case "bar" -> Optional.of("BarRef"); |
| 53 | + default -> Optional.empty(); |
| 54 | + }; |
| 55 | + }); |
| 56 | + |
| 57 | + var customizerCfg = new AutoWrapperSchemaCustomizer(beanFactory, introspector); |
| 58 | + OpenApiCustomizer customizer = customizerCfg.autoResponseWrappers(); |
| 59 | + |
| 60 | + var openAPI = new OpenAPI().components(new Components()); |
| 61 | + |
| 62 | + customizer.customise(openAPI); |
| 63 | + |
| 64 | + var schemas = openAPI.getComponents().getSchemas(); |
| 65 | + assertNotNull(schemas, "schemas should be initialized"); |
| 66 | + assertTrue( |
| 67 | + schemas.containsKey(OpenApiSchemas.SCHEMA_SERVICE_RESPONSE + "FooRef"), |
| 68 | + "Should contain composed schema for FooRef"); |
| 69 | + assertTrue( |
| 70 | + schemas.containsKey(OpenApiSchemas.SCHEMA_SERVICE_RESPONSE + "BarRef"), |
| 71 | + "Should contain composed schema for BarRef"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("Does nothing when no data refs are discovered") |
| 76 | + void noRefs_noSchemasAdded() { |
| 77 | + var beanFactory = mock(ListableBeanFactory.class); |
| 78 | + var introspector = mock(ResponseTypeIntrospector.class); |
| 79 | + |
| 80 | + when(beanFactory.getBeansOfType(RequestMappingHandlerMapping.class)).thenReturn(Map.of()); |
| 81 | + |
| 82 | + var customizerCfg = new AutoWrapperSchemaCustomizer(beanFactory, introspector); |
| 83 | + OpenApiCustomizer customizer = customizerCfg.autoResponseWrappers(); |
| 84 | + |
| 85 | + var openAPI = new OpenAPI().components(new Components()); |
| 86 | + |
| 87 | + customizer.customise(openAPI); |
| 88 | + |
| 89 | + assertTrue( |
| 90 | + openAPI.getComponents().getSchemas() == null |
| 91 | + || openAPI.getComponents().getSchemas().isEmpty(), |
| 92 | + "No schemas should be added when no refs exist"); |
| 93 | + } |
| 94 | + |
| 95 | + static class SampleController { |
| 96 | + public String foo() { |
| 97 | + return "ok"; |
| 98 | + } |
| 99 | + |
| 100 | + public Integer bar() { |
| 101 | + return 42; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments