#104 create fixed, remove update

This commit is contained in:
Nightblade73 2019-05-16 16:31:10 +04:00
parent bde9cb51fb
commit 80de18d3ee
2 changed files with 56 additions and 40 deletions

View File

@ -111,16 +111,16 @@ public class ConferenceService extends BaseService {
}
@Transactional
public Integer create(ConferenceDto conferenceDto) throws IOException {
public Conference create(ConferenceDto conferenceDto) throws IOException {
Conference newConference = copyFromDto(new Conference(), conferenceDto);
newConference = conferenceRepository.save(newConference);
conferenceNotificationService.sendCreateNotification(newConference);
eventService.createFromConference(newConference);
return newConference.getId();
return newConference;
}
@Transactional
public Integer update(ConferenceDto conferenceDto) throws IOException {
public Conference update(ConferenceDto conferenceDto) throws IOException {
Conference conference = conferenceRepository.findOne(conferenceDto.getId());
List<Deadline> oldDeadlines = conference.getDeadlines().stream()
.map(this::copyDeadline)
@ -134,7 +134,7 @@ public class ConferenceService extends BaseService {
conferenceNotificationService.updateConferencesDatesNotification(conference, oldBeginDate, oldEndDate);
}
conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove);
return conference.getId();
return conference;
}
@Transactional

View File

@ -62,11 +62,13 @@ public class ConferenceServiceTest {
@InjectMocks
ConferenceService conferenceService;
private final Integer ID = 1;
private final Integer INDEX = 0;
private final String NAME = "Name";
private final boolean DISABLED = true;
private final Integer YEAR = 2019;
private final static Integer ID = 1;
private final static Integer INDEX = 0;
private final static String NAME = "Name";
private final static String DESCRIPTION = "Desc";
private final static boolean TRUE = true;
private final static Integer YEAR = 2019;
private final static Sort SORT = new Sort(Sort.Direction.DESC, "beginDate");
private List<Conference> conferences;
private List<Deadline> deadlines;
@ -86,14 +88,10 @@ public class ConferenceServiceTest {
public void setUp() throws Exception {
conferences = new ArrayList<>();
conferenceWithId = new Conference();
Conference conferenceWithoutId = new Conference();
conferenceWithId.setId(ID);
conferenceWithId.setTitle(NAME);
conferenceWithId.setDescription("Описание");
conferenceWithoutId.setTitle("Название");
conferenceWithoutId.setDescription("Описание");
conferenceWithId.setDescription(DESCRIPTION);
paperWithId = new Paper();
paperWithId.setId(1);
@ -107,7 +105,7 @@ public class ConferenceServiceTest {
papers.add(paperWithoutId);
deadlines = new ArrayList<>();
deadline = new Deadline(new Date(), "desc");
deadline = new Deadline(new Date(), DESCRIPTION);
deadline.setId(ID);
deadlines.add(deadline);
@ -121,8 +119,6 @@ public class ConferenceServiceTest {
conferenceUsers = new ArrayList<>();
conferenceUsers.add(conferenceUser);
conferences.add(conferenceWithId);
conferenceDto = new ConferenceDto(conferenceWithId);
}
@ -132,10 +128,12 @@ public class ConferenceServiceTest {
when(conferenceRepository.findOne(ID)).thenReturn(conferenceWithId);
when(paperService.findAllNotSelect(new ArrayList<>())).thenReturn(papers);
when(userService.getCurrentUser()).thenReturn(user);
ConferenceDto newConferenceDto = new ConferenceDto(conferenceWithId);
newConferenceDto.setNotSelectedPapers(papers);
newConferenceDto.setDisabledTakePart(!DISABLED);
newConferenceDto.setDisabledTakePart(!TRUE);
ConferenceDto result = conferenceService.getExistConferenceById(ID);
assertEquals(newConferenceDto.getId(), result.getId());
assertEquals(newConferenceDto.getNotSelectedPapers(), result.getNotSelectedPapers());
assertEquals(newConferenceDto.isDisabledTakePart(), result.isDisabledTakePart());
@ -144,9 +142,11 @@ public class ConferenceServiceTest {
@Test
public void getNewConference() {
when(paperService.findAllNotSelect(new ArrayList<>())).thenReturn(papers);
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.setNotSelectedPapers(papers);
ConferenceDto result = conferenceService.getNewConference();
assertEquals(newConferenceDto.getId(), result.getId());
assertEquals(newConferenceDto.getNotSelectedPapers(), result.getNotSelectedPapers());
assertEquals(newConferenceDto.isDisabledTakePart(), result.isDisabledTakePart());
@ -154,29 +154,34 @@ public class ConferenceServiceTest {
@Test
public void findAll() {
when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences);
assertEquals(conferences, conferenceService.findAll());
when(conferenceRepository.findAll(SORT)).thenReturn(conferences);
assertEquals(Collections.singletonList(conferenceWithId), conferenceService.findAll());
}
@Test
public void create() 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);
when(paperService.findPaperById(ID)).thenReturn(paperWithId);
when(paperService.create(new Paper())).thenReturn(paperWithoutId);
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
when(conferenceUserService.saveOrCreate(new ArrayList<>())).thenReturn(conferenceUsers);
when(conferenceRepository.save(new Conference())).thenReturn(conferenceWithId);
assertEquals(conferenceWithId.getId(), conferenceService.create(conferenceDto));
}
@Test
public void update() throws IOException {
when(conferenceRepository.findOne(1)).thenReturn(conferenceWithId);
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);
when(conferenceRepository.save(new Conference())).thenReturn(conferenceWithId);
assertEquals(conferenceWithId.getId(), conferenceService.update(conferenceDto));
conferenceDto.setPapers(papers);
conferenceDto.setDeadlines(deadlines);
conferenceDto.setUsers(conferenceUsers);
conferenceDto.getPaperIds().add(ID);
Conference newConference = new Conference();
newConference.setId(ID);
newConference.setTitle(NAME);
newConference.setDescription(DESCRIPTION);
newConference.setPapers(papers);
newConference.getPapers().add(paperWithId);
newConference.setDeadlines(deadlines);
newConference.setUsers(conferenceUsers);
assertEquals(newConference, conferenceService.create(conferenceDto));
}
@Test
@ -191,6 +196,7 @@ public class ConferenceServiceTest {
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.getDeadlines().add(new Deadline());
conferenceDto.getDeadlines().clear();
assertEquals(newConferenceDto.getDeadlines().get(0), conferenceService.addDeadline(conferenceDto).getDeadlines().get(0));
}
@ -200,18 +206,21 @@ public class ConferenceServiceTest {
newConferenceDto.getRemovedDeadlineIds().add(ID);
conferenceDto.getDeadlines().add(deadline);
ConferenceDto result = conferenceService.removeDeadline(conferenceDto, INDEX);
assertEquals(newConferenceDto.getDeadlines(), result.getDeadlines());
assertEquals(newConferenceDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds());
}
@Test
public void addPaper() {
when(userService.getCurrentUser()).thenReturn(user);
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.getPapers().add(paperWithoutId);
conferenceDto.getPapers().clear();
when(userService.getCurrentUser()).thenReturn(user);
ConferenceDto result = conferenceService.addPaper(conferenceDto);
result.getPapers().get(INDEX).setTitle(NAME); // приходится вручную назначать название, т.е. название зависит от даты
assertEquals(newConferenceDto.getPapers(), result.getPapers());
}
@ -223,6 +232,7 @@ public class ConferenceServiceTest {
conferenceDto.getPapers().add(paperWithId);
conferenceDto.getPapers().add(paperWithoutId);
ConferenceDto result = conferenceService.removePaper(conferenceDto, INDEX);
assertEquals(newConferenceDto.getPapers(), result.getPapers());
assertEquals(newConferenceDto.getNotSelectedPapers(), result.getNotSelectedPapers());
}
@ -230,11 +240,13 @@ public class ConferenceServiceTest {
@Test
public void takePart() throws IOException {
when(userService.getCurrentUser()).thenReturn(user);
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.setUsers(conferenceUsers);
newConferenceDto.setDisabledTakePart(DISABLED);
newConferenceDto.setDisabledTakePart(TRUE);
conferenceDto.getPapers().clear();
ConferenceDto result = conferenceService.takePart(conferenceDto);
assertEquals(newConferenceDto.getUsers(), result.getUsers());
assertEquals(newConferenceDto.isDisabledTakePart(), result.isDisabledTakePart());
}
@ -248,17 +260,20 @@ public class ConferenceServiceTest {
@Test
public void filter() {
when(userService.findById(ID)).thenReturn(user);
when(conferenceRepository.findByUserAndYear(user, YEAR)).thenReturn(conferences);
ConferenceFilterDto conferenceFilterDto = new ConferenceFilterDto();
conferenceFilterDto.setFilterUserId(ID);
conferenceFilterDto.setYear(YEAR);
when(userService.findById(ID)).thenReturn(user);
when(conferenceRepository.findByUserAndYear(user, YEAR)).thenReturn(conferences);
assertEquals(Collections.singletonList(conferenceDto), conferenceService.filter(conferenceFilterDto));
}
@Test
public void isAttachedToConference() {
when(conferenceRepository.isPaperAttached(ID)).thenReturn(true);
when(conferenceRepository.isPaperAttached(ID)).thenReturn(TRUE);
assertTrue(conferenceService.isAttachedToConference(ID));
}
@ -268,6 +283,7 @@ public class ConferenceServiceTest {
when(conferenceRepository.findOne(ID)).thenReturn(conferenceWithId);
when(pingService.addPing(conferenceWithId)).thenReturn(ping);
when(conferenceRepository.updatePingConference(ID)).thenReturn(INDEX);
assertEquals(ping, conferenceService.ping(conferenceDto));
}
}