io.dropwizard.testing.common.Resource Maven / Gradle / Ivy
package io.dropwizard.testing.common;
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jakarta.rs.json.JacksonXmlBindJsonProvider;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.validation.Validators;
import io.dropwizard.logging.common.BootstrapLogging;
import io.dropwizard.testing.junit5.ResourceExtension;
import jakarta.validation.Validator;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.WebTarget;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.servlet.ServletProperties;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
public class Resource {
/**
* A {@link Resource} builder which enables configuration of a Jersey testing environment.
*/
@SuppressWarnings("unchecked")
public static class Builder> {
private final Set> singletons = new HashSet<>();
private final Set> providers = new HashSet<>();
private final Map properties = new HashMap<>();
private MetricRegistry metricRegistry = new MetricRegistry();
private ObjectMapper mapper = Jackson.newObjectMapper();
private Validator validator = Validators.newValidator();
private Consumer clientConfigurator = c -> {
};
private TestContainerFactory testContainerFactory = new InMemoryTestContainerFactory();
private boolean registerDefaultExceptionMappers = true;
private boolean bootstrapLogging = true;
public B setMapper(ObjectMapper mapper) {
this.mapper = mapper;
return (B) this;
}
public B setMetricRegistry(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
return (B) this;
}
public B setValidator(Validator validator) {
this.validator = validator;
return (B) this;
}
public B setClientConfigurator(Consumer clientConfigurator) {
this.clientConfigurator = clientConfigurator;
return (B) this;
}
public B addResource(Object resource) {
return addResource(() -> resource);
}
public B addResource(Supplier