Testing Restlet endpoints after refactoring to using servlet container

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing Restlet endpoints after refactoring to using servlet container

magnuspalmer
After following the "Using the Restlet servlet within a webapp"
documentation at http://camel.apache.org/restlet.html I refactored my
Restlet endpoints by removing protocol, host and port.
So from "restlet:<a href="http://host:port/path/">http://host:port/path/{id}" to "restlet:/path/{id}".
This works great when running in a servlet container.

However my JUnit tests no longer works since, the reason for this is that
when using the applicationContext the protocol and host is set to null and
port is defaulted to 80 in
the org.apache.camel.component.restlet.RestletComponent.createEndpoint(String
uri, String remaining, Map parameters).

The only resolution that I know will work is to manipulate the URI by using
"<a href="http://host:port">http://host:port" in test and "" otherwise.
Any ideas is highly appreciated otherwise I will have to satisfy with
manipulating the URIs.
(I am using both the real applicationContext and a test applicationContext
in my JUnit tests that extends CamelSpringTestSupport, and they have among
other things a Spring context:property-override definition where I can put
configuration).

Looking at the JUnit tests in Camel source all routes are configured using
"restlet:http://host" + portnum + "/path" for the camel-restlet component.


Kind regards, Magnus
Reply | Threaded
Open this post in threaded view
|

Re: Testing Restlet endpoints after refactoring to using servlet container

Claus Ibsen-2
Hi

Maybe if its/was possible to setup the host and port on the
RestletComponent, then the endpoint can fallback and use those options
if non specified in the uri. Then for production they can be / to
indicate servlet, and for unit test, you can set it up to a localhost
8080 or whatever.



On Fri, Jul 13, 2012 at 2:50 PM, Magnus Palmér
<[hidden email]> wrote:

> After following the "Using the Restlet servlet within a webapp"
> documentation at http://camel.apache.org/restlet.html I refactored my
> Restlet endpoints by removing protocol, host and port.
> So from "restlet:<a href="http://host:port/path/">http://host:port/path/{id}" to "restlet:/path/{id}".
> This works great when running in a servlet container.
>
> However my JUnit tests no longer works since, the reason for this is that
> when using the applicationContext the protocol and host is set to null and
> port is defaulted to 80 in
> the org.apache.camel.component.restlet.RestletComponent.createEndpoint(String
> uri, String remaining, Map parameters).
>
> The only resolution that I know will work is to manipulate the URI by using
> "<a href="http://host:port">http://host:port" in test and "" otherwise.
> Any ideas is highly appreciated otherwise I will have to satisfy with
> manipulating the URIs.
> (I am using both the real applicationContext and a test applicationContext
> in my JUnit tests that extends CamelSpringTestSupport, and they have among
> other things a Spring context:property-override definition where I can put
> configuration).
>
> Looking at the JUnit tests in Camel source all routes are configured using
> "restlet:http://host" + portnum + "/path" for the camel-restlet component.
>
>
> Kind regards, Magnus



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Reply | Threaded
Open this post in threaded view
|

Re: Testing Restlet endpoints after refactoring to using servlet container

magnuspalmer
Hi,

Yes, I was thinking about that as well.
But since I only need it when I run unit tests I decided to mock the
URI.getHost, URI.getPort and URI.getScheme instead using PowerMock and
Mockito.

import static
org.powermock.api.support.membermodification.MemberMatcher.method;
import static
org.powermock.api.support.membermodification.MemberModifier.stub;
..
import org.powermock.modules.junit4.rule.PowerMockRule;
..
@PrepareForTest({ URI.class, RestletComponent.class })
public class MonitoringRoutesTest extends CamelSpringTestSupport {
..
  @Rule
  public PowerMockRule rule = new PowerMockRule();
..
  @Override
  public void doPreSetup() throws Exception {
    super.doPreSetup();

    stub(method(URI.class, "getScheme")).toReturn("http");
    stub(method(URI.class, "getHost")).toReturn("localhost");
    stub(method(URI.class, "getPort")).toReturn(8081);
  }
..
  @Test
  public void testListEgs() throws Exception {
    String response = (String) template.requestBodyAndHeader(
      "ahc:http://localhost:8081/admin/executiongroup", null,
      Exchange.HTTP_METHOD, HttpMethods.GET, String.class);
..

Works like a charm.

2012/7/14 Claus Ibsen <[hidden email]>

> Hi
>
> Maybe if its/was possible to setup the host and port on the
> RestletComponent, then the endpoint can fallback and use those options
> if non specified in the uri. Then for production they can be / to
> indicate servlet, and for unit test, you can set it up to a localhost
> 8080 or whatever.
>
>
>
> On Fri, Jul 13, 2012 at 2:50 PM, Magnus Palmér
> <[hidden email]> wrote:
> > After following the "Using the Restlet servlet within a webapp"
> > documentation at http://camel.apache.org/restlet.html I refactored my
> > Restlet endpoints by removing protocol, host and port.
> > So from "restlet:<a href="http://host:port/path/">http://host:port/path/{id}" to "restlet:/path/{id}".
> > This works great when running in a servlet container.
> >
> > However my JUnit tests no longer works since, the reason for this is that
> > when using the applicationContext the protocol and host is set to null
> and
> > port is defaulted to 80 in
> > the
> org.apache.camel.component.restlet.RestletComponent.createEndpoint(String
> > uri, String remaining, Map parameters).
> >
> > The only resolution that I know will work is to manipulate the URI by
> using
> > "<a href="http://host:port">http://host:port" in test and "" otherwise.
> > Any ideas is highly appreciated otherwise I will have to satisfy with
> > manipulating the URIs.
> > (I am using both the real applicationContext and a test
> applicationContext
> > in my JUnit tests that extends CamelSpringTestSupport, and they have
> among
> > other things a Spring context:property-override definition where I can
> put
> > configuration).
> >
> > Looking at the JUnit tests in Camel source all routes are configured
> using
> > "restlet:http://host" + portnum + "/path" for the camel-restlet
> component.
> >
> >
> > Kind regards, Magnus
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: [hidden email]
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>
Reply | Threaded
Open this post in threaded view
|

Re: Testing Restlet endpoints after refactoring to using servlet container

zabin
This post was updated on .
Just curious, why you need to stub the host and port.  I am sure you don't want to test what the rest call will return.  If so, then it will be a restlet test, not yours .
In order to test your route builder which is calling the restlet, you can mock the restlet uri by using MockEndPoint.  That is simpler than stubbing.

MockEndpoint uri = getMockEndpoint("mock:restletUri");
context.addEndpoint("restlet://http://localhost:8080/whatever", uri);

Cheers
Zabin