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.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.MockitoAnnotations;
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);
    }

}

@@ -223,24 +223,93 @@
 dException;%0A
+import by.bsuir.mpp.computershop.service.exception.ServiceException;%0A
 import by.bs
@@ -525,33 +525,126 @@
 ockito.M
-ockitoAnnotations
+atchers;%0Aimport org.mockito.MockitoAnnotations;%0Aimport org.springframework.dao.DataIntegrityViolationException
 ;%0Aimport
@@ -1939,10 +1939,481 @@
 %0A    %7D%0A%0A
+%0A    @Test(expected = ServiceException.class)%0A    public void updateEntityExceptionTest() throws Exception %7B%0A        E entity = getEntitySupplier().getValidEntityWithoutId();%0A        when(getCrudRepository().save(Matchers.%3CE%3Eany())).thenThrow(new DataIntegrityViolationException(%22%22));%0A        when(getCrudRepository().exists(any())).thenReturn(true);%0A%0A        getCrudService().update(entity);%0A%0A        verify(getCrudRepository(), times(1)).save(Matchers.%3CE%3Eany());%0A    %7D%0A
 %7D%0A

Add updateEntityExceptionTest