#104 test copy from dto method

This commit is contained in:
Nightblade73 2019-05-14 17:49:22 +04:00
parent 3a9fde3404
commit a8d987b01f
2 changed files with 39 additions and 10 deletions

View File

@ -181,7 +181,7 @@ public class ConferenceService {
return Arrays.asList(ConferenceUser.Deposit.values());
}
private Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException {
public Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException {
conference.setTitle(conferenceDto.getTitle());
conference.setDescription(conferenceDto.getDescription());
conference.setUrl(conferenceDto.getUrl());

View File

@ -5,11 +5,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Sort;
import ru.ulstu.conference.model.Conference;
import ru.ulstu.conference.model.ConferenceDto;
import ru.ulstu.conference.model.ConferenceUser;
import ru.ulstu.conference.repository.ConferenceRepository;
import ru.ulstu.conference.service.ConferenceService;
import ru.ulstu.conference.service.ConferenceUserService;
@ -17,12 +17,15 @@ import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.service.PaperService;
import ru.ulstu.user.model.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ConferenceServiceTest {
@ -44,7 +47,10 @@ public class ConferenceServiceTest {
private List<Conference> conferences;
private List<Deadline> deadlines;
private List<Paper> papers;
private List<ConferenceUser> conferenceUsers;
private Conference conference;
private ConferenceUser conferenceUser;
private Paper paperWithId;
private Paper paperWithoutId;
@ -56,11 +62,9 @@ public class ConferenceServiceTest {
public void setUp() throws Exception {
conferences = new ArrayList<>();
conference = new Conference();
conference.setId(1);
conference.setTitle("Название");
conference.setDescription("Описание");
conferences.add(conference);
conferenceDto = new ConferenceDto(conference);
paperWithId = new Paper();
paperWithId.setId(1);
@ -69,21 +73,46 @@ public class ConferenceServiceTest {
paperWithoutId = new Paper();
paperWithoutId.setTitle("paperTitleNoId");
papers = new ArrayList<>();
papers.add(paperWithId);
papers.add(paperWithoutId);
deadlines = new ArrayList<>();
deadline = new Deadline(new Date(), "desc");
deadline.setId(1);
deadlines.add(deadline);
conferenceUser = new ConferenceUser();
conferenceUser.setDeposit(ConferenceUser.Deposit.ARTICLE);
conferenceUser.setParticipation(ConferenceUser.Participation.INTRAMURAL);
User user = new User();
user.setFirstName("Name");
conferenceUser.setUser(user);
conferenceUsers = new ArrayList<>();
conferenceUsers.add(conferenceUser);
conference.setPapers(papers);
conference.setDeadlines(deadlines);
conference.setUsers(conferenceUsers);
conferences.add(conference);
conferenceDto = new ConferenceDto(conference);
}
@Test
public void testFindAllConferences() {
Mockito.when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences);
when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences);
assertEquals(conferences, conferenceService.findAll());
}
@Test
public void testCopyFromDto() {
public void testCopyFromDto() throws IOException {
when(paperService.findPaperById(paperWithId.getId())).thenReturn(paperWithId);
when(paperService.create(paperWithoutId)).thenReturn(paperWithoutId);
when(deadlineService.saveOrCreate(conferenceDto.getDeadlines())).thenReturn(deadlines);
when(conferenceUserService.saveOrCreate(conferenceDto.getUsers())).thenReturn(conferenceUsers);
assertEquals(conference, conferenceService.copyFromDto(new Conference(), conferenceDto));
}
}
}