Search in sources :

Example 11 with Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class HttpClientBuilderTest method managedByEnvironment.

@Test
public void managedByEnvironment() throws Exception {
    final Environment environment = mock(Environment.class);
    when(environment.getName()).thenReturn("test-env");
    when(environment.metrics()).thenReturn(new MetricRegistry());
    final LifecycleEnvironment lifecycle = mock(LifecycleEnvironment.class);
    when(environment.lifecycle()).thenReturn(lifecycle);
    final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
    HttpClientBuilder httpClientBuilder = spy(new HttpClientBuilder(environment));
    when(httpClientBuilder.buildWithDefaultRequestConfiguration("test-apache-client")).thenReturn(new ConfiguredCloseableHttpClient(httpClient, RequestConfig.DEFAULT));
    assertThat(httpClientBuilder.build("test-apache-client")).isSameAs(httpClient);
    // Verify that we registered the managed object
    final ArgumentCaptor<Managed> argumentCaptor = ArgumentCaptor.forClass(Managed.class);
    verify(lifecycle).manage(argumentCaptor.capture());
    // Verify that the managed object actually stops the HTTP client
    final Managed managed = argumentCaptor.getValue();
    managed.stop();
    verify(httpClient).close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) MetricRegistry(com.codahale.metrics.MetricRegistry) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Environment(io.dropwizard.setup.Environment) Managed(io.dropwizard.lifecycle.Managed) Test(org.junit.Test)

Example 12 with Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class MultiPartBundleTest method testRun.

@Test
public void testRun() throws Exception {
    final Environment environment = new Environment("multipart-test", Jackson.newObjectMapper(), null, new MetricRegistry(), getClass().getClassLoader());
    new MultiPartBundle().run(environment);
    assertThat(environment.jersey().getResourceConfig().getClasses()).contains(MultiPartFeature.class);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) Test(org.junit.Test)

Example 13 with Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    forceSet(TestProperties.CONTAINER_PORT, "0");
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    dbConfig.setUrl("jdbc:hsqldb:mem:DbTest-" + System.nanoTime() + "?hsqldb.translate_dti_types=false");
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.hsqldb.jdbcDriver");
    dbConfig.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, ImmutableList.of(Person.class));
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("DROP TABLE people IF EXISTS").executeUpdate();
        session.createNativeQuery("CREATE TABLE people (name varchar(100) primary key, email varchar(16), birthday timestamp with time zone)").executeUpdate();
        session.createNativeQuery("INSERT INTO people VALUES ('Coda', 'coda@example.com', '1979-01-02 00:22:00+0:00')").executeUpdate();
        transaction.commit();
    }
    final DropwizardResourceConfig config = DropwizardResourceConfig.forTesting(new MetricRegistry());
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper()));
    config.register(new PersistenceExceptionMapper());
    config.register(new DataExceptionMapper());
    config.register(new EmptyOptionalExceptionMapper());
    return config;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) EmptyOptionalExceptionMapper(io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper) JacksonMessageBodyProvider(io.dropwizard.jersey.jackson.JacksonMessageBodyProvider) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) DropwizardResourceConfig(io.dropwizard.jersey.DropwizardResourceConfig) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session)

Example 14 with Environment

use of io.dropwizard.setup.Environment in project keywhiz by square.

the class KeywhizTestRunner method createInjector.

static Injector createInjector() {
    KeywhizService service = new KeywhizService();
    Bootstrap<KeywhizConfig> bootstrap = new Bootstrap<>(service);
    service.initialize(bootstrap);
    File yamlFile = new File(Resources.getResource("keywhiz-test.yaml").getFile());
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    ObjectMapper objectMapper = bootstrap.getObjectMapper().copy();
    KeywhizConfig config;
    try {
        config = new ConfigurationFactory<>(KeywhizConfig.class, validator, objectMapper, "dw").build(yamlFile);
    } catch (IOException | ConfigurationException e) {
        throw Throwables.propagate(e);
    }
    Environment environment = new Environment(service.getName(), objectMapper, validator, bootstrap.getMetricRegistry(), bootstrap.getClassLoader());
    Injector injector = Guice.createInjector(new ServiceModule(config, environment));
    service.setInjector(injector);
    return injector;
}
Also used : IOException(java.io.IOException) ConfigurationException(io.dropwizard.configuration.ConfigurationException) Injector(com.google.inject.Injector) ConfigurationFactory(io.dropwizard.configuration.ConfigurationFactory) Bootstrap(io.dropwizard.setup.Bootstrap) Environment(io.dropwizard.setup.Environment) File(java.io.File) Validator(javax.validation.Validator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Environment (io.dropwizard.setup.Environment)14 MetricRegistry (com.codahale.metrics.MetricRegistry)7 Test (org.junit.Test)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DataSourceFactory (io.dropwizard.db.DataSourceFactory)3 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)3 Before (org.junit.Before)3 Configuration (io.dropwizard.Configuration)2 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)2 Managed (io.dropwizard.lifecycle.Managed)2 Bootstrap (io.dropwizard.setup.Bootstrap)2 File (java.io.File)2 Connector (org.eclipse.jetty.server.Connector)2 Server (org.eclipse.jetty.server.Server)2 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CharStreams (com.google.common.io.CharStreams)1