I have a route setup that sends an xml document to a third party's web service that works fine, however it was Fire-and-Forget and now I need to change it to store the xml response and the http code.
An example response would be: <Success>TRUE</Success>
Below is an example of my route
from("activemq:test_queue")
.routeId("testRoute")
.bean( generateTestXMLBean )
.to( ExchangePattern.InOut, "http://www.example-webservice.com/xml" )
.process( storeResponseProcessor )
Is it possible to write a unit test that mocks a HttpResponse to be returned after the route sends it to a MockEndpoint so the response can be processed?
Also am I correct in thinking that the response would become the Out message on the exchange?
Any help is much appreciated :)
Yes, you can mock the HttpResponse. Normally I like to use another route to "simulate" the response. For example:
from("activemq:test_queue")
.routeId("testRoute")
.bean( generateTestXMLBean )
.to( ExchangePattern.InOut, "direct:response" )
.process( storeResponseProcessor )
from("direct:response")
.routeId("testResponse")
.setBody(simple("<Success>TRUE</Success>"))
Also, you can add a Mock Endpoint.
from("direct:response")
.routeId("testResponse")
.to("mock:response")
.setBody(simple("<Success>TRUE</Success>"))
And yes, you're right, setting the request with InOut exchange pattern Camel will set the content in the Out Message.
Related
I have to do a unit testing of my servlet by passing the wrong request headers and parameters and getting failure message printed in the console
I have used Mockito framework
I assume your servlet is not third-party but created by you. That means you shouldn't need the mockito for starters ( except of course if you want to mock everything after your servlet ) .
Second. Lets say your servlet accepts APPLICATION-JSON and you send STREAM-OCTET. Shouldn't that return ( response ) the appropriate http error? If you agree with that then asserting the response itself to see if it is the expected http status. ( for example 400 which is bad_request the appropriate response for wrong headers. )
If you want to "hide" the error by returning OK#200 for all the requests you can get a hold of the logger by either injection or reflection ( the first way is preferable ) and parse the output. Printing on the System.out/System.err would be a lot tricker to get a hold of but printing errors there is not a good practice either. But now, if you "hide" the mistake on the headers then your client would never be able to guess what went wrong!
I am using postman to get certain responses. Below is my response.
Here I have some other api request links integrated with this response. Is there any possibility that I can get values inside these apis also. Its like retrieve values forom both parent api request and child api request.
I know this is possible using a java code. But is there any other exsiting software that I can use for this?
In your case I would recommend combining multiple requests into a chain or even a workflow. The idea is to have the first request fetch href endpoints that get called in subsequent requests. For that, the initial request needs a post-request test script that reads the href values from the response and stores it in a environment or global variable.
Like so:
// persist project href for next request
pm.environment.set("projectUrlPath", pm.response.json().embedded.elements[0]._links.project.href);
Your next request in the line would than use this variable to build the request url. Like so:
http://www.example.com{{projectUrlPath}}
The key is to correctly navigate to the right attribute in the response json JavaScript object. This online tool might help you with that:
https://www.w3schools.com/js/tryit.asp?filename=tryjs_json_parse
I am exposing an endpoint e.g. example.com/transactions via Apigee. I am using a Proxy endpoint which routes to my Target endpoint.
If I hit xxx.apigee.net/transactions, i get back my entire payload just as if I hit example.com/transactions. So far so good.
However, if I want to view one specific transaction, going to xxx.apigee.net/transactions/1 does not work. How can I make Apigee understand it needs to pass /1 to the underlying proxy endpoint so that it returns the same as example.com/transactions/1?
How can I pass the {resource-path} variable?
The Apigee API gateway passes the value through already. There is no need for additional manipulation.
I wanted to know if there were any ways to have a POST call on my Play framework REST API without any JSON in the call.
I'm making the API with the logic that if you create anything in the database, then you need to make a POST call, but yet I have this issue that occurs right when it hits the API :
Bad Request
For request 'POST /promotions/add/user?userId=41375&promoId=24265'
[Invalid Json: No content to map due to end-of-input at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1#a5de52b; line: 1, column: 0]]
Is there a way or I have to change the call in GET or make otherwise with userId and promoId in a JSON, which I would like to avoid.
Thanks !
Please check if you have set any content-type in the request. Also check how the requests are handled within the action method like using request.body.as<XXX> etc.
If your POST request is not using the json as the data payload, you may use a form to be bound from the request. In that case, make sure that you have set the Content-Type: application/x-www-form-urlencoded in the request. Sometimes, it doesn't matter you set proper content type header in request. It all depends on how POST request is handled by the controller action methods/API endpoints.
Does JSON messages sent over HTTP in response to a URL request make it REST-compliant?
I believe it is not.
But I am not sure on the detailed reason.
If i have a well-organized website,which responds to URL requests with json representation payload - what does it need to do further to comply with RESTful or JAX-RS?
A simple concise explanation will be much appreciated
There is no restriction regarding the payload of messages in REST and using JSON format in HTTP responses isn't enough to make a service RESTful.
To make short (since it's what you asked for ;-)), what is really important in REST is to respect the HTTP operations (GET, POST, ...) are designed for, the concept of resources and their states (idempotence, ...), leverages headers and status codes, ...
The following link could give you hints about the way to implement a RESTful service / Web API:
Designing a Web API - https://templth.wordpress.com/2014/12/15/designing-a-web-api/
Hope it helps you,
Thierry
JSON is a payload and does not play any role in making your Webservice REST-complaint.
Payload could be XML, CSV, plain text etc etc.
The Webservice will be REST-Complaint when it's following REST protocol (set of rules, not network protocol).
There are up to 4 levels where you can make your REST webservice complaint to.
One of the very basic rules to understand is that - Your Request must not be RPC i.e. you MUST not perform any action using a Payload (Typical SOAP) or URL tunnelling e.g. http://www.example.com/product?id=1234&action=delete
In RESTful world you would define one top level URI for the above. e.g. http://www.example.com/product
and then you will call various URLs to perform other actions.
Such as:
POST - create Data
http://www.example.com/product
Body{ here your payload will describe the Product.}
Assuming you rely on server gennerated product id then return type could be Product Id. Which is again should be set as LOCATION parameter of the return header.
PUT - Update Data
http://www.example.com/product/1234
Body{ here your payload will contain the Product details to change.}
GET - Get Data
http://www.example.com/product/1234
DELETE - Delete Data
http://www.example.com/product/1234