Repository: camel Updated Branches: refs/heads/master a012502d2 -> 8e5dbe513
CAMEL-9705: rest-dsl should detect duplicate verb:uri paths and fail. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8e5dbe51 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8e5dbe51 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8e5dbe51 Branch: refs/heads/master Commit: 8e5dbe5136766ff1f6aa497c41a4696b5ddefaf4 Parents: a012502 Author: Claus Ibsen <[email protected]> Authored: Mon Mar 14 15:44:38 2016 +0100 Committer: Claus Ibsen <[email protected]> Committed: Mon Mar 14 15:44:38 2016 +0100 ---------------------------------------------------------------------- .../apache/camel/model/rest/RestDefinition.java | 18 +++++ .../component/rest/FromRestDuplicateTest.java | 80 ++++++++++++++++++++ .../rest/SpringFromRestDuplicateTest.java | 49 ++++++++++++ .../rest/SpringFromRestDuplicateTest.xml | 47 ++++++++++++ 4 files changed, 194 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8e5dbe51/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java index ab8b746..cd815f4 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java @@ -19,8 +19,10 @@ package org.apache.camel.model.rest; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -547,6 +549,9 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> * REST DSL and turn those into regular Camel routes. */ public List<RouteDefinition> asRouteDefinition(CamelContext camelContext) { + // sanity check this rest definition do not have duplicates + validateUniquePaths(); + List<RouteDefinition> answer = new ArrayList<RouteDefinition>(); if (camelContext.getRestConfigurations().isEmpty()) { camelContext.getRestConfiguration(); @@ -557,6 +562,19 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition> return answer; } + protected void validateUniquePaths() { + Set<String> paths = new HashSet<String>(); + for (VerbDefinition verb : verbs) { + String path = verb.asVerb(); + if (verb.getUri() != null) { + path += ":" + verb.getUri(); + } + if (!paths.add(path)) { + throw new IllegalArgumentException("Duplicate verb detected in rest-dsl: " + path); + } + } + } + /** * Transforms the rest api configuration into a {@link org.apache.camel.model.RouteDefinition} which * Camel routing engine uses to service the rest api docs. http://git-wip-us.apache.org/repos/asf/camel/blob/8e5dbe51/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java new file mode 100644 index 0000000..d9f22cf --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java @@ -0,0 +1,80 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +public class FromRestDuplicateTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("dummy-rest", new DummyRestConsumerFactory()); + return jndi; + } + + public void testDuplicateGet() throws Exception { + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().host("localhost"); + + rest("/users") + .get("{id}").to("log:foo") + .post().to("log:foo") + .get("").to("log:foo") + .get("{id}").to("log:foo"); + + } + }); + fail("Should throw exception"); + } catch (IllegalArgumentException e) { + assertEquals("Duplicate verb detected in rest-dsl: get:{id}", e.getMessage()); + } + } + + public void testDuplicatePost() throws Exception { + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().host("localhost"); + + rest("/users") + .get("{id}").to("log:foo") + .post().to("log:foo") + .get("").to("log:foo") + .put().to("log:foo") + .post().to("log:foo"); + + } + }); + fail("Should throw exception"); + } catch (IllegalArgumentException e) { + assertEquals("Duplicate verb detected in rest-dsl: post", e.getMessage()); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8e5dbe51/components/camel-spring/src/test/java/org/apache/camel/component/rest/SpringFromRestDuplicateTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/rest/SpringFromRestDuplicateTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/rest/SpringFromRestDuplicateTest.java new file mode 100644 index 0000000..fc6b70b --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/rest/SpringFromRestDuplicateTest.java @@ -0,0 +1,49 @@ +/** + * 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; + +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @version + */ +public class SpringFromRestDuplicateTest extends SpringTestSupport { + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + try { + new ClassPathXmlApplicationContext("org/apache/camel/component/rest/SpringFromRestDuplicateTest.xml"); + fail("Should throw exception"); + } catch (Exception e) { + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + assertEquals("Duplicate verb detected in rest-dsl: get:{id}", iae.getMessage()); + } + return null; + } + + @Override + protected void setUp() throws Exception { + // must override as there is no valid spring xml file + createApplicationContext(); + } + + public void testDuplicate() throws Exception { + // noop + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/8e5dbe51/components/camel-spring/src/test/resources/org/apache/camel/component/rest/SpringFromRestDuplicateTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/rest/SpringFromRestDuplicateTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/rest/SpringFromRestDuplicateTest.xml new file mode 100644 index 0000000..3893464 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/rest/SpringFromRestDuplicateTest.xml @@ -0,0 +1,47 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <!-- use a dummy rest consumer factory for the rest engine --> + <bean id="dummy-rest" class="org.apache.camel.component.rest.DummyRestConsumerFactory"/> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + + <rest path="/users"> + <get uri="{id}"> + <to uri="log:foo"/> + </get> + <post> + <to uri="log:foo"/> + </post> + <get> + <to uri="log:foo"/> + </get> + <get uri="{id}"> + <to uri="log:foo"/> + </get> + </rest> + + </camelContext> + +</beans>
