All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.testing.common.DropwizardTestResourceConfig Maven / Gradle / Ivy

There is a newer version: 5.0.0-alpha.2
Show newest version
package io.dropwizard.testing.common;

import io.dropwizard.core.setup.ExceptionMapperBinder;
import io.dropwizard.jersey.DropwizardResourceConfig;
import io.dropwizard.jersey.jackson.JacksonFeature;
import io.dropwizard.jersey.validation.HibernateValidationBinder;
import org.glassfish.jersey.server.ServerProperties;

import javax.servlet.ServletConfig;
import javax.ws.rs.core.Context;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

/**
 * A configuration of a Jersey web application by {@link ResourceTestJerseyConfiguration} with
 * support of injecting a configuration from a {@link ServletConfig}. It allows to use it along
 * with the Grizzly web test container.
 */
class DropwizardTestResourceConfig extends DropwizardResourceConfig {

    /**
     * A registry of passed configuration objects. It's used for obtaining the current configuration
     * via a servlet context.
     */
    static final Map CONFIGURATION_REGISTRY = new ConcurrentHashMap<>();
    static final String CONFIGURATION_ID = "io.dropwizard.testing.junit.resourceTestJerseyConfigurationId";

    DropwizardTestResourceConfig(ResourceTestJerseyConfiguration configuration) {
        super(configuration.metricRegistry);

        if (configuration.registerDefaultExceptionMappers) {
            register(new ExceptionMapperBinder(false));
        }
        for (Class provider : configuration.providers) {
            register(provider);
        }
        property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");
        for (Map.Entry property : configuration.properties.entrySet()) {
            property(property.getKey(), property.getValue());
        }
        register(new JacksonFeature(configuration.mapper));
        register(new HibernateValidationBinder(configuration.validator));
        for (Supplier singleton : configuration.singletons) {
            register(singleton.get());
        }
    }

    DropwizardTestResourceConfig(@Context ServletConfig servletConfig) {
        this(getConfiguration(servletConfig));
    }

    private static ResourceTestJerseyConfiguration getConfiguration(@Context ServletConfig servletConfig) {
        final String id = requireNonNull(servletConfig.getInitParameter(CONFIGURATION_ID), "No configuration id");
        return requireNonNull(CONFIGURATION_REGISTRY.get(id), "No configuration");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy