Created
December 15, 2015 14:56
-
-
Save psamsotha/05c29f22990f9e5bf62c to your computer and use it in GitHub Desktop.
Jersey Client Proxy Test
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
import java.util.logging.Logger; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.client.proxy.WebResourceFactory; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import org.junit.Test; | |
/** | |
* For Stack Overflow question http://stackoverflow.com/q/34289647/2587435 | |
* | |
* Run it like any other JUnit test. Three dependencies to run this test. | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>2.22.1</version> | |
* <scope>test</scope> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.glassfish.jersey.media</groupId> | |
* <artifactId>jersey-media-json-jackson</artifactId> | |
* <version>2.22.1</version> | |
* <scope>test</scope> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.glassfish.jersey.ext</groupId> | |
* <artifactId>jersey-proxy-client</artifactId> | |
* <version>2.22.1</version> | |
* </dependency> | |
* | |
*/ | |
public class ProxyTest extends JerseyTest { | |
public static class Model { | |
private String message; | |
public Model(String message) { | |
this.message = message; | |
} | |
public Model() { | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
@Override | |
public String toString() { | |
return "Model{" + "message=" + message + '}'; | |
} | |
} | |
public static interface Resource { | |
@POST | |
@Consumes("application/json") | |
@Produces("application/json") | |
public Model post(Model model); | |
} | |
@Path("test") | |
public static class ResourceImpl implements Resource { | |
@Override | |
public Model post(Model model) { | |
return model; | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig(ResourceImpl.class); | |
} | |
@Override | |
public void configureClient(ClientConfig config) { | |
config.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Test | |
public void doit() { | |
Resource resource = WebResourceFactory.newResource(Resource.class, target("test")); | |
Model reqModel = new Model("Testing"); | |
Model resModel = resource.post(reqModel); | |
assertNotNull(resModel); | |
assertEquals(reqModel.getMessage(), resModel.getMessage()); | |
System.out.println(resModel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment