|
| 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