Extract interface from EiffelEventParser To enable mocking it in tests. Temporarily keep the name of the implementation to make the diffs more review-friendly. Rename implementation and interface in follow-up. Solves: Jira GER-1715 Change-Id: I5b676e052a3799e8f00192c6306eb6313ccf2268
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/Module.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/Module.java index 28649a0..9f53bee 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/Module.java +++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/Module.java
@@ -39,6 +39,8 @@ import com.googlesource.gerrit.plugins.eventseiffel.mapping.EiffelEventFactory; import com.googlesource.gerrit.plugins.eventseiffel.mapping.EiffelEventMapper; import com.googlesource.gerrit.plugins.eventseiffel.mq.RabbitMqPublisher; +import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParser; +import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf; import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParsingExecutor; import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParsingQueue; import com.googlesource.gerrit.plugins.eventseiffel.rest.RestModule; @@ -98,6 +100,7 @@ .in(Scopes.SINGLETON); bind(EiffelEventParsingExecutor.Scheduled.class).in(Scopes.SINGLETON); bind(EiffelEventParsingExecutor.class).to(EiffelEventParsingExecutor.Scheduled.class); + bind(EiffelEventParserIf.class).to(EiffelEventParser.class); bind(EiffelEventParsingQueue.class).in(Scopes.SINGLETON); bind(EiffelEventHubImpl.class).in(Scopes.SINGLETON); bind(EiffelEventHub.class).to(EiffelEventHubImpl.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java index 34ac69e..df82291 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java +++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java
@@ -59,7 +59,7 @@ import org.eclipse.jgit.revwalk.RevCommit; /** Creates and pushes missing Eiffel events to the Eiffel event queue. */ -public class EiffelEventParser { +public class EiffelEventParser implements EiffelEventParserIf { private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final int NBR_RETRIES = 3; @@ -81,6 +81,10 @@ this.mapper = mapper; } + /* (non-Javadoc) + * @see com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf#createAndScheduleSccFromEvent(com.google.gerrit.extensions.events.RevisionCreatedListener.Event) + */ + @Override public void createAndScheduleSccFromEvent(Event event) { CommitInfo commit = event.getRevision().commit; SourceChangeEventKey scc = @@ -118,31 +122,25 @@ } } - /** - * Creates SCC events for all commits reachable from branch. I.e. create SCCs for already merged - * commit that are missing SCCs. - * - * @param repoName - the repository containing the commits to create events from. - * @param targetBranch - used to populate data.gitIdentifier.branch. + /* (non-Javadoc) + * @see com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf#createAndScheduleSccFromBranch(java.lang.String, java.lang.String) */ - public void createAndScheduleSccFromBranch(String repoName, String targetBranch) { - ObjectId tip = getTipOf(repoName, targetBranch); + @Override + public void createAndScheduleSccFromBranch(String repoName, String branchRef) { + ObjectId tip = getTipOf(repoName, branchRef); if (tip == null) { return; } - createAndScheduleSccFromCommit(repoName, targetBranch, tip.getName()); + createAndScheduleSccFromCommit(repoName, branchRef, tip.getName()); } - /** - * Creates SCC events for all commits reachable from commit. - * - * @param repoName - the repository containing the commits to create events from. - * @param targetBranch - used to populate data.gitIdentifier.branch. - * @param commit - create events from commits reachable from commit. + /* (non-Javadoc) + * @see com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf#createAndScheduleSccFromCommit(java.lang.String, java.lang.String, java.lang.String) */ - public void createAndScheduleSccFromCommit(String repoName, String targetBranch, String commit) { + @Override + public void createAndScheduleSccFromCommit(String repoName, String branchRef, String commit) { - SourceChangeEventKey scc = SourceChangeEventKey.sccKey(repoName, targetBranch, commit); + SourceChangeEventKey scc = SourceChangeEventKey.sccKey(repoName, branchRef, commit); try { createAndScheduleMissingSccs(scc); } catch (IOException @@ -154,15 +152,23 @@ } } - public void createAndScheduleMissingScssFromBranch(String repoName, String branch) { - ObjectId tip = getTipOf(repoName, branch); + /* (non-Javadoc) + * @see com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf#createAndScheduleMissingScssFromBranch(java.lang.String, java.lang.String) + */ + @Override + public void createAndScheduleMissingScssFromBranch(String repoName, String branchRef) { + ObjectId tip = getTipOf(repoName, branchRef); if (tip == null) { return; } - SourceChangeEventKey scs = SourceChangeEventKey.scsKey(repoName, branch, tip.getName()); + SourceChangeEventKey scs = SourceChangeEventKey.scsKey(repoName, branchRef, tip.getName()); createAndScheduleMissingScss(scs, null, null, null); } + /* (non-Javadoc) + * @see com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf#createAndScheduleMissingScss(com.googlesource.gerrit.plugins.eventseiffel.eiffel.SourceChangeEventKey, java.lang.String, com.google.gerrit.extensions.common.AccountInfo, java.lang.Long) + */ + @Override public void createAndScheduleMissingScss( SourceChangeEventKey scs, String commitSha1TransactionEnd, @@ -224,32 +230,36 @@ } } + /* (non-Javadoc) + * @see com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf#createAndScheduleArtc(java.lang.String, java.lang.String, java.lang.Long, boolean) + */ + @Override public void createAndScheduleArtc( - String projectName, String tagName, Long creationTime, boolean force) { + String repoName, String tagName, Long creationTime, boolean force) { try { CompositionDefinedEventKey cd = - CompositionDefinedEventKey.create(mapper.tagCompositionName(projectName), tagName); + CompositionDefinedEventKey.create(mapper.tagCompositionName(repoName), tagName); Optional<UUID> oldCdId = eventHub.getExistingId(cd); if (oldCdId.isEmpty() || force) { - createAndScheduleCd(projectName, tagName, creationTime, force); + createAndScheduleCd(repoName, tagName, creationTime, force); Optional<UUID> cdId = eventHub.getExistingId(cd); if (cdId.isPresent() && !cdId.equals(oldCdId)) { - pushToHub(mapper.toArtc(projectName, tagName, creationTime, cdId.get()), force); + pushToHub(mapper.toArtc(repoName, tagName, creationTime, cdId.get()), force); if (oldCdId.isPresent()) { logger.atInfo().log( - "Event Artc has been forcibly created for: %s, %s", projectName, tagName); + "Event Artc has been forcibly created for: %s, %s", repoName, tagName); } else { - logger.atFine().log("Event Artc has been created for: %s, %s", projectName, tagName); + logger.atFine().log("Event Artc has been created for: %s, %s", repoName, tagName); } } } else { /* Artc event has already been created */ logger.atWarning().log( - "Event Artc has already been created for: %s, %s", projectName, tagName); + "Event Artc has already been created for: %s, %s", repoName, tagName); } } catch (EiffelEventIdLookupException | InterruptedException e) { logger.atSevere().withCause(e).log( - "Event creation failed for: %s, %s to Artc", projectName, tagName); + "Event creation failed for: %s, %s to Artc", repoName, tagName); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIf.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIf.java new file mode 100644 index 0000000..94aa921 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIf.java
@@ -0,0 +1,74 @@ +// Copyright (C) 2022 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.eventseiffel.parsing; + +import com.google.gerrit.extensions.common.AccountInfo; +import com.google.gerrit.extensions.events.RevisionCreatedListener.Event; +import com.googlesource.gerrit.plugins.eventseiffel.eiffel.SourceChangeEventKey; + +public interface EiffelEventParserIf { + + void createAndScheduleSccFromEvent(Event event); + + /** + * Creates SCC events for all commits reachable from branchRef. I.e. create SCCs for already + * merged commit that are missing SCCs. + * + * @param repoName - Name of the repository where the branch exists. + * @param branchRef - Creates missing events for all commits reachable from branch. + */ + void createAndScheduleSccFromBranch(String repoName, String branchRef); + + /** + * Creates SCC events for all commits reachable from commit. + * + * @param repoName - Name of the repository were the commits exists. + * @param branchRef - Ref of the branch to create events for. + * @param commit - Creates missing events for all commits reachable from commit. + */ + void createAndScheduleSccFromCommit(String repoName, String branchRef, String commit); + + /** + * Creates missing SCS events for repoName, branch. + * + * @param repoName - Name of the repository where the branch exists. + * @param branchRef - Creates missing events for all commits reachable from branchRef. + */ + void createAndScheduleMissingScssFromBranch(String repoName, String branchRef); + + /** + * Creates missing SCS events for a submit transaction. submitter and submittedAt is set for all + * events created within the submit transaction, i.e. not reachable from commitSha1TransactionEnd. + * + * @param scs - key for the event that shuold be created, together with parents. + * @param commitSha1TransactionEnd - tip before submit transaction. + * @param submitter - The submitter + * @param submittedAt - When the commit was submitted. + */ + void createAndScheduleMissingScss( + SourceChangeEventKey scs, + String commitSha1TransactionEnd, + AccountInfo submitter, + Long submittedAt); + + /** + * Creates missing ARTC for a tag, together with CD and (if missing) SCS for referenced commit. + * + * @param repoName - Name of the repository were the tag exists. + * @param tagName - The name of the tag. + * @param creationTime - The time at which the tag was created. + * @param force - Whether existing events should be replaced or not. + */ + void createAndScheduleArtc(String repoName, String tagName, Long creationTime, boolean force); +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java index c5d10b8..b457b6f 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java +++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java
@@ -36,12 +36,12 @@ private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private volatile EiffelEventParsingExecutor pool; - private final EiffelEventParser eventParser; + private final EiffelEventParserIf eventParser; private final ConcurrentHashMap<EventParsingWorker, ScheduledFuture<?>> pending = new ConcurrentHashMap<>(); @Inject - public EiffelEventParsingQueue(EiffelEventParsingExecutor pool, EiffelEventParser eventParser) { + public EiffelEventParsingQueue(EiffelEventParsingExecutor pool, EiffelEventParserIf eventParser) { this.pool = pool; this.eventParser = eventParser; }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTestModule.java b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTestModule.java index 3b839dc..579f3b6 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTestModule.java +++ b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTestModule.java
@@ -25,6 +25,8 @@ import com.googlesource.gerrit.plugins.eventseiffel.config.EventMappingConfig; import com.googlesource.gerrit.plugins.eventseiffel.eiffel.api.EventStorage; import com.googlesource.gerrit.plugins.eventseiffel.mapping.EiffelEventFactory; +import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParser; +import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParserIf; import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParsingExecutor; import com.googlesource.gerrit.plugins.eventseiffel.parsing.EiffelEventParsingQueue; import org.junit.Ignore; @@ -42,7 +44,7 @@ bind(EiffelEventParsingExecutor.class).to(EiffelEventParsingExecutor.Direct.class); bind(TestEventPublisher.class).in(Scopes.SINGLETON); bind(EiffelEventHub.Consumer.class).to(TestEventPublisher.class); - + bind(EiffelEventParserIf.class).to(EiffelEventParser.class); bind(EiffelEventParsingQueue.class); bind(EiffelEventFactory.class).toProvider(TestEventFactoryProvider.class);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java index 0c6a3fd..d2d87eb 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java
@@ -342,7 +342,7 @@ bind(EiffelEventHub.class).to(TestEventHub.class); bind(EventMappingConfig.class).toProvider(EventMappingConfig.Provider.class); bind(EiffelEventMapper.class); - bind(EiffelEventParser.class); + bind(EiffelEventParserIf.class).to(EiffelEventParser.class); bind(EiffelConfig.class).toProvider(EiffelConfig.Provider.class).in(Scopes.SINGLETON); bind(EiffelEventFactory.class) .toProvider(EiffelEventFactory.Provider.class)