Skip to content

Commit 5fd9165

Browse files
committed
Merge pull request #24 from ideoplex/work
Addition of unit tests using jersey-client 1.x
2 parents e7bfbd4 + 51c3060 commit 5fd9165

File tree

7 files changed

+178
-6
lines changed

7 files changed

+178
-6
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
jersey-gson
22
===========
33

4-
Sample java web application with jersey and gson that displays a list of users in a [Datatables](http://datatables.net/) table.
4+
Sample java web application with jersey and custom gson serialiation/deserialization
5+
to displays a list of users in a [Datatables](http://datatables.net/) table.
56
The application retrieves the users via Datatables ajax.
67
User edits are submitted to the server and successful response is applied to the local data set in the browser.
78
Long cell text will be truncated with an ellipsis.
89

910
Users can be added via a [Bootstrap](http://getbootstrap.com/) Modal dialog. Users are kept in memory and are not persisted.
1011

11-
The application tests demonstrate the use of [Selenium](http://www.seleniumhq.org/) automation for the addition of users.
12+
The application tests demonstrate the use of [Selenium](http://www.seleniumhq.org/) automation for the addition of users
13+
and the use of jersey-client with gson.
1214

1315
The application can be run from the command line via:
1416

@@ -17,7 +19,7 @@ mvn jetty:run
1719
```
1820

1921
[Maven](https://maven.apache.org/) is configured to autostart/autostop jetty
20-
for the selenium test. Please halt any "mvn jetty:run" invocations before
22+
for tests. Please halt any "mvn jetty:run" invocations before
2123
running the tests via:
2224

2325
```
@@ -32,6 +34,7 @@ mvn test -Dbrowser=chrome
3234

3335
Read about the project (from most recent to oldest):
3436

37+
1. [Jersey Client with Gson](https://ideoplex.com/2015/11/01/jersey-client-with-gson/)
3538
1. [jersey-gson Content-Type Woes](https://ideoplex.com/2015/10/31/jersey-gson-content-type-woes/)
3639
1. [Autostart Jetty in Maven](https://ideoplex.com/2015/10/25/autostart-jetty-in-maven/)
3740
1. [DataTables, Bootstrap and Text Overflow](https://ideoplex.com/2015/08/16/datatables-bootstrap-and-text-overflow/)

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@
122122
<version>${selenium.version}</version>
123123
<scope>test</scope>
124124
</dependency>
125+
<dependency>
126+
<groupId>com.sun.jersey</groupId>
127+
<artifactId>jersey-client</artifactId>
128+
<version>${jersey.client.version}</version>
129+
<scope>test</scope>
130+
</dependency>
125131
</dependencies>
126132
<properties>
127133
<jersey.version>2.12</jersey.version>
134+
<jersey.client.version>1.18</jersey.client.version>
128135
<jetty.version>9.3.0.M2</jetty.version>
129136
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
130137
<testng.version>6.9.4</testng.version>

src/main/java/com/ideoplex/tutorial/GsonReader.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828

2929

3030
import com.google.gson.Gson;
31+
import com.google.gson.GsonBuilder;
3132

3233

3334
@Provider
3435
@Consumes(MediaType.APPLICATION_JSON)
3536
@Singleton
3637
public class GsonReader<T> implements MessageBodyReader<T> {
38+
protected static Gson gson = new GsonBuilder()
39+
.registerTypeAdapter(UserMap.class,new UserMapUnmarshall())
40+
.create();
3741

3842
@Override
3943
public boolean isReadable(Class<?> type, Type genericType,
@@ -46,8 +50,7 @@ public T readFrom(Class<T> type, Type genericType,
4650
Annotation[] antns, MediaType mt,
4751
MultivaluedMap<String, String> mm, InputStream in)
4852
throws IOException, WebApplicationException {
49-
Gson g = new Gson();
50-
return g.fromJson(_convertStreamToString(in), type);
53+
return gson.fromJson(_convertStreamToString(in), type);
5154
}
5255

5356
private String _convertStreamToString(InputStream inputStream)

src/main/java/com/ideoplex/tutorial/MyResource.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.ideoplex.tutorial;
22

3+
import javax.ws.rs.DELETE;
34
import javax.ws.rs.GET;
45
import javax.ws.rs.POST;
56
import javax.ws.rs.Path;
7+
import javax.ws.rs.PathParam;
68
import javax.ws.rs.QueryParam;
79
import javax.ws.rs.Consumes;
810
import javax.ws.rs.Produces;
@@ -61,6 +63,32 @@ public User getUserTemplate() {
6163
return user;
6264
}
6365

66+
@GET
67+
@Path("user/get/{email}")
68+
@Produces(MediaType.APPLICATION_JSON)
69+
public User getUser(@PathParam("email") String email) {
70+
User rVal = users.get(email);
71+
if ( rVal == null ) {
72+
throw new WebApplicationException(Response
73+
.status(Response.Status.NOT_FOUND)
74+
.build());
75+
}
76+
return rVal;
77+
}
78+
79+
@DELETE
80+
@Path("user/delete/{email}")
81+
@Produces(MediaType.APPLICATION_JSON)
82+
public User deleteUser(@PathParam("email") String email) {
83+
User rVal = users.remove(email);
84+
if ( rVal == null ) {
85+
throw new WebApplicationException(Response
86+
.status(Response.Status.NOT_FOUND)
87+
.build());
88+
}
89+
return rVal;
90+
}
91+
6492
@POST
6593
@Path("user/post")
6694
@Consumes(MediaType.APPLICATION_JSON)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.ideoplex.tutorial;
2+
3+
4+
import java.lang.reflect.Type;
5+
6+
import java.util.Collection;
7+
import java.util.Iterator;
8+
import java.util.Map;
9+
10+
import com.google.gson.Gson;
11+
import com.google.gson.JsonArray;
12+
import com.google.gson.JsonObject;
13+
import com.google.gson.JsonElement;
14+
import com.google.gson.JsonDeserializer;
15+
import com.google.gson.JsonDeserializationContext;
16+
17+
18+
public class UserMapUnmarshall implements JsonDeserializer<UserMap> {
19+
private static Gson gson = new Gson();
20+
21+
@Override
22+
public UserMap deserialize(JsonElement mapFormat, Type typeOfSrc, JsonDeserializationContext context) {
23+
Iterator<JsonElement> iterate = mapFormat.getAsJsonObject()
24+
.get("data")
25+
.getAsJsonArray()
26+
.iterator();
27+
28+
UserMap users = new UserMap();
29+
while( iterate.hasNext() ) {
30+
JsonObject user = ((JsonElement) iterate.next()).getAsJsonObject();
31+
String email = user.get("email").getAsString();
32+
User add = new User();
33+
add.setEmail( email );
34+
add.setSurname( user.get("surname").getAsString() );
35+
add.setGivenName( user.get("givenName").getAsString() );
36+
users.put( email, add );
37+
}
38+
39+
return users;
40+
}
41+
}

src/test/java/com/ideoplex/tutorial/BrowserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void addUsers( WebDriver driver )
7676
}
7777

7878
@Parameters({"browser","baseurl","waitajax"})
79-
@Test(invocationCount = 2, groups="browser")
79+
@Test(invocationCount = 2, dependsOnGroups="client", groups="browser")
8080
public void userCreate( String browser, String baseurl, String waitajax )
8181
{
8282
WebDriver driver = "chrome".equalsIgnoreCase(browser)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.ideoplex.tutorial;
2+
3+
4+
import javax.ws.rs.core.MediaType;
5+
6+
import com.sun.jersey.api.client.Client;
7+
import com.sun.jersey.api.client.ClientResponse;
8+
import com.sun.jersey.api.client.WebResource;
9+
import com.sun.jersey.api.client.config.ClientConfig;
10+
import com.sun.jersey.api.client.config.DefaultClientConfig;
11+
12+
import org.testng.Assert;
13+
import org.testng.annotations.Parameters;
14+
import org.testng.annotations.Test;
15+
16+
17+
public class ClientTest {
18+
19+
public static final String className = ClientTest.class.getName();
20+
21+
@Parameters({"baseurl"})
22+
@Test(groups="client")
23+
public void userCreate( String baseurl )
24+
{
25+
final User sample;
26+
final String email;
27+
final String url;
28+
29+
Client client;
30+
WebResource resource;
31+
ClientResponse response;
32+
33+
{
34+
email = "test@" + className;
35+
sample = new User();
36+
sample.setEmail(email);
37+
sample.setGivenName("John Q.");
38+
sample.setSurname("Public");
39+
40+
url = baseurl + "webapi/myresource/user";
41+
42+
ClientConfig config = new DefaultClientConfig();
43+
config.getClasses().add(GsonReader.class);
44+
config.getClasses().add(GsonWriter.class);
45+
46+
client = Client.create(config);
47+
}
48+
49+
{
50+
System.out.println("Verify that " + email + " is not available yet");
51+
resource = client.resource( url + "/get/" + email );
52+
response = resource.accept(MediaType.APPLICATION_JSON)
53+
.get(ClientResponse.class);
54+
assert( response.getStatus() == 404 );
55+
}
56+
57+
{
58+
System.out.println("Verify that user/post on " + email + " succeeds");
59+
resource = client.resource( url + "/post" );
60+
response = resource.accept(MediaType.APPLICATION_JSON)
61+
.type(MediaType.APPLICATION_JSON)
62+
.post(ClientResponse.class,sample);
63+
assert( response.getStatus() == 200 );
64+
User rVal = response.getEntity(User.class);
65+
assert( email.equals(rVal.getEmail()) );
66+
}
67+
68+
{
69+
System.out.println("Verify that " + email + " is in the userMap");
70+
resource = client.resource( url + "/map" );
71+
response = resource.accept(MediaType.APPLICATION_JSON)
72+
.get(ClientResponse.class);
73+
assert( response.getStatus() == 200 );
74+
UserMap rVal = response.getEntity(UserMap.class);
75+
assert( rVal != null );
76+
User look = rVal.get(email);
77+
assert( look != null );
78+
}
79+
80+
{
81+
System.out.println("Verify that delete on " + email + " succeeds");
82+
resource = client.resource( url + "/delete/" + email );
83+
response = resource.accept(MediaType.APPLICATION_JSON)
84+
.delete(ClientResponse.class);
85+
assert( response.getStatus() == 200 );
86+
User rVal = response.getEntity(User.class);
87+
assert( email.equals(rVal.getEmail()) );
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)