package by.bsuir.mpp.computershop.general.service;

import by.bsuir.mpp.computershop.entity.BaseEntity;
import by.bsuir.mpp.computershop.service.CrudService;
import by.bsuir.mpp.computershop.service.exception.EntityNotFoundException;
import by.bsuir.mpp.computershop.service.exception.ServiceException;
import by.bsuir.mpp.computershop.utils.entity.supplier.EntitySupplier;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Matchers;
import org.mockito.MockitoAnnotations;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.repository.CrudRepository;

import java.io.Serializable;

import static org.mockito.Mockito.*;

public abstract class CrudServiceTest<E extends BaseEntity<ID>, ID extends Serializable> {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Before
    public void initTests() {
        MockitoAnnotations.initMocks(this);
    }

    protected abstract CrudService<E, ID> getCrudService();

    protected abstract CrudRepository<E, ID> getCrudRepository();

    protected abstract EntitySupplier<E, ID> getEntitySupplier();

    @Test
    public void findExistingEntityTest() throws Exception {
        E expectedEntity = getEntitySupplier().getValidEntityWithId();
        ID id = expectedEntity.getId();
        when(getCrudRepository().findOne(id)).thenReturn(expectedEntity);

        E actualResult = getCrudService().getOne(id);

        verify(getCrudRepository(), times(1)).findOne(id);
        Assert.assertEquals(expectedEntity, actualResult);
    }


    @Test(expected = EntityNotFoundException.class)
    public void findNotExistingEntityTest() throws Exception {
        ID id = getEntitySupplier().getAnyId();
        when(getCrudRepository().findOne(id)).thenReturn(null);
        E entity = getCrudService().getOne(id);
    }


    @Test(expected = ServiceException.class)
    public void updateEntityExceptionTest() throws Exception {
        E entity = getEntitySupplier().getValidEntityWithoutId();
        when(getCrudRepository().save(Matchers.<E>any())).thenThrow(new DataIntegrityViolationException(""));
        when(getCrudRepository().exists(any())).thenReturn(true);

        getCrudService().update(entity);

        verify(getCrudRepository(), times(1)).save(Matchers.<E>any());
    }
}

@@ -497,32 +497,94 @@
 ectedException;%0A
+import org.mockito.ArgumentCaptor;%0Aimport org.mockito.Captor;%0A
 import org.mocki
@@ -990,24 +990,77 @@
 on.none();%0A%0A
+    @Captor%0A    protected ArgumentCaptor%3CE%3E captor;%0A%0A
     @Before%0A
@@ -2524,11 +2524,463 @@
 );%0A    %7D
+%0A%0A    @Test(expected = EntityNotFoundException.class)%0A    public void updateNotExistingEntityTest() throws Exception %7B%0A        E entity = getEntitySupplier().getValidEntityWithoutId();%0A        ID parameterId = getEntitySupplier().getAnyId();%0A        when(getCrudRepository().exists(parameterId)).thenReturn(false);%0A        when(getCrudRepository().findOne(parameterId)).thenReturn(null);%0A%0A        E actualResult = getCrudService().update(entity);%0A    %7D
 %0A%7D%0A

Add updateNotExistingEntityTest