CAMEL-7354: Rest DSL. Integrate with camel-jetty.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/61a1a2ab Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/61a1a2ab Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/61a1a2ab Branch: refs/heads/master Commit: 61a1a2ab04657b104de4601fef869d416646e767 Parents: c5098c1 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Jul 28 14:04:46 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Jul 28 14:04:46 2014 +0200 ---------------------------------------------------------------------- .../RestJettyBindingModeAutoWithJsonTest.java | 59 +++++++++++++++ .../RestJettyBindingModeAutoWithXmlTest.java | 59 +++++++++++++++ .../rest/RestJettyBindingModeJsonTest.java | 75 +++++++++++++++++++ .../jetty/rest/RestJettyBindingModeXmlTest.java | 75 +++++++++++++++++++ .../jetty/rest/RestJettyPojoInOutTest.java | 2 +- .../rest/RestJettyPostJsonJaxbPojoTest.java | 61 +++++++++++++++ .../rest/RestJettyPostJsonPojoListTest.java | 67 +++++++++++++++++ .../jetty/rest/RestJettyPostJsonPojoTest.java | 61 +++++++++++++++ .../rest/RestJettyPostXmlJaxbPojoTest.java | 79 ++++++++++++++++++++ .../component/jetty/rest/UserJaxbPojo.java | 48 ++++++++++++ .../RestRestletBindingModeAutoWithJsonTest.java | 1 - .../RestRestletBindingModeAutoWithXmlTest.java | 1 - .../restlet/RestRestletBindingModeJsonTest.java | 1 - .../restlet/RestRestletBindingModeXmlTest.java | 1 - 14 files changed, 585 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithJsonTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithJsonTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithJsonTest.java new file mode 100644 index 0000000..c4fb469 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithJsonTest.java @@ -0,0 +1,59 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyBindingModeAutoWithJsonTest extends BaseJettyTest { + + @Test + public void testBindingMode() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserPojo.class); + + String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").consumes("application/json").type(UserPojo.class) + .to("mock:input"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithXmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithXmlTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithXmlTest.java new file mode 100644 index 0000000..2db5138 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeAutoWithXmlTest.java @@ -0,0 +1,59 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyBindingModeAutoWithXmlTest extends BaseJettyTest { + + @Test + public void testBindingMode() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserJaxbPojo.class); + + String body = "<user name=\"Donald Duck\" id=\"123\"></user>"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").consumes("application/xml").type(UserJaxbPojo.class) + .to("mock:input"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java new file mode 100644 index 0000000..103cdc4 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java @@ -0,0 +1,75 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyBindingModeJsonTest extends BaseJettyTest { + + @Test + public void testBindingMode() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserJaxbPojo.class); + + String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Test + public void testBindingModeWrong() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(0); + + // we bind to json, but send in xml, which is not possible + String body = "<user name=\"Donald Duck\" id=\"123\"></user>"; + try { + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + fail("Should have thrown exception"); + } catch (Exception e) { + // expected + } + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.json); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").type(UserJaxbPojo.class) + .to("mock:input"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java new file mode 100644 index 0000000..18ce442 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java @@ -0,0 +1,75 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyBindingModeXmlTest extends BaseJettyTest { + + @Test + public void testBindingMode() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserJaxbPojo.class); + + String body = "<user name=\"Donald Duck\" id=\"123\"></user>"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Test + public void testBindingModeWrong() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(0); + + // we bind to xml, but send in json, which is not possible + String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; + try { + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + fail("Should have thrown exception"); + } catch (Exception e) { + // expected + } + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.xml); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").type(UserJaxbPojo.class) + .to("mock:input"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPojoInOutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPojoInOutTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPojoInOutTest.java index b2df470..f41dcaa 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPojoInOutTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPojoInOutTest.java @@ -37,7 +37,7 @@ public class RestJettyPojoInOutTest extends BaseJettyTest { return new RouteBuilder() { @Override public void configure() throws Exception { - // configure to use restlet on localhost with the given port + // configure to use jetty on localhost with the given port // and enable auto binding mode restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonJaxbPojoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonJaxbPojoTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonJaxbPojoTest.java new file mode 100644 index 0000000..d71db72 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonJaxbPojoTest.java @@ -0,0 +1,61 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyPostJsonJaxbPojoTest extends BaseJettyTest { + + @Test + public void testPostJaxbPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserJaxbPojo.class); + + String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use jetty on localhost with the given port + // and enable auto binding mode + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").type(UserJaxbPojo.class) + .to("mock:input"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoListTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoListTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoListTest.java new file mode 100644 index 0000000..2817cda --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoListTest.java @@ -0,0 +1,67 @@ +/** + * 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.jetty.rest; + +import java.util.List; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyPostJsonPojoListTest extends BaseJettyTest { + + @Test + public void testPostPojoList() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + + String body = "[ {\"id\": 123, \"name\": \"Donald Duck\"}, {\"id\": 456, \"name\": \"John Doe\"} ]"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + List list = mock.getReceivedExchanges().get(0).getIn().getBody(List.class); + assertNotNull(list); + assertEquals(2, list.size()); + + UserPojo user = (UserPojo) list.get(0); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + user = (UserPojo) list.get(1); + assertEquals(456, user.getId()); + assertEquals("John Doe", user.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use jetty on localhost with the given port + // and enable auto binding mode + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").typeList(UserPojo.class) + .to("mock:input"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoTest.java new file mode 100644 index 0000000..51ea5ee --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostJsonPojoTest.java @@ -0,0 +1,61 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyPostJsonPojoTest extends BaseJettyTest { + + @Test + public void testPostPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserPojo.class); + + String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use jetty on localhost with the given port + // and enable auto binding mode + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").type(UserPojo.class) + .to("mock:input"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostXmlJaxbPojoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostXmlJaxbPojoTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostXmlJaxbPojoTest.java new file mode 100644 index 0000000..c070725 --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyPostXmlJaxbPojoTest.java @@ -0,0 +1,79 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyPostXmlJaxbPojoTest extends BaseJettyTest { + + @Test + public void testPostJaxbPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserJaxbPojo.class); + + String body = "<user name=\"Donald Duck\" id=\"123\"></user>"; + template.sendBodyAndHeader("http://localhost:" + getPort() + "/users/new", body, Exchange.CONTENT_TYPE, "text/xml"); + + assertMockEndpointsSatisfied(); + + UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class); + assertNotNull(user); + assertEquals(123, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Test + public void testPostJaxbPojoNoContentType() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:input"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(UserJaxbPojo.class); + + String body = "<user name=\"Donald Duck\" id=\"456\"></user>"; + template.sendBody("http://localhost:" + getPort() + "/users/new", body); + + assertMockEndpointsSatisfied(); + + UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class); + assertNotNull(user); + assertEquals(456, user.getId()); + assertEquals("Donald Duck", user.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use jetty on localhost with the given port + // and enable auto binding mode + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("new").type(UserJaxbPojo.class) + .to("mock:input"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/UserJaxbPojo.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/UserJaxbPojo.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/UserJaxbPojo.java new file mode 100644 index 0000000..cc7d85c --- /dev/null +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/UserJaxbPojo.java @@ -0,0 +1,48 @@ +/** + * 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.jetty.rest; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "user") +@XmlAccessorType(XmlAccessType.FIELD) +public class UserJaxbPojo { + + @XmlAttribute + private int id; + @XmlAttribute + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithJsonTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithJsonTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithJsonTest.java index 33427c6..4ddb615 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithJsonTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithJsonTest.java @@ -48,7 +48,6 @@ public class RestRestletBindingModeAutoWithJsonTest extends RestletTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - // binding mode is json only restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithXmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithXmlTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithXmlTest.java index 29779d5..00fa66a 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithXmlTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeAutoWithXmlTest.java @@ -48,7 +48,6 @@ public class RestRestletBindingModeAutoWithXmlTest extends RestletTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - // binding mode is json only restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java index f03c1b7..2be6098 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeJsonTest.java @@ -65,7 +65,6 @@ public class RestRestletBindingModeJsonTest extends RestletTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - // binding mode is json only restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.json); // use the rest DSL to define the rest services http://git-wip-us.apache.org/repos/asf/camel/blob/61a1a2ab/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java index a698ab8..02b03ca 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletBindingModeXmlTest.java @@ -65,7 +65,6 @@ public class RestRestletBindingModeXmlTest extends RestletTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - // binding mode is json only restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.xml); // use the rest DSL to define the rest services