Search in sources :

Example 6 with DBIFactory

use of io.dropwizard.jdbi.DBIFactory in project dropwizard by dropwizard.

the class OptionalLocalDateTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:optional-local-date-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE IF NOT EXISTS tasks (" + "id INT PRIMARY KEY, " + "assignee VARCHAR(255) NOT NULL, " + "start_date TIMESTAMP, " + "end_date TIMESTAMP, " + "comments VARCHAR(1024) " + ")");
    }
    dao = dbi.onDemand(TaskDao.class);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DBI(org.skife.jdbi.v2.DBI) DBIFactory(io.dropwizard.jdbi.DBIFactory) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 7 with DBIFactory

use of io.dropwizard.jdbi.DBIFactory in project dropwizard by dropwizard.

the class OptionalLocalDateTimeTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:optional-local-date-time" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE IF NOT EXISTS tasks (" + "id INT PRIMARY KEY, " + "assignee VARCHAR(255) NOT NULL, " + "start_date TIMESTAMP, " + "end_date TIMESTAMP, " + "comments VARCHAR(1024) " + ")");
    }
    dao = dbi.onDemand(TaskDao.class);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DBI(org.skife.jdbi.v2.DBI) DBIFactory(io.dropwizard.jdbi.DBIFactory) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 8 with DBIFactory

use of io.dropwizard.jdbi.DBIFactory in project irontest by zheng-wang.

the class IronTestApplication method createSampleResources.

private void createSampleResources(IronTestConfiguration configuration, Environment environment) {
    final DBIFactory factory = new DBIFactory();
    final DBI jdbi = factory.build(environment, configuration.getSampleDatabase(), "sampleDatabase");
    // create DAO objects
    final ArticleDAO articleDAO = jdbi.onDemand(ArticleDAO.class);
    // create database tables
    articleDAO.createTableIfNotExists();
    // register APIs
    environment.jersey().register(new ArticleResource(articleDAO));
    // register SOAP web services
    jaxWsBundle.publishEndpoint(new EndpointBuilder("/article", new ArticleSOAP(articleDAO)));
}
Also used : DBI(org.skife.jdbi.v2.DBI) EndpointBuilder(com.roskart.dropwizard.jaxws.EndpointBuilder) ArticleSOAP(io.irontest.ws.ArticleSOAP) DBIFactory(io.dropwizard.jdbi.DBIFactory)

Example 9 with DBIFactory

use of io.dropwizard.jdbi.DBIFactory in project irontest by zheng-wang.

the class IronTestApplication method createSystemResources.

