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.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;

import de.thpeetz.kontor.comics.TestConstants;

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

    @Autowired
    private PublisherRepository publisherRepository;

    @Test
    @Order(1)
    void checkInitialLoad() {
        assertEquals(TestConstants.PUBLISHER_COUNT, publisherRepository.findAll().size());
    }

    @Test
    @Order(2)
    void savePublisher() {
        Publisher publisher = new Publisher();
        publisher.setName(TestConstants.PUBLISHER_NAME);
        publisherRepository.save(publisher);
        assertEquals(TestConstants.PUBLISHER_COUNT + 1, publisherRepository.findAll().size());
    }

    @Test
    @Order(3)
    void findPublisherByName() {
        Publisher publisher = publisherRepository.findByName(TestConstants.PUBLISHER_NAME);
        assertNotNull(publisher);
        assertEquals(TestConstants.PUBLISHER_NAME, publisher.getName());
        Publisher notFound = publisherRepository.findByName("Cow");
        assertNull(notFound);
        List<Publisher> publishers = publisherRepository
                .findByNameIgnoreCase(TestConstants.PUBLISHER_NAME.toLowerCase());
        assertEquals(1, publishers.size());
        assertEquals(TestConstants.PUBLISHER_NAME, publishers.get(0).getName());
    }

    @Test
    @Order(4)
    void searchPublisher() {
        List<Publisher> publishers = publisherRepository.search("Cow");
        assertEquals(1, publishers.size());
        assertEquals("Top Cow Productions", publishers.get(0).getName());
    }

    @Test
    @Order(5)
    void deletePublisher() {
        Publisher publisher = publisherRepository.findByName(TestConstants.PUBLISHER_NAME);
        assertNotNull(publisher);
        publisherRepository.delete(publisher);
        assertEquals(TestConstants.PUBLISHER_COUNT, publisherRepository.findAll().size());
    }
}
