Search in sources :

Example 1 with OpenAPIService

use of org.springdoc.core.OpenAPIService in project springdoc-openapi by springdoc.

the class AbstractOpenApiResourceTest method preLoadingModeShouldNotOverwriteServers.

@Test
void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
    when(openAPIService.updateServers(any())).thenCallRealMethod();
    when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod();
    doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true);
    doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
    doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any(), any());
    String customUrl = "https://custom.com";
    String generatedUrl = "https://generated.com";
    OpenApiCustomiser openApiCustomiser = openApi -> openApi.setServers(singletonList(new Server().url(customUrl)));
    SpringDocConfigProperties properties = new SpringDocConfigProperties();
    properties.setPreLoadingEnabled(true);
    resource = new EmptyPathsOpenApiResource(GROUP_NAME, openAPIBuilderObjectFactory, requestBuilder, responseBuilder, operationParser, Optional.empty(), Optional.of(singletonList(openApiCustomiser)), Optional.empty(), properties, springDocProviders);
    // wait for executor to be done
    Thread.sleep(1_000);
    // emulate generating base url
    openAPIService.setServerBaseUrl(generatedUrl);
    openAPIService.updateServers(openAPI);
    Locale locale = Locale.US;
    OpenAPI after = resource.getOpenApi(locale);
    assertThat(after.getServers().get(0).getUrl(), is(customUrl));
}
Also used : OperationCustomizer(org.springdoc.core.customizers.OperationCustomizer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) GenericResponseService(org.springdoc.core.GenericResponseService) Mock(org.mockito.Mock) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) Operation(io.swagger.v3.oas.models.Operation) GET(org.springframework.web.bind.annotation.RequestMethod.GET) ParameterIn(io.swagger.v3.oas.annotations.enums.ParameterIn) Collections.singletonList(java.util.Collections.singletonList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) OpenApiMethodFilter(org.springdoc.core.filters.OpenApiMethodFilter) Locale(java.util.Locale) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) OperationService(org.springdoc.core.OperationService) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) OpenAPIService(org.springdoc.core.OpenAPIService) AbstractRequestService(org.springdoc.core.AbstractRequestService) SpringDocProviders(org.springdoc.core.SpringDocProviders) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) RouterOperation(org.springdoc.core.fn.RouterOperation) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) PathItem(io.swagger.v3.oas.models.PathItem) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Paths(io.swagger.v3.oas.models.Paths) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.jupiter.api.Test) ObjectFactory(org.springframework.beans.factory.ObjectFactory) List(java.util.List) Server(io.swagger.v3.oas.models.servers.Server) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Locale(java.util.Locale) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) Server(io.swagger.v3.oas.models.servers.Server) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.junit.jupiter.api.Test)

Aggregations

ParameterIn (io.swagger.v3.oas.annotations.enums.ParameterIn)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Operation (io.swagger.v3.oas.models.Operation)1 PathItem (io.swagger.v3.oas.models.PathItem)1 Paths (io.swagger.v3.oas.models.Paths)1 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)1 StringSchema (io.swagger.v3.oas.models.media.StringSchema)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 Server (io.swagger.v3.oas.models.servers.Server)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.nullValue (org.hamcrest.Matchers.nullValue)1