Updated Branches: refs/heads/master 89f6b1170 -> 0ff9749ad
Added the mode option for the weather data which can be either html, json or xml. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0ff9749a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0ff9749a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0ff9749a Branch: refs/heads/master Commit: 0ff9749ad0c4a649e5ed86a60133ee04b536fd7c Parents: 89f6b11 Author: Babak Vahdat <bvah...@apache.org> Authored: Wed May 29 22:06:23 2013 +0200 Committer: Babak Vahdat <bvah...@apache.org> Committed: Wed May 29 22:06:23 2013 +0200 ---------------------------------------------------------------------- .../component/weather/WeatherConfiguration.java | 24 ++++++++- .../camel/component/weather/WeatherConstants.java | 1 - .../camel/component/weather/WeatherConsumer.java | 6 +- .../camel/component/weather/WeatherMode.java | 26 +++++++++ .../component/weather/BaseWeatherConsumerTest.java | 13 +++-- .../weather/CurrentWeatherConsumerHtmlTest.java | 43 +++++++++++++++ .../weather/CurrentWeatherConsumerXmlTest.java | 42 ++++++++++++++ 7 files changed, 145 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java index 0496951..85bc3a3 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConfiguration.java @@ -23,6 +23,7 @@ import org.apache.camel.spi.UriParam; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; +import static org.apache.camel.component.weather.WeatherMode.JSON; import static org.apache.camel.component.weather.WeatherUnits.METRIC; import static org.apache.camel.util.ObjectHelper.isEmpty; import static org.apache.camel.util.ObjectHelper.notNull; @@ -34,6 +35,8 @@ public class WeatherConfiguration { @UriParam private String period = ""; @UriParam + private WeatherMode mode = JSON; + @UriParam private WeatherUnits units = METRIC; private final WeatherComponent component; @@ -58,6 +61,14 @@ public class WeatherConfiguration { } } + public WeatherMode getMode() { + return mode; + } + + public void setMode(WeatherMode mode) { + this.mode = notNull(mode, "mode"); + } + public WeatherUnits getUnits() { return units; } @@ -90,7 +101,16 @@ public class WeatherConfiguration { } else { answer += "forecast/daily?" + location + "&cnt=" + getPeriod(); } - answer += "&units=" + units.name().toLowerCase(); + + // append the desired measurement unit if not the default (which is metric) + if (getUnits() != METRIC) { + answer += "&units=" + getUnits().name().toLowerCase(); + } + + // append the desired output mode if not the default (which is json) + if (getMode() != JSON) { + answer += "&mode=" + getMode().name().toLowerCase(); + } return answer; } @@ -98,7 +118,7 @@ public class WeatherConfiguration { private String getGeoLocation() throws Exception { String geoLocation = component.getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, new URL("http://freegeoip.net/json/")); if (isEmpty(geoLocation)) { - throw new IllegalStateException("Retrieved an unexpected value: '" + geoLocation + "' for the geographical location"); + throw new IllegalStateException("Got the unexpected value '" + geoLocation + "' for the geolocation"); } ObjectMapper mapper = new ObjectMapper(); http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java index a386580..fc81632 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConstants.java @@ -24,7 +24,6 @@ public final class WeatherConstants { public static final String WEATHER_QUERY = "CamelWeatherQuery"; private WeatherConstants() { - // utility class } } http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java index d4a36cb..881e36b 100644 --- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherConsumer.java @@ -37,11 +37,11 @@ public class WeatherConsumer extends ScheduledPollConsumer { @Override protected int poll() throws Exception { - LOG.debug("Going to execute the query '{}'", query); + LOG.debug("Going to execute the Weather query {}", query); String weather = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, new URL(query)); - LOG.debug("Got back the answer '{}'", weather); + LOG.debug("Got back the Weather information {}", weather); if (ObjectHelper.isEmpty(weather)) { - throw new IllegalStateException("Got the empty string '" + weather + "' as the result of the query '" + query + "'"); + throw new IllegalStateException("Got the unexpected value '" + weather + "' as the result of the query '" + query + "'"); } Exchange exchange = getEndpoint().createExchange(); http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherMode.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherMode.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherMode.java new file mode 100644 index 0000000..9d21c44 --- /dev/null +++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherMode.java @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.weather; + +/** + * The possible output modes of the weather information + */ +public enum WeatherMode { + + HTML, JSON, XML; + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java index 07076e9..d10f8c3 100644 --- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java +++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/BaseWeatherConsumerTest.java @@ -24,6 +24,12 @@ import org.junit.Test; public abstract class BaseWeatherConsumerTest extends CamelTestSupport { + protected void checkWeatherContent(String weather) { + // the default mode is json + assertStringContains(weather, "\"coord\":{"); + assertStringContains(weather, "temp"); + } + @Test public void testGrabbingListOfEntries() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); @@ -35,10 +41,9 @@ public abstract class BaseWeatherConsumerTest extends CamelTestSupport { assertNotNull(exchange); Message in = exchange.getIn(); assertNotNull(in); - assertNotNull(in.getBody()); - String body = assertIsInstanceOf(String.class, in.getBody()); - assertStringContains(body, "\"coord\":{"); - assertStringContains(body, "temp"); + String weather = assertIsInstanceOf(String.class, in.getBody()); + + checkWeatherContent(weather); } } http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java new file mode 100644 index 0000000..fccee92 --- /dev/null +++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerHtmlTest.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.weather; + +import org.apache.camel.builder.RouteBuilder; + +public class CurrentWeatherConsumerHtmlTest extends BaseWeatherConsumerTest { + + @Override + protected void checkWeatherContent(String weather) { + log.debug("The weather in {} format is {}{}", new Object[] {WeatherMode.HTML, LS, weather}); + + assertStringContains(weather, "<!DOCTYPE html>"); + assertStringContains(weather, "<head>"); + assertStringContains(weather, "<body>"); + assertStringContains(weather, "<meta name=\"description\" content=\"A layer with current weather conditions in cities for world wide\" />"); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("weather:foo?mode=HTML").to("mock:result"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0ff9749a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerXmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerXmlTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerXmlTest.java new file mode 100644 index 0000000..780a053 --- /dev/null +++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerXmlTest.java @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.weather; + +import org.apache.camel.builder.RouteBuilder; + +public class CurrentWeatherConsumerXmlTest extends BaseWeatherConsumerTest { + + @Override + protected void checkWeatherContent(String weather) { + log.debug("The weather in {} format is {}{}", new Object[] {WeatherMode.XML, LS, weather}); + + assertStringContains(weather, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"); + assertStringContains(weather, "<coord"); + assertStringContains(weather, "<temperature"); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("weather:foo?mode=XML").to("mock:result"); + } + }; + } + +}