You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/10/07 08:43:27 UTC
[camel] branch master updated: CAMEL-13122: Added unit tests.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new e63e40e CAMEL-13122: Added unit tests.
e63e40e is described below
commit e63e40eae543a05774d2e4b71a614c975cea3a4a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Oct 7 10:43:08 2019 +0200
CAMEL-13122: Added unit tests.
---
.../component/servlet/ServletChoiceBeanTest.java | 86 ++++++++++++++++++++++
.../ServletSetExchangePropertyBeanTest.java | 57 ++++++++++++++
2 files changed, 143 insertions(+)
diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletChoiceBeanTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletChoiceBeanTest.java
new file mode 100644
index 0000000..9d2217e
--- /dev/null
+++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletChoiceBeanTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.servlet;
+
+import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.HttpNotFoundException;
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.WebResponse;
+import com.meterware.servletunit.ServletUnitClient;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class ServletChoiceBeanTest extends ServletCamelRouterTestSupport {
+
+ @Test
+ public void testClient() throws Exception {
+ getMockEndpoint("mock:bean").expectedMessageCount(1);
+
+ WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/hello");
+ req.setParameter("id", "123");
+ ServletUnitClient client = newClient();
+ WebResponse response = client.getResponse(req);
+
+ assertEquals(200, response.getResponseCode());
+ assertEquals("The response message is wrong ", "Client is Donald Duck", response.getText());
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testNoClient() throws Exception {
+ getMockEndpoint("mock:bean").expectedMessageCount(1);
+
+ WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/hello");
+ ServletUnitClient client = newClient();
+ try {
+ client.getResponse(req);
+ fail("Should throw exception");
+ } catch (HttpNotFoundException e) {
+ assertEquals(404, e.getResponseCode());
+ }
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("servlet:/hello")
+ .bean(ServletChoiceBeanTest.class, "findClient(${header.id})")
+ .to("mock:bean")
+ .choice()
+ .when(simple("${body} == null"))
+ .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(404))
+ .otherwise()
+ .setBody(simple("Client is ${body}"))
+ .end();
+ }
+ };
+ }
+
+ public static String findClient(String id) throws Exception {
+ if ("123".equals(id)) {
+ return "Donald Duck";
+ }
+ return null;
+ }
+
+}
diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetExchangePropertyBeanTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetExchangePropertyBeanTest.java
new file mode 100644
index 0000000..926c0e6
--- /dev/null
+++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetExchangePropertyBeanTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.servlet;
+
+import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.WebResponse;
+import com.meterware.servletunit.ServletUnitClient;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class ServletSetExchangePropertyBeanTest extends ServletCamelRouterTestSupport {
+
+ @Test
+ public void testSetProperty() throws Exception {
+ WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/hello");
+ ServletUnitClient client = newClient();
+ WebResponse response = client.getResponse(req);
+
+ assertEquals(200, response.getResponseCode());
+ assertEquals("The response message is wrong ", "", response.getText());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ onException(Exception.class)
+ .handled(true);
+
+ from("servlet:/hello")
+ .setProperty("myProperty").method(ServletSetExchangePropertyBeanTest.class, "throwException");
+ }
+ };
+ }
+
+ public static void throwException() throws Exception {
+ throw new IllegalArgumentException("Forced");
+ }
+
+}