You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/05/07 07:33:09 UTC
camel git commit: CAMEL-9273: More weather stuff. Thanks to Arno
Noordover for the patch.
Repository: camel
Updated Branches:
refs/heads/master b3c3282d9 -> 850fc3d2d
CAMEL-9273: More weather stuff. Thanks to Arno Noordover for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/850fc3d2
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/850fc3d2
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/850fc3d2
Branch: refs/heads/master
Commit: 850fc3d2dc544e405afc7dcc962dec9a985c57e2
Parents: b3c3282
Author: Claus Ibsen <da...@apache.org>
Authored: Sat May 7 09:33:00 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 7 09:33:00 2016 +0200
----------------------------------------------------------------------
components/camel-weather/pom.xml | 5 +
components/camel-weather/readme.MD | 2 +-
.../component/weather/WeatherConfiguration.java | 14 ++-
.../component/weather/WeatherLanguage.java | 2 +-
.../camel/component/weather/WeatherMode.java | 2 +-
.../camel/component/weather/WeatherQuery.java | 43 ++-----
.../camel/component/weather/WeatherUnits.java | 2 +-
.../FreeGeoIpGeoLocationProvider.java | 62 ++++++++++
.../weather/geolocation/GeoLocation.java | 35 ++++++
.../geolocation/GeoLocationProvider.java | 23 ++++
.../weather/BaseWeatherConsumerTest.java | 2 +-
.../weather/CurrentWeatherConsumerHtmlTest.java | 2 +-
.../weather/CurrentWeatherConsumerTest.java | 2 +-
.../weather/CurrentWeatherConsumerXmlTest.java | 2 +-
.../component/weather/WeatherQueryTest.java | 122 +++++++++++++++++++
15 files changed, 276 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-weather/pom.xml b/components/camel-weather/pom.xml
index e35ac46..610290d 100644
--- a/components/camel-weather/pom.xml
+++ b/components/camel-weather/pom.xml
@@ -60,6 +60,11 @@
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<profiles>
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/readme.MD
----------------------------------------------------------------------
diff --git a/components/camel-weather/readme.MD b/components/camel-weather/readme.MD
index 0c680f6..5616725 100644
--- a/components/camel-weather/readme.MD
+++ b/components/camel-weather/readme.MD
@@ -15,7 +15,7 @@ There are five type of queries:
1. by name of the city and the country code (q={city name},{country code})
2. by id of the city (id={city ID}); The current weather can also be
asked for a group of id's using the context group
-3. by latitude and longtitude (lat={lat}&lon={lon})
+3. by latitude and longitude (lat={lat}&lon={lon})
4. by box (bbox=12,32,15,37,10), you must also provide cluster=yes/no; context
must be box/city or box/station
5. find cities/station around location defined by lat/lon; context
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/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 f4da1b9..fefc223 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
@@ -18,6 +18,7 @@ package org.apache.camel.component.weather;
import java.util.Scanner;
+import org.apache.camel.component.weather.geolocation.FreeGeoIpGeoLocationProvider;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriParams;
@@ -32,6 +33,7 @@ import static org.apache.camel.util.ObjectHelper.notNull;
public class WeatherConfiguration {
private final WeatherComponent component;
+ private final WeatherQuery weatherQuery;
@UriPath(description = "The name value is not used.") @Metadata(required = "true")
private String name;
@@ -53,8 +55,8 @@ public class WeatherConfiguration {
private String period = "";
@UriParam(defaultValue = "JSON")
private WeatherMode mode = JSON;
- @UriParam(defaultValue = "METRIC")
- private WeatherUnits units = METRIC;
+ @UriParam
+ private WeatherUnits units;
@UriParam(defaultValue = "en")
private WeatherLanguage language = en;
@UriParam
@@ -62,6 +64,10 @@ public class WeatherConfiguration {
public WeatherConfiguration(WeatherComponent component) {
this.component = notNull(component, "component");
+ weatherQuery = new WeatherQuery(this);
+ FreeGeoIpGeoLocationProvider geoLocationProvider = new FreeGeoIpGeoLocationProvider();
+ geoLocationProvider.setCamelContext(component.getCamelContext());
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
}
public String getPeriod() {
@@ -177,11 +183,11 @@ public class WeatherConfiguration {
}
String getQuery() throws Exception {
- return new WeatherQuery(this.component, this).getQuery();
+ return weatherQuery.getQuery();
}
String getQuery(String location) throws Exception {
- return new WeatherQuery(this.component, this).getQuery(location);
+ return weatherQuery.getQuery(location);
}
public WeatherLanguage getLanguage() {
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherLanguage.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherLanguage.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherLanguage.java
index ec4f35f..cc13b3d 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherLanguage.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherLanguage.java
@@ -42,5 +42,5 @@ public enum WeatherLanguage {
zh_cn,
tr,
hr,
- ca;
+ ca
}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/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
index 9d21c44..3bcae95 100644
--- 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
@@ -21,6 +21,6 @@ package org.apache.camel.component.weather;
*/
public enum WeatherMode {
- HTML, JSON, XML;
+ HTML, JSON, XML
}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
index bf6edb4..45af6f0 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherQuery.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.weather;
+import org.apache.camel.component.weather.geolocation.GeoLocation;
+import org.apache.camel.component.weather.geolocation.GeoLocationProvider;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
@@ -33,10 +35,9 @@ import static org.apache.camel.util.ObjectHelper.notNull;
*/
public class WeatherQuery {
private final WeatherConfiguration weatherConfiguration;
- private final WeatherComponent component;
+ private GeoLocationProvider geoLocationProvider;
- public WeatherQuery(WeatherComponent component, WeatherConfiguration weatherConfiguration) {
- this.component = component;
+ public WeatherQuery(WeatherConfiguration weatherConfiguration) {
this.weatherConfiguration = weatherConfiguration;
}
@@ -67,7 +68,7 @@ public class WeatherQuery {
location = location + "&lang=" + weatherConfiguration.getLanguage();
if (weatherConfiguration.getTopLat() != null && weatherConfiguration.getRightLon() != null) {
- answer += "box?" + location;
+ answer += "box/city?" + location;
} else if (isEmpty(weatherConfiguration.getPeriod())) {
answer += "weather?" + location;
} else {
@@ -75,7 +76,7 @@ public class WeatherQuery {
}
// append the desired measurement unit if not the default (which is metric)
- if (weatherConfiguration.getUnits() != METRIC) {
+ if (weatherConfiguration.getUnits() != null) {
answer += "&units=" + weatherConfiguration.getUnits().name().toLowerCase();
}
@@ -92,34 +93,12 @@ public class WeatherQuery {
}
- /**
- * TODO: shouldn't this method be moved to a class of its own perhaps with an interface
- * that gets injected. For testing purposes you can inject your own version when testing this
- * class.
- */
-
String getCurrentGeoLocation() throws Exception {
- HttpClient httpClient = new HttpClient();
- GetMethod getMethod = new GetMethod("http://freegeoip.io/json/");
- try {
- int statusCode = httpClient.executeMethod(getMethod);
- if (statusCode != HttpStatus.SC_OK) {
- throw new IllegalStateException("Got the unexpected http-status '" + getMethod.getStatusLine() + "' for the geolocation");
- }
- String geoLocation = component.getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, getMethod.getResponseBodyAsStream());
- if (isEmpty(geoLocation)) {
- throw new IllegalStateException("Got the unexpected value '" + geoLocation + "' for the geolocation");
- }
-
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node = mapper.readValue(geoLocation, JsonNode.class);
- JsonNode latitudeNode = notNull(node.get("latitude"), "latitude");
- JsonNode longitudeNode = notNull(node.get("longitude"), "longitude");
-
- return "lat=" + latitudeNode + "&lon=" + longitudeNode;
- } finally {
- getMethod.releaseConnection();
- }
+ GeoLocation geoLocation = geoLocationProvider.getCurrentGeoLocation();
+ return "lat=" + geoLocation.getLatitude() + "&lon=" + geoLocation.getLongitude();
}
+ void setGeoLocationProvider(GeoLocationProvider geoLocationProvider) {
+ this.geoLocationProvider = geoLocationProvider;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
index ef7888f..7646807 100644
--- a/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/WeatherUnits.java
@@ -21,6 +21,6 @@ package org.apache.camel.component.weather;
*/
public enum WeatherUnits {
- IMPERIAL, METRIC;
+ IMPERIAL, METRIC
}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java
new file mode 100644
index 0000000..2d68a1f
--- /dev/null
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/FreeGeoIpGeoLocationProvider.java
@@ -0,0 +1,62 @@
+/**
+ * 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.geolocation;
+
+import org.apache.camel.CamelContext;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
+
+import static org.apache.camel.util.ObjectHelper.isEmpty;
+import static org.apache.camel.util.ObjectHelper.notNull;
+
+public class FreeGeoIpGeoLocationProvider implements GeoLocationProvider {
+
+ private CamelContext camelContext;
+
+ @Override
+ public GeoLocation getCurrentGeoLocation() throws Exception {
+ HttpClient httpClient = new HttpClient();
+ GetMethod getMethod = new GetMethod("http://freegeoip.io/json/");
+ try {
+ int statusCode = httpClient.executeMethod(getMethod);
+ if (statusCode != HttpStatus.SC_OK) {
+ throw new IllegalStateException("Got the unexpected http-status '" + getMethod.getStatusLine() + "' for the geolocation");
+ }
+ String geoLocation = camelContext.getTypeConverter().mandatoryConvertTo(String.class, getMethod.getResponseBodyAsStream());
+ if (isEmpty(geoLocation)) {
+ throw new IllegalStateException("Got the unexpected value '" + geoLocation + "' for the geolocation");
+ }
+
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode node = mapper.readValue(geoLocation, JsonNode.class);
+ JsonNode latitudeNode = notNull(node.get("latitude"), "latitude");
+ JsonNode longitudeNode = notNull(node.get("longitude"), "longitude");
+
+ return new GeoLocation(longitudeNode.asText(), latitudeNode.asText());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ }
+
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocation.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocation.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocation.java
new file mode 100644
index 0000000..8b0c36f
--- /dev/null
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocation.java
@@ -0,0 +1,35 @@
+/**
+ * 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.geolocation;
+
+public class GeoLocation {
+ private final String longitude;
+ private final String latitude;
+
+ public GeoLocation(String longitude, String latitude) {
+ this.longitude = longitude;
+ this.latitude = latitude;
+ }
+
+ public String getLongitude() {
+ return longitude;
+ }
+
+ public String getLatitude() {
+ return latitude;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocationProvider.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocationProvider.java b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocationProvider.java
new file mode 100644
index 0000000..fac64e0
--- /dev/null
+++ b/components/camel-weather/src/main/java/org/apache/camel/component/weather/geolocation/GeoLocationProvider.java
@@ -0,0 +1,23 @@
+/**
+ * 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.geolocation;
+
+public interface GeoLocationProvider {
+
+ GeoLocation getCurrentGeoLocation() throws Exception;
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/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 d45e9ff..1bc4ef1 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
@@ -28,7 +28,7 @@ public abstract class BaseWeatherConsumerTest extends CamelTestSupport {
protected void checkWeatherContent(String weather) {
// the default mode is json
- log.debug("The weather in {} format is {}{}", new Object[] {WeatherMode.JSON, LS, weather});
+ log.debug("The weather in {} format is {}{}", WeatherMode.JSON, LS, weather);
assertStringContains(weather, "\"coord\":{");
assertStringContains(weather, "temp");
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/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
index d0b5aeb..ebb0ec5 100644
--- 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
@@ -22,7 +22,7 @@ public class CurrentWeatherConsumerHtmlTest extends BaseWeatherConsumerTest {
@Override
protected void checkWeatherContent(String weather) {
- log.debug("The weather in {} format is {}{}", new Object[] {WeatherMode.HTML, LS, weather});
+ log.debug("The weather in {} format is {}{}", WeatherMode.HTML, LS, weather);
assertStringContains(weather, "<!DOCTYPE html>");
assertStringContains(weather, "<head>");
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
index b1436ec..d50bf85 100644
--- a/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/CurrentWeatherConsumerTest.java
@@ -22,7 +22,7 @@ public class CurrentWeatherConsumerTest extends BaseWeatherConsumerTest {
@Override
protected void checkWeatherContent(String weather) {
- log.debug("The weather in {} format is {}{}", new Object[] {WeatherMode.XML, LS, weather});
+ log.debug("The weather in {} format is {}{}", WeatherMode.XML, LS, weather);
//assertStringContains(weather, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
assertStringContains(weather, "<coord");
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/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
index de54c63..4798d18 100644
--- 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
@@ -22,7 +22,7 @@ public class CurrentWeatherConsumerXmlTest extends BaseWeatherConsumerTest {
@Override
protected void checkWeatherContent(String weather) {
- log.debug("The weather in {} format is {}{}", new Object[] {WeatherMode.XML, LS, weather});
+ log.debug("The weather in {} format is {}{}", WeatherMode.XML, LS, weather);
assertStringContains(weather, "<coord");
assertStringContains(weather, "<temperature");
http://git-wip-us.apache.org/repos/asf/camel/blob/850fc3d2/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java b/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
new file mode 100644
index 0000000..ae39322
--- /dev/null
+++ b/components/camel-weather/src/test/java/org/apache/camel/component/weather/WeatherQueryTest.java
@@ -0,0 +1,122 @@
+/**
+ * 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.component.weather.geolocation.GeoLocation;
+import org.apache.camel.component.weather.geolocation.GeoLocationProvider;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class WeatherQueryTest {
+
+ private static final String LONGITUDE = "4.13";
+ private static final String LATITUDE = "51.98";
+ private static final String APPID = "9162755b2efa555823cfe0451d7fff38";
+ @Mock
+ private GeoLocationProvider geoLocationProvider;
+
+ @Mock
+ private GeoLocationProvider exceptionThrowingGeoLocationProvider;
+
+ @Before
+ public void setup() throws Exception {
+ GeoLocation location = new GeoLocation(LONGITUDE, LATITUDE);
+ when(geoLocationProvider.getCurrentGeoLocation()).thenReturn(location);
+ when(exceptionThrowingGeoLocationProvider.getCurrentGeoLocation()).thenThrow(new IllegalStateException("Error in getting current location"));
+ }
+
+ @Test
+ public void testBoxedQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setLon("4");
+ weatherConfiguration.setLat("52");
+ weatherConfiguration.setRightLon("6");
+ weatherConfiguration.setTopLat("54");
+ weatherConfiguration.setZoom(8);
+ weatherConfiguration.setUnits(WeatherUnits.METRIC);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/box/city?bbox=4,52,6,54,8&cluster=yes&lang=en&units=metric&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testLatLonQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setLon("4");
+ weatherConfiguration.setLat("52");
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/weather?lat=52&lon=4&lang=nl&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testCurrentLocationQuery() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setPeriod("3");
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setUnits(WeatherUnits.IMPERIAL);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/forecast/daily?lat=51.98&lon=4.13&lang=nl&cnt=3&units=imperial&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+ @Test
+ public void testCurrentLocationQuery2() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLocation("current");
+ weatherConfiguration.setPeriod("3");
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setUnits(WeatherUnits.IMPERIAL);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/forecast/daily?lat=51.98&lon=4.13&lang=nl&cnt=3&units=imperial&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+ @Test
+ public void testGivenLocation() throws Exception {
+ WeatherConfiguration weatherConfiguration = new WeatherConfiguration(new WeatherComponent());
+ weatherConfiguration.setMode(WeatherMode.XML);
+ weatherConfiguration.setLocation("Scheveningen,NL");
+ weatherConfiguration.setLanguage(WeatherLanguage.nl);
+ weatherConfiguration.setUnits(WeatherUnits.IMPERIAL);
+ weatherConfiguration.setAppid(APPID);
+ WeatherQuery weatherQuery = new WeatherQuery(weatherConfiguration);
+ weatherQuery.setGeoLocationProvider(geoLocationProvider);
+ String query = weatherQuery.getQuery();
+ assertThat(query, is("http://api.openweathermap.org/data/2.5/weather?q=Scheveningen,NL&lang=nl&units=imperial&mode=xml&APPID=9162755b2efa555823cfe0451d7fff38"));
+ }
+
+}