Play2: Best way to deliver JSON content - java

Let's say I have REST like endpoints which return JSON data, e.g. from models in my Java Play application. I've found two ways to create the JSON output:
Using JSON templates similar to HTML templates. It could look like this:
{
"name": "${user.name}",
"id": "${user.id}",
. . .
}
What I like about it is the flexibility. I could build a wrapper around the data (for metadata, status messages etc.) with ease. And I easily can influence which things get delivered (things like password fields, timestamps of last login etc. are of course nothing you want to show to the client). A requirement is of course that the content of the template variables should be escaped correctly in order to get valid JSON. What is the best way to do this?
The second approach is to serialize the POJO of the model directly into JSON. Of course this is faster and can be done with less effort. In contrast to the template approach, it is perhaps more reliable since it is an automatic process. BUT: how can I exclude data like password and metadata fields? should I construct a new JSON object with the jackson implementation that only contains the relevant data? Or do I have to create a "json-model" for each model class and transform the real model on each request into the json-model before I can serialize it?
Personally, I like the template approach because of its flexibility. But some of you will say, never construct JSON, XML etc. manually. But is this really a problem in this case, what would you recommend?

Assuming you're using Java, Jackson let you exclude fields using annotations. See: http://jackson.codehaus.org/1.0.0/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html

Related

how to separate business logic in RestAssured

We have REST webservice. It operates over JSON data representation. I would like to provide functional testing. I plan to use RestAssured framework. It provides understandable methods for testing correctness of output json.
Example, get("/method").then().assertThat().body("obj.field", equalTo(5));
But one problem arise: if json structure will change, all tests shall be invalid. For example, if field should be renamed to field2 we shall fix all test with occurrences of field. The problem is very similar to web pages testing problem, where we should check presence of some web elements, etc. It was solved introducing by Page Object pattern. Does some similar solution exist for testing of REST api or could you advise some elegant one?
In the example given in your question you validate the entire body of a response object in which case it is probable you will create brittle tests.
However it looks like REST-Assured already provides all the functionality you need to test specific parts of a JSON response:
JSON example
JSON Advanced Examples
Using JSON Path
You can even map objects and then do whatever you wish with the objects constructed, for example validation and manipulation.
See here for more examples.
Just like with an HTML page, one way to write tests less exposed to changes is to use a strategy to locate the target you want to evaluate.
With a web page you would use an XPath query, a CSS Seletor or directly the id to avoid dependecies over the ancestors.
So how to do it with a JSON ?
Well you could use a Regular expression, but it can get really messy or you could use an XPath like query for JSON :
http://goessner.net/articles/JsonPath/
http://defiantjs.com/
So in your case, writing reliable tests is more about what you evaluate rather than the framework you use to do it.
Changes in REST API (especial public) are less frequent than in GUI. When changes in API are introduced they should be marked with new version and do not break old one (in most cases). So keep your tests as simple as possible, without introducing additional patterns, that will have some benefits - you can easily throw them away and write new. Hihger test framework complexity provides hihger maintanence costs. Any way in REST-Assured you may create ResponseSpecification and reuse it in assertions.

XML schema to Java pojos to json

I'm generating a Java class hierarchy from a complicated xml schema. The content stored in xml (returned via a REST endpoint) is verbose, plus consumers want only subsets of the xml content returned as json (that they define).
I know there are brute force ways to accomplish this, but does anyone know of a more elegant approach? Maybe some kind of mapping that the consumer could pass to the service that would allow them to dynamically select the content (via xpath in the mapping) and also produce custom json wrapping that content?
The consumers will have an understanding of the schema and content structure, but nothing about Java or its object hierarchy.
I know, a lot here. Any suggestions?
You can give Apache Commons JXPath a try. If you have the Java class hierarchy then I assume you have the objects in memory before sending them as XML. With that you can user JXPath to traverse the java objects as if they were XML in XPath fashion.

How to render special XML/JSON Flavours with Playframework

According to James Wards Play Tutorial it's very easy to get a JSON out of your model. Also with XML this should be quite simple.
But most of the time, I have the requirement to build not just an plain XML or JSON Endpoint, but furthermore to deliver special flavours of those. In my case this is GeoJSON or TopoJSON. But also in XML, it could be a simple RSS or ATOM Feed you have to deliver out of your model. Also building a XML fitting to a very nasty XSD schema is still a case sometimes.
What options do you have in play to perform this, or which one of the following would you recommend?:
In case of GeoJSON/TopoJSON: Activate JSON as a template format, and create JSON Templates
In case of ATOM/RSS: Just use an XML Template
Some way to modify the JSON response coming from toJson(tasks)?
Use of a fancy library which does all that out of the box, and everyone knows about it, except me?
If you're doing GeoJSON, just annotate your objects with Jackson annotations according to the GeoJSON spec, it's not hard. If it is hard, then there are a few libraries out there that come with Java objects with the necessary annotations already for you, eg: https://github.com/opendatalab-de/geojson-jackson
An XML template is probably the simplest from Java.
What's your use case? toJson returns a Jackson JSONNode. You can modify it as much as you want. But the better thing to do would be to use Jackson annotations on your objects to get the format right in the first place.
I think you're referring to Jackson, it can do everything you want. It can even do XML if you want it to.

