Search in sources :

Example 1 with GeocoderStatus

use of com.google.code.geocoder.model.GeocoderStatus in project wildfly-camel by wildfly-extras.

the class GeocoderIntegrationTest method testGeocoderComponent.

@Test
public void testGeocoderComponent() throws Exception {
    HttpClientConfigurer configurer = new GeocoderHttpClientConfigurer();
    initialContext.bind("httpClientConfigurer", configurer);
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("geocoder:address:London, England?httpClientConfigurer=#httpClientConfigurer");
        }
    });
    camelctx.start();
    try {
        // Geocoder API is sometimes flaky so retry the request if necessary
        GeocodeResponse result = null;
        int count = 0;
        while (count < 5) {
            ProducerTemplate template = camelctx.createProducerTemplate();
            result = template.requestBody("direct:start", null, GeocodeResponse.class);
            Assert.assertNotNull("Geocoder response is null", result);
            // Skip further testing if we exceeded the API rate limit
            GeocoderStatus status = result.getStatus();
            Assume.assumeFalse("Geocoder API rate limit exceeded", status.equals(OVER_QUERY_LIMIT));
            if (status.equals(OK)) {
                break;
            }
            Thread.sleep(1000);
            count++;
        }
        List<GeocoderResult> results = result.getResults();
        Assert.assertNotNull("Geocoder results is null", result);
        Assert.assertEquals("Expected 1 GeocoderResult to be returned", 1, results.size());
        LatLng location = results.get(0).getGeometry().getLocation();
        Assert.assertEquals("51.5073509", location.getLat().toPlainString());
        Assert.assertEquals("-0.1277583", location.getLng().toPlainString());
    } finally {
        camelctx.stop();
        initialContext.unbind("httpClientConfigurer");
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) GeocoderStatus(com.google.code.geocoder.model.GeocoderStatus) HttpClientConfigurer(org.apache.camel.component.geocoder.http.HttpClientConfigurer) GeocoderHttpClientConfigurer(org.wildfly.camel.test.geocoder.subA.GeocoderHttpClientConfigurer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) GeocoderHttpClientConfigurer(org.wildfly.camel.test.geocoder.subA.GeocoderHttpClientConfigurer) GeocodeResponse(com.google.code.geocoder.model.GeocodeResponse) LatLng(com.google.code.geocoder.model.LatLng) GeocoderResult(com.google.code.geocoder.model.GeocoderResult) Test(org.junit.Test)

Aggregations

GeocodeResponse (com.google.code.geocoder.model.GeocodeResponse)1 GeocoderResult (com.google.code.geocoder.model.GeocoderResult)1 GeocoderStatus (com.google.code.geocoder.model.GeocoderStatus)1 LatLng (com.google.code.geocoder.model.LatLng)1 CamelContext (org.apache.camel.CamelContext)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 HttpClientConfigurer (org.apache.camel.component.geocoder.http.HttpClientConfigurer)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 Test (org.junit.Test)1 GeocoderHttpClientConfigurer (org.wildfly.camel.test.geocoder.subA.GeocoderHttpClientConfigurer)1