Backport tests from master to stable-2.16

The master branch of the plugin-manager contains quite a few
tests: run them also on stable-2.16, so that we can have
a green build and stable over time, with proper validation in
place.

Change-Id: If9b0ba8346b5732f451485788a6c07fb3a93c487
diff --git a/BUILD b/BUILD
index fd1de6a..064033f 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,10 @@
-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 +19,13 @@
         "Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/plugin-manager",
     ],
 )
+
+junit_tests(
+    name = "plugin_manager_tests",
+    srcs = glob(["src/test/java/**/*.java"]),
+    data = ["//:release.war"],
+    visibility = ["//visibility:public"],
+    deps = PLUGIN_TEST_DEPS + [
+        ":plugin-manager__plugin",
+    ],
+)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranch.java b/src/main/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranch.java
index e138e92..6a36133 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranch.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranch.java
@@ -15,7 +15,7 @@
 package com.googlesource.gerrit.plugins.manager;
 
 public class GerritVersionBranch {
-  private static final String GERRIT_NEXT_VERSION = "3.1";
+  private static final String GERRIT_NEXT_VERSION = "3.0";
 
   public static String getBranch(String gerritVersion) {
     if (gerritVersion == null
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..2723c0d
--- /dev/null
+++ b/src/test/java/com/google/gerrit/server/restapi/config/PluginManagerTopMenuIT.java
@@ -0,0 +1,72 @@
+// 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.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;
+
+  @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 {
+    atrScope.set(atrScope.newContext(null, null, identifiedUserFactory.create(user.getId())));
+    assertThat(pluginTopMenuEntries()).isEmpty();
+  }
+
+  @Test
+  public void hideTopMenuByDefault() throws Exception {
+    assertThat(pluginTopMenuEntries()).isEmpty();
+  }
+
+  private List<MenuEntry> pluginTopMenuEntries() throws Exception {
+    List<MenuEntry> topMenuItems = topMenus.apply(new ConfigResource());
+    return topMenuItems;
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranchTest.java b/src/test/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranchTest.java
new file mode 100644
index 0000000..f0f6a4b
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/manager/GerritVersionBranchTest.java
@@ -0,0 +1,67 @@
+// 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.googlesource.gerrit.plugins.manager;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.googlesource.gerrit.plugins.manager.GerritVersionBranch.getBranch;
+
+import org.junit.Test;
+
+public class GerritVersionBranchTest {
+
+  @Test
+  public void getBranchReturnsCorrectBranchForTwoDigitsVersions() throws Exception {
+    // Regular 2.x versions
+    assertBranch("2.13", "stable-2.13");
+    assertBranch("2.14", "stable-2.14");
+    assertBranch("2.15", "stable-2.15");
+    assertBranch("2.16", "stable-2.16");
+  }
+
+  @Test
+  public void getBranchReturnsCorrectBranchForThreeDigitsVersions() throws Exception {
+    // 2.x.y version
+    assertBranch("2.16.10", "stable-2.16");
+  }
+
+  @Test
+  public void getBranchReturnsCorrectBranchForReleaseCandidates() throws Exception {
+    // 2.x-rcx version
+    assertBranch("2.16-rc1", "stable-2.16");
+  }
+
+  @Test
+  public void getBranchReturnsCorrectBranchForDevelopmentOnStableBranches() throws Exception {
+    assertBranch("2.16.8-17-gc8b633d5ce", "stable-2.16");
+  }
+
+  @Test
+  public void getBranchReturnsCorrectBranchForDevelopmentOnMaster() throws Exception {
+    assertBranch("3.0.0-rc2-237-gae0124c68e", "master");
+  }
+
+  @Test
+  public void getBranchReturnsMasterForUnknownVersions() throws Exception {
+    // Unknown versions
+    assertBranch(null, "master");
+    assertBranch("", "master");
+    assertBranch("   ", "master");
+    assertBranch("foo", "master");
+  }
+
+  private static void assertBranch(String version, String expectedBranch) throws Exception {
+    assertThat(getBranch(version)).isEqualTo(expectedBranch);
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/manager/repository/PluginsRepositoryTest.java b/src/test/java/com/googlesource/gerrit/plugins/manager/repository/PluginsRepositoryTest.java
new file mode 100644
index 0000000..f24b154
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/manager/repository/PluginsRepositoryTest.java
@@ -0,0 +1,72 @@
+// 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.googlesource.gerrit.plugins.manager.repository;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.TruthJUnit.assume;
+import static java.util.stream.Collectors.toList;
+
+import com.google.common.collect.ImmutableList;
+import com.google.gerrit.common.Version;
+import com.google.gerrit.server.config.SitePaths;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collection;
+import org.junit.Test;
+
+public class PluginsRepositoryTest {
+
+  private static final ImmutableList<String> GERRIT_CORE_PLUGINS =
+      ImmutableList.of(
+          "codemirror-editor",
+          "commit-message-length-validator",
+          "download-commands",
+          "hooks",
+          "plugin-manager",
+          "replication",
+          "reviewnotes",
+          "singleusergroup");
+
+  @Test
+  public void corePluginsRepositoryShouldReturnCorePluginsFromReleaseWar() throws IOException {
+    SitePaths site = new SitePaths(random());
+    PluginsRepository pluginRepo = new CorePluginsRepository(site, new CorePluginsDescriptions());
+
+    Path pathToReleaseWar =
+        Paths.get(getenv("TEST_SRCDIR"), getenv("TEST_WORKSPACE"), "release.war");
+    assume().that(pathToReleaseWar.toFile().exists()).isTrue();
+    Files.createDirectories(site.bin_dir);
+    Files.createSymbolicLink(site.gerrit_war, pathToReleaseWar);
+
+    Collection<PluginInfo> plugins = pluginRepo.list(Version.getVersion());
+    assertThat(plugins.stream().map(p -> p.name).sorted().collect(toList()))
+        .containsExactlyElementsIn(GERRIT_CORE_PLUGINS)
+        .inOrder();
+  }
+
+  private static String getenv(String name) {
+    String value = System.getenv(name);
+    assume().that(value).isNotNull();
+    return value;
+  }
+
+  private static Path random() throws IOException {
+    Path tmp = Files.createTempFile("gerrit_", "_site");
+    Files.deleteIfExists(tmp);
+    return tmp;
+  }
+}