Add a module with the submit rule that can be used in batch jobs

If a batch job reindexes changes (as we have one at Google), all submit
rules must be bound for this batch job so that the submit records are
correctly computed.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: Ib5a98573dd83682251f017d1db7bc9bdf2704190
diff --git a/java/com/google/gerrit/plugins/codeowners/BatchModule.java b/java/com/google/gerrit/plugins/codeowners/BatchModule.java
new file mode 100644
index 0000000..3267d31
--- /dev/null
+++ b/java/com/google/gerrit/plugins/codeowners/BatchModule.java
@@ -0,0 +1,33 @@
+// Copyright (C) 2020 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.plugins.codeowners;
+
+import com.google.gerrit.common.UsedAt;
+import com.google.gerrit.plugins.codeowners.backend.BackendModule;
+import com.google.inject.AbstractModule;
+
+/**
+ * Binds a subset of the code-owners plugin functionality that should be available in batch jobs.
+ */
+@UsedAt(UsedAt.Project.GOOGLE)
+public class BatchModule extends AbstractModule {
+  @Override
+  protected void configure() {
+    // We only need the CodeOwnerSubmitRule in batch jobs, but since the CodeOwnerSubmitRule depends
+    // on the CodeOwnerBackend, CodeOwnerBackend must be bound too. This means we can simply install
+    // the whole BackendModule.
+    install(new BackendModule());
+  }
+}
diff --git a/javatests/com/google/gerrit/plugins/codeowners/acceptance/batch/BUILD b/javatests/com/google/gerrit/plugins/codeowners/acceptance/batch/BUILD
new file mode 100644
index 0000000..cef4da8
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/codeowners/acceptance/batch/BUILD
@@ -0,0 +1,17 @@
+load("//javatests/com/google/gerrit/acceptance:tests.bzl", "acceptance_tests")
+
+package(
+    default_testonly = True,
+    default_visibility = ["//plugins/code-owners:visibility"],
+)
+
+acceptance_tests(
+    srcs = glob(
+        ["*IT.java"],
+    ),
+    group = "acceptance_batch",
+    deps = [
+        "//plugins/code-owners:code-owners__plugin",
+        "//plugins/code-owners/java/com/google/gerrit/plugins/codeowners/testing",
+    ],
+)
diff --git a/javatests/com/google/gerrit/plugins/codeowners/acceptance/batch/CodeOwnerSubmitRuleBatchIT.java b/javatests/com/google/gerrit/plugins/codeowners/acceptance/batch/CodeOwnerSubmitRuleBatchIT.java
new file mode 100644
index 0000000..9365151
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/codeowners/acceptance/batch/CodeOwnerSubmitRuleBatchIT.java
@@ -0,0 +1,74 @@
+package com.google.gerrit.plugins.codeowners.acceptance.batch;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allowLabel;
+import static com.google.gerrit.plugins.codeowners.testing.SubmitRequirementInfoSubject.assertThatCollection;
+import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
+
+import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
+import com.google.gerrit.acceptance.PushOneCommit;
+import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.acceptance.testsuite.project.ProjectOperations;
+import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
+import com.google.gerrit.extensions.client.ListChangesOption;
+import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.plugins.codeowners.testing.SubmitRequirementInfoSubject;
+import com.google.inject.Inject;
+import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
+import org.eclipse.jgit.junit.TestRepository;
+import org.junit.Test;
+
+/**
+ * Test that verifies that the {@link com.google.gerrit.plugins.codeowners.BatchModule} has bound
+ * all classes that are needed to run {@code
+ * com.google.gerrit.plugins.codeowners.backend.CodeOwnerSubmitRule}.
+ */
+@TestPlugin(name = "code-owners", sysModule = "com.google.gerrit.plugins.codeowners.BatchModule")
+public class CodeOwnerSubmitRuleBatchIT extends LightweightPluginDaemonTest {
+  @Inject private ProjectOperations projectOperations;
+  @Inject private RequestScopeOperations requestScopeOperations;
+
+  @Test
+  public void invokeCodeOwnerSubmitRule() throws Exception {
+    // Upload a change as a non-code owner.
+    TestRepository<InMemoryRepository> testRepo = cloneProject(project, user);
+    PushOneCommit push =
+        pushFactory.create(user.newIdent(), testRepo, "Test Change", "foo/bar.baz", "file content");
+    String changeId = push.to("refs/for/master").getChangeId();
+
+    // Approve by a non-code-owner.
+    projectOperations
+        .project(project)
+        .forUpdate()
+        .add(allowLabel("Code-Review").ref("refs/heads/*").group(REGISTERED_USERS).range(-2, +2))
+        .update();
+    requestScopeOperations.setApiUser(user.id());
+    approve(changeId);
+
+    // Verify that the change is not submittable.
+    ChangeInfo changeInfo = gApi.changes().id(changeId).get(ListChangesOption.SUBMITTABLE);
+    assertThat(changeInfo.submittable).isFalse();
+
+    // Check the submit requirement.
+    SubmitRequirementInfoSubject submitRequirementInfoSubject =
+        assertThatCollection(changeInfo.requirements).onlyElement();
+    submitRequirementInfoSubject.hasStatusThat().isEqualTo("NOT_READY");
+    submitRequirementInfoSubject.hasFallbackTextThat().isEqualTo("Code Owners");
+    submitRequirementInfoSubject.hasTypeThat().isEqualTo("code-owners");
+
+    // Approve by a project owner who is code owner since there is no code owner config file yet,
+    // and hence we are in bootstrapping mode.
+    requestScopeOperations.setApiUser(admin.id());
+    approve(changeId);
+
+    // Verify that the change is submittable now.
+    changeInfo = gApi.changes().id(changeId).get(ListChangesOption.SUBMITTABLE);
+    assertThat(changeInfo.submittable).isTrue();
+
+    // Check the submit requirement.
+    submitRequirementInfoSubject = assertThatCollection(changeInfo.requirements).onlyElement();
+    submitRequirementInfoSubject.hasStatusThat().isEqualTo("OK");
+    submitRequirementInfoSubject.hasFallbackTextThat().isEqualTo("Code Owners");
+    submitRequirementInfoSubject.hasTypeThat().isEqualTo("code-owners");
+  }
+}