package com.novoda.downloadmanager;

import org.junit.Before;
import org.junit.Test;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class LiteBatchFileBuilderTest {

    private static final String ANY_NETWORK_ADDRESS = "http://ak.assets.com/some-file";

    private final InternalBatchBuilder batchBuilder = mock(InternalBatchBuilder.class);
    private final StorageRoot storageRoot = mock(StorageRoot.class);
    private final DownloadBatchId downloadBatchId = mock(DownloadBatchId.class);
    private final BatchFileBuilder liteBatchFileBuilder = new LiteBatchFileBuilder(
            storageRoot,
            downloadBatchId,
            ANY_NETWORK_ADDRESS
    ).withParentBuilder(batchBuilder);

    @Before
    public void setUp() {
        given(downloadBatchId.rawId()).willReturn("my-movie");
    }

    @Test
    public void concatenatesAllPaths() {
        given(storageRoot.path()).willReturn("root");

        whenLiteBatchFileBuilderApply("my/path", "my-movie.mp4");

        verify(batchBuilder).withFile(batchFileWithPath("root/my-movie/my/path/my-movie.mp4"));
    }

    @Test
    public void doesNotAddDuplicateLeadingOrTrailingSeparators() {
        given(storageRoot.path()).willReturn("/root/");
        given(downloadBatchId.rawId()).willReturn("/my-movie/");

        whenLiteBatchFileBuilderApply("/my/path/", "/my-movie.mp4");

        verify(batchBuilder).withFile(batchFileWithPath("/root/my-movie/my/path/my-movie.mp4"));
    }

    @Test
    public void doesNotAddDuplicateSeparatorsWhenInMiddleOfSegments() {
        given(storageRoot.path()).willReturn("/root//to///the//path/");
        given(downloadBatchId.rawId()).willReturn("/my///movie/");

        whenLiteBatchFileBuilderApply("///my/////path/", "//my-movie.mp4");

        verify(batchBuilder).withFile(batchFileWithPath("/root/to/the/path/my/movie/my/path/my-movie.mp4"));
    }

    @Test
    public void ignoresEmptyPath() {
        given(storageRoot.path()).willReturn("root");

        whenLiteBatchFileBuilderApply("", "my-movie.mp4");

        verify(batchBuilder).withFile(batchFileWithPath("root/my-movie/my-movie.mp4"));
    }

    @Test
    public void ignoreSeparatorAsPath() {
        given(storageRoot.path()).willReturn("root/");

        whenLiteBatchFileBuilderApply("/", "/my-movie.mp4");

        verify(batchBuilder).withFile(batchFileWithPath("root/my-movie/my-movie.mp4"));
    }

    @Test
    public void doesNotIgnoreSeparatorAsRoot() {
        given(storageRoot.path()).willReturn("/");

        whenLiteBatchFileBuilderApply("my/path", "/my-movie.mp4");

        verify(batchBuilder).withFile(batchFileWithPath("/my-movie/my/path/my-movie.mp4"));
    }

    private void whenLiteBatchFileBuilderApply(String path, String fileName) {
        liteBatchFileBuilder.saveTo(path, fileName).apply();
    }

    private BatchFile batchFileWithPath(String path) {
        return new BatchFile(
                ANY_NETWORK_ADDRESS,
                path,
                Optional.absent(),
                Optional.absent()
        );
    }

}

@@ -34,33 +34,8 @@
 r;%0A%0A
-import org.junit.Before;%0A
 impo
@@ -417,16 +417,21 @@
 e final 
+Batch
 StorageR
@@ -434,17 +434,22 @@
 ageRoot 
-s
+batchS
 torageRo
@@ -458,16 +458,21 @@
  = mock(
+Batch
 StorageR
@@ -487,89 +487,8 @@
 s);%0A
-    private final DownloadBatchId downloadBatchId = mock(DownloadBatchId.class);%0A
     
@@ -579,17 +579,22 @@
         
-s
+batchS
 torageRo
@@ -601,37 +601,8 @@
 ot,%0A
-            downloadBatchId,%0A
     
@@ -673,116 +673,8 @@
 );%0A%0A
-    @Before%0A    public void setUp() %7B%0A        given(downloadBatchId.rawId()).willReturn(%22my-movie%22);%0A    %7D%0A%0A
     
@@ -726,33 +726,38 @@
 %7B%0A        given(
-s
+batchS
 torageRoot.path(
@@ -767,32 +767,41 @@
 willReturn(%22root
+/my-movie
 %22);%0A%0A        whe
@@ -1042,33 +1042,38 @@
 %7B%0A        given(
-s
+batchS
 torageRoot.path(
@@ -1097,64 +1097,8 @@
 oot/
-%22);%0A        given(downloadBatchId.rawId()).willReturn(%22/
 my-m
@@ -1369,33 +1369,38 @@
 %7B%0A        given(
-s
+batchS
 torageRoot.path(
@@ -1421,16 +1421,17 @@
 %22/root//
+/
 to///the
@@ -1441,64 +1441,8 @@
 ath/
-%22);%0A        given(downloadBatchId.rawId()).willReturn(%22/
 my//
@@ -1699,33 +1699,38 @@
 %7B%0A        given(
-s
+batchS
 torageRoot.path(
@@ -1748,16 +1748,25 @@
 rn(%22root
+/my-movie
 %22);%0A%0A   
@@ -1975,33 +1975,38 @@
 %7B%0A        given(
-s
+batchS
 torageRoot.path(
@@ -2025,16 +2025,24 @@
 n(%22root/
+my-movie
 %22);%0A%0A   
@@ -2268,17 +2268,22 @@
   given(
-s
+batchS
 torageRo
@@ -2306,16 +2306,24 @@
 eturn(%22/
+my-movie
 %22);%0A%0A   

Fix tests