Rest client framework for complex objects - java

I have developed one web service for order management. This web service takes many complex objects as input parameters. I used curl to test and it works fine. Now I am writing a client but having issue when for ArrayList (e.g. the items are coming as ArrayList) objects. It's sending as String. It's seems the limitation the client framework I am using. I have tried one or two open frameworks but they are not working as expected. It will be great if you can suggest some framework with some examples.
Below is the sample curl request, I have removed some extra parameters to keep it simple.
curl -L -v -b agent_cookies.txt -H "Content-Type: application/json" -d
"{"items":{"atg-rest-class-type":"java.util.ArrayList","atg-rest-values":
[{"atg-rest-class-type":"com.bean.CommerceItemInfo","tinSkuNumber":"41589367","itemNumber":
280594,"color": 9,"size":
94,"salePrice":50.00,"taxAmount":3.5,"stateTax":0.48,"countyTax":0.08,"currencyCode":"USD"},{"atg-rest-class-type":"com..bean.CommerceItemInfo",
"tinSkuNumber":"41589375","itemNumber": 280594,"color": 9,"size":
96,"salePrice":100.00,"taxAmount":7,"stateTax":0.96,"countyTax":0.16,"currencyCode":"USD"}]},orderInfo:{...},"clientAddress":{"atg-rest-class-type":"java.util.ArrayList","atg-rest-values":
[{"atg-rest-class-type":"com.bean.ClientAddress",\"firstName\":\"John\",\"lastName\":\"Dao\",\"state\":\"FL\",\"country\":\"US\",\"postalCode\":\"33606\",\"address1\":\"100
S Edison Avenue\",\"address2\":\"Suite
D\",\"city\":\"Tampa\",\"addressType\":\"BOTH\"}]},{......}}"
http://localhost:8080/rest/model/com/web/actor/CartActor/testOrder
Thank you

After some research I found the limitation of ATG client and no way we can send List.I changed the arguments to accept individual beans only.

Related

How do you simulate a REST API interface?

I have a web app which I would like to test using Selenium, with this app communicating with the backend using a REST API.
It is my understanding that Selenium is mainly used to test the flows through an application and the appearance/presence of widgets for each of these states. This would suggest to me that it makes a lot of sense when writing Selenium tests to simulate the backend. Python is my language of choice but I am also familiar with node.js, javascript and JAVA. What approach would you recommend with regard to simulating the REST API. I was thinking of writing a server using Python. I can create this server within my test environment and configure how it responds to requests from the front-end on a test by test basis. Are there any tools, libraries you might recommend me?
I should also add that I am using raml to define my api.
So with my simulation of the backend, the tests would look something like this:
def test_no_table_for_one_user():
# configure reply for api request
rest_sim.get_users_response = (200, [{name: "Foo Bar", address: "West side"}])
navigate_to_users_page()
# test that this users details are presented without the use of a table
...
def test_table_for_multiple_users():
# configure reply for api request
rest_sim.get_users_response = (200, [{name: "Foo Bar", address: "West side"}, {name: "Foo Baz", address: "East side"}])
navigate_to_users_page()
# test that the two users are presented in the form of a table
...
For mocking simple REST API you can try node.js-based json-server.
It's easy to setup, you just need to create JSON file with some data to simulate db, json-server will create all common REST API routes for you.
There are many libraries and tools available to help you with this. What you are talking about is creating a test harness, or emulator/simulator. In most situations, it is generally recommended that the person who builds and develops the backend, provide you with the harness since they are owners of the API and control different versions and change. Reciprocally, you can provide them with your client so they can understand how you use the API.
If they cannot do this, then you will need to create a harness yourself. The best tool to help you do this for HTTP API's is WireMock
http://wiremock.org/
In your example you will probably want to run this as Stand Alone:
http://wiremock.org/docs/running-standalone/
And then using JSON file configuration to define the behaviour.
My preference is to also wrap and deploy you WireMock test harness as a Docker image and publish to a Docker repository so that other people can use it. In this case its simply a case of creating a Dockerfile with the following and running a docker container:
Dockerfile
FROM java:8
WORKDIR /opt
RUN apt-get install wget
RUN wget http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-standalone/2.5.0/wiremock-standalone-2.5.0.jar
RUN mkdir mappings
VOLUME /opt/mappings
EXPOSE 8080
CMD java -jar wiremock-standalone-2.5.0.jar
command line
docker build -t wiremock/apiname:[version] .
docker run -d -p [exposedport]:8080 -v /directory/with/json:/opt/mappings --name apiname wiremock/apiname:[version]
I would choose Python without any problems.
Please have a look at the requests module:
http://docs.python-requests.org/en/master/
It is really easy to install and to make requests by using REST messages.
No matter what you have to do, I will continue with Python.
How to use REST API from requests after installation (with pip for instance):
import requests
class RESTIF():
'''
Class to handle a connection towards your server
'''
def __init__(self):
'''Initialization of a single Session and header dictionary to be used for REST requests.'''
self.nSession = requests.Session()
# Default header values to get initial connection:
self.header = {"Content-Type":"application/json",
"KeepAlive":"true",
"Authorization":"Basic ",
"Cookie": None}
def action(self, URL, JSONdata):
myCreate = self.nSession.post(URL, headers=self.header, data=JSONdata)
The header is quite useful in case of Cookie exchange. Requests will take care of that.
You can handle login to a rest api or send PUT/POST/DELETE/GET messages easily!
Everything is possible and no need to switch to Java or other languages.
Please let me know if you have additional queries or that solved your question. Have a great one!

jaxws-maven-plugin, maven-enunciate-plugin example?

I've inherited a project that contains many jaxws services. I want to add another one and am trying to duplicate a similar working example. I can test that one like this
./soapget.sh soap_serial.xml r.xml
where soapget.sh is
#!/bin/bash
wget "http://localhost:5032/VCWH_QueryService/soap/SettopChannelMapResourceService" --post-file=$1 --header="Content-Type: text/xml" -O $2
This produces a good response, captured in r.xml.
Like the working service, my new service uses three classes. The code compiles ok, assembles into a .war file, and deploys. Now when I try the same thing for the new service I wrote
./bsg.sh soap_rate.xml r2.xml
where bsg.sh is
#!/bin/bash
wget "http://localhost:5032/VCWH_QueryService/soap/BsgHandleResourceService" --post-file=$1 --header="Content-Type: text/xml" -O $2
I get the useless error
2015-11-23 20:26:52 ERROR 500: Internal Server Error
The log files for the project do not contain any more info either.
There are just too many black boxes interacting that I can't figure out what's going on... Maven-Enunciate-Plugin, jax-ws, Java, etc.
For example, how does calling BSGHandleResourceService find its way to the actual code, one of which is called BSGHandleResource.java? Normally I would make those hooks in the web.xml file but that has been taken over by the black boxes.
Are there any jax-ws/maven experts out there that can shed some light?
I was able to find and fix the problem by sending a soap request to the service using SoapUI. That returned useful error messages whereas the other method did not.

cURL commands in java

How to use the cURl command
curl https://na1.salesforce.com/services/data/v20.0/query?q=SELECT+name+from+Account
-H "Authorization: Bearer access_token" -H "X-PrettyPrint:1"
in java to call sales force rest web services
Java has an URLConnection Class that has similar functions as cURL.
For interacting with the salesforce API, it is best to use a client library and not implement it all by yourself, see http://blog.palominolabs.com/2011/03/03/a-new-java-salesforce-api-library/ for an example.
Easiest way is to download cURL java wrapper like https://github.com/pjlegato/curl-java and use cURL directly in your code.
Second way is to use Runtime.getRuntime().exec("crul...") "pattern" and run curl as a normal process.

How to give a POST API request in a browser?

I'm really new to APIs and POST or PUT or DELETE. I'm also new to running APIs using POST or other.
I have given a document which says
Function :- Add new Item
URI :- qtp/qtps
ACTION :- POST
REQUEST :- <n1:qtp xmlns:n1="http://www.mac.com/qts/xml/ns/qtm/qtpManagement"><name>rosa qtp 3</name><ipAddress>171.68.121.232</ipAddress><macAddress>10:0t:24:03:r7:57</macAddress><description>this is rosa qtp </description></n1:qtp>
I have absolutely no idea how to proceed further, But I know that by executing the request I need to Add a new Item in the application server, I tried something with browser myself but it did not work.
Can someone show me how can I work with this or explain me more about this or at-least give me a clue
One of the most useful tools for testing and debugging HTTP requests, in my experience, is cURL (http://curl.haxx.se/).
cURL is actually the under-the-hood library used for HTTP requests by a majority of PHP apps; the command-line version lets you do virtually anything that HTTP can do, and get great debugging data.
In the scenario you describe above, after downloading and installing cURL you'd likely use a command like:
curl --header "Content-Type: application/xml" --data '<XML YOU WANT TO SEND>' -X POST <URL TO WHICH DATA SHOULD BE SENT>
It's not clear from your question what the destination host+url is, but using the specific sample data you provide this would probably look like:
curl --header "Content-Type: application/xml" --data '<n1:qtp xmlns:n1="http://www.mac.com/qts/xml/ns/qtm/qtpManagement"><name>rosa qtp 3</name><ipAddress>171.68.121.232</ipAddress><macAddress>10:0t:24:03:r7:57</macAddress><description>this is rosa qtp </description></n1:qtp>' -X POST http://www.mac.com/qtp/qtps
Install a firebug plugin for that. You can use SOA client.

Converting cURL authentication to Java and retrieving & updating data using REST XML (Pt.2)

This is the second part of my question on converting cURL to Java
The first part is titled:
Converting cURL authentication to Java and retrieving & updating data using REST XML (Pt.1)
Third, how can I implement update, create, and delete for the api in Java? For example:
Update: curl -i -X PUT -H "Content-Type:application/xml" -H "Accept: application/xml" -d "<ticket><description>Take this description</description></ticket>" http://user:password#www.assembla.com/spaces/my_space_id/tickets/1
Delete" curl -i -X DELETE -H "Accept: application/xml" http://user:password#www.assembla.com/spaces/my_space_id/tickets/1
Create: curl -i -X POST -H "Content-Type:application/xml" -H "Accept: application/xml" -d "<ticket><summary>This is a Summary</summary><priority>3</priority></ticket>" the weblink
In other words, how can I convert these cURL code into Java?
I would really appreciate your help. Also, a good reference to do such stuff in Java will be awesome too.
Thanks.
Well, the official API usually helps: Assembla REST API
There is says that you need to use basic authentication.
For accessing REST in Java: Rest clients for Java?
you should be looking out for Apache HttpClient tutorials.
There's some Samplecode of a Android Rest Client application around, try searching for it. Also, I just discovered "resting". I haven't tried it, but might be worth a look.
Also watching the Developing Android REST client applications video from Google.io is recommended, as it is teaching some very important architectural basics and hints.
I placed a similar question some time ago, and got an answer here at Stackoverflow.

Categories