You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/10/12 06:51:49 UTC

[camel-quarkus] 05/05: Test Geocoder with Google maps mock API if GOOGLE_GEOCODER_API_KEY is not provided fixes #1860

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch camel-master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit 3b7ae6e9378f51caac79fe6412293a3dcd2c957d
Author: Zineb Bendhiba <be...@gmail.com>
AuthorDate: Wed Oct 7 16:38:15 2020 +0200

    Test Geocoder with Google maps mock API if GOOGLE_GEOCODER_API_KEY is not provided fixes #1860
---
 integration-tests/geocoder/pom.xml                 |  10 ++
 .../geocoder/it/GeocoderGoogleResource.java        |  14 +--
 .../component/geocoder/it/MockApiService.java      |  54 +++++++++
 .../quarkus/component/geocoder/it/Routes.java      | 129 +++++++++++++++++++++
 .../src/main/resources/application.properties      |  11 +-
 .../component/geocoder/it/GeocoderGoogleIT.java    |   3 -
 .../component/geocoder/it/GeocoderGoogleTest.java  |   2 -
 7 files changed, 209 insertions(+), 14 deletions(-)

diff --git a/integration-tests/geocoder/pom.xml b/integration-tests/geocoder/pom.xml
index a649903..43c52c5 100644
--- a/integration-tests/geocoder/pom.xml
+++ b/integration-tests/geocoder/pom.xml
@@ -57,6 +57,16 @@
             <artifactId>quarkus-resteasy-jsonb</artifactId>
         </dependency>
 
+        <!-- dependencies needed to mock Google maps API -->
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-platform-http</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-integration-test-support-mock-backend</artifactId>
+        </dependency>
+
 
         <!-- test dependencies -->
         <dependency>
diff --git a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleResource.java b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleResource.java
index 0bd2e2b..36c0a47 100644
--- a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleResource.java
+++ b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleResource.java
@@ -31,21 +31,21 @@ import org.jboss.logging.Logger;
 
 @Path("/google")
 @ApplicationScoped
