RestResponse: Add helper methods to perform assertions on status code

Add new helper methods on RestResponse to do assertions on the status code
using the assertThat method from Google Truth.

Modify tests to use the new helper methods.

Change-Id: Ia91199d4e254a3a47d6828b964be3ff37f55ff8e
This commit is contained in:
David Pursehouse
2015-11-06 14:17:08 +09:00
parent 312c546a76
commit 86d184672e
24 changed files with 243 additions and 241 deletions

View File

@@ -14,9 +14,12 @@
package com.google.gerrit.acceptance;
import static com.google.common.truth.Truth.assert_;
import static com.google.gerrit.httpd.restapi.RestApiServlet.JSON_MAGIC;
import static java.nio.charset.StandardCharsets.UTF_8;
import org.apache.http.HttpStatus;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
@@ -36,4 +39,51 @@ public class RestResponse extends HttpResponse {
}
return reader;
}
public void assertStatus(int status) throws Exception {
assert_()
.withFailureMessage(String.format("Expected status code %d", status))
.that(getStatusCode())
.isEqualTo(status);
}
public void assertOK() throws Exception {
assertStatus(HttpStatus.SC_OK);
}
public void assertNotFound() throws Exception {
assertStatus(HttpStatus.SC_NOT_FOUND);
}
public void assertConflict() throws Exception {
assertStatus(HttpStatus.SC_CONFLICT);
}
public void assertForbidden() throws Exception {
assertStatus(HttpStatus.SC_FORBIDDEN);
}
public void assertNoContent() throws Exception {
assertStatus(HttpStatus.SC_NO_CONTENT);
}
public void assertBadRequest() throws Exception {
assertStatus(HttpStatus.SC_BAD_REQUEST);
}
public void assertUnprocessableEntity() throws Exception {
assertStatus(HttpStatus.SC_UNPROCESSABLE_ENTITY);
}
public void assertMethodNotAllowed() throws Exception {
assertStatus(HttpStatus.SC_METHOD_NOT_ALLOWED);
}
public void assertCreated() throws Exception {
assertStatus(HttpStatus.SC_CREATED);
}
public void assertPreconditionFailed() throws Exception {
assertStatus(HttpStatus.SC_PRECONDITION_FAILED);
}
}