package com.gfk.athena.service;

import com.gfk.athena.domain.Book;
import com.gfk.athena.domain.Borrower;
import com.gfk.athena.domain.User;
import com.gfk.athena.repository.BorrowerRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.Arrays;

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

/**
 * @author Rashidi Zin
 */
@RunWith(MockitoJUnitRunner.class)
public class BorrowerServiceTest {

    @Mock
    private BorrowerRepository repository;

    @InjectMocks
    private BorrowerService $;

    private Borrower borrower;

    @Before
    public void before() {
        borrower = new Borrower(new User(), new Book());
    }

    @Test
    public void create() {
        when(
            repository.findAllForCurrentUserWhereReturnDateIsNull()
        ).thenReturn(
            new ArrayList<>()
        );

        borrower = $.create(borrower);

        verify(repository).findAllForCurrentUserWhereReturnDateIsNull();
        verify(repository).save(any(Borrower.class));
    }

    @Test
    public void createWhenUserIsStillBorrowing() {
        when(
            repository.findAllForCurrentUserWhereReturnDateIsNull()
        ).thenReturn(
            Arrays.asList(borrower)
        );

        borrower = $.create(borrower);

        assertNull(borrower);

        verify(repository).findAllForCurrentUserWhereReturnDateIsNull();
        verify(repository, times(0)).save(any(Borrower.class));
    }
}
