"...individual/unit/persistence/HorseFileDaoTest.java" did not exist on "f9a8d5016a9d3e93a29a79a5e61b53914c974fa1"
HorseFileDaoTest.java 2.12 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 46 47 48 49 50
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))));
    }

    @AfterEach
    public void cleanup() {
        new File(FILE_BASE_PATH + "horse.jpg").delete();
    }
}