Add test for CorePluginsRepository Using dependency on release.war in Bazel junit_tests rule, using data attribute, we can depend on release.war content in the tests: [1]. Then we can retrieve the TEST_SRCDIR passed through environment variable to the test and retrieve the content of the release.war from within the tests. The only disadvantage of this approach, is that the test in this form wouldn't work from within IDE. But in this case release.war could be built manually and passed in the test by other means. [1] https://docs.bazel.build/versions/master/build-ref.html#data Change-Id: I17128017e2ec0287baf34e84b35eed1683f3f422
diff --git a/BUILD b/BUILD index d267107..bad06dc 100644 --- a/BUILD +++ b/BUILD
@@ -18,8 +18,9 @@ junit_tests( name = "plugin_manager_tests", srcs = glob(["src/test/java/**/*.java"]), + data = ["//:release.war"], visibility = ["//visibility:public"], - deps = PLUGIN_TEST_DEPS + PLUGIN_DEPS + [ + deps = PLUGIN_TEST_DEPS + [ ":plugin-manager__plugin", ], )
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..7989d4e --- /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 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", + "delete-project", + "download-commands", + "gitiles", + "hooks", + "plugin-manager", + "replication", + "reviewnotes", + "singleusergroup", + "webhooks"); + + @Test + public void corePluginsRepositoryShouldReturnCorePluginsFromReleaseWar() throws IOException { + SitePaths site = new SitePaths(random()); + PluginsRepository pluginRepo = new CorePluginsRepository(site, new CorePluginsDescriptions()); + + Path pathToReleaseWar = + Paths.get(System.getenv("TEST_SRCDIR"), System.getenv("TEST_WORKSPACE"), "release.war"); + if (!pathToReleaseWar.toFile().exists()) { + throw new IllegalStateException("Cannot find release.war"); + } + Files.createDirectories(site.bin_dir); + Files.createSymbolicLink(site.gerrit_war, pathToReleaseWar); + + Collection<PluginInfo> plugins = pluginRepo.list(Version.getVersion()); + assertThat(plugins).hasSize(GERRIT_CORE_PLUGINS.size()); + + assertThat(plugins.stream().map(p -> p.name).sorted().collect(toList())) + .containsExactlyElementsIn(GERRIT_CORE_PLUGINS) + .inOrder(); + } + + private static Path random() throws IOException { + Path tmp = Files.createTempFile("gerrit_", "_site"); + Files.deleteIfExists(tmp); + return tmp; + } +}