How to mock a processor

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

How to mock a processor

Olaf
Hello,

I'm trying to test a simple route which downloads emails and then split attachments, replace body and sends each attachment to an another email. Now, ReplaceBodyProcessor does some network calls and I'd like to mock it out and simulate its work, to avoid external calls. What is the right way to do it?

Thanks in advance!

        @Override
        public void configure() {
         
           from("imap://host?username=user&password=pass")
                .split(new SplitAttachmentsExpression())
                .process(new MailProcessor())
                .process(new ReplaceBodyProcessor())
                .choice()
                .when(header("attachmentName").regex("^.*(txt|TXT)$"))
                    .to("imap://host?username=user2&password=pass2")
                    .id("targetmail");
        }




Reply | Threaded
Open this post in threaded view
|

Re: How to mock a processor

ceposta
Take a look here:

http://camel.apache.org/mock.html

and the "Mocking existing endpoint" section




On Thu, Oct 17, 2013 at 11:15 AM, Olaf <[hidden email]> wrote:

> Hello,
>
> I'm trying to test a simple route which downloads emails and then split
> attachments, replace body and sends each attachment to an another email.
> Now, ReplaceBodyProcessor does some network calls and I'd like to mock it
> out and simulate its work, to avoid external calls. What is the right way
> to
> do it?
>
> Thanks in advance!
>
>         @Override
>         public void configure() {
>
>            from("imap://host?username=user&password=pass")
>                 .split(new SplitAttachmentsExpression())
>                 .process(new MailProcessor())
>                 .process(new ReplaceBodyProcessor())
>                 .choice()
>                 .when(header("attachmentName").regex("^.*(txt|TXT)$"))
>                     .to("imap://host?username=user2&password=pass2")
>                     .id("targetmail");
>         }
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-mock-a-processor-tp5741786.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta
Reply | Threaded
Open this post in threaded view
|

Re: How to mock a processor

contactreji
In reply to this post by Olaf
I shall give u a small advice on this .
Pls note. All components don't support attachment . Attachments in the Camel exchange might be lost while passing throughout some Camel components. So by rule of thumb,  make sure attachments are added to the exchange just before calling an email component.

Cheers
Reji

"Olaf [via Camel]" <[hidden email]> wrote:

>
>
>Hello,
>
>I'm trying to test a simple route which downloads emails and then split
>attachments, replace body and sends each attachment to an another email.
>Now, ReplaceBodyProcessor does some network calls and I'd like to mock it
>out and simulate its work, to avoid external calls. What is the right way to
>do it?
>
>Thanks in advance!
>
>        @Override
>        public void configure() {
>          
>           from("imap://host?username=user&password=pass")
>                .split(new SplitAttachmentsExpression())
>                .process(new MailProcessor())
>                .process(new ReplaceBodyProcessor())
>                .choice()
>                .when(header("attachmentName").regex("^.*(txt|TXT)$"))
>                    .to("imap://host?username=user2&password=pass2")
>                    .id("targetmail");
>        }
>
>
>
>
>
>
>
>
>_______________________________________________
>If you reply to this email, your message will be added to the discussion below:
>http://camel.465427.n5.nabble.com/How-to-mock-a-processor-tp5741786.html
>
>To unsubscribe from Camel - Users, visit
Reji Mathews Sr. Engineer - Middleware Integrations / SOA ( Open Source - Apache Camel & Jboss Fuse ESB | Mule ESB ) LinkedIn - http://in.linkedin.com/pub/reji-mathews/31/9a2/40a Twitter - reji_mathews
Reply | Threaded
Open this post in threaded view
|

Re: How to mock a processor

Olaf
In reply to this post by ceposta
Thanks! That helps a lot.

Somehow, I can only mock with weaveBeId method:

weaveById("ReplaceBodyProcessor").replace().to("mock:ReplaceBodyProcessor");

then it is ReplaceBodyProcessor is skipped in test.

If using
weaveByToString(".*ReplaceBodyProcessor.*").replace().to("mock:ReplaceBodyProcessor");

or public String isMockEndpoints() {return "*";}

my test fails. What am I doing wrong?
Reply | Threaded
Open this post in threaded view
|

Re: How to mock a processor

ceposta
It would be wise to give your nodes IDs so you can intercept them easier:

eg:

.process(new ReplaceBodyProcessor()).id("replaceBody")

I'm not even sure how it did work for you with

weaveById("ReplaceBodyProcessor") because camel uses a default id of
"processN" where N is an incrementing integer if no ID is defined.

weaveByToString should work as you have it...




On Thu, Oct 17, 2013 at 12:11 PM, Olaf <[hidden email]> wrote:

> Thanks! That helps a lot.
>
> Somehow, I can only mock with weaveBeId method:
>
>
> weaveById("ReplaceBodyProcessor").replace().to("mock:ReplaceBodyProcessor");
>
> then it is ReplaceBodyProcessor is skipped in test.
>
> If using
>
> weaveByToString(".*ReplaceBodyProcessor.*").replace().to("mock:ReplaceBodyProcessor");
>
> or public String isMockEndpoints() {return "*";}
>
> my test fails. What am I doing wrong?
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-mock-a-processor-tp5741786p5741793.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta
Reply | Threaded
Open this post in threaded view
|

Re: How to mock a processor

Olaf
I did add the id later, so it worked, but weaveByToString does not work. Anyway, I'm going to use ids