package de.flux.playground.deckcompare.fileupload;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.ui.ExtendedModelMap;

public class FileUploadControllerTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    private FileUploadController controller;
    private ExtendedModelMap model;

    @Before
    public void setUp() throws Exception {
        controller = new FileUploadController(folder.getRoot().getPath());
        model = new ExtendedModelMap();
    }

    @Test
    public void getRequestsShouldUseUploadForm() throws Exception {
        assertEquals("uploadForm", controller.provideUploadInfo(model));
    }

    @Test
    public void shouldListAllFilesInFolder() throws Exception {
        folder.newFile("firstFile");
        folder.newFile("secondFile");

        controller.provideUploadInfo(model);

        assertTrue("Model does not contain files", model.containsKey("files"));

        @SuppressWarnings("unchecked") // we know that files will be of type List<String>
        final List<String> files = (List<String>) model.get("files");
        assertEquals("Model has wrong number of files", 2, files.size());
        assertTrue("Model does not contain firstFile", files.contains("firstFile"));
        assertTrue("Model does not contain secondFile", files.contains("secondFile"));
    }
}

@@ -118,299 +118,727 @@
 org.
-junit.Assert.assertEquals;%0A%0Aimport java.util.List;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0Aimport org.springframework.ui.ExtendedModelMap;%0A%0Apublic class FileUploadControllerTest %7B%0A%0A    @Rule%0A    public TemporaryFolder f
+hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.junit.Assert.assertEquals;%0A%0Aimport java.io.File;%0Aimport java.io.InputStream;%0Aimport java.util.List;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0Aimport org.springframework.mock.web.MockMultipartFile;%0Aimport org.springframework.ui.ExtendedModelMap;%0Aimport org.springframework.web.servlet.mvc.support.RedirectAttributes;%0Aimport org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;%0A%0Apublic class FileUploadControllerTest %7B%0A%0A    @Rule%0A    public TemporaryFolder uploadFolder = new TemporaryFolder();%0A    @Rule%0A    public TemporaryFolder sourceF
 olde
@@ -945,16 +945,67 @@
 p model;
+%0A    private RedirectAttributes redirectAttributes;
 %0A%0A    @B
@@ -1099,17 +1099,23 @@
 troller(
-f
+uploadF
 older.ge
@@ -1170,24 +1170,87 @@
 ModelMap();%0A
+        redirectAttributes = new RedirectAttributesModelMap();%0A
     %7D%0A%0A    @
@@ -1476,33 +1476,39 @@
 ption %7B%0A        
-f
+uploadF
 older.newFile(%22f
@@ -1527,17 +1527,23 @@
         
-f
+uploadF
 older.ne
@@ -2102,11 +2102,726 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldAddFilestoFolder() throws Exception %7B%0A        InputStream stream = FileUploadControllerTest.class.getResourceAsStream(%22/decks/the-fun-ends-here-shl-jinteki-edition-1st-.o8d%22);%0A        MockMultipartFile firstMultipartFile = new MockMultipartFile(%22firstFile%22, stream);%0A%0A        controller.handleFileUpload(%22firstFile%22, firstMultipartFile, redirectAttributes);%0A%0A        assertThat(redirectAttributes.getFlashAttributes().get(%22message%22), is(%22You successfully uploaded firstFile!%22));%0A%0A        File root = uploadFolder.getRoot();%0A        File%5B%5D listFiles = root.listFiles();%0A        assertThat(listFiles.length, is(1));%0A        assertThat(listFiles%5B0%5D.getName(), is(%22firstFile%22));%0A%0A    %7D
 %0A%7D%0A

Add test for post request to upload file controller