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

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cz.muni.fi.pa165.dto.BookCollectionDTO;
import cz.muni.fi.pa165.dto.CreateBookCollectionDTO;
import cz.muni.fi.pa165.entity.BookCollection;
import cz.muni.fi.pa165.facade.BookCollectionFacade;
import cz.muni.fi.pa165.service.BookCollectionService;


/**
 * 
 * 
 * @author Jakub Peschel <jakub.peschel@studentagency.cz>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FacadeTestConfiguration.class)
public class BookCollectionFacadeTest {

    @Inject
    BookCollectionService bookCollectionServiceMock;
    
    @Inject
    BookCollectionFacade facade;

    @Test
    public void testCreate() {
        ArgumentCaptor<BookCollection> captor = ArgumentCaptor.forClass(BookCollection.class);
        CreateBookCollectionDTO collection = new CreateBookCollectionDTO();
        String name = "Test";
        collection.setName(name);
        facade.createBookCollection(collection);
        verify(bookCollectionServiceMock).create(captor.capture());
        BookCollection entity = captor.getValue();
        assertNull(entity.getId());
        assertSame(name, entity.getName());
    }

    private BookCollection prepareBookCollection(Long id){
        BookCollection resultCollection = new BookCollection();
        resultCollection.setId(id);
        resultCollection.setName("Test");
        return resultCollection;
    }
   
    
    @Test
    public void testFindById() {
        BookCollection col = prepareBookCollection(1L);
        when(bookCollectionServiceMock.findById(any(Long.class))).thenReturn(col);
        BookCollectionDTO colDTO = facade.findById(col.getId());
        assertEquals(col.getName(), colDTO.getName());
        assertEquals(col.getId(), colDTO.getId());
        assertEquals(col.getBooks(), colDTO.getBooks());
    }

    @Test
    public void testFindAll() {
        BookCollection col = prepareBookCollection(1L);
        BookCollection col2 = prepareBookCollection(2L);
        List<BookCollection> cols = new ArrayList<>();
        cols.add(col);
        cols.add(col2);
        when(bookCollectionServiceMock.findAll()).thenReturn(cols);
        List<BookCollectionDTO> colDTO = facade.findAll();
        assertEquals(2, colDTO.size());
    }

    @Test
    public void testDelete() {
        BookCollection bookCollection = prepareBookCollection(1L);
        when(bookCollectionServiceMock.findById(1L)).thenReturn(bookCollection);
        facade.delete(1L);
        verify(bookCollectionServiceMock).delete(bookCollection);
    }
}

@@ -70,46 +70,8 @@
 .*;%0A
-import static org.mockito.Matchers.*;%0A
 impo
@@ -503,32 +503,69 @@
 kCollectionDTO;%0A
+import cz.muni.fi.pa165.entity.Book;%0A
 import cz.muni.f
@@ -591,24 +591,65 @@
 Collection;%0A
+import cz.muni.fi.pa165.enums.BookState;%0A
 import cz.mu
@@ -748,16 +748,60 @@
 ce;%0A
+import cz.muni.fi.pa165.service.BookService;
 %0A%0A/**%0A *
  %0A *
@@ -796,21 +796,19 @@
 %0A%0A/**%0A *
- 
 %0A *
- 
 %0A * @aut
@@ -1071,20 +1071,16 @@
 ceMock;%0A
-    
 %0A    @In
@@ -1117,16 +1117,66 @@
 facade;%0A
+    %0A    @Inject%0A    BookService bookService;%0A    
 %0A    @Te
@@ -1444,16 +1444,244 @@
 (name);%0A
+        List%3CLong%3E bookIds = new ArrayList();%0A        bookIds.add(1L);%0A        collection.setBookIds(bookIds);%0A        List%3CBook%3E bookList = prepareBookColl();%0A        when(bookService.findById(1L)).thenReturn(bookList.get(0));%0A
         
@@ -1915,24 +1915,459 @@
 .getName());
+ %0A        assertEquals(1, entity.getBooks().size());%0A        assertEquals(bookList.get(0), entity.getBooks().iterator().next());%0A    %7D%0A%0A    private List%3CBook%3E prepareBookColl() %7B%0A        List%3CBook%3E bookList = new ArrayList();%0A        Book book1 = new Book();%0A        book1.setName(%22Test1%22);%0A        book1.setId(1L);%0A        book1.setIsbn(1L);%0A        book1.setState(BookState.NEW);%0A        bookList.add(book1);%0A        return bookList;
 %0A    %7D%0A%0A    
@@ -2419,16 +2419,17 @@
 Long id)
+ 
 %7B%0A      
@@ -2603,24 +2603,16 @@
 ;%0A    %7D%0A
-   %0A    
 %0A    @Te

Fix for bookCollectionFacadeTest