You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Deepak kumar <dk...@gmail.com> on 2016/05/16 06:53:57 UTC

mock http endpoint and retrieving value form properties file

I have route in my camel context like this:-

<camel:route id="xxx">
			<camel:from uri="direct:test"></camel:from>
			<camel:to uri="bean:testProcessor"></camel:to>
			<camel:to	uri="{{testUrl}}"></camel:to>
			<camel:setHeader headerName="fromEndPoint">
			   <camel:simple>routename</camel:simple>
			</camel:setHeader>
			<camel:to uri="bean:responseHandler"></camel:to>
			
		</camel:route>

How can i test the above route ? When i am extending my test class from
CamelSpringTestSupport then i am not able to fetch the {{testUrl}} value
from the properties file and then how can emulate the behavior of http
service.



--
View this message in context: http://camel.465427.n5.nabble.com/mock-http-endpoint-and-retrieving-value-form-properties-file-tp5782604.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: mock http endpoint and retrieving value form properties file

Posted by Quinn Stevenson <qu...@pronoia-solutions.com>.
Deepak -

Where do you define the bean so Camel can find it?

Normally, I’d put the bean in another Spring XML file (in src/test/resources/META-INF/spring for symmetry), and then change the application context such that it will load both Spring XML files.

protected AbstractApplicationContext createApplicationContext() {
    return new ClassPathXmlApplicationContext("classpath:META-INF/spring/camel-context.xml","classpath:META-INF/spring/test-beans.xml");
}


> On May 18, 2016, at 12:13 AM, Deepak kumar <dk...@gmail.com> wrote:
> 
> Hi Quinn,
> My test class looks like this. When i am trying to run i am getting error
> message like- No bean "testProcessor" found in the registry.
> 
> public class FirstTest extends CamelSpringTestSupport{
> 
> 	@Override
> 	protected AbstractApplicationContext createApplicationContext() {
> 		return new ClassPathXmlApplicationContext("camel-context.xml");
> 	}
> 
> 	@Override
>    public String isMockEndpoints() {
>        return "*";
>    }
> 
> 	@Override
> 	public CamelContext createCamelContext() {
> 	CamelContext context = new DefaultCamelContext();
> 	ActiveMQConnectionFactory cf = new
> ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
> 	ActiveMQConfiguration config=new ActiveMQConfiguration();
> 	config.setConnectionFactory(cf);
> 
> 	ActiveMQComponent activeMQComponent = new ActiveMQComponent();
> 	activeMQComponent.setConfiguration(config);
> 	context.addComponent("jms", activeMQComponent);
> 	TestProcessor dp=new TestProcessor();
> 	return context;
> 	}
> 
> 
> 	@Test
> 	public void test() throws InterruptedException{
> 		MockEndpoint mock = getMockEndpoint("mock:bean:testProcessor");
> 
> 		HashMap header=new HashMap<>();
> 		header.put("token","ABCD");
> 		template.setDefaultEndpointUri("bean:testPreProcessor");
> 		template.sendBodyAndHeader("direct:test","Hello World",header);
> 		assertMockEndpointsSatisfied();
> 
> 	}
> 
> }
> 
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/mock-http-endpoint-and-retrieving-value-form-properties-file-tp5782604p5782727.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: mock http endpoint and retrieving value form properties file

Posted by Deepak kumar <dk...@gmail.com>.
Hi Quinn,
My test class looks like this. When i am trying to run i am getting error
message like- No bean "testProcessor" found in the registry.

public class FirstTest extends CamelSpringTestSupport{

	@Override
	protected AbstractApplicationContext createApplicationContext() {
		return new ClassPathXmlApplicationContext("camel-context.xml");
	}

	@Override
    public String isMockEndpoints() {
        return "*";
    }

	@Override
	public CamelContext createCamelContext() {
	CamelContext context = new DefaultCamelContext();
	ActiveMQConnectionFactory cf = new
ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
	ActiveMQConfiguration config=new ActiveMQConfiguration();
	config.setConnectionFactory(cf);

	ActiveMQComponent activeMQComponent = new ActiveMQComponent();
	activeMQComponent.setConfiguration(config);
	context.addComponent("jms", activeMQComponent);
	TestProcessor dp=new TestProcessor();
	return context;
	}


	@Test
	public void test() throws InterruptedException{
		MockEndpoint mock = getMockEndpoint("mock:bean:testProcessor");

		HashMap header=new HashMap<>();
		header.put("token","ABCD");
		template.setDefaultEndpointUri("bean:testPreProcessor");
		template.sendBodyAndHeader("direct:test","Hello World",header);
		assertMockEndpointsSatisfied();

	}

}




--
View this message in context: http://camel.465427.n5.nabble.com/mock-http-endpoint-and-retrieving-value-form-properties-file-tp5782604p5782727.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: mock http endpoint and retrieving value form properties file

Posted by Quinn Stevenson <qu...@pronoia-solutions.com>.
Deepak -

There are quite a few ways to go about this, but I think the easiest thing for you (given what you said so far), would be to write a bean the behaves the way you want - set the response code header to what you (i.e. Exchange.HTTP_RESPONSE_CODE/CamelHttpResponseCode), throw and exception, whatever.  Then replace the testUrl with a call to your bean.

If you go this route, you won’t actually have to make an HTTP call (you’re basically stubbing it out).  This makes it much easier to test all kinds of error scenarios.

Hopefully that helps

> On May 16, 2016, at 12:30 PM, Deepak kumar <dk...@gmail.com> wrote:
> 
> Hi Quinn ,
> I got it how to get the value from properties file. But how can i test the
> http endpoint here.and that to based on different http response status? if
> the status will return 404 then i will throw custom exception. how can i
> test these scenarios?
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/mock-http-endpoint-and-retrieving-value-form-properties-file-tp5782604p5782674.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


Re: mock http endpoint and retrieving value form properties file

Posted by Deepak kumar <dk...@gmail.com>.
Hi Quinn ,
I got it how to get the value from properties file. But how can i test the
http endpoint here.and that to based on different http response status? if
the status will return 404 then i will throw custom exception. how can i
test these scenarios?



--
View this message in context: http://camel.465427.n5.nabble.com/mock-http-endpoint-and-retrieving-value-form-properties-file-tp5782604p5782674.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: mock http endpoint and retrieving value form properties file

Posted by Quinn Stevenson <qu...@pronoia-solutions.com>.
Since you’re replacing to to URI, you could create another properties file with a different test value in src/test/resources.  Then you could add the a test route for that URI in the SpringCamelTestSupport by implementing the createRouteBuilder method.

> On May 16, 2016, at 12:53 AM, Deepak kumar <dk...@gmail.com> wrote:
> 
> I have route in my camel context like this:-
> 
> <camel:route id="xxx">
> 			<camel:from uri="direct:test"></camel:from>
> 			<camel:to uri="bean:testProcessor"></camel:to>
> 			<camel:to	uri="{{testUrl}}"></camel:to>
> 			<camel:setHeader headerName="fromEndPoint">
> 			   <camel:simple>routename</camel:simple>
> 			</camel:setHeader>
> 			<camel:to uri="bean:responseHandler"></camel:to>
> 			
> 		</camel:route>
> 
> How can i test the above route ? When i am extending my test class from
> CamelSpringTestSupport then i am not able to fetch the {{testUrl}} value
> from the properties file and then how can emulate the behavior of http
> service.
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/mock-http-endpoint-and-retrieving-value-form-properties-file-tp5782604.html
> Sent from the Camel - Users mailing list archive at Nabble.com.