You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Pavan <pa...@gmail.com> on 2012/09/10 09:58:41 UTC

Help on Mock for Async calls

Hi,

I am new to Camel.

I am trying to simulate the endpoint using MockEndPoints. The objective is
the mock the endpoint and verify the headers received.

Following is my code: -

import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.test.CamelTestSupport;

public class TestAsyncCalls extends CamelTestSupport {
	
	@Override
    public String isMockEndpoints() {
        return "ahc*";
    }
	
	public void testmakeAsyncNSApiLoginCall() {
		try {
			getMockEndpoint("mock:result").expectedHeaderReceived("username",
"admin123");
			DefaultExchange dfex = new DefaultExchange(context);
			dfex.getIn().setHeader("username", "admin");
			dfex.getIn().setHeader("password", "admin");
			template.asyncSend("ahc:<some uri>", dfex);
			Thread.sleep(5000);
			assertMockEndpointsSatisfied();
			System.out.println(context.getEndpoints());
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue("Failed to make async call to nsapi login", false);
		}
	}
}

I was expecting the "expectedHeaderReceived()" to fail, since the username
set in the exchange differs. But when I run it, it does not fail. What else
need I be doing to get it to work? Please help.




--
View this message in context: http://camel.465427.n5.nabble.com/Help-on-Mock-for-Async-calls-tp5719008.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help on Mock for Async calls

Posted by Willem jiang <wi...@gmail.com>.
Please check out the unit test[1] I just committed.

[1]http://svn.apache.org/viewvc?rev=1383296&view=rev 

-- 
Willem Jiang

FuseSource
Web: http://www.fusesource.com (http://www.fusesource.com/)
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang 
Weibo: willemjiang





On Tuesday, September 11, 2012 at 4:09 PM, Pavan wrote:

> Thanks for your reply. I got it to work. I modified my code to override the
> createRouteBuilder and used the AdviceWithRouteBuilder to mock the results.
> I am using Camel 2.9.3 jar's.
> 
> Here is my code: -
> 
> @Override
> protected RouteBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
> @Override
> public void configure() throws Exception {
> from("direct:start").to(nsApiAHCUri);
> }
> };
> }
> String nsApiAHCUri = "http://<webservice url>"
> public void testAsyncLoginCallFail() {
> System.out.println("wsUrl: " + nsApiAHCUri);
> try {
> 
> context.getRouteDefinitions().get(0).adviceWith(context, new
> AdviceWithRouteBuilder() {
> @Override
> public void configure() throws Exception {
> interceptSendToEndpoint(nsApiAHCUri) 
> .skipSendToOriginalEndpoint() 
> .to("mock:result"); 
> }
> });
> MockEndpoint mep = getMockEndpoint("mock:result");
> mep.expectedHeaderReceived("username", "admin");
> DefaultExchange dfex = new DefaultExchange(context);
> dfex.getIn().setHeader("username", "admin");
> dfex.getIn().setHeader("password", "admin");
> context.createProducerTemplate().asyncSend(nsApiAHCUri, dfex);
> mep.assertIsSatisfied();
> } catch (Exception e) {
> e.printStackTrace();
> Assert.assertTrue("Failed to make async call ", false);
> }
> }
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Help-on-Mock-for-Async-calls-tp5719008p5719088.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).




Re: Help on Mock for Async calls

Posted by Pavan <pa...@gmail.com>.
Thanks for your reply. I got it to work. I modified my code to override the
createRouteBuilder and used the AdviceWithRouteBuilder to mock the results.
I am using Camel 2.9.3 jar's.

Here is my code: -

@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		return new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("direct:start").to(nsApiAHCUri);
			}
		};
	}
        String nsApiAHCUri = "http://<webservice url>"
	public void testAsyncLoginCallFail() {
		System.out.println("wsUrl: " + nsApiAHCUri);
		try {
			
			 context.getRouteDefinitions().get(0).adviceWith(context, new
AdviceWithRouteBuilder() {
			        @Override
			        public void configure() throws Exception {
			        	 interceptSendToEndpoint(nsApiAHCUri) 
		                    .skipSendToOriginalEndpoint() 
		                    .to("mock:result"); 
			        }
			    });
			MockEndpoint mep = getMockEndpoint("mock:result");
			mep.expectedHeaderReceived("username", "admin");
			DefaultExchange dfex = new DefaultExchange(context);
		         dfex.getIn().setHeader("username", "admin");
		        dfex.getIn().setHeader("password", "admin");
		        context.createProducerTemplate().asyncSend(nsApiAHCUri, dfex);
			mep.assertIsSatisfied();
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue("Failed to make async call ", false);
		}
	}



--
View this message in context: http://camel.465427.n5.nabble.com/Help-on-Mock-for-Async-calls-tp5719008p5719088.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Help on Mock for Async calls

Posted by Willem jiang <wi...@gmail.com>.
Which version of camel are you using?
I just try the below test in the camel trunk the test is passed.


-- 
Willem Jiang

FuseSource
Web: http://www.fusesource.com (http://www.fusesource.com/)
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang 
Weibo: willemjiang




On Monday, September 10, 2012 at 3:58 PM, Pavan wrote:

> Hi,
> 
> I am new to Camel.
> 
> I am trying to simulate the endpoint using MockEndPoints. The objective is
> the mock the endpoint and verify the headers received.
> 
> Following is my code: -
> 
> import org.apache.camel.impl.DefaultExchange;
> import org.apache.camel.test.CamelTestSupport;
> 
> public class TestAsyncCalls extends CamelTestSupport {
> 
> @Override
> public String isMockEndpoints() {
> return "ahc*";
> }
> 
> public void testmakeAsyncNSApiLoginCall() {
> try {
> getMockEndpoint("mock:result").expectedHeaderReceived("username",
> "admin123");
> DefaultExchange dfex = new DefaultExchange(context);
> dfex.getIn().setHeader("username", "admin");
> dfex.getIn().setHeader("password", "admin");
> template.asyncSend("ahc:<some uri>", dfex);
> Thread.sleep(5000);
> assertMockEndpointsSatisfied();
> System.out.println(context.getEndpoints());
> } catch (Exception e) {
> e.printStackTrace();
> Assert.assertTrue("Failed to make async call to nsapi login", false);
> }
> }
> }
> 
> I was expecting the "expectedHeaderReceived()" to fail, since the username
> set in the exchange differs. But when I run it, it does not fail. What else
> need I be doing to get it to work? Please help.
> 
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Help-on-Mock-for-Async-calls-tp5719008.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).
>