Skip to content

Commit d1527e5

Browse files
authored
Adds a unit test to see the behavior of empty the ndjson output codec with empty Event objects. (#6099)
Signed-off-by: David Venable <dlv@amazon.com>
1 parent 97158ab commit d1527e5

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

data-prepper-plugins/parse-json-processor/src/test/java/org/opensearch/dataprepper/plugins/codec/json/NdjsonOutputCodecTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.io.OutputStream;
3030
import java.util.Arrays;
31+
import java.util.Collections;
3132
import java.util.LinkedHashMap;
3233
import java.util.List;
3334
import java.util.Map;
@@ -36,7 +37,10 @@
3637
import java.util.stream.IntStream;
3738

3839
import static org.hamcrest.CoreMatchers.equalTo;
40+
import static org.hamcrest.CoreMatchers.is;
41+
import static org.hamcrest.CoreMatchers.notNullValue;
3942
import static org.hamcrest.MatcherAssert.assertThat;
43+
import static org.hamcrest.Matchers.anEmptyMap;
4044
import static org.mockito.Mockito.mock;
4145
import static org.mockito.Mockito.verifyNoInteractions;
4246

@@ -195,6 +199,40 @@ void write_multiple_using_writer(final int numberOfEvents) throws IOException {
195199
}
196200
}
197201

202+
@ParameterizedTest
203+
@ValueSource(ints = {1, 2, 10})
204+
void writer_write_with_empty_values_writes_each_empty_event(final int numberOfEvents) throws IOException {
205+
final NdjsonOutputCodec objectUnderTest = createObjectUnderTest();
206+
207+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
208+
final OutputCodec.Writer writer = objectUnderTest.createWriter(outputStream, null, codecContext);
209+
210+
IntStream.range(0, numberOfEvents)
211+
.mapToObj(eventMap -> eventFactory.eventBuilder(EventBuilder.class).withData(Collections.emptyMap()).build())
212+
.forEach(event -> {
213+
try {
214+
writer.writeEvent(event);
215+
} catch (IOException e) {
216+
throw new RuntimeException(e);
217+
}
218+
});
219+
220+
objectUnderTest.complete(outputStream);
221+
222+
final String jsonLinesCombined = new String(outputStream.toByteArray());
223+
224+
final String[] jsonLines = jsonLinesCombined.split("\n");
225+
226+
assertThat(jsonLines.length, equalTo(numberOfEvents));
227+
228+
for (int i = 0; i < numberOfEvents; i++) {
229+
final String jsonLine = jsonLines[i];
230+
final Map<?, ?> serializedMap = OBJECT_MAPPER.readValue(jsonLine, Map.class);
231+
232+
assertThat(serializedMap, notNullValue());
233+
assertThat(serializedMap, is(anEmptyMap()));
234+
}
235+
}
198236

199237
private static Map<String, Object> generateEventMap() {
200238
final Map<String, Object> jsonObject = new LinkedHashMap<>();

0 commit comments

Comments
 (0)