Skip to content

Commit

Permalink
added flyway and use jbdc H2 instance for Person GET
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenalexander committed Aug 29, 2014
1 parent b1c01df commit d2f5a75
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ logs
*.iml
*.ipr
*.iws
*.db
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,35 @@

Test project for Dropwizard 7.1 using JDBI to interact with a DB.

Also using Flyway and H2 to create an in memory DB with test data for integration testing.
Also using Flyway and H2 to create an in memory DB with test data for integration testing.

## Setup H2 DB

Creates an H2 DB populated with the migration scripts in src/main/resources/db/migration

```
gradle flywayMigrate -i
```

## Run Dropwizard service

To compile:

```
gradle oneJar
```

To run:

```
java -jar build/libs/dropwizard-jdbi-standalone.jar server config.yml
```

Running the service will start an embedded H2 instance using the person.mv.db file generated by flyway.

To test:

```
gradle test
```

14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'java'
apply plugin: 'flyway'
apply plugin: 'gradle-one-jar'
apply plugin: 'application'
apply plugin: 'idea'
Expand All @@ -21,6 +22,8 @@ buildscript {

dependencies {
classpath 'com.github.rholder:gradle-one-jar:1.0.3'
classpath 'com.h2database:h2:1.4.181'
classpath 'org.flywaydb:flyway-gradle-plugin:3.0'
}
}

Expand All @@ -37,7 +40,10 @@ repositories {
dependencies {
compile (
'io.dropwizard:dropwizard-core:' + dropwizardVersion,
'com.sun.jersey:jersey-core:' + jerseyVersion
'io.dropwizard:dropwizard-jdbi:' + dropwizardVersion,
'com.sun.jersey:jersey-core:' + jerseyVersion,
'com.h2database:h2:1.4.181',
'org.flywaydb:flyway-gradle-plugin:3.0'
)
testCompile (
'io.dropwizard:dropwizard-testing:' + dropwizardVersion,
Expand All @@ -47,6 +53,12 @@ dependencies {
)
}

// Configure flyway
flyway {
url = 'jdbc:h2:file:./persondb'
user = 'sa'
}

// Configure the oneJar task
task oneJar(type: OneJar) {
mainClass = mainClassName
Expand Down
17 changes: 16 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,19 @@ server:
port: 8095
adminConnectors:
- type: http
port: 8096
port: 8096

database:
driverClass: org.h2.Driver
user: sa
# password:
url: jdbc:h2:./persondb
properties:
charSet: UTF-8
maxWaitForConnection: 1s
validationQuery: "/* MyService Health Check */ SELECT 1"
minSize: 8
maxSize: 32
checkConnectionWhileIdle: false
evictionInterval: 10s
minIdleTime: 1 minute
11 changes: 9 additions & 2 deletions src/main/java/com/example/ExampleApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.example;

import com.example.dao.PersonDAO;
import com.example.resources.PersonResource;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.jdbi.DBIFactory;
import org.skife.jdbi.v2.DBI;

public class ExampleApplication extends Application<ExampleConfiguration> {
public static void main(String[] args) throws Exception {
Expand All @@ -20,8 +23,12 @@ public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
}

@Override
public void run(ExampleConfiguration configuration, Environment environment) {
final PersonResource personResource = new PersonResource();
public void run(ExampleConfiguration configuration, Environment environment) throws ClassNotFoundException {
final DBIFactory factory = new DBIFactory();
final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "h2");

final PersonDAO personDAO = jdbi.onDemand(PersonDAO.class);
final PersonResource personResource = new PersonResource(personDAO);

environment.jersey().register(personResource);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/example/ExampleConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package com.example;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

public class ExampleConfiguration extends Configuration {

@Valid
@NotNull
@JsonProperty("database")
private DataSourceFactory database = new DataSourceFactory();

public DataSourceFactory getDataSourceFactory() {
return database;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/example/dao/PersonDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.dao;

import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.SqlQuery;

public interface PersonDAO {
@SqlQuery("select NAME from PERSON where ID = :id")
String findNameById(@Bind("id") int id);
}
11 changes: 10 additions & 1 deletion src/main/java/com/example/resources/PersonResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.resources;

import com.example.core.Person;
import com.example.dao.PersonDAO;

import javax.validation.Valid;
import javax.ws.rs.*;
Expand All @@ -13,6 +14,12 @@
@Produces({MediaType.APPLICATION_JSON})
public class PersonResource {

PersonDAO personDAO;

public PersonResource(PersonDAO personDAO) {
this.personDAO = personDAO;
}

@GET
public List<Person> getAll(){

Expand All @@ -34,9 +41,11 @@ public List<Person> getAll(){
@GET
@Path("/{id}")
public Person get(@PathParam("id") Integer id){
String name = personDAO.findNameById(1);

return new Person()
.setId(id)
.setName("person" + id.toString());
.setName(name);
}

@POST
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/db/migration/V1__Create_person_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table PERSON (
ID int not null,
NAME varchar(100) not null
);
3 changes: 3 additions & 0 deletions src/main/resources/db/migration/V2__Add_people.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
insert into PERSON (ID, NAME) values (1, 'Axel');
insert into PERSON (ID, NAME) values (2, 'Mr. Foo');
insert into PERSON (ID, NAME) values (3, 'Ms. Bar');
9 changes: 8 additions & 1 deletion src/test/java/com/example/resources/PersonResourceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.core.Person;
import com.example.core.PersonTests;
import com.example.dao.PersonDAO;
import com.sun.jersey.api.client.GenericType;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.ClassRule;
Expand All @@ -14,19 +15,22 @@
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

/**
* Tests {@link io.dropwizard.testing.junit.ResourceTestRule}
*/
public class PersonResourceTests {

private static final PersonDAO personDAO = mock(PersonDAO.class);

static {
Logger.getLogger("com.sun.jersey").setLevel(Level.OFF);
}

@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new PersonResource())
.addResource(new PersonResource(personDAO))
.build();

@Test
Expand All @@ -38,7 +42,10 @@ public void getAll() throws Exception {

@Test
public void get() throws Exception {
when(personDAO.findNameById(1)).thenReturn("person1");

Person person = resources.client().resource("/person/1").get(Person.class);

assertEquals("person1", person.getName());
}

Expand Down

0 comments on commit d2f5a75

Please sign in to comment.