### Question:
BatchRequest implements Request { public ItemSource serialize() throws IOException { ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(buffer.getSource()); IndexRequest identicalAction = uniformAction(indexRequests); byte[] actionTemplate = identicalAction != null ? objectWriter.writeValueAsBytes(identicalAction) : null; for (IndexRequest action : indexRequests) { if (actionTemplate == null) { objectWriter.writeValue((OutputStream) byteBufOutputStream, action); } else { byteBufOutputStream.write(actionTemplate); } byteBufOutputStream.writeByte(LINE_SEPARATOR); ByteBuf source = action.getSource().getSource(); buffer.getSource().writeBytes(source); byteBufOutputStream.writeByte(LINE_SEPARATOR); } return buffer; } protected  BatchRequest(Builder builder); ItemSource serialize(); void completed(); Collection<IndexRequest> getIndexRequests(); @Override String getURI(); @Override String getHttpMethodName(); static final String HTTP_METHOD_NAME; static final char LINE_SEPARATOR; }### Answer:
@Test public void canSerializeUniqueItemsSeparately() throws IOException { ObjectWriter writer = spy(new ObjectMapper().writerFor(IndexRequest.class)); CompositeByteBuf byteBuf1 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source1 = new ByteBufItemSource(byteBuf1, source -> {}); String index1 = UUID.randomUUID().toString(); String mappingType = UUID.randomUUID().toString(); IndexRequest action1 = createIndexRequestBuilder(source1) .index(index1) .type(mappingType) .build(); CompositeByteBuf byteBuf2 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source2 = new ByteBufItemSource(byteBuf2, source -> {}); String index2 = UUID.randomUUID().toString(); IndexRequest action2 = createIndexRequestBuilder(source2) .index(index2) .type(mappingType) .build(); BatchRequest request = new BatchRequest.Builder() .withObjectWriter(writer) .withBuffer(new ByteBufItemSource(createDefaultTestByteBuf(), source -> {})) .add(action1) .add(action2) .build(); request.serialize(); ArgumentCaptor<IndexRequest> captor = ArgumentCaptor.forClass(IndexRequest.class); verify(writer, times(2)).writeValue((OutputStream)any(), captor.capture()); List<IndexRequest> allValues = captor.getAllValues(); assertEquals(2, allValues.size()); assertEquals(index1, allValues.get(0).getIndex()); assertEquals(index2, allValues.get(1).getIndex()); assertEquals(mappingType, allValues.get(0).getType()); assertEquals(mappingType, allValues.get(1).getType()); }
@Test public void canSerializeOnceIfAllItemsAreTheSame() throws IOException { ObjectWriter writer = spy(new ObjectMapper().writerFor(IndexRequest.class)); CompositeByteBuf byteBuf1 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source1 = new ByteBufItemSource(byteBuf1, source -> {}); String index = UUID.randomUUID().toString(); String mappingType = UUID.randomUUID().toString(); IndexRequest action1 = createIndexRequestBuilder(source1) .index(index) .type(mappingType) .build(); CompositeByteBuf byteBuf2 = createDefaultTestByteBuf(); ItemSource<ByteBuf> source2 = new ByteBufItemSource(byteBuf2, source -> {}); IndexRequest action2 = createIndexRequestBuilder(source2) .index(index) .type(mappingType) .build(); BatchRequest batchRequest = new BatchRequest.Builder() .withObjectWriter(writer) .withBuffer(new ByteBufItemSource(createDefaultTestByteBuf(), source -> {})) .add(action1) .add(action2) .build(); batchRequest.serialize(); ArgumentCaptor<IndexRequest> captor = ArgumentCaptor.forClass(IndexRequest.class); verify(writer, times(1)).writeValueAsBytes(captor.capture()); List<IndexRequest> allValues = captor.getAllValues(); assertEquals(1, allValues.size()); assertEquals(index, allValues.get(0).getIndex()); }