Add integration test for owners-autoassign
Move checking of change refs outside processEvent() body to make the code
more testable.
Change-Id: Iefeeecb4a4c13072f2a4e1482dd3191021c1414c
diff --git a/owners-autoassign/BUILD b/owners-autoassign/BUILD
index 05eeb89..37eb96c 100644
--- a/owners-autoassign/BUILD
+++ b/owners-autoassign/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_DEPS_NEVERLINK", "PLUGIN_TEST_DEPS", "gerrit_plugin")
 
 gerrit_plugin(
     name = "owners-autoassign",
@@ -18,3 +19,24 @@
         "//owners-common",
     ],
 )
+
+java_library(
+    name = "owners-autoassign_deps",
+    srcs = glob([
+        "src/main/java/**/*.java",
+    ]),
+    visibility = ["//visibility:public"],
+    deps = PLUGIN_DEPS + PLUGIN_DEPS_NEVERLINK + [
+        "//owners-common",
+    ],
+)
+
+junit_tests(
+    name = "owners_autoassign_tests",
+    testonly = 1,
+    srcs = glob(["src/test/java/**/*.java"]),
+    deps = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
+        "//owners-common",
+        ":owners-autoassign_deps",
+    ],
+)
diff --git a/owners-autoassign/src/main/java/com/vmware/gerrit/owners/common/GitRefListener.java b/owners-autoassign/src/main/java/com/vmware/gerrit/owners/common/GitRefListener.java
index c3600e3..2cfd54d 100644
--- a/owners-autoassign/src/main/java/com/vmware/gerrit/owners/common/GitRefListener.java
+++ b/owners-autoassign/src/main/java/com/vmware/gerrit/owners/common/GitRefListener.java
@@ -78,7 +78,10 @@
     try {
       repository = repositoryManager.openRepository(Project.NameKey.parse(projectName));
       try {
-        processEvent(repository, event);
+        String refName = event.getRefName();
+        if (refName.startsWith(RefNames.REFS_CHANGES) && !RefNames.isNoteDbMetaRef(refName)) {
+          processEvent(repository, event);
+        }
       } finally {
         repository.close();
       }
@@ -87,35 +90,32 @@
     }
   }
 
