You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Zemin Hu <ze...@hotmail.com> on 2013/01/23 22:35:37 UTC

HTTP Endpoint construction in CamelSpringTest

I am trying to use CamelSpringTestSupport to build my unit tests. Most of my
routes are in Spring XML format. Here is my first test case:
public class SimpeRouteTest extends CamelSpringTestSupport {
	protected AbstractXmlApplicationContext createApplicationContext() {
		return new
FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
	}

	@Test
	public void testSimpleRoute() throws Exception {
		String response =
template.requestBodyAndHeader("http://localhost:8080/services/test/simple",
"body: Hello World", "MY_HEADER", "my name", String.class);
		System.err.println(response);
	}
}

The Spring XML route:
<routeContext id="test-route-simple"
xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="servlet:///test/simple" />
        <setBody>
        	<constant>hello world</constant>
        </setBody>
    </route>
</routeContext>

I can see the log that route is started during(before) test run:
[                          main] SpringCamelContext             INFO  Route:
route90 started and consuming from: Endpoint[servlet:///test/simple]

if I use "servlet:///test/simple", I got:
You cannot create producer with servlet endpoint, please consider to use
http or http4 endpoint.

if I use "http://localhost:8080/services/test/simple", or
"http://localhost:80/test/simple" or other versions, I got:
I/O exception (java.net.ConnectException) caught when processing request:
Connection refused: connect
I read Testing with Camel in CamelInAction book, it used "file://" which is
working, but there are no samples for "servlet:///" which is most commonly
used.

What's is correct way for the endpoint? How to test "servlet:///" or "http:"
component without having to start routes in a web server?

I am using camel-core 2.9.1, camel-test 2.9.1.
Thanks.



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: HTTP Endpoint construction in CamelSpringTest

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Jan 24, 2013 at 4:56 PM, Zemin Hu <ze...@hotmail.com> wrote:
> You are right. I was trying too hard to use CamelSpringTestSupport, thinking
> it might be simpler to start my route and to test it together. I am wrong.
> This CamelSpringTestSupport can do other tests, say file in Ibsen's book,
> but not for servlet.
> I am going to quit using CamelSpringTestSupport, just go straight with
> HttpUnit or HttpClient to do this kind of test. Besides, I also found it's
> hard to specify request method "GET/POST/PUT/DELETE" with ProducerTemplate.
>

The HTTP components of Camel often has a header that can control to
use GET / POST / etc.
So for that you need to send both body + headers with the producer template.



>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090p5726170.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: HTTP Endpoint construction in CamelSpringTest

Posted by Zemin Hu <ze...@hotmail.com>.
You are right. I was trying too hard to use CamelSpringTestSupport, thinking
it might be simpler to start my route and to test it together. I am wrong.
This CamelSpringTestSupport can do other tests, say file in Ibsen's book,
but not for servlet.
I am going to quit using CamelSpringTestSupport, just go straight with
HttpUnit or HttpClient to do this kind of test. Besides, I also found it's
hard to specify request method "GET/POST/PUT/DELETE" with ProducerTemplate. 




--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090p5726170.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: HTTP Endpoint construction in CamelSpringTest

Posted by Willem jiang <wi...@gmail.com>.
Hi,

What does your routes look like?
Do you need to simulate the Server behavior to the client (which could be a camel route) to test?

If you want to use the HttpUnit, you have to use the HttpUnit client API to invoke the service.
You can also try to start a embed Jetty server which can hold the reference of camel servlet, and create the route builder with Java code.


--  
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem




On Thursday, January 24, 2013 at 10:37 PM, Zemin Hu wrote:

> Actually I have been considering using jetty, I just have not tried out yet.
> Let me know if you have some ready samples in hand for this purpose. I would
> imagine if I use jetty, I have to put "jetty:" in front of all URIs, then
> remove them after unit test, if you want to do test again, you have to
> repeat these steps. Can you think of some other elegant way to do it? And
> another thought: since I can see all the routes actually are started by
> SpringCamelContext when I ran this test, I can find them from the log:
> SpringCamelContext INFO Route: route90 started and consuming
> from: Endpoint[servlet:///test/simple]  
> Where does SpringCamelContext keep them? are the routes in memory? are they
> accessible from some APIs if they are not published as web services?
>  
> I looked at the unit test code for camel-servlet that you pointed out, they
> use something from com.meterware.httpunit to start another servlet
> environment, I already have jetty configured in my environment as plug-in,
> so I don't want to do that. Another reason is that all tests are built
> around Java DSL based routes, no Spring XML DSL route tests. I don't want to
> dig that deep.
> Thanks.
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090p5726165.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).




Re: HTTP Endpoint construction in CamelSpringTest

Posted by Zemin Hu <ze...@hotmail.com>.
Actually I have been considering using jetty, I just have not tried out yet.
Let me know if you have some ready samples in hand for this purpose. I would
imagine if I use jetty, I have to put "jetty:" in front of all URIs, then
remove them after unit test, if you want to do test again, you have to
repeat these steps. Can you think of some other elegant way to do it? And
another thought: since I can see all the routes actually are started by
SpringCamelContext when I ran this test, I can find them from the log:
SpringCamelContext             INFO  Route: route90 started and consuming
from: Endpoint[servlet:///test/simple] 
Where does SpringCamelContext keep them? are the routes in memory? are they
accessible from some APIs if they are not published as web services?

I looked at the unit test code for camel-servlet that you pointed out, they
use something from com.meterware.httpunit to start another servlet
environment, I already have jetty configured in my environment as plug-in,
so I don't want to do that. Another reason is that all tests are built
around Java DSL based routes, no Spring XML DSL route tests. I don't want to
dig that deep.
Thanks.



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090p5726165.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: HTTP Endpoint construction in CamelSpringTest

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

The "servlet" component requires you to run inside a Servlet
container, such as Apache Tomcat, Jetty,
and ESB containers such as ServiceMix etc.

When you run it standalone as unit test, then there is no servlet
container, and you cannot use that.

You can swap the servlet with jetty instead. Then you use Jetty as the
http server for testing.
Or you can look at some of the 3rd party libraries that can simulate a
servlet environment.
In fact I think we do that in camel-servlet source code for testing itself.

https://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/


On Wed, Jan 23, 2013 at 10:35 PM, Zemin Hu <ze...@hotmail.com> wrote:
> I am trying to use CamelSpringTestSupport to build my unit tests. Most of my
> routes are in Spring XML format. Here is my first test case:
> public class SimpeRouteTest extends CamelSpringTestSupport {
>         protected AbstractXmlApplicationContext createApplicationContext() {
>                 return new
> FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
>         }
>
>         @Test
>         public void testSimpleRoute() throws Exception {
>                 String response =
> template.requestBodyAndHeader("http://localhost:8080/services/test/simple",
> "body: Hello World", "MY_HEADER", "my name", String.class);
>                 System.err.println(response);
>         }
> }
>
> The Spring XML route:
> <routeContext id="test-route-simple"
> xmlns="http://camel.apache.org/schema/spring">
>     <route>
>         <from uri="servlet:///test/simple" />
>         <setBody>
>                 <constant>hello world</constant>
>         </setBody>
>     </route>
> </routeContext>
>
> I can see the log that route is started during(before) test run:
> [                          main] SpringCamelContext             INFO  Route:
> route90 started and consuming from: Endpoint[servlet:///test/simple]
>
> if I use "servlet:///test/simple", I got:
> You cannot create producer with servlet endpoint, please consider to use
> http or http4 endpoint.
>
> if I use "http://localhost:8080/services/test/simple", or
> "http://localhost:80/test/simple" or other versions, I got:
> I/O exception (java.net.ConnectException) caught when processing request:
> Connection refused: connect
> I read Testing with Camel in CamelInAction book, it used "file://" which is
> working, but there are no samples for "servlet:///" which is most commonly
> used.
>
> What's is correct way for the endpoint? How to test "servlet:///" or "http:"
> component without having to start routes in a web server?
>
> I am using camel-core 2.9.1, camel-test 2.9.1.
> Thanks.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen