This is an automated email from the ASF dual-hosted git repository.
ppalaga pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push:
new 4780f41 Stub GeoCoder nominatim APIs
4780f41 is described below
commit 4780f41761c77e6fbfd20e7d690399eca6b6d232
Author: James Netherton <[email protected]>
AuthorDate: Tue Oct 26 11:52:28 2021 +0100
Stub GeoCoder nominatim APIs
---
.../component/geocoder/it/FakeNominatimApi.java | 52 ++++++++++++++++++++++
.../geocoder/it/GeocoderNominationResource.java | 37 ++++++++++++---
.../src/main/resources/application.properties | 2 +
.../src/main/resources/nominatimReverse.json | 32 +++++++++++++
.../src/main/resources/nominatimSearch.json | 32 +++++++++++++
5 files changed, 150 insertions(+), 5 deletions(-)
diff --git
a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/FakeNominatimApi.java
b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/FakeNominatimApi.java
new file mode 100644
index 0000000..46a20b4
--- /dev/null
+++
b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/FakeNominatimApi.java
@@ -0,0 +1,52 @@
+/*
+ * 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.quarkus.component.geocoder.it;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.apache.commons.io.IOUtils;
+
+/**
+ * Stubs nominatim API responses in absence of WireMock support:
+ *
+ * https://github.com/apache/camel-quarkus/issues/2033
+ */
+@Path("/fake/nominatim/api")
+public class FakeNominatimApi {
+
+ @GET
+ @Path("/reverse")
+ @Produces("application/json")
+ public String reverse() throws IOException {
+ InputStream resource =
FakeNominatimApi.class.getResourceAsStream("/nominatimReverse.json");
+ return IOUtils.toString(resource, StandardCharsets.UTF_8);
+ }
+
+ @GET
+ @Path("/search")
+ @Produces("application/json")
+ public String search() throws IOException {
+ InputStream resource =
FakeNominatimApi.class.getResourceAsStream("/nominatimSearch.json");
+ return IOUtils.toString(resource, StandardCharsets.UTF_8);
+ }
+}
diff --git
a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderNominationResource.java
b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderNominationResource.java
index 44f4ebb..dfa7ab6 100644
---
a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderNominationResource.java
+++
b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderNominationResource.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.quarkus.component.geocoder.it;
+import java.util.Optional;
+
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
@@ -24,11 +26,15 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
+import io.quarkus.runtime.LaunchMode;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.geocoder.GeoCoderConstants;
import org.apache.camel.component.geocoder.GeocoderStatus;
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
@Path("/nomination")
@@ -36,16 +42,22 @@ import org.jboss.logging.Logger;
public class GeocoderNominationResource {
private static final Logger LOG =
Logger.getLogger(GeocoderNominationResource.class);
+ @ConfigProperty(name = "quarkus.http.test-port")
+ Optional<Integer> httpTestPort;
+
+ @ConfigProperty(name = "quarkus.http.port")
+ Optional<Integer> httpPort;
+
@Inject
ProducerTemplate producerTemplate;
@Path("address/{address}")
@GET
@Produces(MediaType.APPLICATION_JSON)
- public GeocoderResult getByCurrentLocation(@PathParam("address") String
address) {
+ public GeocoderResult getByCurrentLocation(@PathParam("address") String
address) throws Exception {
LOG.infof("Retrieve info from address %s", address);
Exchange result = producerTemplate.request("geocoder:address:" +
address +
-
"?type=NOMINATIM&serverUrl=RAW(https://nominatim.openstreetmap.org)", exchange
-> {
+ "?type=NOMINATIM&serverUrl=RAW(" + getServerUrl() + ")",
exchange -> {
exchange.getMessage().setBody("Hello Body");
});
return extractResult(result);
@@ -54,10 +66,11 @@ public class GeocoderNominationResource {
@Path("lat/{lat}/lon/{lon}")
@GET
@Produces(MediaType.APPLICATION_JSON)
- public GeocoderResult getByCoordinate(@PathParam("lat") String latitude,
@PathParam("lon") String longitude) {
+ public GeocoderResult getByCoordinate(@PathParam("lat") String latitude,
@PathParam("lon") String longitude)
+ throws Exception {
LOG.infof("Retrieve info from georgraphic coordinates latitude : %s,
longitude %s", latitude, longitude);
Exchange result = producerTemplate.request("geocoder:latlng:" +
latitude + "," + longitude +
-
"?type=NOMINATIM&serverUrl=RAW(https://nominatim.openstreetmap.org)", exchange
-> {
+ "?type=NOMINATIM&serverUrl=RAW(" + getServerUrl() + ")",
exchange -> {
exchange.getMessage().setBody("Hello Body");
});
return extractResult(result);
@@ -69,7 +82,12 @@ public class GeocoderNominationResource {
* @param exchange
* @return
*/
- private GeocoderResult extractResult(Exchange exchange) {
+ private GeocoderResult extractResult(Exchange exchange) throws Exception {
+ Exception exception = exchange.getException();
+ if (exception != null) {
+ throw exception;
+ }
+
Message message = exchange.getIn();
return new GeocoderResult()
.withLat(extractString(message, GeoCoderConstants.LAT))
@@ -96,4 +114,13 @@ public class GeocoderNominationResource {
return message.getHeader(name, String.class);
}
+ private String getServerUrl() {
+ Config config = ConfigProvider.getConfig();
+ Optional<String> wiremockUrl = config.getOptionalValue("wiremock.url",
String.class);
+ if (wiremockUrl.isPresent()) {
+ int port = LaunchMode.current().equals(LaunchMode.TEST) ?
httpTestPort.get() : httpPort.get();
+ return String.format("http://localhost:%d/fake/nominatim/api",
port);
+ }
+ return "https://nominatim.openstreetmap.org";
+ }
}
diff --git
a/integration-tests/geocoder/src/main/resources/application.properties
b/integration-tests/geocoder/src/main/resources/application.properties
index 03f2551..0dd34c5 100644
--- a/integration-tests/geocoder/src/main/resources/application.properties
+++ b/integration-tests/geocoder/src/main/resources/application.properties
@@ -20,3 +20,5 @@
#### properties for Google maps services
# add your API KEY to run the examples
google.api.key=${GOOGLE_API_KEY:AIzaFakeKey}
+
+quarkus.native.resources.includes=nominatim*.json
diff --git
a/integration-tests/geocoder/src/main/resources/nominatimReverse.json
b/integration-tests/geocoder/src/main/resources/nominatimReverse.json
new file mode 100644
index 0000000..a658369
--- /dev/null
+++ b/integration-tests/geocoder/src/main/resources/nominatimReverse.json
@@ -0,0 +1,32 @@
+{
+ "place_id": 169146043,
+ "licence": "Data © OpenStreetMap contributors, ODbL 1.0.
https://osm.org/copyright",
+ "osm_type": "way",
+ "osm_id": 279767995,
+ "lat": "40.714128099999996",
+ "lon": "-73.96131110000002",
+ "place_rank": 30,
+ "category": "building",
+ "type": "yes",
+ "importance": 0,
+ "addresstype": "building",
+ "name": null,
+ "display_name": "281, Bedford Avenue, Brooklyn, Kings County, New York,
11211, United States",
+ "address": {
+ "house_number": "281",
+ "road": "Bedford Avenue",
+ "suburb": "Brooklyn",
+ "city_district": "Kings County",
+ "city": "New York",
+ "state": "New York",
+ "postcode": "11211",
+ "country": "United States",
+ "country_code": "us"
+ },
+ "boundingbox": [
+ "40.714064",
+ "40.7141922",
+ "-73.9614164",
+ "-73.9612058"
+ ]
+}
\ No newline at end of file
diff --git a/integration-tests/geocoder/src/main/resources/nominatimSearch.json
b/integration-tests/geocoder/src/main/resources/nominatimSearch.json
new file mode 100644
index 0000000..0374f5a
--- /dev/null
+++ b/integration-tests/geocoder/src/main/resources/nominatimSearch.json
@@ -0,0 +1,32 @@
+[
+ {
+ "place_id": 107106886,
+ "licence": "Data © OpenStreetMap contributors, ODbL 1.0.
https://osm.org/copyright",
+ "osm_type": "way",
+ "osm_id": 26663743,
+ "boundingbox": [
+ "37.3695898",
+ "37.3724355",
+ "-6.0562787",
+ "-6.0555991"
+ ],
+ "lat": "37.3709153",
+ "lon": "-6.0559655",
+ "display_name": "Calle Marie Curie, Urbanización Valdovina, Residencial
Andana, Tomares, Sevilla, Andalucía, 41940, España",
+ "place_rank": 26,
+ "category": "highway",
+ "type": "residential",
+ "importance": 0.6,
+ "address": {
+ "road": "Calle Marie Curie",
+ "neighbourhood": "Urbanización Valdovina",
+ "hamlet": "Residencial Andana",
+ "town": "Tomares",
+ "state_district": "Sevilla",
+ "state": "Andalucía",
+ "postcode": "41940",
+ "country": "España",
+ "country_code": "es"
+ }
+ }
+]
\ No newline at end of file