Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #24 from ideoplex/work
Addition of unit tests using jersey-client 1.x
- Loading branch information
Showing
7 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/ideoplex/tutorial/UserMapUnmarshall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.ideoplex.tutorial; | ||
|
||
|
||
import java.lang.reflect.Type; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonDeserializationContext; | ||
|
||
|
||
public class UserMapUnmarshall implements JsonDeserializer<UserMap> { | ||
private static 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.ideoplex.tutorial; | ||
|
||
|
||
import javax.ws.rs.core.MediaType; | ||
|
||
import com.sun.jersey.api.client.Client; | ||
import com.sun.jersey.api.client.ClientResponse; | ||
import com.sun.jersey.api.client.WebResource; | ||
import com.sun.jersey.api.client.config.ClientConfig; | ||
import com.sun.jersey.api.client.config.DefaultClientConfig; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Parameters; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
public class ClientTest { | ||
|
||
public static final String className = ClientTest.class.getName(); | ||
|
||
@Parameters({"baseurl"}) | ||
@Test(groups="client") | ||
public void userCreate( String baseurl ) | ||
{ | ||
final User sample; | ||
final String email; | ||
final String url; | ||
|
||
Client client; | ||
WebResource resource; | ||
ClientResponse response; | ||
|
||
{ | ||
email = "test@" + className; | ||
sample = new User(); | ||
sample.setEmail(email); | ||
sample.setGivenName("John Q."); | ||
sample.setSurname("Public"); | ||
|
||
url = baseurl + "webapi/myresource/user"; | ||
|
||
ClientConfig config = new DefaultClientConfig(); | ||
config.getClasses().add(GsonReader.class); | ||
config.getClasses().add(GsonWriter.class); | ||
|
||
client = Client.create(config); | ||
} | ||
|
||
{ | ||
System.out.println("Verify that " + email + " is not available yet"); | ||
resource = client.resource( url + "/get/" + email ); | ||
response = resource.accept(MediaType.APPLICATION_JSON) | ||
.get(ClientResponse.class); | ||
assert( response.getStatus() == 404 ); | ||
} | ||
|
||
{ | ||
System.out.println("Verify that user/post on " + email + " succeeds"); | ||
resource = client.resource( url + "/post" ); | ||
response = resource.accept(MediaType.APPLICATION_JSON) | ||
.type(MediaType.APPLICATION_JSON) | ||
.post(ClientResponse.class,sample); | ||
assert( response.getStatus() == 200 ); | ||
User rVal = response.getEntity(User.class); | ||
assert( email.equals(rVal.getEmail()) ); | ||
} | ||
|
||
{ | ||
System.out.println("Verify that " + email + " is in the userMap"); | ||
resource = client.resource( url + "/map" ); | ||
response = resource.accept(MediaType.APPLICATION_JSON) | ||
.get(ClientResponse.class); | ||
assert( response.getStatus() == 200 ); | ||
UserMap rVal = response.getEntity(UserMap.class); | ||
assert( rVal != null ); | ||
User look = rVal.get(email); | ||
assert( look != null ); | ||
} | ||
|
||
{ | ||
System.out.println("Verify that delete on " + email + " succeeds"); | ||
resource = client.resource( url + "/delete/" + email ); | ||
response = resource.accept(MediaType.APPLICATION_JSON) | ||
.delete(ClientResponse.class); | ||
assert( response.getStatus() == 200 ); | ||
User rVal = response.getEntity(User.class); | ||
assert( email.equals(rVal.getEmail()) ); | ||
} | ||
} | ||
} |