package net.petrikainulainen.spring.datajpa.service; import net.petrikainulainen.spring.datajpa.dto.PersonDTO; import net.petrikainulainen.spring.datajpa.model.Person; import net.petrikainulainen.spring.datajpa.model.PersonTestUtil; import net.petrikainulainen.spring.datajpa.repository.PersonRepository; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import java.util.ArrayList; import java.util.List; import static junit.framework.Assert.assertEquals; import static org.mockito.Mockito.*; /** * @author Petri Kainulainen */ public class RepositoryPersonServiceTest { private static final long PERSON_COUNT = 4; private static final int PAGE_INDEX = 1; private static final Long PERSON_ID = Long.valueOf(5); private static final String FIRST_NAME = "Foo"; private static final String FIRST_NAME_UPDATED = "FooUpdated"; private static final String LAST_NAME = "Bar"; private static final String LAST_NAME_UPDATED = "BarUpdated"; private static final String SEARCH_TERM = "foo"; private RepositoryPersonService personService; private PersonRepository personRepositoryMock; @Before public void setUp() { personService = new RepositoryPersonService(); personRepositoryMock = mock(PersonRepository.class); personService.setPersonRepository(personRepositoryMock); } @Test public void create() { PersonDTO created = PersonTestUtil.createDTO(null, FIRST_NAME, LAST_NAME); Person persisted = PersonTestUtil.createModelObject(PERSON_ID, FIRST_NAME, LAST_NAME); when(personRepositoryMock.save(any(Person.class))).thenReturn(persisted); Person returned = personService.create(created); ArgumentCaptor<Person> personArgument = ArgumentCaptor.forClass(Person.class); verify(personRepositoryMock, times(1)).save(personArgument.capture()); verifyNoMoreInteractions(personRepositoryMock); assertPerson(created, personArgument.getValue()); assertEquals(persisted, returned); } @Test public void count() { when(personRepositoryMock.count(any(Specification.class))).thenReturn(PERSON_COUNT); long personCount = personService.count(SEARCH_TERM); verify(personRepositoryMock, times(1)).count(any(Specification.class)); verifyNoMoreInteractions(personRepositoryMock); assertEquals(PERSON_COUNT, personCount); } @Test public void delete() throws PersonNotFoundException { Person deleted = PersonTestUtil.createModelObject(PERSON_ID, FIRST_NAME, LAST_NAME); when(personRepositoryMock.findOne(PERSON_ID)).thenReturn(deleted); Person returned = personService.delete(PERSON_ID); verify(personRepositoryMock, times(1)).findOne(PERSON_ID); verify(personRepositoryMock, times(1)).delete(deleted); verifyNoMoreInteractions(personRepositoryMock); assertEquals(deleted, returned); } @Test(expected = PersonNotFoundException.class) public void deleteWhenPersonIsNotFound() throws PersonNotFoundException { when(personRepositoryMock.findOne(PERSON_ID)).thenReturn(null); personService.delete(PERSON_ID); verify(personRepositoryMock, times(1)).findOne(PERSON_ID); verifyNoMoreInteractions(personRepositoryMock); } @Test public void findAll() { List<Person> persons = new ArrayList<Person>(); when(personRepositoryMock.findAll(any(Sort.class))).thenReturn(persons); List<Person> returned = personService.findAll(); ArgumentCaptor<Sort> sortArgument = ArgumentCaptor.forClass(Sort.class); verify(personRepositoryMock, times(1)).findAll(sortArgument.capture()); verifyNoMoreInteractions(personRepositoryMock); Sort actualSort = sortArgument.getValue(); assertEquals(Sort.Direction.ASC, actualSort.getOrderFor("lastName").getDirection()); assertEquals(persons, returned); } @Test public void findById() { Person person = PersonTestUtil.createModelObject(PERSON_ID, FIRST_NAME, LAST_NAME); when(personRepositoryMock.findOne(PERSON_ID)).thenReturn(person); Person returned = personService.findById(PERSON_ID); verify(personRepositoryMock, times(1)).findOne(PERSON_ID); verifyNoMoreInteractions(personRepositoryMock); assertEquals(person, returned); } @Test public void search() { List<Person> expected = new ArrayList<Person>(); Page expectedPage = new PageImpl(expected); when(personRepositoryMock.findAll(any(Specification.class), any(Pageable.class))).thenReturn(expectedPage); List<Person> actual = personService.search(SEARCH_TERM, PAGE_INDEX); ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class); verify(personRepositoryMock, times(1)).findAll(any(Specification.class), pageArgument.capture()); verifyNoMoreInteractions(personRepositoryMock); Pageable pageSpecification = pageArgument.getValue(); assertEquals(PAGE_INDEX, pageSpecification.getPageNumber()); assertEquals(RepositoryPersonService.NUMBER_OF_PERSONS_PER_PAGE, pageSpecification.getPageSize()); assertEquals(Sort.Direction.ASC, pageSpecification.getSort().getOrderFor("lastName").getDirection()); assertEquals(expected, actual); } @Test public void update() throws PersonNotFoundException { PersonDTO updated = PersonTestUtil.createDTO(PERSON_ID, FIRST_NAME_UPDATED, LAST_NAME_UPDATED); Person person = PersonTestUtil.createModelObject(PERSON_ID, FIRST_NAME, LAST_NAME); when(personRepositoryMock.findOne(updated.getId())).thenReturn(person); Person returned = personService.update(updated); verify(personRepositoryMock, times(1)).findOne(updated.getId()); verifyNoMoreInteractions(personRepositoryMock); assertPerson(updated, returned); } @Test(expected = PersonNotFoundException.class) public void updateWhenPersonIsNotFound() throws PersonNotFoundException { PersonDTO updated = PersonTestUtil.createDTO(PERSON_ID, FIRST_NAME_UPDATED, LAST_NAME_UPDATED); when(personRepositoryMock.findOne(updated.getId())).thenReturn(null); personService.update(updated); verify(personRepositoryMock, times(1)).findOne(updated.getId()); verifyNoMoreInteractions(personRepositoryMock); } private void assertPerson(PersonDTO expected, Person actual) { assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getFirstName(), actual.getFirstName()); assertEquals(expected.getLastName(), expected.getLastName()); } }