Package cz.muni.fi.pa165

Source Code of cz.muni.fi.pa165.ReservationServiceImplTest

package cz.muni.fi.pa165;

import cz.muni.fi.pa165.library.backend.ReservationDAO;
import cz.muni.fi.pa165.library.backend.ReaderDAO;
import cz.muni.fi.pa165.library.backend.Reader;
import cz.muni.fi.pa165.library.backend.ReservationServiceImpl;
import cz.muni.fi.pa165.library.backend.BookDAO;
import cz.muni.fi.pa165.library.backend.DTOConvertor;
import cz.muni.fi.pa165.library.backend.Book;
import cz.muni.fi.pa165.library.backend.Reservation;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.ReaderTO;
import cz.muni.fi.pa165.library.api.ReservationService;
import cz.muni.fi.pa165.library.api.ReservationTO;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.dao.DataAccessException;


/**
*
* @author Filip Ligac
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(DTOConvertor.class)
public class ReservationServiceImplTest extends TestCase {
   
    @Mock
    private ReservationDAO mockReservationDAO;
   
    @InjectMocks
    private ReservationService reservationService = new ReservationServiceImpl();
   
    @Mock
    private ReaderDAO readerDAO;
   
    @Mock
    private BookDAO bookDAO;
   
    @Mock
    private Reservation reservation1, reservation2;
   
    @Mock
    private ReservationTO reservation1TO, reservation2TO;
   
    @Mock
    private Book book1, book2, book3, book4, book5, book6, book7;
   
    @Mock
    private BookTO book1TO, book2TO, book3TO, book4TO, book5TO, book6TO, book7TO;

    @Mock
    private Reader reader1, reader2;
   
    @Mock
    private ReaderTO reader1TO, reader2TO;
   
    @Mock
    private List<Reservation> reservationList;
   
    @Mock
    private List<ReservationTO> reservationListTO;
   
    @Mock
    private Iterator iterator;
   
    @Mock
    private Date reservationDate;
   
    public ReservationServiceImplTest() {
    }
   
    @Before
    @Override
    public void setUp() throws Exception {
        PowerMockito.mockStatic(DTOConvertor.class);
       
        when(DTOConvertor.convertReservationEntityToTO(null)).thenReturn(null);
        when(DTOConvertor.convertReservationTOToEntity(null)).thenReturn(null);
        when(DTOConvertor.convertReservationEntityToTO(reservation1)).thenReturn(reservation1TO);
        when(DTOConvertor.convertReservationTOToEntity(reservation1TO)).thenReturn(reservation1);
       
        when(DTOConvertor.convertReservationEntityListToTOList(null)).thenReturn(null);
        when(DTOConvertor.convertReservationTOListToEntityList(null)).thenReturn(null);
        when(DTOConvertor.convertReservationEntityListToTOList(reservationList)).thenReturn(reservationListTO);
        when(DTOConvertor.convertReservationTOListToEntityList(reservationListTO)).thenReturn(reservationList);
       
        when(DTOConvertor.convertBookEntityToTO(null)).thenReturn(null);
        when(DTOConvertor.convertBookTOToEntity(null)).thenReturn(null);
        when(DTOConvertor.convertBookEntityToTO(book1)).thenReturn(book1TO);
        when(DTOConvertor.convertBookTOToEntity(book1TO)).thenReturn(book1);

        when(DTOConvertor.convertReaderEntityToTO(null)).thenReturn(null);
        when(DTOConvertor.convertReaderTOToEntity(null)).thenReturn(null);
        when(DTOConvertor.convertReaderEntityToTO(reader1)).thenReturn(reader1TO);
        when(DTOConvertor.convertReaderTOToEntity(reader1TO)).thenReturn(reader1);
       
        when(reservation1TO.getReaderTO()).thenReturn(reader1TO);
        when(reservation1TO.getBookTO()).thenReturn(book1TO);
        when(reservation1TO.getReservationDate()).thenReturn(reservationDate);
        when(reservation1TO.getReservationID()).thenReturn(1l);
       
        when(reservation2TO.getReaderTO()).thenReturn(reader1TO);
        when(reservation2TO.getBookTO()).thenReturn(book2TO);
        when(reservation2TO.getReservationDate()).thenReturn(reservationDate);
        when(reservation2TO.getReservationID()).thenReturn(2l);
       
        when(reservationListTO.iterator()).thenReturn(iterator);
        when(reservationListTO.get(0)).thenReturn(reservation1TO);
        when(reservationListTO.get(1)).thenReturn(reservation2TO);
    }
   
    @After
    @Override
    public void tearDown() {
    }

    @Test(expected = DataAccessException.class)
    public void testInsertReservationWithNullArgument() {
        when(mockReservationDAO.insertReservation(null)).thenThrow(new DataAccessException("Null argument") {});
       
        reservationService.insertReservation(null);
    }
   
    @Test(expected = DataAccessException.class)
    public void testInsertReservationWithIncorrectReaderID() {  
        when(reservation1TO.getReaderTO().getId()).thenReturn(new Long(-5));
        when(mockReservationDAO.insertReservation(reservation1)).thenThrow(new DataAccessException("Incorrect reader") {});
       
        reservationService.insertReservation(reservation1TO);
    }
   
    @Test(expected = DataAccessException.class)
    public void testInsertReservationWithNullReaderID() {
        when(reservation1TO.getReaderTO().getId()).thenReturn(null);
        when(mockReservationDAO.insertReservation(reservation1)).thenThrow(new DataAccessException("Null reader") {});
       
        reservationService.insertReservation(reservation1TO);
   
   
    @Test(expected = DataAccessException.class)
    public void testInsertReservationWithIncorrectBookID() {
        when(reservation1TO.getBookTO().getId()).thenReturn(new Long(-5));
        when(mockReservationDAO.insertReservation(reservation1)).thenThrow(new DataAccessException("Incorrect book") {});
       
        reservationService.insertReservation(reservation1TO);
   
   
    @Test(expected = DataAccessException.class)
    public void testInsertReservationWithNullBookID() {
        when(reservation1TO.getBookTO().getId()).thenReturn(new Long(-5));
        when(mockReservationDAO.insertReservation(reservation1)).thenThrow(new DataAccessException("Null book") {});
       
        reservationService.insertReservation(reservation1TO);
    }
   
    @Test(expected = DataAccessException.class)
    public void testInsertReservationWithIncorrectReservationDate() {
        when(reservation1TO.getReservationDate()).thenReturn(null);
        when(mockReservationDAO.insertReservation(reservation1)).thenThrow(new DataAccessException("Incorrect reservation date") {});
       
        reservationService.insertReservation(reservation1TO);
    }
   
    @Test
    public void testFindAllReservations() {
        when(mockReservationDAO.findAllReservations()).thenReturn(reservationList);       
       
        assertEquals(reservationListTO, reservationService.findAllReservations());
    }
   
    @Test
    public void testFindReservationByID() {
        when(mockReservationDAO.findReservationByID(1l)).thenReturn(reservation1);
       
        assertEquals(reservation1TO, reservationService.findReservationByID(1l));
    }
   
    @Test(expected = DataAccessException.class)
    public void testFindReservationByNullID() {
        when(mockReservationDAO.findReservationByID(null)).thenThrow(new DataAccessException("Null reservation ID") {});
       
        reservationService.findReservationByID(null);
    }
   
    @Test(expected = DataAccessException.class)
    public void testFindReservationByIncorrectID() {
        when(mockReservationDAO.findReservationByID(new Long(-100))).thenThrow(new DataAccessException("Incorrect reservation ID") {});
       
        reservationService.findReservationByID(new Long(-100));
    }
   
   
    @Test
    public void testDeleteReservation() {
        reservationService.deleteReservation(reservation1TO);
       
        assertEquals(null, reservationService.findReservationByID(1l));
    }
   
    @Test(expected = DataAccessException.class)
    public void testDeleteNullReservation() {
        when(mockReservationDAO.deleteReservation(null)).thenThrow(new DataAccessException("Null reservation") {});
       
        reservationService.deleteReservation(null);
    }
}
TOP

Related Classes of cz.muni.fi.pa165.ReservationServiceImplTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.