It doesn’t make sense to have full test coverage of the jersey-gson project
in selenium.
Let’s fill in the gaps with some jersey-client unit tests.
First,
we need a UserMap deserializer to mirror the existing serializer.
The existing serializer sends the UserMap as an object whose value is an Array of rows
(this simplifies interaction with the DataTables front end).
We deserialize the UserMap by iterating through the array,
turning each row into a User object,
and adding each User object to the Map.
... publicclassUserMapUnmarshallimplementsJsonDeserializer<UserMap> { privatestatic Gson gson = new Gson();
@Override public UserMap deserialize(JsonElement mapFormat, Type typeOfSrc, JsonDeserializationContext context){ Iterator<JsonElement> iterate = mapFormat.getAsJsonObject() .get("data") .getAsJsonArray() .iterator();
UserMap users = new UserMap(); while( iterate.hasNext() ) { JsonObject user = ((JsonElement) iterate.next()).getAsJsonObject(); String email = user.get("email").getAsString(); User add = new User(); add.setEmail( email ); add.setSurname( user.get("surname").getAsString() ); add.setGivenName( user.get("givenName").getAsString() ); users.put( email, add ); }
return users; } }
Next,
we need to register the new UserMapUnmarshall class for use by the GsonReader class -
just as we registered the UserMapMarshall class for the GsonWriter class.
Let’s complete our suite of server services by adding a method to get a single user and a method to delete a user.
The new feature in this code is the use of the PathParam annotation to extract values from the request path.
And the remainder of the new code builds on features that we’ve used before.
Now we can add a set of jersey client unit tests to exercise our server methods.
I’ve split the code into multiple blocks for for clarity.
The first block of code registers the GsonReader and GsonWriter classes for use by the client
(explicit registration is not needed on the server because the automated server scan searches for the annotated classes).
The full source for this version of Jersey, Gson and Datatables is on github.
BTW, I’m using jersey-client 1.18 for expediency,
I’ll update this for 2.x in the near future.