Mock request/response to/from external REST service - java

One of my application's methods uses javax.ws.rs.Client to POST a request with an XML payload to an external REST service and receive back an XML response. I then convert this response to JSON and return it to the method caller. How do I write a unit test that mocks out the external REST service, so I can focus on sending an XML payload, receiving back an XML response which is converted to JSON, so I can look for a specific property in the JSON to pass the test? I need to use Mockito testing framework.
I imagine the test method would need access to sample XML request and XML response strings (w/ hard coded values). If so, where would I store these strings which are a bit lengthy, particularly the response?

Related

How to make Spring Boot API accept a client's custom 'x-www-form-urlencoded' format for the nested objects parsing

The Spring boot API could not parse the x-www-form-urlencoded type request since the client posted the data differently than what Java supports.
The incoming request body looks like below:
DeliveryPostCode=TEST123000&EnableCOD=false&Basket%5B0%5D%5BId%5D=383293820&Basket%5B0%5D%5BCategory%5D=Smartphone
In which a Basket is an object which has Id, Category as fields.
Decoding the above URL gives -
DeliveryPostCode=TEST123000&EnableCOD=false&Basket[0][Id]=383293820&Basket[0][Category]=Smartphone
which does not work for Java
If we can get a request which looks like below, It works for Java -
DeliveryPostCode=TEST123000&EnableCOD=false&Basket[0].Id=383293820&Basket[0].Category=Smartphone
Since the client is something which we cannot afford to change. We have to do some manipulation in Java before Controller takes this
OR
a modify in middleware node js layer we have between client & server which acts as a proxy.
Please suggest.

Do we need to stub the other micro service in Spring cloud contract

#marcin
I am doing a pilot on implementing the spring cloud contract for Micro services which has around 50+ services talking to each other. I have few questions which I haven't found the answer precisely in your document.
The service which I am building has controller which processes and transforms my input payload to the desired output in json format. This json is used to build desired structure that should match the response in groovy (Our contract). However the controller, is sending json to another services with some URL as shown below.
request_url=http://localhost:8090/services/rest/transact/v2/pay/validate/0000118228/new response_body=null
Basically it is expecting the Response back from the other service by making use of this json and now response_body=null
My question is do I need to create a stub or mock the service? to make use of this response as an input to produce expected output from the response. Basically the microservice is expecting a ServiceResponse.
Another question is do we need to load in-memory data while doing the contract testing or do we need to just test the controller itself?
I don't really follow your description... "The service which I am building has controller which transforms my input payload sent from groovy and giving the desired output in json format" . Sent from which groovy? Groovy application? Can you explain that in more depth?
But I guess I can try to answer the question anyways...
My question is do I need to create a stub or mock the service? to make use of this response as input to produce expected output from the response. It is expecting a ServiceResponse.
If I understand correctly - service you mean a class not an application? If that's the case then, yes, in the controller I would inject a stubbed service.
Another question is do we need to load in-memory data while doing the contract testing or do we need to just test the controller itself?
That's connected with the previous answer. Your controller doesn't delegate work to any real implementation of a service, so no access to the DB takes place. If you check out the samples (https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer/src/test/java/com/example/BeerRestBase.java) you'll see that the base class has mocks injected to it and no real integration takes place
EDIT:
"The service which I am building has controller which transforms my input payload sent from groovy and giving the desired output in json format" is actually the description of what is done via the Spring Cloud Contract generated test. The next sentence was
However the controller, is sending json to another services with some URL as shown below.
In Contract testing, I don't care what your controller further on does. If it's in the controller where you send the request to some other application then you should wrap it in a service class. Then such a service you would mock out in your contract tests. What we care about in the Contract tests is whether we can communicate. Not whether the whole end to end functionality is working correctly.

XML input for a RESTful web service

I have an xml input file from the following URL,
http://maps.googleapis.com/maps/api/geocode/xml?address=new%20york%20&%20sensor=false;
i would like to read the latitude and longitude positions from the XML file and i intend to write a method in the RESTful web service which calculate the location with minimum distance from each other.
i am very new to web services but i am aware that i should write my logic within the GET method and the #consumes and #produces will be XML.
i need some help to understand how should i pass the XML to the web service.
Should i use JDOM to extract the value from the requires tag and pass it as a parameter to the web service ?
need some help !
HTTP GET method won't work here since you need to send some data in the payload. You can use POST method which allows you to send a message body.

Jersey client get sent data to String

I am building some JUnit tests for a REST client using Jersey, I therefore need to have a copy of the data sent to the server to run some tests in JUnit.
Currently my clients invokes:
Invocation invocation = serviceWebTarget.request(MediaType.APPLICATION_JSON).
buildPut(Entity.json((QARecord) valuesList.get(0)));
Response response = invocation.invoke();
In between the two calls the QARecord object is serialized to JSON and sent to the server but I cannot find a way to access it.
By debugging the code I found no variable in either invocation or response which contains the converted JSON text.
How can get the sent data into a String or a File for my JUnit test to check what has been sent?
As i understood you want to check what exactly client will sent to server as a request, am i right ?
If yes how exactly does your Unit test look like ?
For instance Jersey provides JerseyTest class which is base for testing of client code.
In few words such test will run special testcontainer which is able to execute your handlers inside.
By combining it with Mockito / or creating your own handlers by yourself, you can verify what is "captured" by them as a client request at the end of the test (when response is received by client). Among others it'll give you possibility to check not only what your client code is sending to server but also check behaviour of client by emulating various responses (successful or exceptional).
If you just want to get content of what client is really send to the server you can write jersey client filter and get body of request from there.
Filters and Interceptors

How to obtain data from a webservice in a JSF action method?

I'm trying to determine the correct API calls on FacesContext to do the following when processing a backing bean action:
Within the action method construct a URL of dynamic parameters
Send the constructed URL to service
Parse the returned service response parameters
Continue with action method processing based on reponse string from request.
Any suggestions on the highlevel API calls for steps 2 and 3 to send me in the right direction would be very appreicated. Note, the service I'm calling is external to this application in a blackbox. Instructions are: send URL in specified format, parse response to see what happened.
This problem is not specific to JSF in particular, so you'll find nothing in JSF API. The standard Java API offers java.net.URL or, which allows more fine grained control, java.net.URLConnection to fire HTTP requests and obtain the response as an InputStream which you can then freely parse the usual Java way.
InputStream response = new URL("http://google.com").openStream();
// ...
Depending on the content type of the response, there may be 3rd party API's which ease the parsing. For example, Google Gson if it's JSON or Jsoup if it's HTML/XML.

Categories

Resources