ServletTest: Clean up buildJson methods

Put type argument first so the path and query string are adjacent,
which helps readability.

Change "additional" query string arg to just a query string. The
failure behavior if callers forget to start with "&" is confusing,
since it just screws up the format arg. Whether format is appended or
prepended is an implementation detail.

Change-Id: I9c2ac8dcd62c65bcffddcf6262114122e6049ee1
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/HostIndexServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/HostIndexServletTest.java
index 9edfc26..f96a37c 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/HostIndexServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/HostIndexServletTest.java
@@ -120,7 +120,7 @@
   @Test
   public void rootJson() throws Exception {
     String name = repo.getRepository().getDescription().getRepositoryName();
-    Map<String, RepositoryDescription> res = buildJson("/", REPOS);
+    Map<String, RepositoryDescription> res = buildJson(REPOS, "/");
 
     assertThat(res).hasSize(1);
     assertThat(res).containsKey(name);
@@ -130,7 +130,7 @@
 
   @Test
   public void fooSubdirJson() throws Exception {
-    Map<String, RepositoryDescription> res = buildJson("/foo/", REPOS);
+    Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/");
 
     assertThat(res).hasSize(1);
     assertThat(res).containsKey("bar/repo");
@@ -140,7 +140,7 @@
 
   @Test
   public void fooBarSubdirJson() throws Exception {
-    Map<String, RepositoryDescription> res = buildJson("/foo/bar/", REPOS);
+    Map<String, RepositoryDescription> res = buildJson(REPOS, "/foo/bar/");
 
     assertThat(res).hasSize(1);
     assertThat(res).containsKey("repo");
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/LogServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/LogServletTest.java
index a7c9d70..893e259 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/LogServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/LogServletTest.java
@@ -37,7 +37,7 @@
   public void basicLog() throws Exception {
     RevCommit commit = repo.branch("HEAD").commit().create();
 
-    Log response = buildJson("/repo/+log", LOG, "");
+    Log response = buildJson(LOG, "/repo/+log");
     assertThat(response.log).hasSize(1);
     verifyJsonCommit(response.log.get(0), commit);
     assertThat(response.log.get(0).treeDiff).isNull();
@@ -50,7 +50,7 @@
     RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
     RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));
 
-    Log response = buildJson("/repo/+log/master", LOG, "&name-status=1");
+    Log response = buildJson(LOG, "/repo/+log/master", "name-status=1");
     assertThat(response.log).hasSize(2);
 
     Commit jc2 = response.log.get(0);
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java
index 4bc906a..144e311 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/PathServletTest.java
@@ -213,7 +213,7 @@
         .add("baz", "baz contents")
         .create());
 
-    Tree tree = buildJson("/repo/+/master/", Tree.class);
+    Tree tree = buildJson(Tree.class, "/repo/+/master/");
     assertThat(tree.id).isEqualTo(c.getTree().name());
     assertThat(tree.entries).hasSize(2);
     assertThat(tree.entries.get(0).mode).isEqualTo(0100644);
@@ -225,7 +225,7 @@
     assertThat(tree.entries.get(1).id).isEqualTo(repo.get(c.getTree(), "foo").name());
     assertThat(tree.entries.get(1).name).isEqualTo("foo");
 
-    tree = buildJson("/repo/+/master/foo", Tree.class);
+    tree = buildJson(Tree.class, "/repo/+/master/foo");
     assertThat(tree.id).isEqualTo(repo.get(c.getTree(), "foo").name());
     assertThat(tree.entries).hasSize(1);
     assertThat(tree.entries.get(0).mode).isEqualTo(0100644);
@@ -233,7 +233,7 @@
     assertThat(tree.entries.get(0).id).isEqualTo(repo.get(c.getTree(), "foo/bar").name());
     assertThat(tree.entries.get(0).name).isEqualTo("bar");
 
-    tree = buildJson("/repo/+/master/foo/", Tree.class);
+    tree = buildJson(Tree.class, "/repo/+/master/foo/");
     assertThat(tree.id).isEqualTo(repo.get(c.getTree(), "foo").name());
     assertThat(tree.entries).hasSize(1);
     assertThat(tree.entries.get(0).mode).isEqualTo(0100644);
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/RefServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/RefServletTest.java
index 1cc33e5..c0455fa 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/RefServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/RefServletTest.java
@@ -200,7 +200,7 @@
   }
 
   private Map<String, RefJsonData> buildRefJson(String path) throws Exception {
-    return buildJson(path, new TypeToken<Map<String, RefJsonData>>() {});
+    return buildJson(new TypeToken<Map<String, RefJsonData>>() {}, path);
   }
 
   @Test
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/ServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/ServletTest.java
index b0a1dd1..8f6224a 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/ServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/ServletTest.java
@@ -19,6 +19,7 @@
 import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 
+import com.google.common.base.Strings;
 import com.google.common.net.HttpHeaders;
 import com.google.gson.FieldNamingPolicy;
 import com.google.gson.Gson;
@@ -92,8 +93,10 @@
     return res;
   }
 
-  private String buildJsonRaw(String path, String additionalQueryString) throws Exception {
-    FakeHttpServletResponse res = buildResponse(path, "format=json" + additionalQueryString, SC_OK);
+  private String buildJsonRaw(String path, String queryString) throws Exception {
+    String fmt = "format=JSON";
+    queryString = Strings.isNullOrEmpty(queryString) ? fmt : fmt + "&" + queryString;
+    FakeHttpServletResponse res = buildResponse(path, queryString, SC_OK);
     assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json");
     String body = res.getActualBodyString();
     String magic = ")]}'\n";
@@ -101,22 +104,22 @@
     return body.substring(magic.length());
   }
 
-  protected <T> T buildJson(String path, Class<T> classOfT, String additionalQueryString)
+  protected <T> T buildJson(Class<T> classOfT, String path, String queryString)
       throws Exception {
-    return newGson().fromJson(buildJsonRaw(path, additionalQueryString), classOfT);
+    return newGson().fromJson(buildJsonRaw(path, queryString), classOfT);
   }
 
-  protected <T> T buildJson(String path, Class<T> classOfT) throws Exception {
-    return buildJson(path, classOfT, "");
+  protected <T> T buildJson(Class<T> classOfT, String path) throws Exception {
+    return buildJson(classOfT, path, null);
   }
 
-  protected <T> T buildJson(String path, TypeToken<T> typeOfT, String additionalQueryString)
+  protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString)
       throws Exception {
-    return newGson().fromJson(buildJsonRaw(path, additionalQueryString), typeOfT.getType());
+    return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType());
   }
 
-  protected <T> T buildJson(String path, TypeToken<T> typeOfT) throws Exception {
-    return buildJson(path, typeOfT, "");
+  protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception {
+    return buildJson(typeOfT, path, null);
   }
 
   private static Gson newGson() {
diff --git a/gitiles-servlet/src/test/java/com/google/gitiles/blame/BlameServletTest.java b/gitiles-servlet/src/test/java/com/google/gitiles/blame/BlameServletTest.java
index dedbfde..03a962f 100644
--- a/gitiles-servlet/src/test/java/com/google/gitiles/blame/BlameServletTest.java
+++ b/gitiles-servlet/src/test/java/com/google/gitiles/blame/BlameServletTest.java
@@ -71,6 +71,6 @@
   }
 
   private Map<String, List<RegionJsonData>> getBlameJson(String path) throws Exception {
-    return buildJson(path, new TypeToken<Map<String, List<RegionJsonData>>>() {});
+    return buildJson(new TypeToken<Map<String, List<RegionJsonData>>>() {}, path);
   }
 }