+@Produces(MediaType.APPLICATION_JSON)
 public class GeocoderGoogleResource {
     private static final Logger LOG = Logger.getLogger(GeocoderGoogleResource.class);
 
     @Inject
     ProducerTemplate producerTemplate;
 
-    @ConfigProperty(name = "google.api.key")
-    String apiKey;
+    @ConfigProperty(name = "google.api.key", defaultValue = "AIzaFakeKey")
+    String googleApiKey;
 
     @GET
-    @Produces(MediaType.APPLICATION_JSON)
     public GeocodingResult[] getByCurrentLocation() {
         LOG.infof("Retrieve info from current location");
         final GeocodingResult[] response = producerTemplate.requestBody(
-                "geocoder:address:current?headersOnly=false&apiKey=" + apiKey,
+                String.format("geocoder:address:current?apiKey=%s", googleApiKey),
                 "Hello World", GeocodingResult[].class);
         LOG.infof("Response : %s", response);
         return response;
@@ -53,11 +53,10 @@ public class GeocoderGoogleResource {
 
     @Path("address/{address}")
     @GET
-    @Produces(MediaType.APPLICATION_JSON)
     public GeocodingResult[] getByAddress(@PathParam("address") String address) {
         LOG.infof("Retrieve info from address : %s", address);
         final GeocodingResult[] response = producerTemplate.requestBody(
-                "geocoder:address:" + address + "?apiKey=" + apiKey,
+                String.format("geocoder:address:%s?apiKey=%s", address, googleApiKey),
                 "Hello World", GeocodingResult[].class);
         LOG.infof("Response: %s", response);
         return response;
@@ -65,11 +64,10 @@ public class GeocoderGoogleResource {
 
     @Path("lat/{lat}/lon/{lon}")
     @GET
-    @Produces(MediaType.APPLICATION_JSON)
     public GeocodingResult[] getByCoordinate(@PathParam("lat") String latitude, @PathParam("lon") String longitude) {
         LOG.infof("Retrieve  info from georgraphic coordinates latitude : %s, longitude %s", latitude, longitude);
         final GeocodingResult[] response = producerTemplate.requestBody(
-                "geocoder:latlng:" + latitude + "," + longitude + "?apiKey=" + apiKey,
+                String.format("geocoder:latlng:%s,%s?apiKey=%s", latitude, longitude, googleApiKey),
                 "Hello World", GeocodingResult[].class);
         LOG.infof("Response : %s", response);
         return response;
diff --git a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/MockApiService.java b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/MockApiService.java
new file mode 100644
index 0000000..d86a7a7
--- /dev/null
+++ b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/MockApiService.java
@@ -0,0 +1,54 @@
+/*
+ * 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.lang.reflect.Field;
+
+import javax.enterprise.context.ApplicationScoped;
+
+import com.google.maps.GeoApiContext;
+
+@ApplicationScoped
+public class MockApiService {
+
+    public GeoApiContext createGeoApiContext(String baseUri, String apiKey)
+            throws IllegalAccessException, InstantiationException, NoSuchFieldException {
+        GeoApiContext.Builder builder = createGeoApiContext(baseUri);
+        builder.apiKey(apiKey);
+        return builder.build();
+    }
+
+    /**
+     * Creates a Builder and sets a new baseUrl for mock with reflection, because it is impossible to set it differently!
+     *
+     * @param  baseUrl
+     * @return
+     * @throws IllegalAccessException
+     * @throws InstantiationException
+     * @throws NoSuchFieldException
+     */
+    public GeoApiContext.Builder createGeoApiContext(String baseUrl)
+            throws IllegalAccessException, InstantiationException, NoSuchFieldException {
+        Class<?> clazz = GeoApiContext.Builder.class;
+        Object builder = clazz.newInstance();
+
+        Field f1 = builder.getClass().getDeclaredField("baseUrlOverride");
+        f1.setAccessible(true);
+        f1.set(builder, baseUrl);
+        return (GeoApiContext.Builder) builder;
+    }
+}
diff --git a/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/Routes.java b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/Routes.java
new file mode 100644
index 0000000..3d96cde
--- /dev/null
+++ b/integration-tests/geocoder/src/main/java/org/apache/camel/quarkus/component/geocoder/it/Routes.java
@@ -0,0 +1,129 @@
+/*
+ * 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 javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import io.quarkus.arc.Unremovable;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.geocoder.GeoCoderComponent;
+import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+
+@ApplicationScoped
+public class Routes extends RouteBuilder {
+
+    @ConfigProperty(name = "google.api.key")
+    String googleApiKey;
+    @ConfigProperty(name = "quarkus.http.test-port")
+    int httpTestPort;
+    @ConfigProperty(name = "quarkus.http.port")
+    int httpPort;
+    @Inject
+    MockApiService mockApiService;
+
+    private String getBaseUri() {
+        final boolean isNativeMode = "executable".equals(System.getProperty("org.graalvm.nativeimage.kind"));
+        return "AIzaFakeKey".equals(googleApiKey)
+                ? "http://localhost:" + (isNativeMode ? httpPort : httpTestPort)
+                : "https://maps.googleapis.com";
+    }
+
+    /**
+     * We need to implement some conditional configuration of the {@link GeoCoderComponent} thus we create it
+     * programmatically and publish via CDI.
+     *
+     * @return a configured {@link GeoCoderComponent}
+     */
+    @Produces
+    @ApplicationScoped
+    @Unremovable
+    @Named("geocoder")
+    GeoCoderComponent geocoderComponent() throws IllegalAccessException, NoSuchFieldException, InstantiationException {
+        final GeoCoderComponent result = new GeoCoderComponent();
+        result.setCamelContext(getContext());
+        result.setGeoApiContext(mockApiService.createGeoApiContext(getBaseUri(), googleApiKey));
+        return result;
+    }
+
+    @Override
+    public void configure() throws Exception {
+        if (MockBackendUtils.startMockBackend(true)) {
+            from("platform-http:///maps/api/geocode/json?httpMethodRestrict=GET")
+                    .process(e -> load(createMockGeocodeResponse(), e));
+            from("platform-http:///geolocation/v1/geolocate?httpMethodRestrict=POST")
+                    .process(e -> load(createMockGeolocateResponse(), e));
+        }
+    }
+
+    private String createMockGeolocateResponse() {
+        return "{\n" +
+                "  \"location\": {\n" +
+                "    \"lat\": 71.5388001,\n" +
+                "    \"lng\": -66.885417\n" +
+                "  },\n" +
+                "  \"accuracy\": 578963\n" +
+                "} ";
+    }
+
+    private String createMockGeocodeResponse() {
+        return "{\n"
+                + "   \"results\" : [\n"
+                + "      {\n"
+                + "         \"address_components\" : [\n"
+                + "            {\n"
+                + "               \"long_name\" : \"1600\",\n"
+                + "               \"short_name\" : \"1600\",\n"
+                + "               \"types\" : [ \"street_number\" ]\n"
+                + "            }\n"
+                + "         ],\n"
+                + "         \"formatted_address\" : \"1600 Amphitheatre Parkway, Mountain View, "
+                + "CA 94043, USA\",\n"
+                + "         \"geometry\" : {\n"
+                + "            \"location\" : {\n"
+                + "               \"lat\" : 37.4220033,\n"
+                + "               \"lng\" : -122.0839778\n"
+                + "            },\n"
+                + "            \"location_type\" : \"ROOFTOP\",\n"
+                + "            \"viewport\" : {\n"
+                + "               \"northeast\" : {\n"
+                + "                  \"lat\" : 37.4233522802915,\n"
+                + "                  \"lng\" : -122.0826288197085\n"
+                + "               },\n"
+                + "               \"southwest\" : {\n"
+                + "                  \"lat\" : 37.4206543197085,\n"
+                + "                  \"lng\" : -122.0853267802915\n"
+                + "               }\n"
+                + "            }\n"
+                + "         },\n"
+                + "         \"types\" : [ \"street_address\" ]\n"
+                + "      }\n"
+                + "   ],\n"
+                + "   \"status\" : \"OK\"\n"
+                + "}";
+
+    }
+
+    private void load(String response, Exchange exchange) {
+        exchange.getMessage().setBody(response);
+    }
+
+}
diff --git a/integration-tests/geocoder/src/main/resources/application.properties b/integration-tests/geocoder/src/main/resources/application.properties
index e30c662..8668957 100644
--- a/integration-tests/geocoder/src/main/resources/application.properties
+++ b/integration-tests/geocoder/src/main/resources/application.properties
@@ -19,6 +19,15 @@
 ################################
 #### properties for Google maps services
 # add your API KEY to run the examples
-google.api.key={{env.GOOGLE_API_KEY}}
+google.api.key=${GOOGLE_API_KEY:AIzaFakeKey}
+
+# You may want to export CAMEL_QUARKUS_START_MOCK_BACKEND=false to avoid starting he the mock Google Maps API
+# to make sure that you test against the real remote Google Maps API
+camel.quarkus.start-mock-backend=true
+
+# this configuration is needed only to mock Google Maps API
+quarkus.index-dependency.gmaps.group-id=com.google.maps
+quarkus.index-dependency.gmaps.artifact-id=google-maps-services
+quarkus.camel.native.reflection.include-patterns=com.google.maps.GeoApiContext$Builder
 
 
diff --git a/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleIT.java b/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleIT.java
index 1104d79..ed78c6e 100644
--- a/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleIT.java
+++ b/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleIT.java
@@ -17,10 +17,7 @@
 package org.apache.camel.quarkus.component.geocoder.it;
 
 import io.quarkus.test.junit.NativeImageTest;
-import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
 
 @NativeImageTest
-@EnabledIfEnvironmentVariable(named = "GOOGLE_API_KEY", matches = ".+")
 class GeocoderGoogleIT extends GeocoderGoogleTest {
-
 }
diff --git a/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleTest.java b/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleTest.java
index dbd8fb6..673002b 100644
--- a/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleTest.java
+++ b/integration-tests/geocoder/src/test/java/org/apache/camel/quarkus/component/geocoder/it/GeocoderGoogleTest.java
@@ -20,13 +20,11 @@ import io.quarkus.test.common.http.TestHTTPEndpoint;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
 
 import static org.hamcrest.Matchers.hasKey;
 
 @QuarkusTest
 @TestHTTPEndpoint(GeocoderGoogleResource.class)
-@EnabledIfEnvironmentVariable(named = "GOOGLE_API_KEY", matches = ".+")
 class GeocoderGoogleTest {
 
     @Test