Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub GeoCoder nominatim APIs #3235

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,28 +26,38 @@
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")
@ApplicationScoped
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);
Expand All @@ -54,10 +66,11 @@ public GeocoderResult getByCurrentLocation(@PathParam("address") String address)
@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);
Expand All @@ -69,7 +82,12 @@ public GeocoderResult getByCoordinate(@PathParam("lat") String latitude, @PathPa
* @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))
Expand All @@ -96,4 +114,13 @@ private String extractString(Message message, String name) {
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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"
]
}
32 changes: 32 additions & 0 deletions integration-tests/geocoder/src/main/resources/nominatimSearch.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]