package de.thpeetz.kontor.bookshelf.data;

import de.thpeetz.kontor.bookshelf.TestConstants;
import org.junit.jupiter.api.Disabled;
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 java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
@Disabled
class AuthorRepositoryTest {

    @Autowired
    AuthorRepository authorRepository;

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

    @Test
    @Order(2)
    void saveAutor() {
        Author author = new Author();
        author.setFirstName(TestConstants.AUTHOR_FIRSTNAME);
        author.setLastName(TestConstants.AUTHOR_LASTNAME);
        authorRepository.save(author);
        assertEquals(TestConstants.AUTHOR_COUNT+1, authorRepository.findAll().size());
    }

    @Test
    @Order(3)
    void findAuthor() {
        Author existingAuthor = authorRepository.findAll().get(0);
        assertNotNull(existingAuthor);
        Author found = authorRepository.findByFirstNameAndLastName(existingAuthor.getFirstName(), existingAuthor.getLastName());
        assertEquals(existingAuthor, found);
    }

    @Test
    @Order(4)
    void searchAuthor() {
        List<Author> authors = authorRepository.search("glas");
        assertEquals(1, authors.size());
        assertEquals("Douglas", authors.get(0).getFirstName());
        List<Author> authors2 = authorRepository.search("dams");
        assertEquals(1, authors2.size());
        assertEquals("Adams", authors2.get(0).getLastName());
    }

    @Test
    @Order(5)
    void deleteAuthor() {
        Author existingAuthor = authorRepository.findByFirstNameAndLastName(TestConstants.AUTHOR_FIRSTNAME, TestConstants.AUTHOR_LASTNAME);
        assertNotNull(existingAuthor);
        authorRepository.delete(existingAuthor);
        assertEquals(TestConstants.AUTHOR_COUNT, authorRepository.findAll().size());
    }
}
