Simple jUnit test is not making REST login call, it works normally perfectly.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple jUnit test is not making REST login call, it works normally perfectly.

Michael Joyner
Hi everyone,

I am just learning camel testing and my first attempt is simple. I just want to login and for fun, return the HTTP JSON response.
For some reason it's not logging in. It has the POST body for the login that is sent to from this:
<setBody id="setBody2">
<simple>resource:classpath:templates/loginrequest.txt</simple>
</setBody>

Here is the Login Route:
It's simple, set headers, then REST call, then unmarshal to JSON.

<route customId="true" id="loginRoute">
<description>Login to SugarCRM and return the result of the login.</description>
<from id="_from2" uri="direct:login"/>
<setBody id="setBody2">
<simple>resource:classpath:templates/loginrequest.txt</simple>
</setBody>
<setHeader headerName="Content-Type" id="setHeader6">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="Accept" id="setHeader7">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="CamelHttpMethod" id="setHeader8">
<constant>POST</constant>
</setHeader>
<setHeader headerName="Host" id="setHeader9">
<constant>crm.fabricut.com</constant>
</setHeader>
<to id="to1" uri="{{baseUrl}}/rest/v10/oauth2/token?platform=base&amp;throwExceptionOnFailure=false"/>
<unmarshal id="unmarshal2">
<json library="Jackson"/>
</unmarshal>
</route>

Here is the Unit (Integration) test :
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@DisableJmx(false)
public class AccountscheckerSugarCRMConnectionTest {

@Produce(uri = "direct:login")
ProducerTemplate directLogin;

@SuppressWarnings("rawtypes")
@Test
public void loginToSugarCRM() throws Exception {

Exchange result = directLogin.send((exchange) -> {
exchange.getIn().setBody("login");
});

System.out.println(result.getIn().getBody().toString()); // CAMEL_HTTP_STATUS
}
}

Reply | Threaded
Open this post in threaded view
|

Re: Simple jUnit test is not making REST login call, it works normally perfectly.

Claus Ibsen-2
Hi

Welcome to the Camel community.

What version of Camel do you use?
And what does the system out print?

And you can try without the unmarshal process to log the raw data from
the http response.

Mind that the output of HTTP may be streaming based and only readable
once. See this FAQ
http://camel.apache.org/why-is-my-message-body-empty.html

On Fri, Aug 10, 2018 at 12:47 AM, Michael Joyner
<[hidden email]> wrote:

> Hi everyone,
>
> I am just learning camel testing and my first attempt is simple. I just want to login and for fun, return the HTTP JSON response.
> For some reason it's not logging in. It has the POST body for the login that is sent to from this:
> <setBody id="setBody2">
> <simple>resource:classpath:templates/loginrequest.txt</simple>
> </setBody>
>
> Here is the Login Route:
> It's simple, set headers, then REST call, then unmarshal to JSON.
>
> <route customId="true" id="loginRoute">
> <description>Login to SugarCRM and return the result of the login.</description>
> <from id="_from2" uri="direct:login"/>
> <setBody id="setBody2">
> <simple>resource:classpath:templates/loginrequest.txt</simple>
> </setBody>
> <setHeader headerName="Content-Type" id="setHeader6">
> <constant>application/json</constant>
> </setHeader>
> <setHeader headerName="Accept" id="setHeader7">
> <constant>application/json</constant>
> </setHeader>
> <setHeader headerName="CamelHttpMethod" id="setHeader8">
> <constant>POST</constant>
> </setHeader>
> <setHeader headerName="Host" id="setHeader9">
> <constant>crm.fabricut.com</constant>
> </setHeader>
> <to id="to1" uri="{{baseUrl}}/rest/v10/oauth2/token?platform=base&amp;throwExceptionOnFailure=false"/>
> <unmarshal id="unmarshal2">
> <json library="Jackson"/>
> </unmarshal>
> </route>
>
> Here is the Unit (Integration) test :
> @RunWith(CamelSpringBootRunner.class)
> @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
> @DisableJmx(false)
> public class AccountscheckerSugarCRMConnectionTest {
>
> @Produce(uri = "direct:login")
> ProducerTemplate directLogin;
>
> @SuppressWarnings("rawtypes")
> @Test
> public void loginToSugarCRM() throws Exception {
>
> Exchange result = directLogin.send((exchange) -> {
> exchange.getIn().setBody("login");
> });
>
> System.out.println(result.getIn().getBody().toString()); // CAMEL_HTTP_STATUS
> }
> }
>



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Reply | Threaded
Open this post in threaded view
|

Re: Simple jUnit test is not making REST login call, it works normally perfectly.

Michael Joyner
Thank you Claus for the nice reply.

The "light bulb" went on and I figured out that the stream is used when going to the unmarshal component.
I did this:
<route customId="true" id="loginRoute" streamCache="true">
and all is fine!
On Aug 10 2018, at 12:00 am, Claus Ibsen <[hidden email]> wrote:

>
> Hi
> Welcome to the Camel community.
> What version of Camel do you use?
> And what does the system out print?
>
> And you can try without the unmarshal process to log the raw data from
> the http response.
>
> Mind that the output of HTTP may be streaming based and only readable
> once. See this FAQ
> http://camel.apache.org/why-is-my-message-body-empty.html
>
> On Fri, Aug 10, 2018 at 12:47 AM, Michael Joyner
> <[hidden email]> wrote:
> > Hi everyone,
> >
> > I am just learning camel testing and my first attempt is simple. I just want to login and for fun, return the HTTP JSON response.
> > For some reason it's not logging in. It has the POST body for the login that is sent to from this:
> > <setBody id="setBody2">
> > <simple>resource:classpath:templates/loginrequest.txt</simple>
> > </setBody>
> >
> > Here is the Login Route:
> > It's simple, set headers, then REST call, then unmarshal to JSON.
> >
> > <route customId="true" id="loginRoute">
> > <description>Login to SugarCRM and return the result of the login.</description>
> > <from id="_from2" uri="direct:login"/>
> > <setBody id="setBody2">
> > <simple>resource:classpath:templates/loginrequest.txt</simple>
> > </setBody>
> > <setHeader headerName="Content-Type" id="setHeader6">
> > <constant>application/json</constant>
> > </setHeader>
> > <setHeader headerName="Accept" id="setHeader7">
> > <constant>application/json</constant>
> > </setHeader>
> > <setHeader headerName="CamelHttpMethod" id="setHeader8">
> > <constant>POST</constant>
> > </setHeader>
> > <setHeader headerName="Host" id="setHeader9">
> > <constant>crm.fabricut.com</constant>
> > </setHeader>
> > <to id="to1" uri="{{baseUrl}}/rest/v10/oauth2/token?platform=base&amp;throwExceptionOnFailure=false"/>
> > <unmarshal id="unmarshal2">
> > <json library="Jackson"/>
> > </unmarshal>
> > </route>
> >
> > Here is the Unit (Integration) test :
> > @RunWith(CamelSpringBootRunner.class)
> > @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
> > @DisableJmx(false)
> > public class AccountscheckerSugarCRMConnectionTest {
> >
> > @Produce(uri = "direct:login")
> > ProducerTemplate directLogin;
> >
> > @SuppressWarnings("rawtypes")
> > @Test
> > public void loginToSugarCRM() throws Exception {
> >
> > Exchange result = directLogin.send((exchange) -> {
> > exchange.getIn().setBody("login");
> > });
> >
> > System.out.println(result.getIn().getBody().toString()); // CAMEL_HTTP_STATUS
> > }
> > }
> >
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>