Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from scottescue/issue/21/support-serializing-l…
…azy-loaded-entities Building the ability to serialize lazy loaded entity associations
- Loading branch information
Showing
14 changed files
with
653 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/com/scottescue/dropwizard/entitymanager/AbstractIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.scottescue.dropwizard.entitymanager; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.dropwizard.Application; | ||
import io.dropwizard.Configuration; | ||
import io.dropwizard.db.DataSourceFactory; | ||
import io.dropwizard.jackson.Jackson; | ||
import io.dropwizard.jersey.jackson.JacksonMessageBodyProvider; | ||
import io.dropwizard.testing.ConfigOverride; | ||
import io.dropwizard.testing.DropwizardTestSupport; | ||
import io.dropwizard.testing.ResourceHelpers; | ||
import org.glassfish.jersey.client.JerseyClientBuilder; | ||
import org.junit.After; | ||
|
||
import javax.ws.rs.client.Client; | ||
|
||
public abstract class AbstractIntegrationTest { | ||
|
||
public static class TestConfiguration extends Configuration { | ||
private DataSourceFactory dataSource = new DataSourceFactory(); | ||
|
||
TestConfiguration(@JsonProperty("dataSource") DataSourceFactory dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public DataSourceFactory getDataSource() { | ||
return dataSource; | ||
} | ||
} | ||
|
||
final protected Client client = new JerseyClientBuilder() | ||
.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper())) | ||
.build(); | ||
|
||
private DropwizardTestSupport dropwizardTestSupport; | ||
|
||
@After | ||
public void tearDown() { | ||
dropwizardTestSupport.after(); | ||
client.close(); | ||
} | ||
|
||
protected void setup(Class<? extends Application<TestConfiguration>> applicationClass) { | ||
dropwizardTestSupport = new DropwizardTestSupport<>(applicationClass, ResourceHelpers.resourceFilePath("integration-test.yaml"), | ||
ConfigOverride.config("dataSource.url", "jdbc:hsqldb:mem:DbTest" + System.nanoTime() + "?hsqldb.translate_dti_types=false")); | ||
dropwizardTestSupport.before(); | ||
} | ||
|
||
protected String getUrlPrefix() { | ||
return "http://localhost:" + dropwizardTestSupport.getLocalPort(); | ||
} | ||
|
||
protected String getUrl(String path) { | ||
return getUrlPrefix() + path; | ||
} | ||
|
||
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "unchecked"}) | ||
protected static <T extends Throwable> T unwrapThrowable(Class<T> type, Throwable throwable) { | ||
Throwable cause = throwable.getCause(); | ||
if (cause == null) { | ||
return null; | ||
} | ||
return cause.getClass().equals(type) ? (T) cause : unwrapThrowable(type, cause); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/com/scottescue/dropwizard/entitymanager/AbstractTestApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.scottescue.dropwizard.entitymanager; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.dropwizard.db.PooledDataSourceFactory; | ||
import io.dropwizard.setup.Bootstrap; | ||
import io.dropwizard.setup.Environment; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
import javax.persistence.PersistenceException; | ||
|
||
public abstract class AbstractTestApplication extends io.dropwizard.Application<AbstractIntegrationTest.TestConfiguration> { | ||
final EntityManagerBundle<AbstractIntegrationTest.TestConfiguration> entityManagerBundle = new EntityManagerBundle<AbstractIntegrationTest.TestConfiguration>( | ||
supportedEntities(), | ||
new EntityManagerFactoryFactory(), | ||
new SharedEntityManagerFactory()) { | ||
@Override | ||
public PooledDataSourceFactory getDataSourceFactory(AbstractIntegrationTest.TestConfiguration configuration) { | ||
return configuration.getDataSource(); | ||
} | ||
}; | ||
|
||
@Override | ||
public void initialize(Bootstrap<AbstractIntegrationTest.TestConfiguration> bootstrap) { | ||
bootstrap.addBundle(entityManagerBundle); | ||
onInitialize(bootstrap); | ||
} | ||
|
||
@Override | ||
public void run(AbstractIntegrationTest.TestConfiguration configuration, Environment environment) throws Exception { | ||
final EntityManagerFactory entityManagerFactory = entityManagerBundle.getEntityManagerFactory(); | ||
initDatabase(entityManagerFactory); | ||
|
||
environment.jersey().register(new UnitOfWorkApplicationListener("hr-db", entityManagerFactory)); | ||
onRun(configuration, environment); | ||
} | ||
|
||
protected void initDatabase(EntityManagerFactory entityManagerFactory) { | ||
final EntityManager entityManager = entityManagerFactory.createEntityManager(); | ||
final EntityTransaction transaction = entityManager.getTransaction(); | ||
try { | ||
transaction.begin(); | ||
|
||
onInitDatabase(entityManager); | ||
|
||
transaction.commit(); | ||
} catch (PersistenceException e) { | ||
if (transaction.isActive()) { | ||
transaction.rollback(); | ||
} | ||
} finally { | ||
entityManager.close(); | ||
} | ||
} | ||
|
||
protected abstract ImmutableList<Class<?>> supportedEntities(); | ||
|
||
protected void onInitialize(Bootstrap<AbstractIntegrationTest.TestConfiguration> bootstrap) {} | ||
|
||
protected void onRun(AbstractIntegrationTest.TestConfiguration configuration, Environment environment) throws Exception {} | ||
|
||
protected void onInitDatabase(EntityManager entityManager) {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.