public
Created

  • Download Gist
AbstractControllerTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
package controllers;
 
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.callAction;
import static play.test.Helpers.fakeRequest;
import static play.test.Helpers.status;
 
import java.util.HashMap;
import java.util.Map;
 
import models.User;
import play.mvc.HandlerRef;
import play.mvc.Http.Cookie;
import play.mvc.Http.Session;
import play.mvc.Result;
import play.test.FakeRequest;
import play.test.Helpers;
import test.FakeApplicationAwareTest;
 
public abstract class AbstractControllerTest extends FakeApplicationAwareTest {
 
public static class Given {
 
private Result result;
private HandlerRef action;
private User loggedUser;
private Map<String, String> formData = new HashMap<String, String>();
 
public Given(HandlerRef action) {
this.action = action;
}
 
public static Given action(HandlerRef action) {
return new Given(action);
}
 
public Given withUser(User user) {
this.loggedUser = user;
return this;
}
 
public Given withInput(String name, String value) {
this.formData.put(name, value);
return this;
}
 
public Given withInput(String name, Long value) {
this.formData.put(name, String.valueOf(value));
return this;
}
 
public Given withInput(String name, Integer value) {
this.formData.put(name, String.valueOf(value));
return this;
}
 
public Given expectStatus(int expectedStatus) {
assertThat(status(result)).isEqualTo(expectedStatus);
return this;
}
 
public Given expectContentType(String contentType) {
assertThat(Helpers.contentType(result)).isEqualTo(contentType);
return this;
}
 
public Given expectTextPresent(String text) {
assertThat(Helpers.contentAsString(this.result)).contains(text);
return this;
}
 
public Given expectTextNotPresent(String text) {
assertThat(Helpers.contentAsString(this.result)).doesNotContain(text);
return this;
}
 
public Given expectRedirectTo(String location) {
assertThat(Helpers.header("Location", this.result)).isEqualTo(location);
return this;
}
 
public Given expectedCookie(String expectedValue) {
Cookie cookie = Helpers.cookie("PLAY_SESSION", this.result);
assertThat(cookie.value()).isEqualTo(expectedValue);
return this;
}
 
public Given expectedSessionValue(String key, String value) {
Session session = Helpers.session(this.result);
assertThat(session.get(key)).isEqualTo(value);
return this;
}
 
public Given execute() {
FakeRequest request = fakeRequest();
request.withFormUrlEncodedBody(this.formData);
 
if(loggedUser != null) {
request.withSession(Secured.SESSION_KEY, String.valueOf(loggedUser.id));
}
 
this.result = callAction(this.action, request);
 
return this;
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.