Add top-menu integration tests

Demonstrate that the plugin-manager is able to load
and install correctly on the top-menu bar, showing
the plugin management item.

Change-Id: I1ce612e857a23e26b1c81e5185ad2d63308eb13d
diff --git a/BUILD b/BUILD
index 9b4f025..d267107 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,5 @@
-load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+load("//tools/bzl:junit.bzl", "junit_tests")
+load("//tools/bzl:plugin.bzl", "PLUGIN_DEPS", "PLUGIN_TEST_DEPS", "gerrit_plugin")
 
 gerrit_plugin(
     name = "plugin-manager",
@@ -13,3 +14,12 @@
     ],
     resources = glob(["src/main/resources/**/*"]),
 )
+
+junit_tests(
+    name = "plugin_manager_tests",
+    srcs = glob(["src/test/java/**/*.java"]),
+    visibility = ["//visibility:public"],
+    deps = PLUGIN_TEST_DEPS + PLUGIN_DEPS + [
+        ":plugin-manager__plugin",
+    ],
+)
diff --git a/src/test/java/com/google/gerrit/server/restapi/config/PluginManagerTopMenuIT.java b/src/test/java/com/google/gerrit/server/restapi/config/PluginManagerTopMenuIT.java
new file mode 100644
index 0000000..4323d77
--- /dev/null
+++ b/src/test/java/com/google/gerrit/server/restapi/config/PluginManagerTopMenuIT.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.server.restapi.config; // Needed to get access to the ListTopMenus
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.gerrit.acceptance.GerritConfig;
+import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
+import com.google.gerrit.acceptance.NoHttpd;
+import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
+import com.google.gerrit.extensions.client.MenuItem;
+import com.google.gerrit.extensions.webui.TopMenu.MenuEntry;
+import com.google.gerrit.server.config.ConfigResource;
+import com.google.inject.Inject;
+import java.util.List;
+import java.util.Optional;
+import org.junit.Test;
+
+@NoHttpd
+@TestPlugin(
+    name = "plugin-manager",
+    sysModule = "com.googlesource.gerrit.plugins.manager.Module",
+    httpModule = "com.googlesource.gerrit.plugins.manager.WebModule")
+public class PluginManagerTopMenuIT extends LightweightPluginDaemonTest {
+
+  @Inject ListTopMenus topMenus;
+
+  @Inject private RequestScopeOperations requestScopeOperations;
+
+  @Test
+  @GerritConfig(name = "plugins.allowRemoteAdmin", value = "true")
+  public void showTopMenuForGerritAdministratorsWhenAllowRemoteAdmin() throws Exception {
+    assertThat(pluginTopMenuEntries()).isNotEmpty();
+  }
+
+  @Test
+  @GerritConfig(name = "plugins.allowRemoteAdmin", value = "true")
+  public void topMenuContainsPluginsManagementItem() throws Exception {
+    Optional<MenuEntry> topMenuEntry = pluginTopMenuEntries().stream().findFirst();
+    assertThat(topMenuEntry.map(m -> m.name)).isEqualTo(Optional.of("Plugins"));
+
+    Optional<MenuItem> pluginsMenuItem = topMenuEntry.flatMap(m -> m.items.stream().findFirst());
+    assertThat(pluginsMenuItem.map(m -> m.name)).isEqualTo(Optional.of("Manage"));
+  }
+
+  @Test
+  @GerritConfig(name = "plugins.allowRemoteAdmin", value = "true")
+  public void hideTopMenuForRegularUsersEvenWhenAllowRemoteAdmin() throws Exception {
+    requestScopeOperations.setApiUser(user.id());
+    assertThat(pluginTopMenuEntries()).isEmpty();
+  }
+
+  @Test
+  public void hideTopMenuByDefault() throws Exception {
+    assertThat(pluginTopMenuEntries()).isEmpty();
+  }
+
+  private List<MenuEntry> pluginTopMenuEntries() {
+    List<MenuEntry> topMenuItems = topMenus.apply(new ConfigResource()).value();
+    return topMenuItems;
+  }
+}