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");
}
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");
}
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"));
}
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");
}
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);
}
}
Aggregations