camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zregv...@apache.org
Subject [2/2] camel git commit: CAMEL-11051 Support query parameters in rest-sw...
Date Wed, 22 Mar 2017 21:44:52 GMT
CAMEL-11051 Support query parameters in rest-sw...

...agger

Adds REST endpoint `queryParameters` property based on the Swagger
specification. It is expected that the same-named parameters are present
in the headers of the incoming message.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c0f89d60
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c0f89d60
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c0f89d60

Branch: refs/heads/master
Commit: c0f89d60f3eb7a7933b35d2cd8df94f81701f849
Parents: 35638b2
Author: Zoran Regvart <zregvart@apache.org>
Authored: Wed Mar 22 20:20:13 2017 +0100
Committer: Zoran Regvart <zregvart@apache.org>
Committed: Wed Mar 22 22:44:34 2017 +0100

----------------------------------------------------------------------
 .../rest/swagger/RestSwaggerEndpoint.java       | 23 ++++++++++++++
 .../camel/component/rest/swagger/Pets.java      | 33 ++++++++++++++++++++
 .../rest/swagger/RestSwaggerComponentTest.java  | 24 +++++++++++++-
 .../rest/swagger/RestSwaggerEndpointTest.java   | 22 +++++++++++++
 4 files changed, 101 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c0f89d60/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
b/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
index 463d534..6046a99 100644
--- a/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
+++ b/components/camel-rest-swagger/src/main/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpoint.java
@@ -37,6 +37,7 @@ import io.swagger.models.Operation;
 import io.swagger.models.Path;
 import io.swagger.models.Scheme;
 import io.swagger.models.Swagger;
+import io.swagger.models.parameters.Parameter;
 import io.swagger.parser.SwaggerParser;
 import io.swagger.util.Json;
 
@@ -52,6 +53,7 @@ import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ResourceHelper;
 
 import static org.apache.camel.component.rest.swagger.RestSwaggerHelper.isHostParam;
@@ -327,6 +329,12 @@ public final class RestSwaggerEndpoint extends DefaultEndpoint {
             parameters.put("produces", determinedProducers);
         }
 
+        final String queryParameters = operation.getParameters().stream().filter(p ->
"query".equals(p.getIn()))
+            .map(RestSwaggerEndpoint::queryParameterExpression).collect(Collectors.joining("&"));
+        if (isNotEmpty(queryParameters)) {
+            parameters.put("queryParameters", queryParameters);
+        }
+
         return parameters;
     }
 
@@ -487,4 +495,19 @@ public final class RestSwaggerEndpoint extends DefaultEndpoint {
         return null;
     }
 
