Search in sources :

Example 1 with DropwizardResourceConfig

use of io.dropwizard.jersey.DropwizardResourceConfig in project dropwizard by dropwizard.

the class HibernateBundleTest method setUp.

@BeforeEach
void setUp() throws Exception {
    when(environment.healthChecks()).thenReturn(healthChecks);
    when(environment.jersey()).thenReturn(jerseyEnvironment);
    when(jerseyEnvironment.getResourceConfig()).thenReturn(new DropwizardResourceConfig());
    when(factory.build(eq(bundle), any(Environment.class), any(DataSourceFactory.class), anyList(), eq("hibernate"))).thenReturn(sessionFactory);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DropwizardResourceConfig(io.dropwizard.jersey.DropwizardResourceConfig) Environment(io.dropwizard.setup.Environment) JerseyEnvironment(io.dropwizard.jersey.setup.JerseyEnvironment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with DropwizardResourceConfig

use of io.dropwizard.jersey.DropwizardResourceConfig in project dropwizard by dropwizard.

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    dbConfig.setProperties(Collections.singletonMap("hibernate.jdbc.time_zone", "UTC"));
    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:h2:mem:DbTest-" + System.nanoTime());
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.h2.Driver");
    dbConfig.setValidationQuery("SELECT 1");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, Collections.singletonList(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();
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new PersistenceExceptionMapper());
    config.register(new JacksonFeature(Jackson.newObjectMapper()));
    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) JacksonFeature(io.dropwizard.jersey.jackson.JacksonFeature) 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)

Aggregations

DataSourceFactory (io.dropwizard.db.DataSourceFactory)2 DropwizardResourceConfig (io.dropwizard.jersey.DropwizardResourceConfig)2 Environment (io.dropwizard.setup.Environment)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 JacksonFeature (io.dropwizard.jersey.jackson.JacksonFeature)1 EmptyOptionalExceptionMapper (io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper)1 JerseyEnvironment (io.dropwizard.jersey.setup.JerseyEnvironment)1 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1