package com.example.helloworld; import com.example.helloworld.api.Saying; import com.example.helloworld.core.Person; import io.dropwizard.testing.ConfigOverride; import io.dropwizard.testing.ResourceHelpers; import io.dropwizard.testing.junit.DropwizardAppRule; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import java.io.File; import java.io.IOException; import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; public class IntegrationTest { private static final String TMP_FILE = createTempFile(); private static final String CONFIG_PATH = ResourceHelpers.resourceFilePath("test-example.yml"); @ClassRule public static final DropwizardAppRule<HelloWorldConfiguration> RULE = new DropwizardAppRule<>( HelloWorldApplication.class, CONFIG_PATH, ConfigOverride.config("database.url", "jdbc:h2:" + TMP_FILE)); @BeforeClass public static void migrateDb() throws Exception { RULE.getApplication().run("db", "migrate", CONFIG_PATH); } private static String createTempFile() { try { return File.createTempFile("test-example", null).getAbsolutePath(); } catch (IOException e) { throw new IllegalStateException(e); } } @Test public void testHelloWorld() throws Exception { final Optional<String> name = Optional.of("Dr. IntegrationTest"); final Saying saying = RULE.client().target("http://localhost:" + RULE.getLocalPort() + "/hello-world") .queryParam("name", name.get()) .request() .get(Saying.class); assertThat(saying.getContent()).isEqualTo(RULE.getConfiguration().buildTemplate().render(name)); } @Test public void testPostPerson() throws Exception { final Person person = new Person("Dr. IntegrationTest", "Chief Wizard"); final Person newPerson = RULE.client().target("http://localhost:" + RULE.getLocalPort() + "/people") .request() .post(Entity.entity(person, MediaType.APPLICATION_JSON_TYPE)) .readEntity(Person.class); assertThat(newPerson.getId()).isNotNull(); assertThat(newPerson.getFullName()).isEqualTo(person.getFullName()); assertThat(newPerson.getJobTitle()).isEqualTo(person.getJobTitle()); } }