|
28 | 28 | import java.io.IOException; |
29 | 29 | import java.io.OutputStream; |
30 | 30 | import java.util.Arrays; |
| 31 | +import java.util.Collections; |
31 | 32 | import java.util.LinkedHashMap; |
32 | 33 | import java.util.List; |
33 | 34 | import java.util.Map; |
|
36 | 37 | import java.util.stream.IntStream; |
37 | 38 |
|
38 | 39 | import static org.hamcrest.CoreMatchers.equalTo; |
| 40 | +import static org.hamcrest.CoreMatchers.is; |
| 41 | +import static org.hamcrest.CoreMatchers.notNullValue; |
39 | 42 | import static org.hamcrest.MatcherAssert.assertThat; |
| 43 | +import static org.hamcrest.Matchers.anEmptyMap; |
40 | 44 | import static org.mockito.Mockito.mock; |
41 | 45 | import static org.mockito.Mockito.verifyNoInteractions; |
42 | 46 |
|
@@ -195,6 +199,40 @@ void write_multiple_using_writer(final int numberOfEvents) throws IOException { |
195 | 199 | } |
196 | 200 | } |
197 | 201 |
|
| 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 | + } |
198 | 236 |
|
199 | 237 | private static Map<String, Object> generateEventMap() { |
200 | 238 | final Map<String, Object> jsonObject = new LinkedHashMap<>(); |
|
0 commit comments