-  private void processEvent(Repository repository, Event event) {
-    final String refName = event.getRefName();
-    if (refName.startsWith(RefNames.REFS_CHANGES) && !RefNames.isNoteDbMetaRef(refName)) {
-      Change.Id cId = Change.Id.fromRef(refName);
-      Changes changes = api.changes();
-      // The provider injected by Gerrit is shared with other workers on the
-      // same local thread and thus cannot be closed in this event listener.
-      try {
-        ChangeApi cApi = changes.id(cId.id);
-        ChangeInfo change = cApi.get();
-        if (change == null) {
-          return;
-        }
-        PatchList patchList = getPatchList(event, change);
-        if (patchList != null) {
-          PathOwners owners = new PathOwners(accounts, repository, change.branch, patchList);
-          Set<Account.Id> allReviewers = Sets.newHashSet();
-          allReviewers.addAll(owners.get().values());
-          for (Matcher matcher : owners.getMatchers().values()) {
-            allReviewers.addAll(matcher.getOwners());
-          }
-          logger.debug("Autoassigned reviewers are: {}", allReviewers.toString());
-          reviewerManager.addReviewers(cApi, allReviewers);
-        }
-      } catch (RestApiException e) {
-        logger.warn("Could not open change: {}", cId, e);
-      } catch (ReviewerManagerException e) {
-        logger.warn("Could not add reviewers for change: {}", cId, e);
+  void processEvent(Repository repository, Event event) {
+    Change.Id cId = Change.Id.fromRef(event.getRefName());
+    Changes changes = api.changes();
+    // The provider injected by Gerrit is shared with other workers on the
+    // same local thread and thus cannot be closed in this event listener.
+    try {
+      ChangeApi cApi = changes.id(cId.id);
+      ChangeInfo change = cApi.get();
+      if (change == null) {
+        return;
       }
+      PatchList patchList = getPatchList(event, change);
+      if (patchList != null) {
+        PathOwners owners = new PathOwners(accounts, repository, change.branch, patchList);
+        Set<Account.Id> allReviewers = Sets.newHashSet();
+        allReviewers.addAll(owners.get().values());
+        for (Matcher matcher : owners.getMatchers().values()) {
+          allReviewers.addAll(matcher.getOwners());
+        }
+        logger.debug("Autoassigned reviewers are: {}", allReviewers.toString());
+        reviewerManager.addReviewers(cApi, allReviewers);
+      }
+    } catch (RestApiException e) {
+      logger.warn("Could not open change: {}", cId, e);
+    } catch (ReviewerManagerException e) {
+      logger.warn("Could not add reviewers for change: {}", cId, e);
     }
   }
 
diff --git a/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/GitRefListenerIT.java b/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/GitRefListenerIT.java
new file mode 100644
index 0000000..aa6db4f
--- /dev/null
+++ b/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/GitRefListenerIT.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.vmware.gerrit.owners.common;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
+import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
+import com.google.gerrit.reviewdb.client.RefNames;
+import com.google.inject.AbstractModule;
+import org.eclipse.jgit.transport.ReceiveCommand.Type;
+import org.junit.Test;
+
+@TestPlugin(
+    name = "owners-autoassign",
+    sysModule = "com.vmware.gerrit.owners.common.GitRefListenerIT$TestModule")
+public class GitRefListenerIT extends LightweightPluginDaemonTest {
+
+  public static class TestModule extends AbstractModule {
+    @Override
+    protected void configure() {
+      bind(GitReferenceUpdatedListener.class).to(GitRefListenerTest.class);
+    }
+  }
+
+  @Test
+  public void shouldNotProcessNoteDbOnlyRefs() {
+    GitRefListenerTest gitRefListener = getPluginInstance(GitRefListenerTest.class);
+
+    String aRefChange = RefNames.REFS_CHANGES + "01/01" + RefNames.META_SUFFIX;
+    String anOldObjectId = "anOldRef";
+    String aNewObjectId = "aNewRef";
+
+    ReferenceUpdatedEventTest refUpdatedEvent =
+        new ReferenceUpdatedEventTest(
+            project, aRefChange, anOldObjectId, aNewObjectId, Type.CREATE);
+
+    gitRefListener.onGitReferenceUpdated(refUpdatedEvent);
+    assertEquals(0, gitRefListener.getProcessedEvents());
+  }
+
+  @Test
+  public void shouldProcessRefChanges() {
+    GitRefListenerTest gitRefListener = getPluginInstance(GitRefListenerTest.class);
+
+    String aRefChange = RefNames.REFS_CHANGES + "01/01/01";
+    String anOldObjectId = "anOldRef";
+    String aNewObjectId = "aNewRef";
+
+    ReferenceUpdatedEventTest refUpdatedEvent =
+        new ReferenceUpdatedEventTest(
+            project, aRefChange, anOldObjectId, aNewObjectId, Type.CREATE);
+
+    gitRefListener.onGitReferenceUpdated(refUpdatedEvent);
+    assertEquals(1, gitRefListener.getProcessedEvents());
+  }
+
+  private <T> T getPluginInstance(Class<T> clazz) {
+    return plugin.getSysInjector().getInstance(clazz);
+  }
+}
diff --git a/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/GitRefListenerTest.java b/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/GitRefListenerTest.java
new file mode 100644
index 0000000..86ab341
--- /dev/null
+++ b/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/GitRefListenerTest.java
@@ -0,0 +1,32 @@
+package com.vmware.gerrit.owners.common;
+
+import com.google.gerrit.extensions.api.GerritApi;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.server.patch.PatchListCache;
+import com.google.inject.Inject;
+import org.eclipse.jgit.lib.Repository;
+import org.junit.Ignore;
+
+@Ignore
+public class GitRefListenerTest extends GitRefListener {
+  int processedEvents = 0;
+
+  @Inject
+  public GitRefListenerTest(
+      GerritApi api,
+      PatchListCache patchListCache,
+      GitRepositoryManager repositoryManager,
+      Accounts accounts,
+      ReviewerManager reviewerManager) {
+    super(api, patchListCache, repositoryManager, accounts, reviewerManager);
+  }
+
+  @Override
+  void processEvent(Repository repository, Event event) {
+    processedEvents++;
+  }
+
+  int getProcessedEvents() {
+    return processedEvents;
+  }
+}
diff --git a/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/ReferenceUpdatedEventTest.java b/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/ReferenceUpdatedEventTest.java
new file mode 100644
index 0000000..601ec8d
--- /dev/null
+++ b/owners-autoassign/src/test/java/com/vmware/gerrit/owners/common/ReferenceUpdatedEventTest.java
@@ -0,0 +1,76 @@
+package com.vmware.gerrit.owners.common;
+
+import com.google.gerrit.extensions.api.changes.NotifyHandling;
+import com.google.gerrit.extensions.common.AccountInfo;
+import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
+import com.google.gerrit.reviewdb.client.Project;
+import org.eclipse.jgit.transport.ReceiveCommand;
+import org.junit.Ignore;
+
+@Ignore
+public class ReferenceUpdatedEventTest implements GitReferenceUpdatedListener.Event {
+
+  private final String projectName;
+  private final String ref;
+  private final String oldObjectId;
+  private final String newObjectId;
+  private final ReceiveCommand.Type type;
+
+  public ReferenceUpdatedEventTest(
+      Project.NameKey project,
+      String ref,
+      String oldObjectId,
+      String newObjectId,
+      ReceiveCommand.Type type) {
+    this.projectName = project.get();
+    this.ref = ref;
+    this.oldObjectId = oldObjectId;
+    this.newObjectId = newObjectId;
+    this.type = type;
+  }
+
+  @Override
+  public String getProjectName() {
+    return projectName;
+  }
+
+  @Override
+  public String getRefName() {
+    return ref;
+  }
+
+  @Override
+  public String getOldObjectId() {
+    return oldObjectId;
+  }
+
+  @Override
+  public String getNewObjectId() {
+    return newObjectId;
+  }
+
+  @Override
+  public boolean isCreate() {
+    return type == ReceiveCommand.Type.CREATE;
+  }
+
+  @Override
+  public boolean isDelete() {
+    return type == ReceiveCommand.Type.DELETE;
+  }
+
+  @Override
+  public boolean isNonFastForward() {
+    return type == ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
+  }
+
+  @Override
+  public AccountInfo getUpdater() {
+    return null;
+  }
+
+  @Override
+  public NotifyHandling getNotify() {
+    return NotifyHandling.NONE;
+  }
+}