HorseFileDaoTest.java 2.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package at.ac.tuwien.sepm.assignment.individual.unit.persistence;

import static org.junit.jupiter.api.Assertions.*;

import at.ac.tuwien.sepm.assignment.individual.persistence.impl.HorseFileDao;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@SpringBootTest
@ActiveProfiles("test")
public class HorseFileDaoTest {
    @Value("${spring.upload.path}")
    private String FILE_BASE_PATH;
    @Autowired
    HorseFileDao horseFileDao;

    @Test
    @DisplayName("Creating a file with the correct type should result in a saved file")
    public void savingFile_correctType_shouldWriteFile() throws FileNotFoundException, IOException {
        File image = new File("src/test/resources/at/ac/tuwien/sepm/assignment/individual/integration/horse.jpg");
        horseFileDao.save(new MockMultipartFile("file", image.getName(), MediaType.IMAGE_JPEG_VALUE,  new FileInputStream(image)));
        File savedImage = new File(FILE_BASE_PATH + "horse.jpg");
        assertTrue(savedImage.exists());
        assertTrue(savedImage.isFile());
    }

    @Test
    @DisplayName("Creating a file with the correct type should result in a saved file")
    public void savingFile_incorrectType_shouldThrowIOException() throws FileNotFoundException, IOException {
        File image = new File("src/test/resources/at/ac/tuwien/sepm/assignment/individual/integration/horse.jpg");
        assertThrows(IOException.class, () -> horseFileDao.save(new MockMultipartFile("file", image.getName(), MediaType.TEXT_HTML_VALUE,  new FileInputStream(image))));
    }

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    @Test
    @DisplayName("Deleting a file with a correct name should result in a deleted file")
    public void deletingFile_correctName_shouldDeleteFile() throws FileNotFoundException, IOException {
        // Save the file
        File image = new File("src/test/resources/at/ac/tuwien/sepm/assignment/individual/integration/horse.jpg");
        horseFileDao.save(new MockMultipartFile("file", image.getName(), MediaType.IMAGE_JPEG_VALUE,  new FileInputStream(image)));

        // Then delete it
        horseFileDao.delete("horse.jpg");

        assertFalse(new File(FILE_BASE_PATH + "horse.jpg").exists());
    }

    @Test
    @DisplayName("Deleting a file with an empty name should throw an Exception")
    public void deletingFile_emptyName_shouldThrowIO() {
        assertThrows(IOException.class, () -> horseFileDao.delete(""));
    }

65 66 67 68 69
    @AfterEach
    public void cleanup() {
        new File(FILE_BASE_PATH + "horse.jpg").delete();
    }
}