@Test
public void canSerializeOnceIfAllItemsAreTheSame() throws IOException {

    // given
    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();
    // when
    bulk.serializeRequest();
    // then
    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());
}