-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses mocking in the SQS Source test to simplify the unit tests and re…
…duce build times. This knocks off close to a minute from the build. (#3303) Signed-off-by: David Venable <[email protected]>
- Loading branch information
Showing
2 changed files
with
139 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 105 additions & 36 deletions
141
...org/opensearch/dataprepper/plugins/source/sqssource/handler/RawSqsMessageHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,132 @@ | ||
package org.opensearch.dataprepper.plugins.source.sqssource.handler; | ||
|
||
import com.linecorp.armeria.client.retry.Backoff; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.buffer.common.BufferAccumulator; | ||
import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSet; | ||
import org.opensearch.dataprepper.model.configuration.PluginSetting; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import org.opensearch.dataprepper.plugins.aws.sqs.common.SqsService; | ||
import org.opensearch.dataprepper.plugins.aws.sqs.common.metrics.SqsMetrics; | ||
import org.opensearch.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer; | ||
import software.amazon.awssdk.services.sqs.SqsClient; | ||
import software.amazon.awssdk.services.sqs.model.DeleteMessageBatchRequestEntry; | ||
import software.amazon.awssdk.services.sqs.model.Message; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RawSqsMessageHandlerTest { | ||
@Mock | ||
private SqsService sqsService; | ||
|
||
static final Duration BUFFER_TIMEOUT = Duration.ofSeconds(10); | ||
@Mock | ||
private BufferAccumulator bufferAccumulator; | ||
|
||
static final int NO_OF_RECORDS_TO_ACCUMULATE = 100; | ||
private AcknowledgementSet acknowledgementSet; | ||
private List<String> messageBodies; | ||
private List<Message> messages; | ||
|
||
private BlockingBuffer<Record<Event>> getBuffer() { | ||
final HashMap<String, Object> integerHashMap = new HashMap<>(); | ||
integerHashMap.put("buffer_size", 2); | ||
integerHashMap.put("batch_size", 2); | ||
final PluginSetting pluginSetting = new PluginSetting("blocking_buffer", integerHashMap); | ||
pluginSetting.setPipelineName("pipeline"); | ||
return new BlockingBuffer<>(pluginSetting); | ||
@BeforeEach | ||
void setUp() { | ||
messageBodies = IntStream.range(0, 3).mapToObj(i -> UUID.randomUUID().toString()) | ||
.collect(Collectors.toList()); | ||
|
||
messages = messageBodies.stream() | ||
.map(body -> { | ||
Message message = mock(Message.class); | ||
when(message.body()).thenReturn(body); | ||
return message; | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
|
||
acknowledgementSet = null; | ||
} | ||
|
||
private RawSqsMessageHandler createObjectUnderTest() { | ||
return new RawSqsMessageHandler(sqsService); | ||
} | ||
|
||
@Test | ||
void handleMessages_writes_to_buffer_and_flushes() throws Exception { | ||
createObjectUnderTest().handleMessages(messages, bufferAccumulator, acknowledgementSet); | ||
|
||
InOrder inOrder = inOrder(bufferAccumulator); | ||
|
||
ArgumentCaptor<Record<Event>> recordArgumentCaptor = ArgumentCaptor.forClass(Record.class); | ||
|
||
inOrder.verify(bufferAccumulator, times(messages.size())).add(recordArgumentCaptor.capture()); | ||
inOrder.verify(bufferAccumulator).flush(); | ||
|
||
List<Object> actualEventData = recordArgumentCaptor.getAllValues() | ||
.stream() | ||
.map(Record::getData) | ||
.map(e -> e.get("message", Object.class)) | ||
.collect(Collectors.toList()); | ||
|
||
assertThat(actualEventData.size(), equalTo(messages.size())); | ||
|
||
for (int i = 0; i < actualEventData.size(); i++){ | ||
Object messageData = actualEventData.get(i); | ||
assertThat(messageData, instanceOf(String.class)); | ||
assertThat(messageData, equalTo(messageBodies.get(i))); | ||
} | ||
} | ||
|
||
@Test | ||
void sqs_messages_handler_will_read_sqs_message_and_push_to_buffer(){ | ||
final BlockingBuffer<Record<Event>> buffer = getBuffer(); | ||
AcknowledgementSet acknowledgementSet = mock(AcknowledgementSet.class); | ||
String message = UUID.randomUUID().toString(); | ||
String messageId = UUID.randomUUID().toString(); | ||
String receiptHandle = UUID.randomUUID().toString(); | ||
List<Message> messageList = List.of(Message.builder().body(message).messageId(messageId).receiptHandle(receiptHandle).build()); | ||
SqsService sqsService = new SqsService(mock(SqsMetrics.class),mock(SqsClient.class),mock(Backoff.class)); | ||
final BufferAccumulator<Record<Event>> recordBufferAccumulator = BufferAccumulator.create(buffer, NO_OF_RECORDS_TO_ACCUMULATE, BUFFER_TIMEOUT); | ||
RawSqsMessageHandler rawSqsMessageHandler = new RawSqsMessageHandler(sqsService); | ||
final List<DeleteMessageBatchRequestEntry> deleteMessageBatchRequestEntries = rawSqsMessageHandler.handleMessages(messageList,recordBufferAccumulator, acknowledgementSet); | ||
final List<Record<Event>> bufferEvents = new ArrayList<>(buffer.read((int) Duration.ofSeconds(10).toMillis()).getKey()); | ||
final String bufferMessage = bufferEvents.get(0).getData().get("message", String.class); | ||
|
||
assertThat(bufferMessage, CoreMatchers.equalTo(message)); | ||
|
||
assertThat(deleteMessageBatchRequestEntries.get(0).receiptHandle(),equalTo(receiptHandle)); | ||
assertThat(deleteMessageBatchRequestEntries.get(0).id(),equalTo(messageId)); | ||
void handleMessages_returns_deleteList() throws Exception { | ||
List<DeleteMessageBatchRequestEntry> stubbedDeleteList = List.of(mock(DeleteMessageBatchRequestEntry.class)); | ||
when(sqsService.getDeleteMessageBatchRequestEntryList(messages)) | ||
.thenReturn(stubbedDeleteList); | ||
|
||
List<DeleteMessageBatchRequestEntry> actualList = createObjectUnderTest().handleMessages(messages, bufferAccumulator, acknowledgementSet); | ||
|
||
assertThat(actualList, equalTo(stubbedDeleteList)); | ||
} | ||
|
||
@Nested | ||
class WithAcknowledgementSet { | ||
@BeforeEach | ||
void setUp() { | ||
acknowledgementSet = mock(AcknowledgementSet.class); | ||
} | ||
|
||
@Test | ||
void handleMessages_with_acknowledgementSet_adds_events() throws Exception { | ||
createObjectUnderTest().handleMessages(messages, bufferAccumulator, acknowledgementSet); | ||
|
||
ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); | ||
|
||
verify(acknowledgementSet, times(messages.size())).add(eventArgumentCaptor.capture()); | ||
|
||
List<Object> actualEventData = eventArgumentCaptor.getAllValues() | ||
.stream() | ||
.map(e -> e.get("message", Object.class)) | ||
.collect(Collectors.toList()); | ||
|
||
assertThat(actualEventData.size(), equalTo(messages.size())); | ||
|
||
for (int i = 0; i < actualEventData.size(); i++) { | ||
Object messageData = actualEventData.get(i); | ||
assertThat(messageData, instanceOf(String.class)); | ||
assertThat(messageData, equalTo(messageBodies.get(i))); | ||
} | ||
} | ||
} | ||
} |