Search in sources :

Example 6 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerEndpointTest method shouldHonourHostPrecedence.

@Test
public void shouldHonourHostPrecedence() {
    final RestConfiguration globalRestConfiguration = new RestConfiguration();
    final RestConfiguration componentRestConfiguration = new RestConfiguration();
    final RestConfiguration specificRestConfiguration = new RestConfiguration();
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getRestConfiguration()).thenReturn(globalRestConfiguration);
    when(camelContext.getRestConfiguration("rest-swagger", false)).thenReturn(componentRestConfiguration);
    when(camelContext.getRestConfiguration("petstore", false)).thenReturn(specificRestConfiguration);
    final RestSwaggerComponent component = new RestSwaggerComponent();
    component.setCamelContext(camelContext);
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("petstore:http://specification-uri#getPetById", "http://specification-uri#getPetById", component);
    final Swagger swagger = new Swagger();
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://specification-uri");
    globalRestConfiguration.setHost("global-rest");
    globalRestConfiguration.setScheme("http");
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://global-rest");
    globalRestConfiguration.setHost("component-rest");
    globalRestConfiguration.setScheme("http");
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://component-rest");
    specificRestConfiguration.setHost("specific-rest");
    specificRestConfiguration.setScheme("http");
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://specific-rest");
    swagger.host("specification").scheme(Scheme.HTTP);
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://specification");
    component.setHost("http://component");
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://component");
    endpoint.setHost("http://endpoint");
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://endpoint");
}
Also used : CamelContext(org.apache.camel.CamelContext) Swagger(io.swagger.models.Swagger) RestConfiguration(org.apache.camel.spi.RestConfiguration) Test(org.junit.Test)

Example 7 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerEndpointTest method shouldDetermineHostFromSpecification.

@Test
public void shouldDetermineHostFromSpecification() {
    final RestSwaggerComponent component = new RestSwaggerComponent();
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:http://some-uri#getPetById", "http://some-uri#getPetById", component);
    final Swagger swagger = new Swagger();
    swagger.host("petstore.swagger.io");
    assertThat(endpoint.determineHost(swagger)).isEqualTo("http://petstore.swagger.io");
    swagger.schemes(Arrays.asList(Scheme.HTTPS));
    assertThat(endpoint.determineHost(swagger)).isEqualTo("https://petstore.swagger.io");
}
Also used : Swagger(io.swagger.models.Swagger) Test(org.junit.Test)

Example 8 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerEndpointTest method shouldDetermineEndpointParameters.

@Test
public void shouldDetermineEndpointParameters() {
    final CamelContext camelContext = mock(CamelContext.class);
    final RestSwaggerComponent component = new RestSwaggerComponent();
    component.setCamelContext(camelContext);
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("uri", "remaining", component);
    endpoint.setHost("http://petstore.swagger.io");
    final Swagger swagger = new Swagger();
    final Operation operation = new Operation();
    assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"));
    component.setComponentName("xyz");
    assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"));
    swagger.consumes("application/json").produces("application/xml");
    assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"), entry("consumes", "application/xml"), entry("produces", "application/json"));
    component.setProduces("application/json");
    component.setConsumes("application/atom+xml");
    assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"), entry("consumes", "application/atom+xml"), entry("produces", "application/json"));
    endpoint.setProduces("application/atom+xml");
    endpoint.setConsumes("application/json");
    assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "xyz"), entry("consumes", "application/json"), entry("produces", "application/atom+xml"));
    endpoint.setComponentName("zyx");
    assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(entry("host", "http://petstore.swagger.io"), entry("componentName", "zyx"), entry("consumes", "application/json"), entry("produces", "application/atom+xml"));
}
Also used : CamelContext(org.apache.camel.CamelContext) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Test(org.junit.Test)

Example 9 with Swagger

use of io.swagger.models.Swagger in project camel by apache.

the class RestSwaggerEndpointTest method shouldDetermineBasePath.

@Test
public void shouldDetermineBasePath() {
    final RestConfiguration restConfiguration = new RestConfiguration();
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getRestConfiguration("rest-swagger", true)).thenReturn(restConfiguration);
    final Swagger swagger = new Swagger();
    final RestSwaggerComponent component = new RestSwaggerComponent();
    component.setCamelContext(camelContext);
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:getPetById", "getPetById", component);
    assertThat(endpoint.determineBasePath(swagger)).as("When no base path is specified on component, endpoint or rest configuration it should default to `/`").isEqualTo("/");
    restConfiguration.setContextPath("/rest");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified in REST configuration and not specified in component the base path should be from the REST configuration").isEqualTo("/rest");
    swagger.basePath("/specification");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified in the specification it should take precedence the one specified in the REST configuration").isEqualTo("/specification");
    component.setBasePath("/component");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified on the component it should take precedence over Swagger specification and REST configuration").isEqualTo("/component");
    endpoint.setBasePath("/endpoint");
    assertThat(endpoint.determineBasePath(swagger)).as("When base path is specified on the endpoint it should take precedence over any other").isEqualTo("/endpoint");
}
Also used : CamelContext(org.apache.camel.CamelContext) Swagger(io.swagger.models.Swagger) RestConfiguration(org.apache.camel.spi.RestConfiguration) Test(org.junit.Test)

Example 10 with Swagger

use of io.swagger.models.Swagger in project swagger-core by swagger-api.

the class ApiDeclarationServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final Swagger swagger = (Swagger) getServletContext().getAttribute("swagger");
    if (swagger == null) {
        response.setStatus(404);
        return;
    }
    final String pathInfo = request.getPathInfo();
    if ("/swagger.json".equals(pathInfo)) {
        response.getWriter().println(Json.mapper().writeValueAsString(swagger));
    } else if ("/swagger.yaml".equals(pathInfo)) {
        response.getWriter().println(Yaml.mapper().writeValueAsString(swagger));
    } else {
        response.setStatus(404);
    }
}
Also used : Swagger(io.swagger.models.Swagger)

Aggregations

Swagger (io.swagger.models.Swagger)164 Test (org.testng.annotations.Test)114 Operation (io.swagger.models.Operation)39 Parameter (io.swagger.models.parameters.Parameter)30 QueryParameter (io.swagger.models.parameters.QueryParameter)30 BodyParameter (io.swagger.models.parameters.BodyParameter)26 Reader (io.swagger.jaxrs.Reader)23 PathParameter (io.swagger.models.parameters.PathParameter)22 SpecFilter (io.swagger.core.filter.SpecFilter)21 Test (org.junit.Test)19 Path (io.swagger.models.Path)18 Response (io.swagger.models.Response)14 SerializableParameter (io.swagger.models.parameters.SerializableParameter)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Model (io.swagger.models.Model)10 NicknamedOperation (io.swagger.resources.NicknamedOperation)10 HeaderParameter (io.swagger.models.parameters.HeaderParameter)9 BeanConfig (io.swagger.jaxrs.config.BeanConfig)8 Map (java.util.Map)8 Info (io.swagger.models.Info)7