package de.thpeetz.kontor.media.services;

import de.thpeetz.kontor.common.views.SearchFilter;
import de.thpeetz.kontor.media.TestConstants;
import de.thpeetz.kontor.media.data.MediaFile;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

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

@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MediaFileServiceTest {

    @Autowired
    private MediaFileService mediaFileService;

    @Test
    @Order(1)
    void testFindAllMediaFiles() {
        assertTrue(mediaFileService.findAllMediaFiles(null).isEmpty());
    }

    @Test
    @Order(2)
    void testSaveMediaFile() {
        int mediaFileCount = mediaFileService.findAllMediaFiles(null).size();
        MediaFile mediaFile = new MediaFile();
        mediaFile.setUrl(TestConstants.URL);
        mediaFileService.saveMediaFile(mediaFile);
        assertEquals(mediaFileCount +1, mediaFileService.findAllMediaFiles(null).size());
    }

    @Test
    @Order(3)
    void testDeleteMediaFile() {
        int mediaFileCount = mediaFileService.findAllMediaFiles(null).size();
        SearchFilter searchFilter = new SearchFilter();
        searchFilter.setSearchTerm(TestConstants.URL);
        List<MediaFile> mediaFileList = mediaFileService.findAllMediaFiles(searchFilter);
        assertEquals(1, mediaFileList.size());
        mediaFileService.deleteMediaFile(mediaFileList.get(0));
        assertEquals(--mediaFileCount, mediaFileService.findAllMediaFiles(null).size());
    }
}
