package de.thpeetz.kontor.comics.repository;

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

import java.util.List;

import de.thpeetz.kontor.comics.data.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
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;

@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class ArtistRepositoryTest {

    private Artist stanLee;
    private static final String ARTISTNAME = "Lee, Stan";

    @Autowired
    ArtistRepository artistRepository;

    @BeforeEach
    void setupData() {
        stanLee = new Artist();
        stanLee.setName(ARTISTNAME);
    }

    @Test
    @Order(1)
    void checkInitialLoad() {
        int count = artistRepository.findAll().size();
        assertEquals(5, count);
    }

    @Test
    @Order(2)
    void saveArtist() {
        int count = artistRepository.findAll().size();
        artistRepository.save(stanLee);
        assertEquals(count+1, artistRepository.findAll().size());
    }

    @Test
    @Order(3)
    void findArtist() {
        List<Artist> artists = artistRepository.findByNameIgnoreCase("lEE, sTAN");
        assertTrue(artists.size() > 0);
        assertEquals(artists.get(0).getName(), stanLee.getName());
    }

    @Test
    @Order(4)
    void searchArtist() {
        List<Artist> artists = artistRepository.search("Lee");
        assertEquals(1, artists.size());
        assertEquals(ARTISTNAME, artists.get(0).getName());
        List<Artist> artists2 = artistRepository.search("Stan");
        assertEquals(1, artists2.size());
        assertEquals(ARTISTNAME, artists2.get(0).getName());
    }

    @Test
    @Order(5)
    void deleteArtist() {
        int count = artistRepository.findAll().size();
        Artist artist = artistRepository.findByName(ARTISTNAME);
        artistRepository.delete(artist);
        assertEquals(count-1, artistRepository.findAll().size());
    }
}
