Created
August 13, 2012 00:49
-
-
Save anonymous/3335952 to your computer and use it in GitHub Desktop.
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
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment