package com.novoda.downloadmanager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

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

public class UnlinkedDataRemoverTest {
    
    private static final String FIRST_FILE = "a filename";
    private static final String SECOND_FILE = "another filename";
    private static final String THIRD_FILE = "yet another filename"; // Orphan

    @Test
    public void removeUnlinkedFiles_whenDataRemoverCalled() throws Exception {
        LocalFilesDirectory localFilesDirectory = new FakeLocalFilesDirectory(Arrays.asList(FIRST_FILE, SECOND_FILE, THIRD_FILE));
        UnlinkedDataRemover remover = new UnlinkedDataRemover(localFilesDirectory, new FakeV2DatabaseFiles(Arrays.asList(FIRST_FILE, SECOND_FILE)));
        remover.remove();

        List<String> expectedContents = Arrays.asList(FIRST_FILE, SECOND_FILE);
        List<String> actualContents = localFilesDirectory.contents();

        assertThat(actualContents).isEqualTo(expectedContents);
    }

    private static class FakeLocalFilesDirectory implements LocalFilesDirectory {
        
        private List<String> fileList;

        FakeLocalFilesDirectory(List<String> fileList) {
            this.fileList = new ArrayList<>(fileList);
        }

        @Override
        public List<String> contents() {
            return fileList;
        }

        @Override
        public boolean deleteFile(String filename) {
            boolean fileWasRemoved = false;
            List<String> newFileList = new ArrayList<>();
            for (String s : fileList) {
                if (filename.equals(s)) {
                    fileWasRemoved = true;
                } else {
                    newFileList.add(s);
                }
            }
            fileList = newFileList;
            return fileWasRemoved;
        }
    }

    private static class FakeV2DatabaseFiles implements V2DatabaseFiles {
        
        private final List<String> fileList;

        private FakeV2DatabaseFiles(List<String> fileList) {
            this.fileList = fileList;
        }

        @Override
        public List<String> fileNames() {
            return fileList;
        }
    }
}

@@ -227,20 +227,16 @@
 rTest %7B%0A
-    
 %0A    pri
@@ -260,16 +260,23 @@
  String 
+LINKED_
 FIRST_FI
@@ -326,16 +326,23 @@
  String 
+LINKED_
 SECOND_F
@@ -399,16 +399,25 @@
  String 
+UNLINKED_
 THIRD_FI
@@ -448,18 +448,8 @@
 me%22;
- // Orphan
 %0A%0A  
@@ -619,32 +619,39 @@
 y(Arrays.asList(
+LINKED_
 FIRST_FILE, SECO
@@ -638,32 +638,39 @@
 KED_FIRST_FILE, 
+LINKED_
 SECOND_FILE, THI
@@ -666,16 +666,25 @@
 D_FILE, 
+UNLINKED_
 THIRD_FI
@@ -755,29 +755,8 @@
 ver(
-localFilesDirectory, 
 new 
@@ -781,32 +781,39 @@
 s(Arrays.asList(
+LINKED_
 FIRST_FILE, SECO
@@ -800,32 +800,39 @@
 KED_FIRST_FILE, 
+LINKED_
 SECOND_FILE)));%0A
@@ -828,16 +828,37 @@
 D_FILE))
+, localFilesDirectory
 );%0A     
@@ -933,16 +933,23 @@
 .asList(
+LINKED_
 FIRST_FI
@@ -952,16 +952,23 @@
 T_FILE, 
+LINKED_
 SECOND_F
@@ -1197,24 +1197,16 @@
 ctory %7B%0A
-        
 %0A       

Remove comment by stating which constants are linked and which are not.