blob: c0099737096099d79b69e8b1a9001c3eb2632d19 [file] [log] [blame]
// 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.pubsub;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.gerritforge.gerrit.eventbroker.EventMessage;
import com.google.api.core.ApiFutures;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.gerrit.json.OutputFormat;
import com.google.gerrit.server.events.ProjectCreatedEvent;
import java.io.IOException;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PubSubPublisherTest {
PubSubPublisher objectUnderTest;
@Mock PubSubConfiguration confMock;
@Mock PublisherProvider publisherProviderMock;
@Mock Publisher publisherMock;
@Mock PubSubPublisherMetrics pubSubPublisherMetricsMock;
private static final String TOPIC = "foo";
private static final EventMessage eventMessage =
new EventMessage(
new EventMessage.Header(UUID.randomUUID(), UUID.randomUUID()), new ProjectCreatedEvent());
@Before
public void setUp() throws IOException {
when(publisherProviderMock.get(TOPIC)).thenReturn(publisherMock);
objectUnderTest =
new PubSubPublisher(
confMock,
publisherProviderMock,
OutputFormat.JSON_COMPACT.newGson(),
pubSubPublisherMetricsMock,
TOPIC);
}
@Test
public void shouldIncrementFailedToPublishMessageWhenAsyncPublishFails() {
when(confMock.isSendAsync()).thenReturn(true);
when(publisherMock.publish(any()))
.thenReturn(ApiFutures.immediateFailedFuture(new Exception("Something went wrong")));
objectUnderTest.publish(eventMessage);
verify(pubSubPublisherMetricsMock, only()).incrementFailedToPublishMessage();
}
@Test
public void shouldIncrementFailedToPublishMessageWhenSyncPublishFails() {
when(confMock.isSendAsync()).thenReturn(false);
when(publisherMock.publish(any()))
.thenReturn(ApiFutures.immediateFailedFuture(new Exception("Something went wrong")));
objectUnderTest.publish(eventMessage);
verify(pubSubPublisherMetricsMock, only()).incrementFailedToPublishMessage();
}
@Test
public void shouldIncrementSuccessToPublishMessageWhenAsyncPublishSucceeds() {
when(confMock.isSendAsync()).thenReturn(true);
when(publisherMock.publish(any())).thenReturn(ApiFutures.immediateFuture("some-message-id"));
objectUnderTest.publish(eventMessage);
verify(pubSubPublisherMetricsMock, only()).incrementSucceedToPublishMessage();
}
@Test
public void shouldIncrementSuccessToPublishMessageWhenSyncPublishSucceeds() {
when(confMock.isSendAsync()).thenReturn(false);
when(publisherMock.publish(any())).thenReturn(ApiFutures.immediateFuture("some-message-id"));
objectUnderTest.publish(eventMessage);
verify(pubSubPublisherMetricsMock, only()).incrementSucceedToPublishMessage();
}
}