Adapt to new SubmitRule interface in Gerrit core Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: I9b3d709c27306e63dcd20b7738cd077c8d730e0f
diff --git a/java/com/google/gerrit/plugins/checks/rules/ChecksSubmitRule.java b/java/com/google/gerrit/plugins/checks/rules/ChecksSubmitRule.java index 4850ef4..a1df4f5 100644 --- a/java/com/google/gerrit/plugins/checks/rules/ChecksSubmitRule.java +++ b/java/com/google/gerrit/plugins/checks/rules/ChecksSubmitRule.java
@@ -15,7 +15,6 @@ package com.google.gerrit.plugins.checks.rules; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; import com.google.common.flogger.FluentLogger; import com.google.gerrit.common.data.SubmitRecord; import com.google.gerrit.common.data.SubmitRecord.Status; @@ -31,7 +30,7 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import java.io.IOException; -import java.util.Collection; +import java.util.Optional; @Singleton public class ChecksSubmitRule implements SubmitRule { @@ -60,7 +59,7 @@ } @Override - public Collection<SubmitRecord> evaluate(ChangeData changeData) { + public Optional<SubmitRecord> evaluate(ChangeData changeData) { Project.NameKey project = changeData.project(); Change.Id changeId = changeData.getId(); @@ -71,7 +70,7 @@ String errorMessage = String.format("failed to load the current patch set of change %s", changeId); logger.atSevere().withCause(e).log(errorMessage); - return singletonRecordForRuleError(errorMessage); + return recordForRuleError(errorMessage); } boolean areAllRequiredCheckersPassing; @@ -82,24 +81,24 @@ String errorMessage = String.format("failed to evaluate check states for change %s", changeId); logger.atSevere().withCause(e).log(errorMessage); - return singletonRecordForRuleError(errorMessage); + return recordForRuleError(errorMessage); } SubmitRecord submitRecord = new SubmitRecord(); if (areAllRequiredCheckersPassing) { submitRecord.status = Status.OK; - return ImmutableList.of(submitRecord); + return Optional.of(submitRecord); } submitRecord.status = Status.NOT_READY; submitRecord.requirements = ImmutableList.of(DEFAULT_SUBMIT_REQUIREMENT_FOR_CHECKS); - return ImmutableSet.of(submitRecord); + return Optional.of(submitRecord); } - private static Collection<SubmitRecord> singletonRecordForRuleError(String reason) { + private static Optional<SubmitRecord> recordForRuleError(String reason) { SubmitRecord submitRecord = new SubmitRecord(); submitRecord.errorMessage = reason; submitRecord.status = SubmitRecord.Status.RULE_ERROR; - return ImmutableList.of(submitRecord); + return Optional.of(submitRecord); } }
diff --git a/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java b/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java index cdb533d..2d75e6b 100644 --- a/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java +++ b/javatests/com/google/gerrit/plugins/checks/rules/ChecksSubmitRuleTest.java
@@ -15,11 +15,11 @@ package com.google.gerrit.plugins.checks.rules; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.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.Account; @@ -29,7 +29,7 @@ import com.google.gerrit.server.query.change.ChangeData; import com.google.gerrit.server.util.time.TimeUtil; import java.io.IOException; -import java.util.Collection; +import java.util.Optional; import org.easymock.EasyMock; import org.eclipse.jgit.lib.ObjectId; import org.junit.Test; @@ -46,7 +46,7 @@ expect(cd.currentPatchSet()).andThrow(new IllegalStateException("Fail for test")); replay(cd); - Collection<SubmitRecord> submitRecords = checksSubmitRule.evaluate(cd); + Optional<SubmitRecord> submitRecords = checksSubmitRule.evaluate(cd); assertErrorRecord(submitRecords, "failed to load the current patch set of change 1"); } @@ -73,16 +73,15 @@ .build()); replay(cd); - Collection<SubmitRecord> submitRecords = checksSubmitRule.evaluate(cd); + Optional<SubmitRecord> submitRecords = checksSubmitRule.evaluate(cd); assertErrorRecord(submitRecords, "failed to evaluate check states for change 1"); } private static void assertErrorRecord( - Collection<SubmitRecord> submitRecords, String expectedErrorMessage) { - assertThat(submitRecords).hasSize(1); + Optional<SubmitRecord> submitRecord, String expectedErrorMessage) { + assertThat(submitRecord).isPresent(); - SubmitRecord submitRecord = Iterables.getOnlyElement(submitRecords); - assertThat(submitRecord.status).isEqualTo(SubmitRecord.Status.RULE_ERROR); - assertThat(submitRecord.errorMessage).isEqualTo(expectedErrorMessage); + assertThat(submitRecord.get().status).isEqualTo(SubmitRecord.Status.RULE_ERROR); + assertThat(submitRecord.get().errorMessage).isEqualTo(expectedErrorMessage); } }