Search in sources :

Example 46 with CamelContext

use of org.apache.camel.CamelContext in project camel by apache.

the class RestSwaggerEndpointTest method shouldComplainForUnknownOperations.

@Test(expected = IllegalArgumentException.class)
public void shouldComplainForUnknownOperations() throws Exception {
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
    final RestSwaggerComponent component = new RestSwaggerComponent(camelContext);
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:unknown", "unknown", component);
    endpoint.createProducer();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Test(org.junit.Test)

Example 47 with CamelContext

use of org.apache.camel.CamelContext in project camel by apache.

the class RestSwaggerEndpointTest method shouldCreateProducers.

@Test
public void shouldCreateProducers() throws Exception {
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
    final Endpoint endpointDelegate = mock(Endpoint.class);
    when(camelContext.getEndpoint("rest:GET:/v2:/pet/{petId}")).thenReturn(endpointDelegate);
    final Producer delegateProducer = mock(Producer.class);
    when(endpointDelegate.createProducer()).thenReturn(delegateProducer);
    final RestSwaggerComponent component = new RestSwaggerComponent(camelContext);
    component.setHost("http://petstore.swagger.io");
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:getPetById", "getPetById", component);
    final Producer producer = endpoint.createProducer();
    assertThat(producer).isSameAs(delegateProducer);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) Test(org.junit.Test)

Example 48 with CamelContext

use of org.apache.camel.CamelContext in project camel by apache.

the class RestSwaggerEndpointTest method shouldLoadSwaggerSpecifications.

@Test
public void shouldLoadSwaggerSpecifications() throws IOException {
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
    assertThat(RestSwaggerEndpoint.loadSpecificationFrom(camelContext, RestSwaggerComponent.DEFAULT_SPECIFICATION_URI)).isNotNull();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Test(org.junit.Test)

Example 49 with CamelContext

use of org.apache.camel.CamelContext 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 50 with CamelContext

use of org.apache.camel.CamelContext in project camel by apache.

the class RestSwaggerEndpoint method determineBasePath.

String determineBasePath(final Swagger swagger) {
    if (isNotEmpty(basePath)) {
        return basePath;
    }
    final String componentBasePath = component().getBasePath();
    if (isNotEmpty(componentBasePath)) {
        return componentBasePath;
    }
    final String specificationBasePath = swagger.getBasePath();
    if (isNotEmpty(specificationBasePath)) {
        return specificationBasePath;
    }
    final CamelContext camelContext = getCamelContext();
    final RestConfiguration specificConfiguration = camelContext.getRestConfiguration(assignedComponentName, false);
    if (specificConfiguration != null && isNotEmpty(specificConfiguration.getContextPath())) {
        return specificConfiguration.getContextPath();
    }
    final RestConfiguration restConfiguration = camelContext.getRestConfiguration("rest-swagger", true);
    final String restConfigurationBasePath = restConfiguration.getContextPath();
    if (isNotEmpty(restConfigurationBasePath)) {
        return restConfigurationBasePath;
    }
    return RestSwaggerComponent.DEFAULT_BASE_PATH;
}
Also used : CamelContext(org.apache.camel.CamelContext) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Aggregations

CamelContext (org.apache.camel.CamelContext)1478 Test (org.junit.Test)691 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)684 RouteBuilder (org.apache.camel.builder.RouteBuilder)448 ProducerTemplate (org.apache.camel.ProducerTemplate)434 ConnectionFactory (javax.jms.ConnectionFactory)220 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)210 Exchange (org.apache.camel.Exchange)109 HashMap (java.util.HashMap)93 Endpoint (org.apache.camel.Endpoint)52 DefaultExchange (org.apache.camel.impl.DefaultExchange)50 IOException (java.io.IOException)46 Map (java.util.Map)45 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)44 CountDownLatch (java.util.concurrent.CountDownLatch)42 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)42 ArrayList (java.util.ArrayList)41 Processor (org.apache.camel.Processor)40 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)37 CamelExecutionException (org.apache.camel.CamelExecutionException)33