Skip to content

Instantly share code, notes, and snippets.

View rowanl's full-sized avatar

Rowan rowanl

View GitHub Profile
@rowanl
rowanl / Charity.java
Created January 17, 2018 19:28
playframework-kubernetes - Charity model
@Entity
@Table(name="charity")
public class Charity extends Model {
@Id
public Integer id;
@Column(name="name")
public String name;
@rowanl
rowanl / CharityRepository.java
Last active January 17, 2018 20:05
playframework-kubernetes - Asynchronously find all models using Ebean in a non-blocking fashion
private <T> CompletionStage<T> wrapContext(Supplier<T> toRun) {
return supplyAsync(toRun, executionContext);
}
public CompletionStage<List<Charity>> findList() {
return wrapContext(() -> ebeanServer.find(Charity.class).findList());
}
@rowanl
rowanl / DatabaseExecutionContext.java
Created January 17, 2018 19:32
playframework-kubernetes - Execution context for database calls
public class DatabaseExecutionContext extends CustomExecutionContext {
@Inject
public DatabaseExecutionContext(ActorSystem actorSystem) {
super(actorSystem, "database.dispatcher");
}
}
@rowanl
rowanl / CharityController.java
Created January 17, 2018 19:41
playframework-kubernetes - Serving an HTML page
public class CharityController {
private final CharityRepository charityRepository;
private final HttpExecutionContext ec;
private final LocalDateTime endDateTime;
private final Clock clock;
@Inject
public CharityController(CharityRepository charityRepository,
HttpExecutionContext ec,
@rowanl
rowanl / routes
Created January 17, 2018 19:56
playframework-kubernetes - Route for index page
GET / controllers.CharityController.index()
@rowanl
rowanl / CharityControllerTest.java
Created January 17, 2018 19:59
playframework-kubernetes - Mocking time dependency in tests using Guice
public class CharityControllerTest extends WithServer {
private Clock clock;
@Override
protected Application provideApplication() {
return new GuiceApplicationBuilder()
.overrides(bindClockToMockInstance())
.build();
}
@rowanl
rowanl / CharityControllerTest.java
Created January 17, 2018 20:06
playframework-kubernetes - Functional test with mocked time dependency
@Test
public void Given_IsBeforeEndDate_When_GetRequestIsMadeToIndexPage_Then_StatusIs200() {
// GIVEN
long beforeEndDate = 9L;
given(clock.instant()).willReturn(ofEpochMilli(beforeEndDate));
given(clock.getZone()).willReturn(ZoneId.of("+0"));
// WHEN
WSResponse result = newClient(providePort()).url("/").get().toCompletableFuture().join();
@rowanl
rowanl / CharityRepositoryTest.java
Last active January 17, 2018 20:12
playframework-kubernetes - Testing a repository that uses Ebean
public class CharityRepositoryTest extends WithApplication {
private CharityRepository repository;
@Before
public void setUp() {
repository = instanceOf(CharityRepository.class);
CallableSql cleanCharityTable = getDefaultServer().createCallableSql("delete from charity");
getDefaultServer().execute(cleanCharityTable);
@rowanl
rowanl / CharityRepositoryTest.java
Created January 17, 2018 20:14
playframework-kubernetes - Testing Ebean queries using BDD
@Test
public void Given_CharityObjectsExist_When_FindListIsCalled_Then_AllCharityObjectsAreReturned() {
// GIVEN
rangeClosed(1, 4).boxed().map(Charity::new).forEach(Model::save);
// WHEN
List<Charity> result = repository.findList().toCompletableFuture().join();
// THEN
@rowanl
rowanl / CharityRepository.java
Created January 17, 2018 20:15
playframework-kubernetes - Empty implementation of find all method
public CompletionStage<List<Charity>> findList() {
return null;
}