/**
 * Copyright (c) 2016, German Neuroinformatics Node (G-Node)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted under the terms of the BSD License. See
 * LICENSE file in the root of the Project.
 */

package org.g_node.micro.commons;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import org.apache.commons.io.FileUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for the {@link FileService} class. Output and Error streams are redirected
 * from the console to a different PrintStream and reset after tests are finished
 * to avoid mixing tool error messages with actual test error messages.
 *
 * @author Michael Sonntag (sonntag@bio.lmu.de)
 */
public class FileServiceTest {

    private final String tmpRoot = System.getProperty("java.io.tmpdir");
    private final String testFolderName = "fileservicetest";
    private final String testFileName = "test.txt";
    private final Path testFileFolder = Paths.get(tmpRoot, testFolderName);

    /**
     * Create a testfolder and the main test file in the java temp directory.
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {
        final File currTestFile = this.testFileFolder.resolve(this.testFileName).toFile();
        FileUtils.write(currTestFile, "This is a normal test file");
    }

    /**
     * Remove all created folders and files after the tests are done.
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {
        if (Files.exists(this.testFileFolder)) {
            FileUtils.deleteDirectory(this.testFileFolder.toFile());
        }
    }

    /**
     * Check, that the method returns false when presented with a
     * String referencing a non existing file and returns true if it is
     * presented with String referencing an existing file.
     * @throws Exception
     */
    @Test
    public void testCheckFile() throws Exception {
        final String testNonExistingFilePath =
                this.testFileFolder
                        .resolve("IdoNotExist")
                        .toAbsolutePath().normalize().toString();

        assertThat(FileService.checkFile(testNonExistingFilePath)).isFalse();

        final String testExistingFilePath =
                this.testFileFolder
                        .resolve(this.testFileName)
                        .toAbsolutePath().normalize().toString();

        assertThat(FileService.checkFile(testExistingFilePath)).isTrue();
    }

    /**
     * Check, that a file without a file type or a file type that
     * is not supported returns false and test that a file with a supported
     * file type returns true. This test creates two additional files
     * @throws Exception
     */
    @Test
    public void testCheckFileType() throws Exception {
        final String testFileType = "test";
        final String testFileTypeExt = "test.tex";

        final File currTestFileType = this.testFileFolder.resolve(testFileType).toFile();
        final File currTestFileTypeExt = this.testFileFolder.resolve(testFileTypeExt).toFile();

        FileUtils.write(currTestFileType, "This is a normal test file");
        FileUtils.write(currTestFileTypeExt, "This is a normal test file");

        final List<String> testFileTypes = Collections.singletonList("TXT");

        assertThat(
            FileService.checkFileExtension(
                    this.testFileFolder
                            .resolve(testFileType)
                            .toAbsolutePath().normalize().toString(),
                    testFileTypes)
            ).isFalse();

        assertThat(
            FileService.checkFileExtension(
                    this.testFileFolder
                            .resolve(testFileTypeExt)
                            .toAbsolutePath().normalize().toString(),
                    testFileTypes)
        ).isFalse();

        assertThat(
            FileService.checkFileExtension(
                    this.testFileFolder
                            .resolve(this.testFileName)
                            .toAbsolutePath().normalize().toString(),
                    testFileTypes)
        ).isTrue();
    }

}

@@ -2787,20 +2787,25 @@
  a file 
-type
+extension
  or a fi
@@ -2807,20 +2807,25 @@
  a file 
-type
+extension
  that%0A  
@@ -2845,16 +2845,17 @@
 upported
+,
  returns
@@ -2915,12 +2915,17 @@
 ile 
-type
+extension
  ret
@@ -2973,16 +2973,17 @@
 al files
+.
 %0A     * 
@@ -3047,20 +3047,29 @@
 heckFile
-Type
+ExtensionList
 () throw
@@ -3111,20 +3111,27 @@
 testFile
-Type
+NoExtension
  = %22test
@@ -3162,23 +3162,36 @@
 testFile
-TypeExt
+UnsupportedExtension
  = %22test
@@ -3229,20 +3229,27 @@
 TestFile
-Type
+NoExtension
  = this.
@@ -3279,20 +3279,27 @@
 testFile
-Type
+NoExtension
 ).toFile
@@ -3333,20 +3333,22 @@
 TestFile
-Type
+Unsupp
 Ext = th
@@ -3381,23 +3381,36 @@
 testFile
-TypeExt
+UnsupportedExtension
 ).toFile
@@ -3450,20 +3450,27 @@
 TestFile
-Type
+NoExtension
 , %22This 
@@ -3530,20 +3530,22 @@
 TestFile
-Type
+Unsupp
 Ext, %22Th
@@ -3608,20 +3608,25 @@
 testFile
-Type
+Extension
 s = Coll
@@ -3680,32 +3680,36 @@
 at(%0A            
+    
 FileService.chec
@@ -3748,149 +3748,59 @@
     
-this.testFileFolder%0A                            .resolve(testFileType)%0A                            .toAbsolutePath().normalize().toString(),%0A
+    currTestFileNoExtension.getAbsolutePath(),%0A    
     
@@ -3819,35 +3819,36 @@
     testFile
-Types)%0A    
+Extensions)%0A
         ).is
@@ -3869,32 +3869,36 @@
     assertThat(%0A
+    
             File
@@ -3949,152 +3949,55 @@
     
-this.testFileFolder%0A                            .resolve(testFileTypeExt)%0A                            .toAbsolutePath().normalize().toString(),%0A
+    currTestFileUnsuppExt.getAbsolutePath(),%0A  
     
@@ -4008,32 +4008,34 @@
             
+  
 testFile
 Types)%0A     
@@ -4014,36 +4014,41 @@
         testFile
-Type
+Extension
 s)%0A        ).isF
@@ -4080,32 +4080,36 @@
 at(%0A            
+  
+  
 FileService.chec
@@ -4136,32 +4136,36 @@
                 
+    
 this.testFileFol
@@ -4188,32 +4188,36 @@
                 
+  
+  
 .resolve(this.te
@@ -4220,32 +4220,36 @@
 s.testFileName)%0A
+    
                 
@@ -4322,24 +4322,28 @@
         
+ 
+   
 testFile
 Types)%0A 
@@ -4338,12 +4338,17 @@
 File
-Type
+Extension
 s)%0A 

Update check file type test