Simplify invocation of QueryPendingChecks

* Make calls to the setQuery(String) method chainable (return the
  QueryPendingChecks instance from this method)
* Add a parameterless apply method to QueryPendingChecks that delegates
  to apply(TopLevelResource.INSTANCE)

This way the call to invoke QueryPendingChecks in PendingChecksImpl
becomes a one-liner and the method that did the invocation can be
inlined.

Also add a test to invoke QueryPendingChecks via REST to make sure that
changing the return type of the setQuery(String) method in
QueryPendingChecks that is annotated with @Option is not a problem.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I14606891178e602e087d2de066ead1e1e1172bfb
diff --git a/java/com/google/gerrit/plugins/checks/api/PendingChecksImpl.java b/java/com/google/gerrit/plugins/checks/api/PendingChecksImpl.java
index 4d70071..e3a1a94 100644
--- a/java/com/google/gerrit/plugins/checks/api/PendingChecksImpl.java
+++ b/java/com/google/gerrit/plugins/checks/api/PendingChecksImpl.java
@@ -17,7 +17,6 @@
 import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
 
 import com.google.gerrit.extensions.restapi.RestApiException;
-import com.google.gerrit.extensions.restapi.TopLevelResource;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
@@ -37,18 +36,12 @@
     return new QueryRequest() {
       @Override
       public List<PendingChecksInfo> get() throws RestApiException {
-        return PendingChecksImpl.this.query(this);
+        try {
+          return queryPendingChecksProvider.get().setQuery(getQuery()).apply();
+        } catch (Exception e) {
+          throw asRestApiException("Cannot query pending checks", e);
+        }
       }
     };
   }
-
-  private List<PendingChecksInfo> query(QueryRequest queryRequest) throws RestApiException {
-    try {
-      QueryPendingChecks queryPendingChecks = queryPendingChecksProvider.get();
-      queryPendingChecks.setQuery(queryRequest.getQuery());
-      return queryPendingChecks.apply(TopLevelResource.INSTANCE);
-    } catch (Exception e) {
-      throw asRestApiException("Cannot query pending checks", e);
-    }
-  }
 }
diff --git a/java/com/google/gerrit/plugins/checks/api/QueryPendingChecks.java b/java/com/google/gerrit/plugins/checks/api/QueryPendingChecks.java
index ef5d324..cededfa 100644
--- a/java/com/google/gerrit/plugins/checks/api/QueryPendingChecks.java
+++ b/java/com/google/gerrit/plugins/checks/api/QueryPendingChecks.java
@@ -66,8 +66,9 @@
       aliases = {"-q"},
       metaVar = "QUERY",
       usage = "check query")
-  public void setQuery(String queryString) {
+  public QueryPendingChecks setQuery(String queryString) {
     this.queryString = queryString;
+    return this;
   }
 
   @Inject
@@ -86,6 +87,11 @@
     this.changeQueryProcessorProvider = changeQueryProcessorProvider;
   }
 
+  public List<PendingChecksInfo> apply()
+      throws RestApiException, IOException, ConfigInvalidException, OrmException {
+    return apply(TopLevelResource.INSTANCE);
+  }
+
   @Override
   public List<PendingChecksInfo> apply(TopLevelResource resource)
       throws RestApiException, IOException, ConfigInvalidException, OrmException {
diff --git a/javatests/com/google/gerrit/plugins/checks/acceptance/api/QueryPendingChecksIT.java b/javatests/com/google/gerrit/plugins/checks/acceptance/api/QueryPendingChecksIT.java
index 7783dbf..b5c9978 100644
--- a/javatests/com/google/gerrit/plugins/checks/acceptance/api/QueryPendingChecksIT.java
+++ b/javatests/com/google/gerrit/plugins/checks/acceptance/api/QueryPendingChecksIT.java
@@ -21,6 +21,7 @@
 import static org.hamcrest.CoreMatchers.instanceOf;
 
 import com.google.common.collect.Iterables;
+import com.google.gerrit.acceptance.RestResponse;
 import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
 import com.google.gerrit.common.data.Permission;
 import com.google.gerrit.extensions.restapi.BadRequestException;
@@ -35,6 +36,7 @@
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.server.project.testing.Util;
 import com.google.gerrit.testing.TestTimeUtil;
+import com.google.gson.reflect.TypeToken;
 import com.google.inject.Inject;
 import java.sql.Timestamp;
 import java.time.Instant;
@@ -564,6 +566,24 @@
     assertThat(pendingChecksList).isEmpty();
   }
 
+  @Test
+  public void pendingChecksViaRest() throws Exception {
+    CheckerUuid checkerUuid = checkerOperations.newChecker().repository(project).create();
+    checkOperations
+        .newCheck(CheckKey.create(project, patchSetId, checkerUuid))
+        .setState(CheckState.NOT_STARTED)
+        .upsert();
+
+    RestResponse r =
+        adminRestSession.get(
+            String.format("/plugins/checks/checks.pending/?q=checker:%s", checkerUuid.get()));
+    r.assertOK();
+    List<PendingChecksInfo> pendingChecksList =
+        newGson().fromJson(r.getReader(), new TypeToken<List<PendingChecksInfo>>() {}.getType());
+    r.consume();
+    assertThat(pendingChecksList).isNotEmpty();
+  }
+
   private void assertInvalidQuery(String query, String expectedMessage) throws RestApiException {
     try {
       pendingChecksApi.query(query).get();