CacheOperationsIT: Assert that all REST calls were successful

Otherwise the error message is misleading if a REST call fails and we
try to parse a JSON object from the response.

Change-Id: I4d48849feba8cd0cd98ad47f9cb798901c3d5871
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2016-12-28 08:39:36 +01:00
parent 1b44bb8430
commit 63a6529c70
2 changed files with 36 additions and 14 deletions

View File

@ -23,6 +23,7 @@ import com.google.gerrit.extensions.restapi.RawInput;
import com.google.gerrit.server.OutputFormat; import com.google.gerrit.server.OutputFormat;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpStatus;
import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Request;
import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.InputStreamEntity;
@ -37,6 +38,18 @@ public class RestSession extends HttpSession {
super(server, account); super(server, account);
} }
public RestResponse getOK(String endPoint)
throws Exception {
return get(endPoint, HttpStatus.SC_OK);
}
public RestResponse get(String endPoint, int expectedStatus)
throws Exception {
RestResponse r = get(endPoint);
r.assertStatus(expectedStatus);
return r;
}
public RestResponse get(String endPoint) throws IOException { public RestResponse get(String endPoint) throws IOException {
return getWithHeader(endPoint, null); return getWithHeader(endPoint, null);
} }
@ -102,6 +115,18 @@ public class RestSession extends HttpSession {
return post(endPoint, null); return post(endPoint, null);
} }
public RestResponse postOK(String endPoint, Object content)
throws Exception {
return post(endPoint, content, HttpStatus.SC_OK);
}
public RestResponse post(String endPoint, Object content, int expectedStatus)
throws Exception {
RestResponse r = post(endPoint, content);
r.assertStatus(expectedStatus);
return r;
}
public RestResponse post(String endPoint, Object content) throws IOException { public RestResponse post(String endPoint, Object content) throws IOException {
return postWithHeader(endPoint, content, null); return postWithHeader(endPoint, content, null);
} }

View File

@ -33,15 +33,14 @@ public class CacheOperationsIT extends AbstractDaemonTest {
@Test @Test
public void flushAll() throws Exception { public void flushAll() throws Exception {
RestResponse r = adminRestSession.get("/config/server/caches/project_list"); RestResponse r = adminRestSession.getOK("/config/server/caches/project_list");
CacheInfo cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); CacheInfo cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isGreaterThan((long) 0); assertThat(cacheInfo.entries.mem).isGreaterThan((long) 0);
r = adminRestSession.post("/config/server/caches/", new PostCaches.Input(FLUSH_ALL)); r = adminRestSession.postOK("/config/server/caches/", new PostCaches.Input(FLUSH_ALL));
r.assertOK();
r.consume(); r.consume();
r = adminRestSession.get("/config/server/caches/project_list"); r = adminRestSession.getOK("/config/server/caches/project_list");
cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isNull(); assertThat(cacheInfo.entries.mem).isNull();
} }
@ -62,24 +61,23 @@ public class CacheOperationsIT extends AbstractDaemonTest {
@Test @Test
public void flush() throws Exception { public void flush() throws Exception {
RestResponse r = adminRestSession.get("/config/server/caches/project_list"); RestResponse r = adminRestSession.getOK("/config/server/caches/project_list");
CacheInfo cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); CacheInfo cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isGreaterThan((long)0); assertThat(cacheInfo.entries.mem).isGreaterThan((long)0);
r = adminRestSession.get("/config/server/caches/projects"); r = adminRestSession.getOK("/config/server/caches/projects");
cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isGreaterThan((long)1); assertThat(cacheInfo.entries.mem).isGreaterThan((long)1);
r = adminRestSession.post("/config/server/caches/", r = adminRestSession.postOK("/config/server/caches/",
new PostCaches.Input(FLUSH, Arrays.asList("accounts", "project_list"))); new PostCaches.Input(FLUSH, Arrays.asList("accounts", "project_list")));
r.assertOK();
r.consume(); r.consume();
r = adminRestSession.get("/config/server/caches/project_list"); r = adminRestSession.getOK("/config/server/caches/project_list");
cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isNull(); assertThat(cacheInfo.entries.mem).isNull();
r = adminRestSession.get("/config/server/caches/projects"); r = adminRestSession.getOK("/config/server/caches/projects");
cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isGreaterThan((long)1); assertThat(cacheInfo.entries.mem).isGreaterThan((long)1);
} }
@ -102,7 +100,7 @@ public class CacheOperationsIT extends AbstractDaemonTest {
@Test @Test
public void flush_UnprocessableEntity() throws Exception { public void flush_UnprocessableEntity() throws Exception {
RestResponse r = adminRestSession.get("/config/server/caches/projects"); RestResponse r = adminRestSession.getOK("/config/server/caches/projects");
CacheInfo cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); CacheInfo cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isGreaterThan((long)0); assertThat(cacheInfo.entries.mem).isGreaterThan((long)0);
@ -111,7 +109,7 @@ public class CacheOperationsIT extends AbstractDaemonTest {
r.assertUnprocessableEntity(); r.assertUnprocessableEntity();
r.consume(); r.consume();
r = adminRestSession.get("/config/server/caches/projects"); r = adminRestSession.getOK("/config/server/caches/projects");
cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class); cacheInfo = newGson().fromJson(r.getReader(), CacheInfo.class);
assertThat(cacheInfo.entries.mem).isGreaterThan((long)0); assertThat(cacheInfo.entries.mem).isGreaterThan((long)0);
} }
@ -121,9 +119,8 @@ public class CacheOperationsIT extends AbstractDaemonTest {
allowGlobalCapabilities(REGISTERED_USERS, allowGlobalCapabilities(REGISTERED_USERS,
GlobalCapability.FLUSH_CACHES, GlobalCapability.VIEW_CACHES); GlobalCapability.FLUSH_CACHES, GlobalCapability.VIEW_CACHES);
try { try {
RestResponse r = userRestSession.post("/config/server/caches/", RestResponse r = userRestSession.postOK("/config/server/caches/",
new PostCaches.Input(FLUSH, Arrays.asList("projects"))); new PostCaches.Input(FLUSH, Arrays.asList("projects")));
r.assertOK();
r.consume(); r.consume();
userRestSession userRestSession