Merge "Add acceptance test for plugin URL bindings"
diff --git a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
index a945e34..5286ceb 100644
--- a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
+++ b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
@@ -1587,19 +1587,27 @@
 
   protected AutoCloseable installPlugin(String pluginName, Class<? extends Module> sysModuleClass)
       throws Exception {
-    checkArgument(
-        (sysModuleClass.getModifiers() & Modifier.STATIC) != 0,
-        "module must be static: %s",
-        sysModuleClass.getName());
+    return installPlugin(pluginName, sysModuleClass, null, null);
+  }
+
+  protected AutoCloseable installPlugin(
+      String pluginName,
+      @Nullable Class<? extends Module> sysModuleClass,
+      @Nullable Class<? extends Module> httpModuleClass,
+      @Nullable Class<? extends Module> sshModuleClass)
+      throws Exception {
+    checkStatic(sysModuleClass);
+    checkStatic(httpModuleClass);
+    checkStatic(sshModuleClass);
     TestServerPlugin plugin =
         new TestServerPlugin(
             pluginName,
             "http://example.com/" + pluginName,
             pluginUserFactory.create(pluginName),
             getClass().getClassLoader(),
-            sysModuleClass.getName(),
-            null,
-            null,
+            sysModuleClass != null ? sysModuleClass.getName() : null,
+            httpModuleClass != null ? httpModuleClass.getName() : null,
+            sshModuleClass != null ? sshModuleClass.getName() : null,
             sitePaths.data_dir.resolve(pluginName));
     plugin.start(pluginGuiceEnvironment);
     pluginGuiceEnvironment.onStartPlugin(plugin);
@@ -1608,4 +1616,13 @@
       pluginGuiceEnvironment.onStopPlugin(plugin);
     };
   }
+
+  private static void checkStatic(@Nullable Class<? extends Module> moduleClass) {
+    if (moduleClass != null) {
+      checkArgument(
+          (moduleClass.getModifiers() & Modifier.STATIC) != 0,
+          "module must be static: %s",
+          moduleClass.getName());
+    }
+  }
 }
diff --git a/javatests/com/google/gerrit/acceptance/rest/binding/PluginProvidedRestApiBindingsIT.java b/javatests/com/google/gerrit/acceptance/rest/binding/PluginProvidedRestApiBindingsIT.java
new file mode 100644
index 0000000..82c065a
--- /dev/null
+++ b/javatests/com/google/gerrit/acceptance/rest/binding/PluginProvidedRestApiBindingsIT.java
@@ -0,0 +1,75 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.acceptance.rest.binding;
+
+import static javax.servlet.http.HttpServletResponse.SC_OK;
+
+import com.google.common.collect.ImmutableList;
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.acceptance.rest.util.RestApiCallHelper;
+import com.google.gerrit.acceptance.rest.util.RestCall;
+import com.google.inject.Singleton;
+import com.google.inject.servlet.ServletModule;
+import java.io.IOException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.junit.Test;
+
+/**
+ * Tests for checking plugin-provided REST API bindings.
+ *
+ * <p>These tests only verify that the plugin-provided REST endpoints are correctly bound, they do
+ * not test the functionality of the plugin REST endpoints.
+ */
+public class PluginProvidedRestApiBindingsIT extends AbstractDaemonTest {
+
+  /**
+   * Plugin REST endpoints bound by {@link MyPluginModule} with Guice serlvet definitions.
+   *
+   * <p>Each URL contains a placeholder for the plugin identifier.
+   *
+   * <p>Currently does not include any resource or documentation URLs, since those would require
+   * installing a plugin from a jar, which is trickier than just defining a module in this file.
+   */
+  private static final ImmutableList<RestCall> SERVER_TOP_LEVEL_PLUGIN_ENDPOINTS =
+      ImmutableList.of(RestCall.get("/plugins/%s/hello"));
+
+  static class MyPluginModule extends ServletModule {
+    @Override
+    public void configureServlets() {
+      serve("/hello").with(HelloServlet.class);
+    }
+  }
+
+  @Singleton
+  static class HelloServlet extends HttpServlet {
+    private static final long serialVersionUID = 1L;
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
+      res.setStatus(SC_OK);
+      res.getWriter().println("Hello world");
+    }
+  }
+
+  @Test
+  public void serverPluginTopLevelEndpoints() throws Exception {
+    String pluginName = "my-plugin";
+    try (AutoCloseable ignored = installPlugin(pluginName, null, MyPluginModule.class, null)) {
+      RestApiCallHelper.execute(adminRestSession, SERVER_TOP_LEVEL_PLUGIN_ENDPOINTS, pluginName);
+    }
+  }
+}
diff --git a/javatests/com/google/gerrit/acceptance/rest/binding/PluginsRestApiBindingsIT.java b/javatests/com/google/gerrit/acceptance/rest/binding/PluginsRemoteAdminRestApiBindingsIT.java
similarity index 94%
rename from javatests/com/google/gerrit/acceptance/rest/binding/PluginsRestApiBindingsIT.java
rename to javatests/com/google/gerrit/acceptance/rest/binding/PluginsRemoteAdminRestApiBindingsIT.java
index 5616ebc..d60148e 100644
--- a/javatests/com/google/gerrit/acceptance/rest/binding/PluginsRestApiBindingsIT.java
+++ b/javatests/com/google/gerrit/acceptance/rest/binding/PluginsRemoteAdminRestApiBindingsIT.java
@@ -27,12 +27,12 @@
 import org.junit.Test;
 
 /**
- * Tests for checking the bindings of the plugins REST API.
+ * Tests for checking the remote administration bindings of the plugins REST API.
  *
  * <p>These tests only verify that the plugin REST endpoints are correctly bound, they do no test
  * the functionality of the plugin REST endpoints.
  */
-public class PluginsRestApiBindingsIT extends AbstractDaemonTest {
+public class PluginsRemoteAdminRestApiBindingsIT extends AbstractDaemonTest {
   /**
    * Plugin REST endpoints to be tested, each URL contains a placeholder for the plugin identifier.
    */