Creating multithreaded Java server and clients, but messages have to be in XML format

I've got to write a multithreaded chat program, using a server and clients but each message sent has to be in XML.
Is it simpler/easier just to write out all the code in java, and then try and somehow alter it so the messages are sent in XMl format, or would it be simpler just to try and go for it in XML and hope it works. I'll admit I don't know that much about XML. :)
Also any links to any relevant online help/tutorials would be much appreciated.
Thanks.
When messing with XML in Java, PLEASE consider using JAXB or something similar. It allows you to work with a normal object graph in memory and then serialize that to XML in one operation (and the other way around).
Manipulating XML through the DOM API is a slow way to lose your sanity, do not do it for any non-trivial amount of XML.
I fail to see what the program being multithreaded or a server have to do with it though...
Check out XStream. You can use this to marshall a normal Java object into XML, and back again into an object, without having to do anything instrusive like define interfaces or specify schema etc. i.e. it works out of the box for objects you already have defined. For most cases it's seamless in its default mode.
XStream produces a direct XML serialised representation of a Java object (i.e. XML elements represent each field of a Java object directly). You can customise this further as/when you require. If you want to define persisted objects in terms of schema (XSD) then it's not appropriate. However if you're transporting objects where persistence is short-term and you're not worried about conforming to some schema then it's definitely of use.
e.g.
Person person = new Person("Brian Agnew");
XStream xStream = new XStream();
System.out.println(xStream.toXML(person));
and conversion from XML to the Person object is similarly trivial.
(note XStream is thread-safe)
There is something called XML RPC. This examples pretty much shows what you're looking for:
http://docstore.mik.ua/orelly/xml/jxml/ch11_02.htm
It would be simpler to use existing XMPP clients and servers and not write your own at all.
If this is in fact homework, then I would suggest writing the client and server as you have suggested, using all java, but use a String as the message. You can then easily add parsing of the string to/from XML when all other parts are working.
I would suggest to also have a look at Betwixt and Digester. For Digester there are some tutorials which can be found in the Digister-wiki. Betwixt provides some pretty good tutorials right on its website.
Additionally to these two tools there is a list of alternatives that can be found in the Reference section of http://wiki.apache.org/commons/Digester/WhyUseDigester
You're on the right page trying to break the task into smaller pieces.

How to prepopulate model objects with test data from file?

I have some model objects I'm using in my Java client application. Later these model objects will be populated / retrieved from remote services (e.g. SOAP). Now I want to do manual / automatic testing of the frontend before implementing these services. The model objects are mostly POJO and I want to store some sample test data in files and populate them with some easy method.
E.g. having model object School (with name (String) and teachers (List)) and Teacher with lastname and firstname, I want to store actual test data in some XML / text file and create some schools containing teachers from these data.
What are you using in this situation? I'm not familiar with TTD yet, but I can't imagine that there is no generic framework for doing this.
[edit]
I've choosen Spring to mock up my sample data / services, but the other alternatives mentioned here would have worked as well.
Sounds like a good use of XML serialization. You can use any XML serialization tool you like: XStream, etc.
Another nice tool is SOAP UI. If you point it to the WSDL for your service it'll create the XML request for you. Fill in the values and off you go. These can be saved, so perhaps that's a good way to generate test cases.
You can also use Spring to mock your remote service(s) and their responses.
In this case, all you have to do is loading an applicationContext that will simulate your backend system(s) by replying exactly what you want for your test purpose.
Why not keep the test data in Java? You have no extra stages, formats or libraries to deal with. It's fast and you have the power and familiarity of Java on your side.
First, I'd agree with duffymo that XStream and SOAP UI are viable options. However, I've also used the approach described by Tom Hawtin, as described below.
A helper class constructs a set of test instances of the model classes, some valid and some invalid in specific ways, and builds the appropriate object graphs. An initial test case uses a valid object object graph. Successive tests substitute invalid objects for valid ones in the initial setup, checking that the appropriate errors are returned.
The helper class provides a single point of control for constructing objects whose contents are appropriately related for the scenarios needed in testing.

Categories