private void createSystemResources(IronTestConfiguration configuration, Environment environment) {
    final DBIFactory dbiFactory = new DBIFactory();
    final DBI jdbi = dbiFactory.build(environment, configuration.getSystemDatabase(), "systemDatabase");
    // create DAO objects
    final FolderDAO folderDAO = jdbi.onDemand(FolderDAO.class);
    final EnvironmentDAO environmentDAO = jdbi.onDemand(EnvironmentDAO.class);
    final EndpointDAO endpointDAO = jdbi.onDemand(EndpointDAO.class);
    final TestcaseDAO testcaseDAO = jdbi.onDemand(TestcaseDAO.class);
    final TeststepDAO teststepDAO = jdbi.onDemand(TeststepDAO.class);
    final AssertionDAO assertionDAO = jdbi.onDemand(AssertionDAO.class);
    final UtilsDAO utilsDAO = jdbi.onDemand(UtilsDAO.class);
    final FolderTreeNodeDAO folderTreeNodeDAO = jdbi.onDemand(FolderTreeNodeDAO.class);
    final UserDefinedPropertyDAO udpDAO = jdbi.onDemand(UserDefinedPropertyDAO.class);
    final DataTableColumnDAO dataTableColumnDAO = jdbi.onDemand(DataTableColumnDAO.class);
    final DataTableCellDAO dataTableCellDAO = jdbi.onDemand(DataTableCellDAO.class);
    final TestcaseRunDAO testcaseRunDAO = jdbi.onDemand(TestcaseRunDAO.class);
    final TestcaseIndividualRunDAO testcaseIndividualRunDAO = jdbi.onDemand(TestcaseIndividualRunDAO.class);
    final TeststepRunDAO teststepRunDAO = jdbi.onDemand(TeststepRunDAO.class);
    UserDAO userDAO = null;
    if (isInTeamMode(configuration)) {
        userDAO = jdbi.onDemand(UserDAO.class);
    }
    AppInfo appInfo = new AppInfo();
    if (isInTeamMode(configuration)) {
        appInfo.setAppMode(AppMode.TEAM);
        // ignore bindHost
        DefaultServerFactory server = (DefaultServerFactory) configuration.getServerFactory();
        List<ConnectorFactory> applicationConnectors = server.getApplicationConnectors();
        HttpConnectorFactory httpConnectorFactory = (HttpConnectorFactory) applicationConnectors.get(0);
        httpConnectorFactory.setBindHost(null);
        // turn on user authentication and authorization
        environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<SimplePrincipal>().setAuthenticator(new ResourceAuthenticator(userDAO)).setAuthorizer(new ResourceAuthorizer()).buildAuthFilter()));
        environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthResponseFilter());
    }
    // create database tables
    // order is important!!! (there are foreign keys linking them)
    folderDAO.createSequenceIfNotExists();
    folderDAO.createTableIfNotExists();
    folderDAO.insertARootNodeIfNotExists();
    environmentDAO.createSequenceIfNotExists();
    environmentDAO.createTableIfNotExists();
    endpointDAO.createSequenceIfNotExists();
    endpointDAO.createTableIfNotExists();
    testcaseDAO.createSequenceIfNotExists();
    testcaseDAO.createTableIfNotExists();
    teststepDAO.createSequenceIfNotExists();
    teststepDAO.createTableIfNotExists();
    assertionDAO.createSequenceIfNotExists();
    assertionDAO.createTableIfNotExists();
    udpDAO.createSequenceIfNotExists();
    udpDAO.createTableIfNotExists();
    dataTableColumnDAO.createSequenceIfNotExists();
    dataTableColumnDAO.createTableIfNotExists();
    dataTableCellDAO.createSequenceIfNotExists();
    dataTableCellDAO.createTableIfNotExists();
    testcaseRunDAO.createSequenceIfNotExists();
    testcaseRunDAO.createTableIfNotExists();
    testcaseIndividualRunDAO.createSequenceIfNotExists();
    testcaseIndividualRunDAO.createTableIfNotExists();
    teststepRunDAO.createSequenceIfNotExists();
    teststepRunDAO.createTableIfNotExists();
    if (isInTeamMode(configuration)) {
        userDAO.createSequenceIfNotExists();
        userDAO.createTableIfNotExists();
        userDAO.insertBuiltinAdminUserIfNotExists();
    }
    // register APIs
    environment.jersey().register(new SystemResource(appInfo));
    environment.jersey().register(new ManagedEndpointResource(appInfo, endpointDAO));
    environment.jersey().register(new TestcaseResource(testcaseDAO, teststepDAO));
    environment.jersey().register(new FolderResource(folderDAO));
    environment.jersey().register(new FolderTreeNodeResource(folderTreeNodeDAO));
    environment.jersey().register(new TeststepResource(appInfo, teststepDAO, udpDAO, utilsDAO));
    environment.jersey().register(new WSDLResource());
    environment.jersey().register(new EnvironmentResource(environmentDAO));
    environment.jersey().register(new TestcaseRunResource(testcaseDAO, udpDAO, teststepDAO, utilsDAO, testcaseRunDAO, teststepRunDAO));
    environment.jersey().register(new AssertionResource(udpDAO, teststepDAO, utilsDAO));
    environment.jersey().register(new UDPResource(udpDAO));
    environment.jersey().register(new DataTableResource(utilsDAO));
    if (isInTeamMode(configuration)) {
        environment.jersey().register(new UserResource(userDAO));
    }
    // if turned on in config.yml, register jersey LoggingFilter (used for logging Iron Test resource oriented HTTP API requests and responses)
    DefaultLoggingFactory defaultLoggingFactory = (DefaultLoggingFactory) configuration.getLoggingFactory();
    if (defaultLoggingFactory.getLoggers().containsKey(LoggingFilter.class.getName())) {
        environment.jersey().register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true));
    }
    // register exception mappers
    environment.jersey().register(new IronTestLoggingExceptionMapper());
}
Also used : ResourceAuthenticator(io.irontest.auth.ResourceAuthenticator) LoggingFilter(org.glassfish.jersey.filter.LoggingFilter) DefaultLoggingFactory(io.dropwizard.logging.DefaultLoggingFactory) DBI(org.skife.jdbi.v2.DBI) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) ConnectorFactory(io.dropwizard.jetty.ConnectorFactory) BasicCredentialAuthFilter(io.dropwizard.auth.basic.BasicCredentialAuthFilter) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) ResourceAuthorizer(io.irontest.auth.ResourceAuthorizer) AuthResponseFilter(io.irontest.auth.AuthResponseFilter) AppInfo(io.irontest.models.AppInfo) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) SimplePrincipal(io.irontest.auth.SimplePrincipal) DBIFactory(io.dropwizard.jdbi.DBIFactory) DefaultServerFactory(io.dropwizard.server.DefaultServerFactory)

