### Question:
ActionFileUpgradeUtil { static void mergeRequest( DownloadRequest request, DefaultDownloadIndex downloadIndex, boolean addNewDownloadAsCompleted, long nowMs) throws IOException { Download download = downloadIndex.getDownload(request.id); if (download != null) { download = DownloadManager.mergeRequest(download, request, download.stopReason, nowMs); } else { download = new Download( request, addNewDownloadAsCompleted ? Download.STATE_COMPLETED : STATE_QUEUED, nowMs, nowMs, C.LENGTH_UNSET, Download.STOP_REASON_NONE, Download.FAILURE_REASON_NONE); } downloadIndex.putDownload(download); } private  ActionFileUpgradeUtil(); @SuppressWarnings("deprecation") static void upgradeAndDelete(
      File actionFilePath,
      @Nullable DownloadIdProvider downloadIdProvider,
      DefaultDownloadIndex downloadIndex,
      boolean deleteOnFailure,
      boolean addNewDownloadsAsCompleted);  }### Answer:
@Test public void mergeRequest_nonExistingDownload_createsNewDownload() throws IOException { byte[] data = new byte[] {1, 2, 3, 4}; DownloadRequest request = new DownloadRequest( "id", TYPE_PROGRESSIVE, Uri.parse("https: asList( new StreamKey( 0, 1, 2), new StreamKey( 3, 4, 5)), "key123", data); ActionFileUpgradeUtil.mergeRequest( request, downloadIndex, false, NOW_MS); assertDownloadIndexContainsRequest(request, Download.STATE_QUEUED); }
@Test public void mergeRequest_existingDownload_createsMergedDownload() throws IOException { StreamKey streamKey1 = new StreamKey( 3, 4, 5); StreamKey streamKey2 = new StreamKey( 0, 1, 2); DownloadRequest request1 = new DownloadRequest( "id", TYPE_PROGRESSIVE, Uri.parse("https: asList(streamKey1), "key123", new byte[] {1, 2, 3, 4}); DownloadRequest request2 = new DownloadRequest( "id", TYPE_PROGRESSIVE, Uri.parse("https: asList(streamKey2), "key123", new byte[] {5, 4, 3, 2, 1}); ActionFileUpgradeUtil.mergeRequest( request1, downloadIndex, false, NOW_MS); ActionFileUpgradeUtil.mergeRequest( request2, downloadIndex, false, NOW_MS); Download download = downloadIndex.getDownload(request2.id); assertThat(download).isNotNull(); assertThat(download.request.type).isEqualTo(request2.type); assertThat(download.request.customCacheKey).isEqualTo(request2.customCacheKey); assertThat(download.request.data).isEqualTo(request2.data); assertThat(download.request.uri).isEqualTo(request2.uri); assertThat(download.request.streamKeys).containsExactly(streamKey1, streamKey2); assertThat(download.state).isEqualTo(Download.STATE_QUEUED); }
@Test public void mergeRequest_addNewDownloadAsCompleted() throws IOException { StreamKey streamKey1 = new StreamKey( 3, 4, 5); StreamKey streamKey2 = new StreamKey( 0, 1, 2); DownloadRequest request1 = new DownloadRequest( "id1", TYPE_PROGRESSIVE, Uri.parse("https: asList(streamKey1), "key123", new byte[] {1, 2, 3, 4}); DownloadRequest request2 = new DownloadRequest( "id2", TYPE_PROGRESSIVE, Uri.parse("https: asList(streamKey2), "key123", new byte[] {5, 4, 3, 2, 1}); ActionFileUpgradeUtil.mergeRequest( request1, downloadIndex, false, NOW_MS); ActionFileUpgradeUtil.mergeRequest( request1, downloadIndex, true, NOW_MS); assertThat(downloadIndex.getDownload(request1.id).state).isEqualTo(Download.STATE_QUEUED); ActionFileUpgradeUtil.mergeRequest( request2, downloadIndex, true, NOW_MS); assertThat(downloadIndex.getDownload(request2.id).state).isEqualTo(Download.STATE_COMPLETED); }