+    static String queryParameterExpression(final Parameter parameter) {
+        final String name = parameter.getName();
+        if (ObjectHelper.isEmpty(name)) {
+            return "";
+        }
+
+        final StringBuilder expression = new StringBuilder(name).append("={").append(name);
+        if (!parameter.getRequired()) {
+            expression.append('?');
+        }
+        expression.append('}');
+
+        return expression.toString();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c0f89d60/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/Pets.java
----------------------------------------------------------------------
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/Pets.java
b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/Pets.java
new file mode 100644
index 0000000..23e07ec
--- /dev/null
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/Pets.java
@@ -0,0 +1,33 @@
+/**
+ * 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.rest.swagger;
+
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "pets")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Pets {
+
+    @XmlElement(name = "Pet")
+    public List<Pet> pets;
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c0f89d60/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
----------------------------------------------------------------------
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
index c69649c..f8f002d 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
@@ -51,6 +51,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
 import static com.github.tomakehurst.wiremock.client.WireMock.post;
 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
 
 @RunWith(Parameterized.class)
@@ -98,6 +99,20 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
             equalTo("application/xml, application/json")));
     }
 
+    @Test
+    public void shouldBeGettingPetsByStatus() {
+        final Pets pets = template.requestBodyAndHeader("direct:findPetsByStatus", NO_BODY,
"status", "available",
+            Pets.class);
+
+        assertNotNull(pets);
+        assertNotNull(pets.pets);
+        assertEquals(2, pets.pets.size());
+
+        petstore.verify(
+            getRequestedFor(urlPathEqualTo("/v2/pet/findByStatus")).withQueryParam("status",
equalTo("available"))
+                .withHeader("Accept", equalTo("application/xml, application/json")));
+    }
+
     @Override
     protected CamelContext createCamelContext() throws Exception {
         final CamelContext camelContext = super.createCamelContext();
@@ -116,14 +131,17 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                final JAXBContext jaxbContext = JAXBContext.newInstance(Pet.class);
+                final JAXBContext jaxbContext = JAXBContext.newInstance(Pet.class, Pets.class);
 
                 final JaxbDataFormat jaxb = new JaxbDataFormat(jaxbContext);
+
                 jaxb.setJaxbProviderProperties(Collections.singletonMap(Marshaller.JAXB_FORMATTED_OUTPUT,
false));
 
                 from("direct:getPetById").to("petStore:getPetById").unmarshal(jaxb);
 
                 from("direct:addPet").marshal(jaxb).to("petStore:addPet").unmarshal(jaxb);
+
+                from("direct:findPetsByStatus").to("petStore:findPetsByStatus").unmarshal(jaxb);
             }
         };
     }
@@ -147,6 +165,10 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
         petstore.stubFor(
             get(urlEqualTo("/v2/pet/14")).willReturn(aResponse().withStatus(HttpURLConnection.HTTP_OK).withBody(
                 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Pet><id>14</id><name>Olafur
Eliason Arnalds</name></Pet>")));
+
+        petstore.stubFor(get(urlPathEqualTo("/v2/pet/findByStatus")).withQueryParam("status",
equalTo("available"))
+            .willReturn(aResponse().withStatus(HttpURLConnection.HTTP_OK).withBody(
+                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><pets><Pet><id>1</id><name>Olafur
Eliason Arnalds</name></Pet><Pet><name>Jean-Luc Picard</name></Pet></pets>")));
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c0f89d60/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
----------------------------------------------------------------------
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
index 31b74ed..e0ea5b4 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
@@ -24,6 +24,7 @@ import java.util.Collections;
 import io.swagger.models.Operation;
 import io.swagger.models.Scheme;
 import io.swagger.models.Swagger;
+import io.swagger.models.parameters.QueryParameter;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
@@ -76,6 +77,15 @@ public class RestSwaggerEndpointTest {
     }
 
     @Test
+    public void shouldCreateQueryParameterExpressions() {
+        assertThat(RestSwaggerEndpoint.queryParameterExpression(new QueryParameter())).isEmpty();
+        assertThat(RestSwaggerEndpoint.queryParameterExpression(new QueryParameter().name("q").required(true)))
+            .isEqualTo("q={q}");
+        assertThat(RestSwaggerEndpoint.queryParameterExpression(new QueryParameter().name("q").required(false)))
+            .isEqualTo("q={q?}");
+    }
+
+    @Test
     public void shouldDetermineBasePath() {
         final RestConfiguration restConfiguration = new RestConfiguration();
 
@@ -155,6 +165,18 @@ public class RestSwaggerEndpointTest {
         assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(
             entry("host", "http://petstore.swagger.io"), entry("componentName", "zyx"),
             entry("consumes", "application/json"), entry("produces", "application/atom+xml"));
+
+        operation.addParameter(new QueryParameter().name("q").required(true));
+        assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(
+            entry("host", "http://petstore.swagger.io"), entry("componentName", "zyx"),
+            entry("consumes", "application/json"), entry("produces", "application/atom+xml"),
+            entry("queryParameters", "q={q}"));
+
+        operation.addParameter(new QueryParameter().name("o"));
+        assertThat(endpoint.determineEndpointParameters(swagger, operation)).containsOnly(
+            entry("host", "http://petstore.swagger.io"), entry("componentName", "zyx"),
+            entry("consumes", "application/json"), entry("produces", "application/atom+xml"),
+            entry("queryParameters", "q={q}&o={o?}"));
     }
 
     @Test


Mime
View raw message