Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.codehaus.jackson.map.Module;
import org.junit.After;
import org.junit.Before;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.test.framework.AppDescriptor;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.sun.jersey.test.framework.spi.container.grizzly2.web.GrizzlyWebTestContainerFactory;
import com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory;
import com.yammer.dropwizard.bundles.JavaBundle;
import com.yammer.dropwizard.jersey.JacksonMessageBodyProvider;
import com.yammer.dropwizard.jersey.LoggingExceptionMapper;
import com.yammer.dropwizard.jersey.caching.CacheControlledResourceMethodDispatchAdapter;
import com.yammer.dropwizard.json.Json;
import com.yammer.metrics.jersey.InstrumentedResourceMethodDispatchAdapter;
/**
* A base test class for testing Dropwizard resources using the Grizzly web
* server. Needed when we want Jersey to be able to inject an
* {@link HttpServletRequest} object. The {@link InMemoryTestContainerFactory}
* doesn't support this kind of injection.
*/
public abstract class GrizzlyResourceTest {
private final Set<Object> singletons = Sets.newHashSet();
private final Set<Class<?>> providers = Sets.newHashSet();
private final List<Module> modules = Lists.newArrayList();
private JerseyTest test;
protected abstract void setUpResources() throws Exception;
protected void addResource(Object resource) {
singletons.add(resource);
}
public void addProvider(Class<?> klass) {
providers.add(klass);
}
protected void addJacksonModule(Module module) {
modules.add(module);
}
protected Json getJson() {
return new Json();
}
protected Client client() {
return test.client();
}
@Before
public void setUpJersey() throws Exception {
setUpResources();
this.test = new JerseyTest(new GrizzlyWebTestContainerFactory()) {
@Override
protected AppDescriptor configure() {
ClientConfig config = new DefaultClientConfig();
// taken from DropwizardResourceConfig
config.getFeatures().put(ResourceConfig.FEATURE_DISABLE_WADL, Boolean.TRUE);
config.getSingletons().add(new LoggingExceptionMapper<Throwable>() { }); // create a subclass to pin it to Throwable
config.getClasses().add(InstrumentedResourceMethodDispatchAdapter.class);
config.getClasses().add(CacheControlledResourceMethodDispatchAdapter.class);
for (Object provider : JavaBundle.DEFAULT_PROVIDERS) {
config.getSingletons().add(provider);
}
for (Class<?> provider : providers) {
config.getClasses().add(provider);
}
Json json = getJson();
for (Module module : modules) {
json.registerModule(module);
}
config.getSingletons().add(new JacksonMessageBodyProvider(json));
config.getSingletons().addAll(singletons);
return new WebAppDescriptor.Builder(<PACKEGE_NAME_WHERE_YOUR_RESOURCE_CLASSES_RESIDE>).clientConfig(config).build();
}
};
test.setUp();
}
@After
public void tearDownJersey() throws Exception {
if (test != null) {
test.tearDown();
}
}
}
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.12</version>
</dependency>
ClientResponse response = client().resource("http://localhost:9998/oauth/register")
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment