package com.flightstats.filesystem;

import com.google.common.base.Joiner;
import com.google.common.collect.Ordering;
import java.nio.file.Files;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Stream;

import static junit.framework.Assert.assertEquals;

public class LocalFileSystemTest {
    @Test
    public void testListFiles() throws Exception {
        Path tempdir = Files.createTempDirectory("test");
        try {
            for (int x = 1; x < 9; x++) {
                for (int y=1; y < 9; y++) {
                    final Path dir = tempdir.resolve(String.valueOf(x) + "/" + String.valueOf(y));
                    dir.toFile().mkdirs();
                    new FileWriter(dir.resolve(String.valueOf(x)).toFile()).append("").close();
                }
            }
            final List<Path> paths = new LocalFileSystem().listFiles(Paths.get("/tmp/t/9/9/"));
            assertEquals(11, paths.size());
            assertEquals(
                    "/tmp/t/9/1/9\n" +
                    "/tmp/t/9/10/9\n" +
                    "/tmp/t/9/2/9\n" +
                    "/tmp/t/9/3/9\n" +
                    "/tmp/t/9/4/9\n" +
                    "/tmp/t/9/5/9\n" +
                    "/tmp/t/9/6/9\n" +
                    "/tmp/t/9/7/9\n" +
                    "/tmp/t/9/8/9\n" +
                    "/tmp/t/9/9/9\n" +
                    "/tmp/t/9/9/9",

                    Joiner.on("\n").join(Ordering.natural().sortedCopy(paths)));
        } finally {
            for (int x = 1; x < 9; x++) {
                for (int y=1; y < 9; y++) {
                    final Path dir = tempdir.resolve(String.valueOf(x) + "/" + String.valueOf(y));
                    dir.resolve(String.valueOf(x)).toFile().delete();
                    dir.toFile().delete();
                }
                tempdir.resolve(String.valueOf(x)).toFile().delete();
            }

            tempdir.toFile().delete();
//            for (int x = 1; x < 9; x++) {
//                for (int y=1; y < 9; y++) {
//
//                }
//            }
//            tempdir.delete();
        }
    }
}

@@ -64,24 +64,64 @@
 ase.Joiner;%0A
+import com.google.common.collect.Lists;%0A
 import com.g
@@ -1050,29 +1050,28 @@
 les(
-Paths.get(%22/tmp/t/9/9
+tempdir.resolve(%228/8
 /%22))
@@ -1101,10 +1101,9 @@
 als(
-11
+9
 , pa
@@ -1131,95 +1131,117 @@
     
-assertEquals(%0A                    %22/tmp/t/9/1/9%5Cn%22 +%0A                    %22/tmp/t/9/10/9
+final List%3CPath%3E relativePaths = Lists.transform(paths, tempdir::relativize);%0A            assertEquals(%228/1/8
 %5Cn%22 
@@ -1266,27 +1266,28 @@
     
-%22/tmp/t/9/2/9
+    %228/2/8
 %5Cn%22 +%0A
+    
     
@@ -1307,20 +1307,13 @@
    %22
-/tmp/t/9/3/9
+8/3/8
 %5Cn%22 
@@ -1338,21 +1338,18 @@
     
-%22/tmp/t/9/4/9
+    %228/4/8
 %5Cn%22 
@@ -1374,21 +1374,18 @@
     
-%22/tmp/t/9/5/9
+    %228/5/8
 %5Cn%22 
@@ -1410,21 +1410,18 @@
     
-%22/tmp/t/9/6/9
+    %228/6/8
 %5Cn%22 
@@ -1446,21 +1446,18 @@
     
-%22/tmp/t/9/7/9
+    %228/7/8
 %5Cn%22 
@@ -1482,21 +1482,18 @@
     
-%22/tmp/t/9/8/9
+    %228/8/8
 %5Cn%22 
@@ -1518,60 +1518,18 @@
     
-%22/tmp/t/9/9/9%5Cn%22 +%0A                    %22/tmp/t/9/9/9
+    %228/8/8
 %22,%0A%0A
@@ -1599,17 +1599,25 @@
 tedCopy(
-p
+relativeP
 aths)));

fix test