1818import org .opensearch .dataprepper .event .TestEventFactory ;
1919import org .opensearch .dataprepper .metrics .PluginMetrics ;
2020import org .opensearch .dataprepper .model .buffer .Buffer ;
21+ import org .opensearch .dataprepper .model .codec .DecompressionEngine ;
2122import org .opensearch .dataprepper .model .codec .InputCodec ;
2223import org .opensearch .dataprepper .model .configuration .PipelineDescription ;
24+ import org .opensearch .dataprepper .model .configuration .PluginModel ;
2325import org .opensearch .dataprepper .model .configuration .PluginSetting ;
2426import org .opensearch .dataprepper .model .event .Event ;
2527import org .opensearch .dataprepper .model .event .EventBuilder ;
2628import org .opensearch .dataprepper .model .plugin .PluginFactory ;
2729import org .opensearch .dataprepper .model .record .Record ;
2830import org .opensearch .dataprepper .plugins .buffer .blockingbuffer .BlockingBuffer ;
2931import org .opensearch .dataprepper .plugins .buffer .blockingbuffer .BlockingBufferConfig ;
32+ import org .opensearch .dataprepper .plugins .codec .CompressionOption ;
3033import org .slf4j .Logger ;
3134import org .slf4j .LoggerFactory ;
3235
@@ -90,6 +93,31 @@ private FileSource createObjectUnderTest() {
9093 return new FileSource (fileSourceConfig , pluginMetrics , pluginFactory , TestEventFactory .getTestEventFactory ());
9194 }
9295
96+ /**
97+ * Variant of creatgeObjectUnderTest that uses mocks for the configuration instead of object mapper, so we can
98+ * pass concrete mocks to the FileSource through the FileSourceConfig.
99+ * @param codec the codec to use in the configuration
100+ * @param engine the {@link DecompressionEngine} to use in the configuration
101+ * @return
102+ */
103+ private FileSource createObjectUnderTest (PluginModel codec , DecompressionEngine engine ) {
104+ FileSourceConfig fileSourceConfig = mock (FileSourceConfig .class );
105+
106+ when (fileSourceConfig .getFilePathToRead ()).thenReturn (TEST_FILE_PATH_PLAIN );
107+
108+ if (codec != null ) {
109+ when (fileSourceConfig .getCodec ()).thenReturn (codec );
110+ }
111+
112+ if (engine != null ) {
113+ CompressionOption compressionOption = mock (CompressionOption .class );
114+ when (compressionOption .getDecompressionEngine ()).thenReturn (engine );
115+ when (fileSourceConfig .getCompression ()).thenReturn (compressionOption );
116+ }
117+
118+ return new FileSource (fileSourceConfig , pluginMetrics , pluginFactory , TestEventFactory .getTestEventFactory ());
119+ }
120+
93121 @ Nested
94122 class WithRecord {
95123 private static final String TEST_PIPELINE_NAME = "pipeline" ;
@@ -285,6 +313,9 @@ class WithCodec {
285313 @ Mock
286314 private Buffer buffer ;
287315
316+ @ Mock
317+ private DecompressionEngine decompressionEngine ;
318+
288319 @ BeforeEach
289320 void setUp () {
290321 Map <String , String > codecConfiguration = Map .of (UUID .randomUUID ().toString (), UUID .randomUUID ().toString ());
@@ -297,21 +328,18 @@ void setUp() {
297328
298329 @ Test
299330 void start_will_parse_codec_with_correct_inputStream () throws IOException {
300- createObjectUnderTest ().start (buffer );
331+ final FileInputStream decompressedStream = new FileInputStream (TEST_FILE_PATH_PLAIN );
332+ DecompressionEngine mockEngine = mock (DecompressionEngine .class );
333+ when (mockEngine .createInputStream (any (InputStream .class ))).thenReturn (decompressedStream );
301334
302- final ArgumentCaptor <InputStream > inputStreamArgumentCaptor = ArgumentCaptor .forClass (InputStream .class );
335+ PluginModel fakeCodec = mock (PluginModel .class );
336+ when (fakeCodec .getPluginName ()).thenReturn ("fake_codec" );
337+ when (fakeCodec .getPluginSettings ()).thenReturn (Map .of ());
303338
304- await ().atMost (2 , TimeUnit .SECONDS )
305- .untilAsserted (() -> verify (inputCodec ).parse (any (InputStream .class ), any (Consumer .class )));
306- verify (inputCodec ).parse (inputStreamArgumentCaptor .capture (), any (Consumer .class ));
307-
308- final InputStream actualInputStream = inputStreamArgumentCaptor .getValue ();
339+ createObjectUnderTest (fakeCodec , mockEngine ).start (buffer );
309340
310- final byte [] actualBytes = actualInputStream .readAllBytes ();
311- final FileInputStream fileInputStream = new FileInputStream (TEST_FILE_PATH_PLAIN );
312- final byte [] expectedBytes = fileInputStream .readAllBytes ();
313-
314- assertThat (actualBytes , equalTo (expectedBytes ));
341+ await ().atMost (2 , TimeUnit .SECONDS )
342+ .untilAsserted (() -> verify (inputCodec ).parse (eq (decompressedStream ), any (Consumer .class )));
315343 }
316344
317345 @ Test
0 commit comments