### Question:
BufferedBulk extends Bulk { public ByteBuf serializeRequest() throws IOException { ReusableByteBufOutputStream byteBufOutputStream = new ReusableByteBufOutputStream(bulkSource.getSource()); BulkableAction sameAction = getSameItem(actions); byte[] actionTemplate = sameAction != null ? objectWriter.writeValueAsBytes(sameAction) : null; for (BulkableAction action : actions) { if (actionTemplate == null) { objectWriter.writeValue((OutputStream) byteBufOutputStream, action); } else { byteBufOutputStream.write(actionTemplate); } byteBufOutputStream.writeByte(LINE_SEPARATOR); ByteBuf source = ((BufferedIndex)action).getSource().getSource(); bulkSource.getSource().writeBytes(source); byteBufOutputStream.writeByte(LINE_SEPARATOR); } return bulkSource.getSource(); } BufferedBulk(BufferedBulk.Builder builder); BufferedBulkResult deserializeResponse(InputStream responseBody); ByteBuf serializeRequest(); String getURI(); void completed(); Collection<BulkableAction> getActions(); static final char LINE_SEPARATOR; }### Answer:
@Test public void canSerializeUniqueItemsSeparately() throws IOException { ObjectWriter writer = spy(new ObjectMapper().writerFor(BufferedIndex.class)); CompositeByteBuf byteBuf1 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source1 = new ByteBufItemSource(byteBuf1, source -> {}); String index1 = UUID.randomUUID().toString(); BulkableAction action1 = new BufferedIndex.Builder(source1) .index(index1) .build(); CompositeByteBuf byteBuf2 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source2 = new ByteBufItemSource(byteBuf2, source -> {}); String index2 = UUID.randomUUID().toString(); BulkableAction action2 = new BufferedIndex.Builder(source2) .index(index2) .build(); BufferedBulk bulk = (BufferedBulk) new BufferedBulk.Builder() .withObjectWriter(writer) .withObjectReader(mock(ObjectReader.class)) .withBuffer(new ByteBufItemSource(createDefaultTestByteBuf(), source -> {})) .addAction(action1) .addAction(action2) .build(); bulk.serializeRequest(); ArgumentCaptor<BufferedIndex> captor = ArgumentCaptor.forClass(BufferedIndex.class); verify(writer, times(2)).writeValue((OutputStream)any(), captor.capture()); List<BufferedIndex> allValues = captor.getAllValues(); assertEquals(2, allValues.size()); assertEquals(index1, allValues.get(0).getIndex()); assertEquals(index2, allValues.get(1).getIndex()); }
@Test public void canSerializeOnceIfAllItemsAreTheSame() throws IOException { ObjectWriter writer = spy(new ObjectMapper().writerFor(BufferedIndex.class)); CompositeByteBuf byteBuf1 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source1 = new ByteBufItemSource(byteBuf1, source -> {}); String index = UUID.randomUUID().toString(); BulkableAction action1 = new BufferedIndex.Builder(source1) .index(index) .build(); CompositeByteBuf byteBuf2 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source2 = new ByteBufItemSource(byteBuf2, source -> {}); BulkableAction action2 = new BufferedIndex.Builder(source2) .index(index) .build(); BufferedBulk bulk = (BufferedBulk) new BufferedBulk.Builder() .withObjectWriter(writer) .withObjectReader(mock(ObjectReader.class)) .withBuffer(new ByteBufItemSource(createDefaultTestByteBuf(), source -> {})) .addAction(action1) .addAction(action2) .build(); bulk.serializeRequest(); ArgumentCaptor<BufferedIndex> captor = ArgumentCaptor.forClass(BufferedIndex.class); verify(writer, times(1)).writeValueAsBytes(captor.capture()); List<BufferedIndex> allValues = captor.getAllValues(); assertEquals(1, allValues.size()); assertEquals(index, allValues.get(0).getIndex()); }