package com.novoda.downloadmanager;

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

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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 LiteBatchFileBuilder liteBatchFileBuilder = new LiteBatchFileBuilder(
            storageRoot,
            downloadBatchId,
            ANY_NETWORK_ADDRESS
    );

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

    @Test
    public void concatenatesAllPaths() {
        when(storageRoot.path()).thenReturn("root");

        liteBatchFileBuilder
                .withParentBuilder(batchBuilder)
                .saveTo("my/path", "my-movie.mp4")
                .apply();

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

    @Test
    public void doesNotAddDuplicatePathSeparators() {
        when(storageRoot.path()).thenReturn("root/");
        when(downloadBatchId.rawId()).thenReturn("/my-movie/");

        liteBatchFileBuilder
                .withParentBuilder(batchBuilder)
                .saveTo("/my/path/", "/my-movie.mp4")
                .apply();

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

    @Test
    public void ignoresEmptyPath() {
        when(storageRoot.path()).thenReturn("root");

        liteBatchFileBuilder
                .withParentBuilder(batchBuilder)
                .saveTo("", "my-movie.mp4")
                .apply();

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

    @Test
    public void ignoreSeparatorAsPath() {
        when(storageRoot.path()).thenReturn("root/");

        liteBatchFileBuilder
                .withParentBuilder(batchBuilder)
                .saveTo("/", "/my-movie.mp4")
                .apply();

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

    @Test
    public void doesNotIgnoreSeparatorAsRoot() {
        when(storageRoot.path()).thenReturn("/");

        liteBatchFileBuilder
                .withParentBuilder(batchBuilder)
                .saveTo("my/path", "/my-movie.mp4")
                .apply();

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

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

}

@@ -79,16 +79,60 @@
 .Test;%0A%0A
+import static org.mockito.BDDMockito.given;%0A
 import s
@@ -163,16 +163,16 @@
 o.mock;%0A
-
 import s
@@ -208,48 +208,8 @@
 ify;
-%0Aimport static org.mockito.Mockito.when;
 %0A%0Apu
@@ -794,34 +794,35 @@
 tUp() %7B%0A        
-wh
+giv
 en(downloadBatch
@@ -825,36 +825,36 @@
 atchId.rawId()).
-then
+will
 Return(%22my-movie
@@ -915,34 +915,35 @@
 ths() %7B%0A        
-wh
+giv
 en(storageRoot.p
@@ -941,36 +941,36 @@
 ageRoot.path()).
-then
+will
 Return(%22root%22);%0A
@@ -1293,34 +1293,35 @@
 ors() %7B%0A        
-wh
+giv
 en(storageRoot.p
@@ -1319,36 +1319,36 @@
 ageRoot.path()).
-then
+will
 Return(%22root/%22);
@@ -1356,18 +1356,19 @@
         
-wh
+giv
 en(downl
@@ -1383,28 +1383,28 @@
 Id.rawId()).
-then
+will
 Return(%22/my-
@@ -1723,34 +1723,35 @@
 ath() %7B%0A        
-wh
+giv
 en(storageRoot.p
@@ -1749,36 +1749,36 @@
 ageRoot.path()).
-then
+will
 Return(%22root%22);%0A
@@ -2074,34 +2074,35 @@
 ath() %7B%0A        
-wh
+giv
 en(storageRoot.p
@@ -2100,36 +2100,36 @@
 ageRoot.path()).
-then
+will
 Return(%22root/%22);
@@ -2447,10 +2447,11 @@
     
-wh
+giv
 en(s
@@ -2473,12 +2473,12 @@
 ()).
-then
+will
 Retu

Use BDD mockito