public static void appendMultipleAttachmentsToBuilder(final MultiPartMIMEWriter.Builder builder,
                                                        final RestLiDataSourceIterator restLiDataSourceIterator)
  {
    builder.appendDataSourceIterator(new MultiPartMIMEDataSourceIterator()
    {
      @Override
      public void abandonAllDataSources()
      {
        restLiDataSourceIterator.abandonAllDataSources();
      }

      @Override
      public void registerDataSourceReaderCallback(MultiPartMIMEDataSourceIteratorCallback callback)
      {
        restLiDataSourceIterator.registerDataSourceReaderCallback(new RestLiDataSourceIteratorCallback()
        {
          @Override
          public void onNewDataSourceWriter(RestLiAttachmentDataSourceWriter dataSourceWriter)
          {
            callback.onNewDataSource(new MultiPartMIMEDataSourceWriter()
            {
              @Override
              public Map<String, String> dataSourceHeaders()
              {
                final Map<String, String> dataSourceHeaders = new TreeMap<>();
                dataSourceHeaders.put(RestConstants.HEADER_CONTENT_ID, dataSourceWriter.getAttachmentID());
                return dataSourceHeaders;
              }

              @Override
              public void onInit(WriteHandle wh)
              {
                dataSourceWriter.onInit(wh);
              }

              @Override
              public void onWritePossible()
              {
                dataSourceWriter.onWritePossible();
              }

              @Override
              public void onAbort(Throwable e)
              {
                dataSourceWriter.onAbort(e);
              }
            });
          }

          @Override
          public void onFinished()
          {
            callback.onFinished();
          }

          @Override
          public void onAbandonComplete()
          {
            callback.onAbandonComplete();
          }

          @Override
          public void onStreamError(Throwable throwable)
          {
            callback.onStreamError(throwable);
          }
        });
      }
    });
  }
@Test
  public void testMultipleAttachments()
  {
    final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
    final List<RestLiTestAttachmentDataSource> testAttachmentDataSources = generateTestDataSources();

    final RestLiTestAttachmentDataSourceIterator dataSourceIterator = new RestLiTestAttachmentDataSourceIterator(testAttachmentDataSources,
                                                                                                                 new IllegalArgumentException());

    //Let each data source know its parent, so that when the data source is done, it can notify it's parent to call onNewDataSourceWriter()
    for (final RestLiTestAttachmentDataSource dataSource : testAttachmentDataSources)
    {
      dataSource.setParentDataSourceIterator(dataSourceIterator);
    }

    AttachmentUtils.appendMultipleAttachmentsToBuilder(builder, dataSourceIterator);

    final StreamRequest streamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(
        URI.create("foo"), "related", builder.build());

    final MultiPartMIMEReader streamRequestReader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
    final CountDownLatch streamRequestReaderLatch = new CountDownLatch(1);
    final MIMETestUtils.MultiPartMIMEFullReaderCallback
        streamRequestReaderCallback = new MIMETestUtils.MultiPartMIMEFullReaderCallback(streamRequestReaderLatch);
    streamRequestReader.registerReaderCallback(streamRequestReaderCallback);
    try
    {
      streamRequestReaderLatch.await(3000, TimeUnit.MILLISECONDS);
    }
    catch (InterruptedException interruptedException)
    {
      Assert.fail();
    }
    verifyAttachments(streamRequestReaderCallback.getSinglePartMIMEReaderCallbacks(), testAttachmentDataSources);
  }