|
11 | 11 | import org.junit.jupiter.api.BeforeEach; |
12 | 12 | import org.junit.jupiter.api.Test; |
13 | 13 | import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.junit.jupiter.params.ParameterizedTest; |
| 15 | +import org.junit.jupiter.params.provider.Arguments; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
14 | 17 | import org.mockito.ArgumentCaptor; |
15 | 18 | import org.mockito.Mock; |
16 | 19 | import org.mockito.MockedConstruction; |
|
54 | 57 | import java.util.Map; |
55 | 58 | import java.util.Optional; |
56 | 59 | import java.util.UUID; |
| 60 | +import java.util.stream.Stream; |
57 | 61 |
|
58 | 62 | import static org.hamcrest.MatcherAssert.assertThat; |
59 | 63 | import static org.hamcrest.Matchers.equalTo; |
@@ -353,6 +357,62 @@ void doOutput_with_invalid_version_expression_catches_NumberFormatException_and_ |
353 | 357 | verify(dynamicDocumentVersionDroppedEvents).increment(); |
354 | 358 | } |
355 | 359 |
|
| 360 | + @ParameterizedTest |
| 361 | + @MethodSource("invalidVersionExceptionProvider") |
| 362 | + void doOutput_with_invalid_version_expression_does_not_add_event_to_bulk_request( |
| 363 | + final Class<? extends RuntimeException> exceptionType) throws IOException { |
| 364 | + when(pluginSetting.getName()).thenReturn("opensearch"); |
| 365 | + final String versionExpression = UUID.randomUUID().toString(); |
| 366 | + when(indexConfiguration.getVersionExpression()).thenReturn(versionExpression); |
| 367 | + |
| 368 | + final Event event = mock(JacksonEvent.class); |
| 369 | + final String document = UUID.randomUUID().toString(); |
| 370 | + when(event.toJsonString()).thenReturn(document); |
| 371 | + final EventHandle eventHandle = mock(EventHandle.class); |
| 372 | + when(event.getEventHandle()).thenReturn(eventHandle); |
| 373 | + final String index = UUID.randomUUID().toString(); |
| 374 | + when(event.formatString(versionExpression, expressionEvaluator)).thenThrow(exceptionType); |
| 375 | + when(event.formatString(indexConfiguration.getIndexAlias(), expressionEvaluator)).thenReturn(index); |
| 376 | + final Record<Event> eventRecord = new Record<>(event); |
| 377 | + |
| 378 | + final OpenSearchSink objectUnderTest = createObjectUnderTest(); |
| 379 | + when(indexManagerFactory.getIndexManager(any(IndexType.class), eq(openSearchClient), any(RestHighLevelClient.class), eq(openSearchSinkConfiguration), any(TemplateStrategy.class), any())) |
| 380 | + .thenReturn(indexManager); |
| 381 | + doNothing().when(indexManager).setupIndex(); |
| 382 | + objectUnderTest.initialize(); |
| 383 | + |
| 384 | + when(indexManager.getIndexName(anyString())).thenReturn(index); |
| 385 | + |
| 386 | + final DlqObject dlqObject = mock(DlqObject.class); |
| 387 | + final DlqObject.Builder dlqObjectBuilder = mock(DlqObject.Builder.class); |
| 388 | + when(dlqObjectBuilder.withEventHandle(eventHandle)).thenReturn(dlqObjectBuilder); |
| 389 | + when(dlqObjectBuilder.withFailedData(any(FailedDlqData.class))).thenReturn(dlqObjectBuilder); |
| 390 | + when(dlqObjectBuilder.withPluginName(pluginSetting.getName())).thenReturn(dlqObjectBuilder); |
| 391 | + when(dlqObjectBuilder.withPluginId(pluginSetting.getName())).thenReturn(dlqObjectBuilder); |
| 392 | + when(dlqObjectBuilder.withPipelineName(pipelineDescription.getPipelineName())).thenReturn(dlqObjectBuilder); |
| 393 | + when(dlqObject.getFailedData()).thenReturn(mock(FailedDlqData.class)); |
| 394 | + doNothing().when(dlqObject).releaseEventHandle(false); |
| 395 | + when(dlqObjectBuilder.build()).thenReturn(dlqObject); |
| 396 | + |
| 397 | + try (final MockedStatic<DocumentBuilder> documentBuilderMockedStatic = mockStatic(DocumentBuilder.class); |
| 398 | + final MockedStatic<DlqObject> dlqObjectMockedStatic = mockStatic(DlqObject.class)) { |
| 399 | + documentBuilderMockedStatic.when(() -> DocumentBuilder.build(eq(event), eq(null), eq(null), eq(null), eq(null))) |
| 400 | + .thenReturn(UUID.randomUUID().toString()); |
| 401 | + dlqObjectMockedStatic.when(DlqObject::builder).thenReturn(dlqObjectBuilder); |
| 402 | + objectUnderTest.doOutput(List.of(eventRecord)); |
| 403 | + } |
| 404 | + |
| 405 | + verify(dynamicDocumentVersionDroppedEvents).increment(); |
| 406 | + verify(event, times(0)).getJsonNode(); |
| 407 | + } |
| 408 | + |
| 409 | + private static Stream<Arguments> invalidVersionExceptionProvider() { |
| 410 | + return Stream.of( |
| 411 | + Arguments.of(NumberFormatException.class), |
| 412 | + Arguments.of(RuntimeException.class) |
| 413 | + ); |
| 414 | + } |
| 415 | + |
356 | 416 | @Test |
357 | 417 | void test_routing_field_in_document() throws IOException { |
358 | 418 | String routingFieldKey = UUID.randomUUID().toString(); |
|
0 commit comments