Monday, September 29, 2014

Example



 package testingWS;

 import org.junit.Test;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class TestingUsingJerseyClient {

@Test
public void testgetUsingJerseyClient(){
Client client = Client.create();
WebResource webResource = client
  .resource("http://localhost:8080/abcrAuthorization/api/authz");
ClientResponse response = webResource.accept("application/json")
                   .get(ClientResponse.class);


if (response.getStatus() != 200) {
  throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}

String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println("Output from Server .... \n"+output);
}

@Test
public void testpostUsingJerseyClient(){
Client client = Client.create();
WebResource webResource = client
  .resource("http://localhost:8080/abc/users/post");
String input = "{\"singer\":\"Mr. Big\",\"title\":\"Wild World\"}";
ClientResponse response = webResource.type("application/json")
  .post(ClientResponse.class, input);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
    + response.getStatus());
}
String output = response.getEntity(String.class);

}

}

No comments:

Post a Comment