package cz.muni.fi.pa165.service.test;

import cz.muni.fi.pa165.config.ServiceConfiguration;
import cz.muni.fi.pa165.dao.BookCollectionDao;
import cz.muni.fi.pa165.entity.Book;
import cz.muni.fi.pa165.entity.BookCollection;
import cz.muni.fi.pa165.enums.BookState;
import cz.muni.fi.pa165.service.BookCollectionService;
import cz.muni.fi.pa165.service.BookCollectionServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertSame;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.MockitoAnnotations;

/**
 * Tests for BookCollectionService
 *
 * @author Jakub Peschel
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ServiceConfiguration.class)
public class BookCollectionServiceTest {

    @Mock
    private BookCollectionDao daoMock;

    @InjectMocks
    BookCollectionService service = new BookCollectionServiceImpl();

    private BookCollection collection;
    private Book book;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        book = new Book();
        book.setId(1L);
        book.setName("testBook");
        book.setIsbn(1L);
        book.setState(BookState.MEDIUM_DAMAGE);
        collection = new BookCollection();
        collection.setName("testCollection");
    }

    @Test
    public void testCreate() {
        collection.addBook(book);
        service.create(collection);
        verify(daoMock).create(collection);
    }

    public void testCreateNoBook() {
        service.create(collection);
        verify(daoMock).create(collection);
    }

    @Test
    public void testFindById() {
        when(daoMock.findById(any(Long.class))).thenReturn(collection);
        Long testId = 10l;
        assertSame(collection, service.findById(testId));
    }

    @Test
    public void testFindAll() {
        List<BookCollection> collections = new ArrayList<>();
        collections.add(collection);
        when(daoMock.findAll()).thenReturn(collections);
        assertSame(collections, service.findAll());
    }

    @Test
    public void testDelete() {
        service.delete(collection);
        verify(daoMock).delete(collection);
    }

    @Test
    public void testAddBookToCollection() {
        when(daoMock.findById(1L)).thenReturn(collection);
        service.addBookToCollection(1L, book);
        verify(daoMock).update(collection);
        assertSame(collection.getBooks().size(), 1);
        assertSame(collection.getBooks().get(1), book);
    }

    @Test
    public void testRemoveBookToCollection() {
        collection.addBook(book);
        when(daoMock.findById(1L)).thenReturn(collection);
        service.removeBookFromCollection(1L, book);
        verify(daoMock).update(collection);
        assertSame(collection.getBooks().size(), 0);
    }
}

@@ -2761,32 +2761,35 @@
      assertSame(
+1, 
 collection.getBo
@@ -2804,11 +2804,8 @@
 ze()
-, 1
 );%0A 
@@ -2814,32 +2814,38 @@
      assertSame(
+book, 
 collection.getBo
@@ -2858,16 +2858,10 @@
 get(
-1), book
+0)
 );%0A 
@@ -3123,32 +3123,35 @@
      assertSame(
+0, 
 collection.getBo
@@ -3166,11 +3166,8 @@
 ze()
-, 0
 );%0A 

fix of tests