From 80de18d3eeca5434490107b796b92fd7d7e10010 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 16 May 2019 16:31:10 +0400 Subject: [PATCH] #104 create fixed, remove update --- .../conference/service/ConferenceService.java | 8 +- .../service/ConferenceServiceTest.java | 88 +++++++++++-------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 633fa2e..38ce659 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -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 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 diff --git a/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java b/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java index 5925aed..95f4820 100644 --- a/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java +++ b/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java @@ -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 conferences; private List 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)); } } \ No newline at end of file