Search in sources :

Example 51 with CamelContext

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

the class RestSwaggerEndpoint method determineHost.

String determineHost(final Swagger swagger) {
    if (isNotEmpty(host)) {
        return host;
    }
    final String componentHost = component().getHost();
    if (isNotEmpty(componentHost)) {
        return componentHost;
    }
    final String swaggerScheme = pickBestScheme(specificationUri.getScheme(), swagger.getSchemes());
    final String swaggerHost = swagger.getHost();
    if (isNotEmpty(swaggerScheme) && isNotEmpty(swaggerHost)) {
        return swaggerScheme + "://" + swaggerHost;
    }
    final CamelContext camelContext = getCamelContext();
    final RestConfiguration specificRestConfiguration = camelContext.getRestConfiguration(assignedComponentName, false);
    final String specificConfigurationHost = hostFrom(specificRestConfiguration);
    if (specificConfigurationHost != null) {
        return specificConfigurationHost;
    }
    final RestConfiguration componentRestConfiguration = camelContext.getRestConfiguration("rest-swagger", false);
    final String componentConfigurationHost = hostFrom(componentRestConfiguration);
    if (componentConfigurationHost != null) {
        return componentConfigurationHost;
    }
    final RestConfiguration globalRestConfiguration = camelContext.getRestConfiguration();
    final String globalConfigurationHost = hostFrom(globalRestConfiguration);
    if (globalConfigurationHost != null) {
        return globalConfigurationHost;
    }
    final String specificationScheme = specificationUri.getScheme();
    if (specificationUri.isAbsolute() && specificationScheme.toLowerCase().startsWith("http")) {
        try {
            return new URI(specificationUri.getScheme(), specificationUri.getUserInfo(), specificationUri.getHost(), specificationUri.getPort(), null, null, null).toString();
        } catch (final URISyntaxException e) {
            throw new IllegalStateException("Unable to create a new URI from: " + specificationUri, e);
        }
    }
    final boolean areTheSame = "rest-swagger".equals(assignedComponentName);
    throw new IllegalStateException("Unable to determine destionation host for requests. The Swagger specification" + " does not specify `scheme` and `host` parameters, the specification URI is not absolute with `http` or" + " `https` scheme, and no RestConfigurations configured with `scheme`, `host` and `port` were found for `" + (areTheSame ? "rest-swagger` component" : assignedComponentName + "` or `rest-swagger` components") + " and there is no global RestConfiguration with those properties");
}
Also used : CamelContext(org.apache.camel.CamelContext) RestConfiguration(org.apache.camel.spi.RestConfiguration) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 52 with CamelContext

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

the class RestSwaggerComponentTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    final CamelContext camelContext = super.createCamelContext();
    final RestSwaggerComponent component = new RestSwaggerComponent();
    component.setComponentName(componentName);
    component.setHost("http://localhost:" + petstore.port());
    camelContext.addComponent("petStore", component);
    return camelContext;
}
Also used : CamelContext(org.apache.camel.CamelContext)

Example 53 with CamelContext

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

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

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

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