package com.novoda.downloadmanager;

import java.util.Collections;

import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public class BatchBuilderTest {

    private static final DownloadBatchId DOWNLOAD_BATCH_ID = DownloadBatchIdCreator.createSanitizedFrom("download_batch_id");
    private static final String DOWNLOAD_BATCH_TITLE = "download_batch_title";
    private static final DownloadFileId DOWNLOAD_FILE_ID = new LiteDownloadFileId("download_file_id");

    @Test
    public void returnsBatch_whenOptionalParametersAreNotSupplied() {
        Batch batch = Batch.with(DOWNLOAD_BATCH_ID, DOWNLOAD_BATCH_TITLE)
                .downloadFrom("net_address").apply()
                .build();

        BatchFile expectedBatchFile = new BatchFile("net_address", Optional.absent(), Optional.absent());
        Batch expectedBatch = new Batch(DOWNLOAD_BATCH_ID, DOWNLOAD_BATCH_TITLE, Collections.singletonList(expectedBatchFile));

        assertThat(batch).isEqualTo(expectedBatch);
    }

    @Test
    public void returnsBatch_whenOptionalParametersAreSupplied() {
        Batch batch = Batch.with(DOWNLOAD_BATCH_ID, DOWNLOAD_BATCH_TITLE)
                .downloadFrom("net_address").withIdentifier(DOWNLOAD_FILE_ID).saveTo("/foo/bar/", "5MB.zip").apply()
                .build();

        BatchFile expectedBatchFile = new BatchFile("net_address", Optional.of(DOWNLOAD_FILE_ID), Optional.of("/foo/bar/5MB.zip"));
        Batch expectedBatch = new Batch(DOWNLOAD_BATCH_ID, DOWNLOAD_BATCH_TITLE, Collections.singletonList(expectedBatchFile));

        assertThat(batch).isEqualTo(expectedBatch);
    }

    @Test(expected = IllegalArgumentException.class)
    public void throwsException_whenDuplicatedFileIDsAreSupplied() {
        Batch.with(DOWNLOAD_BATCH_ID, DOWNLOAD_BATCH_TITLE)
                .downloadFrom("net_address").withIdentifier(DOWNLOAD_FILE_ID).apply()
                .downloadFrom("another_address").withIdentifier(DOWNLOAD_FILE_ID).apply()
                .build();
    }

    @Test(expected = IllegalArgumentException.class)
    public void throwsException_whenDuplicatedNetworkAddressWithoutFileIDsAreSupplied() {
        Batch.with(DOWNLOAD_BATCH_ID, DOWNLOAD_BATCH_TITLE)
                .downloadFrom("net_address").apply()
                .downloadFrom("net_address").apply()
                .build();
    }

}

@@ -665,35 +665,50 @@
 wnloadFrom(%22
-net_address
+http://example.com/5mb.zip
 %22).apply()%0A 
@@ -790,58 +790,83 @@
 le(%22
-net_address%22, Optional.absent(), Optional.absent()
+http://example.com/5mb.zip%22, Optional.absent(), %22download_batch_id/5mb.zip%22
 );%0A 
@@ -1230,35 +1230,50 @@
 wnloadFrom(%22
-net_address
+http://example.com/5mb.zip
 %22).withIdent
@@ -1304,17 +1304,16 @@
 saveTo(%22
-/
 foo/bar/
@@ -1317,18 +1317,18 @@
 ar/%22, %225
-MB
+mb
 .zip%22).a
@@ -1414,27 +1414,42 @@
 chFile(%22
-net_address
+http://example.com/5mb.zip
 %22, Optio
@@ -1478,21 +1478,26 @@
 D), 
-Optional.of(%22
+%22download_batch_id
 /foo
@@ -1506,17 +1506,16 @@
 ar/5
-MB
+mb
 .zip%22)
-)
 ;%0A  

Fix tests for batch builder.