Search in sources :

Example 66 with Swagger

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

the class RestSwaggerEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    final CamelContext camelContext = getCamelContext();
    final Swagger swagger = loadSpecificationFrom(camelContext, specificationUri);
    final Map<String, Path> paths = swagger.getPaths();
    for (final Entry<String, Path> pathEntry : paths.entrySet()) {
        final Path path = pathEntry.getValue();
        final Optional<Entry<HttpMethod, Operation>> maybeOperationEntry = path.getOperationMap().entrySet().stream().filter(operationEntry -> operationId.equals(operationEntry.getValue().getOperationId())).findAny();
        if (maybeOperationEntry.isPresent()) {
            final Entry<HttpMethod, Operation> operationEntry = maybeOperationEntry.get();
            final String uriTemplate = pathEntry.getKey();
            final HttpMethod httpMethod = operationEntry.getKey();
            final String method = httpMethod.name();
            final Operation operation = operationEntry.getValue();
            return createProducerFor(swagger, operation, method, uriTemplate);
        }
    }
    final String supportedOperations = paths.values().stream().flatMap(p -> p.getOperations().stream()).map(Operation::getOperationId).collect(Collectors.joining(", "));
    throw new IllegalArgumentException("The specified operation with ID: `" + operationId + "` cannot be found in the Swagger specification loaded from `" + specificationUri + "`. Operations defined in the specification are: " + supportedOperations);
}
Also used : CamelContext(org.apache.camel.CamelContext) UriPath(org.apache.camel.spi.UriPath) Path(io.swagger.models.Path) Scheme(io.swagger.models.Scheme) SwaggerParser(io.swagger.parser.SwaggerParser) Metadata(org.apache.camel.spi.Metadata) UriPath(org.apache.camel.spi.UriPath) URISyntaxException(java.net.URISyntaxException) Swagger(io.swagger.models.Swagger) Json(io.swagger.util.Json) StringHelper.before(org.apache.camel.util.StringHelper.before) HashMap(java.util.HashMap) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) ObjectHelper.notNull(org.apache.camel.util.ObjectHelper.notNull) StringHelper.notEmpty(org.apache.camel.util.StringHelper.notEmpty) UriEndpoint(org.apache.camel.spi.UriEndpoint) Processor(org.apache.camel.Processor) UriParam(org.apache.camel.spi.UriParam) ObjectHelper.isNotEmpty(org.apache.camel.util.ObjectHelper.isNotEmpty) Path(io.swagger.models.Path) RestSwaggerHelper.isHostParam(org.apache.camel.component.rest.swagger.RestSwaggerHelper.isHostParam) Map(java.util.Map) ResourceHelper(org.apache.camel.util.ResourceHelper) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Operation(io.swagger.models.Operation) RestSwaggerHelper.isMediaRange(org.apache.camel.component.rest.swagger.RestSwaggerHelper.isMediaRange) ExchangePattern(org.apache.camel.ExchangePattern) CamelContext(org.apache.camel.CamelContext) StringHelper.after(org.apache.camel.util.StringHelper.after) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) Optional.ofNullable(java.util.Optional.ofNullable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Producer(org.apache.camel.Producer) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) HttpMethod(io.swagger.models.HttpMethod) InputStream(java.io.InputStream) RestConfiguration(org.apache.camel.spi.RestConfiguration) Operation(io.swagger.models.Operation) Entry(java.util.Map.Entry) Swagger(io.swagger.models.Swagger) HttpMethod(io.swagger.models.HttpMethod)

Example 67 with Swagger

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

the class RestSwaggerEndpoint method loadSpecificationFrom.

/**
     * Loads the Swagger definition model from the given path. Tries to resolve
     * the resource using Camel's resource loading support, if it fails uses
     * Swagger's resource loading support instead.
     *
     * @param uri URI of the specification
     * @param camelContext context to use
     * @return the specification
     * @throws IOException
     */
static Swagger loadSpecificationFrom(final CamelContext camelContext, final URI uri) throws IOException {
    final ObjectMapper mapper = Json.mapper();
    final SwaggerParser swaggerParser = new SwaggerParser();
    final String uriAsString = uri.toString();
    try (InputStream stream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uriAsString)) {
        final JsonNode node = mapper.readTree(stream);
        return swaggerParser.read(node);
    } catch (final IOException e) {
        // try Swaggers loader
        final Swagger swagger = swaggerParser.read(uriAsString);
        if (swagger != null) {
            return swagger;
        }
        throw new IllegalArgumentException("The given Swagger specification could not be loaded from `" + uri + "`. Tried loading using Camel's resource resolution and using Swagger's own resource resolution." + " Swagger tends to swallow exceptions while parsing, try specifying Java system property `debugParser`" + " (e.g. `-DdebugParser=true`), the exception that occured when loading using Camel's resource" + " loader follows", e);
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) InputStream(java.io.InputStream) Swagger(io.swagger.models.Swagger) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 68 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 69 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 70 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)

Aggregations

Swagger (io.swagger.models.Swagger)184 Test (org.testng.annotations.Test)115 Operation (io.swagger.models.Operation)45 Parameter (io.swagger.models.parameters.Parameter)33 QueryParameter (io.swagger.models.parameters.QueryParameter)33 BodyParameter (io.swagger.models.parameters.BodyParameter)29 Path (io.swagger.models.Path)26 PathParameter (io.swagger.models.parameters.PathParameter)25 Reader (io.swagger.jaxrs.Reader)23 SpecFilter (io.swagger.core.filter.SpecFilter)21 Test (org.junit.Test)19 SwaggerParser (io.swagger.parser.SwaggerParser)16 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)16 Response (io.swagger.models.Response)14 SerializableParameter (io.swagger.models.parameters.SerializableParameter)14 HashMap (java.util.HashMap)14 Info (io.swagger.models.Info)12 Map (java.util.Map)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Model (io.swagger.models.Model)10