| // Copyright (C) 2021 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; | |
| import com.google.common.collect.Lists; | |
| import com.google.gerrit.extensions.restapi.NotImplementedException; | |
| import com.google.inject.Inject; | |
| import com.googlesource.gerrit.plugins.eventseiffel.cache.EiffelEventIdCache; | |
| import com.googlesource.gerrit.plugins.eventseiffel.cache.EiffelEventIdLookupException; | |
| import com.googlesource.gerrit.plugins.eventseiffel.eiffel.EventKey; | |
| import com.googlesource.gerrit.plugins.eventseiffel.eiffel.SourceChangeEventKey; | |
| import com.googlesource.gerrit.plugins.eventseiffel.eiffel.dto.EiffelEvent; | |
| import java.util.List; | |
| import java.util.Optional; | |
| import java.util.Set; | |
| import java.util.UUID; | |
| import java.util.stream.Collectors; | |
| import org.junit.Ignore; | |
| @Ignore | |
| public class TestEventHub implements EiffelEventHub { | |
| public static List<EiffelEvent> EVENTS = Lists.newArrayList(); | |
| private final EiffelEventIdCache idCache; | |
| @Inject | |
| public TestEventHub(EiffelEventIdCache idCache) { | |
| this.idCache = idCache; | |
| } | |
| @Override | |
| public void push(EiffelEvent event) { | |
| EVENTS.add(event); | |
| } | |
| @Override | |
| public Optional<UUID> getExistingId(EventKey key) throws EiffelEventIdLookupException { | |
| Optional<UUID> eventId = find(key); | |
| if (eventId.isPresent()) { | |
| return eventId; | |
| } | |
| return idCache.getEventId(key); | |
| } | |
| @Override | |
| public Optional<UUID> getScsForCommit(String repo, String commit, List<String> branches) | |
| throws EiffelEventIdLookupException { | |
| List<SourceChangeEventKey> keys = | |
| branches.stream() | |
| .map(branch -> SourceChangeEventKey.scsKey(repo, branch, commit)) | |
| .collect(Collectors.toList()); | |
| Optional<UUID> id = keys.stream().map(key -> find(key)).flatMap(Optional::stream).findAny(); | |
| if (id.isPresent()) return id; | |
| return idCache.getScsForCommit(repo, commit, branches); | |
| } | |
| private Optional<UUID> find(EventKey key) { | |
| for (EiffelEvent event : EVENTS) { | |
| if (key.equals(EventKey.fromEvent(event))) { | |
| return Optional.of(event.meta.id); | |
| } | |
| } | |
| return Optional.empty(); | |
| } | |
| @Override | |
| public Set<EiffelEvent> take(int max) throws InterruptedException { | |
| return null; | |
| } | |
| @Override | |
| public void ack(Set<EiffelEvent> events) { | |
| throw new NotImplementedException(); | |
| } | |
| @Override | |
| public void nak(Set<EiffelEvent> events) { | |
| throw new NotImplementedException(); | |
| } | |
| @Override | |
| public void startPublishing() { | |
| throw new NotImplementedException(); | |
| } | |
| @Override | |
| public void stopPublishing() { | |
| throw new NotImplementedException(); | |
| } | |
| @Override | |
| public boolean isOpen() { | |
| throw new NotImplementedException(); | |
| } | |
| } |