Add unit tests to cover error conditions in ChecksSubmitRule

It's difficult to trigger the error conditions from an integration test,
but doing it with mocks is quite straight-forward.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I9aae3def5e31de1da80144da357b88498f15bcae
diff --git a/javatests/com/google/gerrit/plugins/checks/rules/BUILD b/javatests/com/google/gerrit/plugins/checks/rules/BUILD
new file mode 100644
index 0000000..8fc3b58
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/checks/rules/BUILD
@@ -0,0 +1,17 @@
+load("//tools/bzl:junit.bzl", "junit_tests")
+
+junit_tests(
+    name = "rules_tests",
+    size = "small",
+    srcs = glob(["*.java"]),
+    deps = [
+        "//java/com/google/gerrit/common:server",
+        "//java/com/google/gerrit/reviewdb:server",
+        "//java/com/google/gerrit/server",
+        "//java/com/google/gerrit/testing:gerrit-test-util",
+        "//java/com/google/gwtorm",
+        "//lib:guava",
+        "//lib/truth",
+        "//plugins/checks:checks__plugin",
+    ],
+)
diff --git a/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java b/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java
new file mode 100644
index 0000000..27f7522
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java
@@ -0,0 +1,82 @@
+// 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.plugins.checks.rules;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
+import com.google.common.collect.Iterables;
+import com.google.gerrit.common.data.SubmitRecord;
+import com.google.gerrit.plugins.checks.Checks;
+import com.google.gerrit.reviewdb.client.Change;
+import com.google.gerrit.reviewdb.client.PatchSet;
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.server.project.SubmitRuleOptions;
+import com.google.gerrit.server.query.change.ChangeData;
+import com.google.gerrit.testing.GerritBaseTests;
+import com.google.gwtorm.server.OrmException;
+import java.util.Collection;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+public class ChecksSubmitRuleTest extends GerritBaseTests {
+  @Test
+  public void loadingCurrentPatchSetFails() throws Exception {
+    ChecksSubmitRule checksSubmitRule =
+        new ChecksSubmitRule(EasyMock.createStrictMock(Checks.class));
+
+    ChangeData cd = EasyMock.createStrictMock(ChangeData.class);
+    expect(cd.project()).andReturn(new Project.NameKey("My-Project"));
+    expect(cd.getId()).andReturn(new Change.Id(1));
+    expect(cd.currentPatchSet()).andThrow(new OrmException("Fail for test"));
+    replay(cd);
+
+    Collection<SubmitRecord> submitRecords =
+        checksSubmitRule.evaluate(cd, SubmitRuleOptions.defaults());
+    assertErrorRecord(submitRecords, "failed to load the current patch set of change 1");
+  }
+
+  @Test
+  public void getCombinedCheckStateFails() throws Exception {
+    Checks checks = EasyMock.createStrictMock(Checks.class);
+    expect(checks.getCombinedCheckState(anyObject(), anyObject()))
+        .andThrow(new OrmException("Fail for test"));
+    replay(checks);
+
+    ChecksSubmitRule checksSubmitRule = new ChecksSubmitRule(checks);
+
+    Change.Id changeId = new Change.Id(1);
+    ChangeData cd = EasyMock.createStrictMock(ChangeData.class);
+    expect(cd.project()).andReturn(new Project.NameKey("My-Project"));
+    expect(cd.getId()).andReturn(new Change.Id(1));
+    expect(cd.currentPatchSet()).andReturn(new PatchSet(new PatchSet.Id(changeId, 1)));
+    replay(cd);
+
+    Collection<SubmitRecord> submitRecords =
+        checksSubmitRule.evaluate(cd, SubmitRuleOptions.defaults());
+    assertErrorRecord(submitRecords, "failed to evaluate check states for change 1");
+  }
+
+  private static void assertErrorRecord(
+      Collection<SubmitRecord> submitRecords, String expectedErrorMessage) {
+    assertThat(submitRecords).hasSize(1);
+
+    SubmitRecord submitRecord = Iterables.getOnlyElement(submitRecords);
+    assertThat(submitRecord.status).isEqualTo(SubmitRecord.Status.RULE_ERROR);
+    assertThat(submitRecord.errorMessage).isEqualTo(expectedErrorMessage);
+  }
+}