Example 10 with DBIFactory

use of io.dropwizard.jdbi.DBIFactory in project dropwizard by dropwizard.

the class DBIClient method before.

@Override
protected void before() throws Throwable {
    final Environment environment = new Environment("test", Jackson.newObjectMapper(), Validators.newValidator(), new MetricRegistry(), getClass().getClassLoader());
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:tcp://localhost/fldb");
    dataSourceFactory.setUser("sa");
    dataSourceFactory.setPassword("");
    // Set the time zone of the database
    final DBIFactory dbiFactory = new DBIFactory() {

        @Override
        protected Optional<TimeZone> databaseTimeZone() {
            return Optional.of(dbTimeZone);
        }
    };
    dbi = dbiFactory.build(environment, dataSourceFactory, "test-jdbi-time-zones");
    // Start the DB pool
    managedObjects = environment.lifecycle().getManagedObjects();
    for (LifeCycle managedObject : managedObjects) {
        managedObject.start();
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) TimeZone(java.util.TimeZone) DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) DBIFactory(io.dropwizard.jdbi.DBIFactory)

Aggregations

DBIFactory (io.dropwizard.jdbi.DBIFactory)19 DBI (org.skife.jdbi.v2.DBI)18 DataSourceFactory (io.dropwizard.db.DataSourceFactory)16 Before (org.junit.Before)15 Handle (org.skife.jdbi.v2.Handle)15 TableRow (com.airbnb.airpal.sql.beans.TableRow)1 QueryStoreMapper (com.airbnb.airpal.sql.jdbi.QueryStoreMapper)1 URIArgumentFactory (com.airbnb.airpal.sql.jdbi.URIArgumentFactory)1 UUIDArgumentFactory (com.airbnb.airpal.sql.jdbi.UUIDArgumentFactory)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Provides (com.google.inject.Provides)1 Singleton (com.google.inject.Singleton)1 EndpointBuilder (com.roskart.dropwizard.jaxws.EndpointBuilder)1 AuthDynamicFeature (io.dropwizard.auth.AuthDynamicFeature)1 BasicCredentialAuthFilter (io.dropwizard.auth.basic.BasicCredentialAuthFilter)1 ConnectorFactory (io.dropwizard.jetty.ConnectorFactory)1 HttpConnectorFactory (io.dropwizard.jetty.HttpConnectorFactory)1 DefaultLoggingFactory (io.dropwizard.logging.DefaultLoggingFactory)1 DefaultServerFactory (io.dropwizard.server.DefaultServerFactory)1 Environment (io.dropwizard.setup.Environment)1