From 9da5fd8a7a8ae3d4f8fff8aea2d5349f1fc3ca2a Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 00:02:30 +0400 Subject: [PATCH 01/68] #104 shock, the first unit test works --- .../conference/service/ConferenceService.java | 2 + .../module_test/ConferenceServiceTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/test/java/conference/module_test/ConferenceServiceTest.java diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 38de435..8ffbf70 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -1,6 +1,7 @@ package ru.ulstu.conference.service; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -43,6 +44,7 @@ public class ConferenceService { private final ConferenceNotificationService conferenceNotificationService; private final EventService eventService; + @Autowired public ConferenceService(ConferenceRepository conferenceRepository, ConferenceUserService conferenceUserService, DeadlineService deadlineService, diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java new file mode 100644 index 0000000..44c92ea --- /dev/null +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -0,0 +1,37 @@ +package conference.module_test; + +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.repository.ConferenceRepository; +import ru.ulstu.conference.service.ConferenceService; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class ConferenceServiceTest { + + @Mock + ConferenceRepository conferenceRepository; + + @InjectMocks + ConferenceService conferenceService; + + @Test + public void testFindAllConferences() { + List conferences = new ArrayList<>(); + Conference conference = new Conference(); + conference.setId(1); + conferences.add(conference); + Mockito.when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences); + assertEquals(conferences, conferenceService.findAll()); + } +} From 3a9fde3404bd3422dabde9a2a5459508bd043765 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 16:20:19 +0400 Subject: [PATCH 02/68] #104 setUp fields --- .../module_test/ConferenceServiceTest.java | 60 +++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java index 44c92ea..de83ae7 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -1,5 +1,6 @@ package conference.module_test; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -8,10 +9,17 @@ 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.repository.ConferenceRepository; import ru.ulstu.conference.service.ConferenceService; +import ru.ulstu.conference.service.ConferenceUserService; +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 java.util.ArrayList; +import java.util.Date; import java.util.List; import static org.junit.Assert.assertEquals; @@ -22,16 +30,60 @@ public class ConferenceServiceTest { @Mock ConferenceRepository conferenceRepository; + @Mock + DeadlineService deadlineService; + + @Mock + ConferenceUserService conferenceUserService; + + @Mock + PaperService paperService; + @InjectMocks ConferenceService conferenceService; + private List conferences; + private List deadlines; + private Conference conference; + + private Paper paperWithId; + private Paper paperWithoutId; + private Deadline deadline; + + private ConferenceDto conferenceDto; + + @Before + 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); + paperWithId.setTitle("paperTitle"); + + paperWithoutId = new Paper(); + paperWithoutId.setTitle("paperTitleNoId"); + + deadlines = new ArrayList<>(); + deadline = new Deadline(new Date(),"desc"); + deadline.setId(1); + deadlines.add(deadline); + } + @Test public void testFindAllConferences() { - List conferences = new ArrayList<>(); - Conference conference = new Conference(); - conference.setId(1); - conferences.add(conference); Mockito.when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences); assertEquals(conferences, conferenceService.findAll()); } + + @Test + public void testCopyFromDto() { + + + } } From a8d987b01f5e62f3a48652c248c7f55904518e00 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 17:49:22 +0400 Subject: [PATCH 03/68] #104 test copy from dto method --- .../conference/service/ConferenceService.java | 2 +- .../module_test/ConferenceServiceTest.java | 47 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 8ffbf70..0e4dd54 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -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()); diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java index de83ae7..87a77a8 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -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 conferences; private List deadlines; + private List papers; + private List 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 = 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)); } + } From e6cb991463b4a6d0eb549d6dc9418b63b5fc97e0 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 17:54:18 +0400 Subject: [PATCH 04/68] #104 test is member --- .../module_test/ConferenceServiceTest.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java index 87a77a8..1e8f306 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -18,6 +18,7 @@ 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 ru.ulstu.user.service.UserService; import java.io.IOException; import java.util.ArrayList; @@ -42,6 +43,9 @@ public class ConferenceServiceTest { @Mock PaperService paperService; + @Mock + UserService userService; + @InjectMocks ConferenceService conferenceService; @@ -57,6 +61,7 @@ public class ConferenceServiceTest { private Deadline deadline; private ConferenceDto conferenceDto; + private User user; @Before public void setUp() throws Exception { @@ -85,7 +90,7 @@ public class ConferenceServiceTest { conferenceUser = new ConferenceUser(); conferenceUser.setDeposit(ConferenceUser.Deposit.ARTICLE); conferenceUser.setParticipation(ConferenceUser.Participation.INTRAMURAL); - User user = new User(); + user = new User(); user.setFirstName("Name"); conferenceUser.setUser(user); @@ -115,4 +120,9 @@ public class ConferenceServiceTest { assertEquals(conference, conferenceService.copyFromDto(new Conference(), conferenceDto)); } + @Test + public void testIsCurrentUserParticipant() throws IOException { + when(userService.getCurrentUser()).thenReturn(user); + assertEquals(true, conferenceService.isCurrentUserParticipant(conferenceUsers)); + } } From 1c5c05b5e466ff364d9e335615018b03151e39a1 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 18:01:50 +0400 Subject: [PATCH 05/68] #104 test take part --- .../conference/module_test/ConferenceServiceTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java index 1e8f306..1ccbc85 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -125,4 +125,12 @@ public class ConferenceServiceTest { when(userService.getCurrentUser()).thenReturn(user); assertEquals(true, conferenceService.isCurrentUserParticipant(conferenceUsers)); } + + @Test + public void testTakePart() throws IOException { + when(userService.getCurrentUser()).thenReturn(user); + ConferenceDto newConferenceDto = new ConferenceDto(); + conferenceService.takePart(newConferenceDto); + assertEquals(newConferenceDto.getUsers(), conferenceDto.getUsers()); + } } From c88c77cd7db6800f8d0df39351a0f54916ad02f2 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 18:20:01 +0400 Subject: [PATCH 06/68] #104 test find one dto --- .../ulstu/conference/model/ConferenceDto.java | 27 +++++++++++++++++++ .../conference/service/ConferenceService.java | 1 - .../module_test/ConferenceServiceTest.java | 6 +++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index 2e2f13b..944cd9d 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -13,6 +13,7 @@ import javax.validation.constraints.Size; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Objects; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -218,4 +219,30 @@ public class ConferenceDto { return BEGIN_DATE + beginDate.toString().split(" ")[0] + " " + END_DATE + endDate.toString().split(" ")[0]; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ConferenceDto that = (ConferenceDto) o; + return ping == that.ping && + disabledTakePart == that.disabledTakePart && + Objects.equals(id, that.id) && + Objects.equals(title, that.title) && + Objects.equals(description, that.description) && + Objects.equals(url, that.url) && + Objects.equals(beginDate, that.beginDate) && + Objects.equals(endDate, that.endDate) && + Objects.equals(deadlines, that.deadlines) && + Objects.equals(removedDeadlineIds, that.removedDeadlineIds) && + Objects.equals(userIds, that.userIds) && + Objects.equals(paperIds, that.paperIds) && + Objects.equals(papers, that.papers) && + Objects.equals(notSelectedPapers, that.notSelectedPapers) && + Objects.equals(users, that.users); + } + + @Override + public int hashCode() { + return Objects.hash(id, title, description, url, ping, beginDate, endDate, deadlines, removedDeadlineIds, userIds, paperIds, papers, notSelectedPapers, users, disabledTakePart); + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 0e4dd54..cdb33ef 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -149,7 +149,6 @@ public class ConferenceService { Paper paper = new Paper(); paper.setTitle(userService.getCurrentUser().getLastName() + "_" + conferenceDto.getTitle() + "_" + (new Date()).getTime()); paper.setStatus(Paper.PaperStatus.DRAFT); - conferenceDto.getPapers().add(paper); } diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java index 1ccbc85..6f2146b 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -133,4 +133,10 @@ public class ConferenceServiceTest { conferenceService.takePart(newConferenceDto); assertEquals(newConferenceDto.getUsers(), conferenceDto.getUsers()); } + + @Test + public void testFindOneDto() throws IOException { + when(conferenceRepository.findOne(1)).thenReturn(conference); + assertEquals(conferenceDto, conferenceService.findOneDto(1)); + } } From 11cab955b4d51bbc3b11413a0e0d844b05edfb5a Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Tue, 14 May 2019 18:51:03 +0400 Subject: [PATCH 07/68] #104 test create --- .../ulstu/conference/model/ConferenceDto.java | 11 ++-- .../module_test/ConferenceServiceTest.java | 52 +++++++++++++++---- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index 944cd9d..76dbb90 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -221,8 +221,12 @@ public class ConferenceDto { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } ConferenceDto that = (ConferenceDto) o; return ping == that.ping && disabledTakePart == that.disabledTakePart && @@ -243,6 +247,7 @@ public class ConferenceDto { @Override public int hashCode() { - return Objects.hash(id, title, description, url, ping, beginDate, endDate, deadlines, removedDeadlineIds, userIds, paperIds, papers, notSelectedPapers, users, disabledTakePart); + return Objects.hash(id, title, description, url, ping, beginDate, endDate, deadlines, removedDeadlineIds, + userIds, paperIds, papers, notSelectedPapers, users, disabledTakePart); } } diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/conference/module_test/ConferenceServiceTest.java index 6f2146b..200b6f1 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/conference/module_test/ConferenceServiceTest.java @@ -11,12 +11,14 @@ 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.ConferenceNotificationService; import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.conference.service.ConferenceUserService; 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.timeline.service.EventService; import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; @@ -26,6 +28,7 @@ import java.util.Date; import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -46,6 +49,12 @@ public class ConferenceServiceTest { @Mock UserService userService; + @Mock + ConferenceNotificationService conferenceNotificationService; + + @Mock + EventService eventService; + @InjectMocks ConferenceService conferenceService; @@ -53,7 +62,9 @@ public class ConferenceServiceTest { private List deadlines; private List papers; private List conferenceUsers; - private Conference conference; + + private Conference conferenceWithId; + private Conference conferenceWithoutId; private ConferenceUser conferenceUser; private Paper paperWithId; @@ -66,10 +77,15 @@ public class ConferenceServiceTest { @Before public void setUp() throws Exception { conferences = new ArrayList<>(); - conference = new Conference(); + conferenceWithId = new Conference(); + conferenceWithoutId = new Conference(); - conference.setTitle("Название"); - conference.setDescription("Описание"); + conferenceWithId.setId(1); + conferenceWithId.setTitle("Название"); + conferenceWithId.setDescription("Описание"); + + conferenceWithoutId.setTitle("Название"); + conferenceWithoutId.setDescription("Описание"); paperWithId = new Paper(); paperWithId.setId(1); @@ -97,12 +113,16 @@ public class ConferenceServiceTest { conferenceUsers = new ArrayList<>(); conferenceUsers.add(conferenceUser); - conference.setPapers(papers); - conference.setDeadlines(deadlines); - conference.setUsers(conferenceUsers); + conferenceWithId.setPapers(papers); + conferenceWithId.setDeadlines(deadlines); + conferenceWithId.setUsers(conferenceUsers); - conferences.add(conference); - conferenceDto = new ConferenceDto(conference); + conferenceWithoutId.setPapers(papers); + conferenceWithoutId.setDeadlines(deadlines); + conferenceWithoutId.setUsers(conferenceUsers); + + conferences.add(conferenceWithId); + conferenceDto = new ConferenceDto(conferenceWithId); } @Test @@ -117,7 +137,7 @@ public class ConferenceServiceTest { 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)); + assertEquals(conferenceWithoutId, conferenceService.copyFromDto(new Conference(), conferenceDto)); } @Test @@ -136,7 +156,17 @@ public class ConferenceServiceTest { @Test public void testFindOneDto() throws IOException { - when(conferenceRepository.findOne(1)).thenReturn(conference); + when(conferenceRepository.findOne(1)).thenReturn(conferenceWithId); assertEquals(conferenceDto, conferenceService.findOneDto(1)); } + + @Test + public void testCreate() 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(conferenceRepository.save(conferenceWithoutId)).thenReturn(conferenceWithoutId); + assertNull(conferenceService.create(conferenceDto)); + } } From 95fc9da47decf785efc8aed710c02936b0994a38 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 15 May 2019 00:49:54 +0400 Subject: [PATCH 08/68] lk base, created from dev --- .../ulstu/user/controller/UserController.java | 7 - .../user/controller/UserControllerV2.java | 178 ++++++++++++++++++ .../ru/ulstu/user/service/UserService.java | 21 +-- .../user/service/UserSessionService.java | 4 + src/main/resources/templates/default.html | 10 +- .../resources/templates/users/profile.html | 75 ++++++++ 6 files changed, 269 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/controller/UserControllerV2.java create mode 100644 src/main/resources/templates/users/profile.html diff --git a/src/main/java/ru/ulstu/user/controller/UserController.java b/src/main/java/ru/ulstu/user/controller/UserController.java index d7db909..6806d19 100644 --- a/src/main/java/ru/ulstu/user/controller/UserController.java +++ b/src/main/java/ru/ulstu/user/controller/UserController.java @@ -141,13 +141,6 @@ public class UserController extends OdinController { return new Response<>(userService.activateUser(activationKey)); } - // TODO: add page for user edit (user-profile) - @PostMapping("/change-information") - public Response changeInformation(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.changeInformation( {} )", userDto.getLogin()); - return new Response<>(userService.updateUserInformation(userDto)); - } - // TODO: add page for user password change (user-profile) @PostMapping("/change-password") public Response changePassword(@Valid @RequestBody UserDto userDto) { diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java new file mode 100644 index 0000000..f15872e --- /dev/null +++ b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java @@ -0,0 +1,178 @@ +package ru.ulstu.user.controller; + +import com.sun.org.apache.xpath.internal.operations.Mod; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.access.annotation.Secured; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.configuration.Constants; +import ru.ulstu.core.model.response.PageableItems; +import ru.ulstu.core.model.response.Response; +import ru.ulstu.odin.controller.OdinController; +import ru.ulstu.odin.model.OdinMetadata; +import ru.ulstu.odin.model.OdinVoid; +import ru.ulstu.odin.service.OdinService; +import ru.ulstu.user.model.*; +import ru.ulstu.user.service.UserService; +import ru.ulstu.user.service.UserSessionService; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import javax.validation.Valid; + +import static ru.ulstu.user.controller.UserController.URL; + +@Controller +@RequestMapping(value = "/users") +public class UserControllerV2 extends OdinController { + public static final String URL = Constants.API_1_0 + "users"; + public static final String ROLES_URL = "/roles"; + public static final String ROLES_META_URL = ROLES_URL + OdinController.META_LIST_URL; + public static final String SESSIONS_URL = "/sessions"; + public static final String SESSIONS_META_URL = SESSIONS_URL + OdinController.META_LIST_URL; + public static final String REGISTER_URL = "/register"; + public static final String ACTIVATE_URL = "/activate"; + public static final String PASSWORD_RESET_REQUEST_URL = "/password-reset-request"; + public static final String PASSWORD_RESET_URL = "/password-reset"; + + private final Logger log = LoggerFactory.getLogger(UserController.class); + + private final UserService userService; + private final UserSessionService userSessionService; + private final OdinService odinRolesService; + private final OdinService odinSessionsService; + + public UserControllerV2(UserService userService, + UserSessionService userSessionService, + OdinService odinRolesService, + OdinService odinSessionsService) { + super(UserListDto.class, UserDto.class); + this.userService = userService; + this.userSessionService = userSessionService; + this.odinRolesService = odinRolesService; + this.odinSessionsService = odinSessionsService; + } + + @GetMapping("/profile") + public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + modelMap.addAttribute("userDto", new UserDto(userSessionService.getUserDtoBySessionId(sessionId))); + } + + @PostMapping("/profile") + public void updateUserProfile(ModelMap modelMap, HttpServletRequest request, UserDto userDto) { + HttpSession session = request.getSession(false); + final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); + User user = userSessionService.getUserDtoBySessionId(sessionId); + modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); + } + + @GetMapping(ROLES_URL) + @Secured(UserRoleConstants.ADMIN) + public Response> getUserRoles() { + log.debug("REST: UserController.getUserRoles()"); + return new Response<>(userService.getUserRoles()); + } + + @GetMapping(ROLES_META_URL) + @Secured(UserRoleConstants.ADMIN) + public Response getUserRolesMetaData() { + log.debug("REST: UserController.getUserRolesMetaData()"); + return new Response<>(odinRolesService.getListModel(UserRoleDto.class)); + } + + @GetMapping(SESSIONS_URL) + @Secured(UserRoleConstants.ADMIN) + public Response> getUserSessions(@RequestParam(value = "offset", defaultValue = "0") int offset, + @RequestParam(value = "count", defaultValue = "10") int count) { + log.debug("REST: UserController.getUserSessions()"); + return new Response<>(userSessionService.getSessions(offset, count)); + } + + @GetMapping(SESSIONS_META_URL) + @Secured(UserRoleConstants.ADMIN) + public Response getUserSessionsMetaData() { + log.debug("REST: UserController.getUserSessionsMetaData()"); + return new Response<>(odinSessionsService.getListModel(UserSessionListDto.class)); + } + + @GetMapping("") + @Secured(UserRoleConstants.ADMIN) + public Response> getAllUsers(@RequestParam(value = "offset", defaultValue = "0") int offset, + @RequestParam(value = "count", defaultValue = "10") int count) { + log.debug("REST: UserController.getAllUsers( {}, {} )", offset, count); + return new Response<>(userService.getAllUsers(offset, count)); + } + + @GetMapping("/{userId}") + @Secured(UserRoleConstants.ADMIN) + public Response getUser(@PathVariable Integer userId) { + log.debug("REST: UserController.getUser( {} )", userId); + return new Response<>(userService.getUserWithRolesById(userId)); + } + + + @PostMapping("") + @Secured(UserRoleConstants.ADMIN) + public Response createUser(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.createUser( {} )", userDto.getLogin()); + return new Response<>(userService.createUser(userDto)); + } + + @PutMapping("") + @Secured(UserRoleConstants.ADMIN) + public Response updateUser(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.updateUser( {} )", userDto.getLogin()); + return new Response<>(userService.updateUser(userDto)); + } + + @DeleteMapping("/{userId}") + @Secured(UserRoleConstants.ADMIN) + public Response deleteUser(@PathVariable Integer userId) { + log.debug("REST: UserController.deleteUser( {} )", userId); + return new Response<>(userService.deleteUser(userId)); + } + + @PostMapping(REGISTER_URL) + public Response registerUser(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.registerUser( {} )", userDto.getLogin()); + return new Response<>(userService.createUser(userDto)); + } + + @PostMapping(ACTIVATE_URL) + public Response activateUser(@RequestParam("key") String activationKey) { + log.debug("REST: UserController.activateUser( {} )", activationKey); + return new Response<>(userService.activateUser(activationKey)); + } + + // TODO: add page for user password change (user-profile) + @PostMapping("/change-password") + public Response changePassword(@Valid @RequestBody UserDto userDto) { + log.debug("REST: UserController.changePassword( {} )", userDto.getLogin()); + return new Response<>(userService.changeUserPassword(userDto)); + } + + @PostMapping(PASSWORD_RESET_REQUEST_URL) + public Response requestPasswordReset(@RequestParam("email") String email) { + log.debug("REST: UserController.requestPasswordReset( {} )", email); + return new Response<>(userService.requestUserPasswordReset(email)); + } + + @PostMapping(PASSWORD_RESET_URL) + public Response finishPasswordReset(@RequestParam("key") String key, + @RequestBody UserResetPasswordDto userResetPasswordDto) { + log.debug("REST: UserController.requestPasswordReset( {} )", key); + return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); + } +} diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index c98e4b7..202cf10 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -205,23 +205,10 @@ public class UserService implements UserDetailsService { return userMapper.userEntityToUserDto(user); } - public UserDto updateUserInformation(UserDto userDto) { - if (userDto.getId() == null) { - throw new EntityIdIsNullException(); - } - if (!Objects.equals( - Optional.ofNullable(getUserByEmail(userDto.getEmail())) - .map(BaseEntity::getId).orElse(userDto.getId()), - userDto.getId())) { - throw new UserEmailExistsException(userDto.getEmail()); - } - User user = userRepository.findOne(userDto.getId()); - if (user == null) { - throw new UserNotFoundException(userDto.getId().toString()); - } - user.setFirstName(userDto.getFirstName()); - user.setLastName(userDto.getLastName()); - user.setEmail(userDto.getEmail()); + public UserDto updateUserInformation(User user, UserDto updateUser) { + user.setFirstName(updateUser.getFirstName()); + user.setLastName(updateUser.getLastName()); + user.setEmail(updateUser.getEmail()); user = userRepository.save(user); log.debug("Updated Information for User: {}", user.getLogin()); return userMapper.userEntityToUserDto(user); diff --git a/src/main/java/ru/ulstu/user/service/UserSessionService.java b/src/main/java/ru/ulstu/user/service/UserSessionService.java index 0d985cd..03415de 100644 --- a/src/main/java/ru/ulstu/user/service/UserSessionService.java +++ b/src/main/java/ru/ulstu/user/service/UserSessionService.java @@ -54,4 +54,8 @@ public class UserSessionService { userSessionRepository.save(userSession); log.debug("User session {} closed", sessionId); } + + public User getUserDtoBySessionId(String sessionId) { + return userSessionRepository.findOneBySessionId(sessionId).getUser(); + } } diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 65e0be6..4f9ec12 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -61,8 +61,14 @@ - diff --git a/src/main/resources/templates/users/profile.html b/src/main/resources/templates/users/profile.html new file mode 100644 index 0000000..5468d1b --- /dev/null +++ b/src/main/resources/templates/users/profile.html @@ -0,0 +1,75 @@ + + + + + + + + +
+
+
+
+
+

Личный кабинет

+
+
+
+
+
+
+ +
+ + +

Incorrect firstName

+

+
+
+ + +

Incorrect lastName

+

+
+
+ + +

Incorrect email

+

+
+
+ + +

Incorrect login

+

+
+
+ +
+
+
+
+
+
+
+ + From 00c5d5f755732ec8b22922df44ec8aa7b5e4bbd7 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Wed, 15 May 2019 00:54:30 +0400 Subject: [PATCH 09/68] #104 test refactoring --- .../ulstu/conference/model/ConferenceDto.java | 2 - .../conference/service/ConferenceService.java | 41 ++++--- .../service}/ConferenceServiceTest.java | 109 ++++++++++++++---- 3 files changed, 109 insertions(+), 43 deletions(-) rename src/test/java/{conference/module_test => ru/ulstu/conference/service}/ConferenceServiceTest.java (64%) diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index 76dbb90..606ddae 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -234,8 +234,6 @@ public class ConferenceDto { Objects.equals(title, that.title) && Objects.equals(description, that.description) && Objects.equals(url, that.url) && - Objects.equals(beginDate, that.beginDate) && - Objects.equals(endDate, that.endDate) && Objects.equals(deadlines, that.deadlines) && Objects.equals(removedDeadlineIds, that.removedDeadlineIds) && Objects.equals(userIds, that.userIds) && diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index cdb33ef..c69d273 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -64,7 +64,7 @@ public class ConferenceService { } public ConferenceDto getExistConferenceById(Integer id) { - ConferenceDto conferenceDto = findOneDto(id); + ConferenceDto conferenceDto = new ConferenceDto(conferenceRepository.findOne(id)); conferenceDto.setNotSelectedPapers(getNotSelectPapers(conferenceDto.getPaperIds())); conferenceDto.setDisabledTakePart(isCurrentUserParticipant(conferenceDto.getUsers())); return conferenceDto; @@ -72,7 +72,7 @@ public class ConferenceService { public ConferenceDto getNewConference() { ConferenceDto conferenceDto = new ConferenceDto(); - conferenceDto.setNotSelectedPapers(getNotSelectPapers(new ArrayList())); + conferenceDto.setNotSelectedPapers(getNotSelectPapers(new ArrayList<>())); return conferenceDto; } @@ -87,10 +87,6 @@ public class ConferenceService { return conferences; } - public ConferenceDto findOneDto(Integer id) { - return new ConferenceDto(conferenceRepository.findOne(id)); - } - public void save(ConferenceDto conferenceDto) throws IOException { if (isEmpty(conferenceDto.getId())) { create(conferenceDto); @@ -127,44 +123,51 @@ public class ConferenceService { } @Transactional - public void delete(Integer conferenceId) { + public boolean delete(Integer conferenceId) { if (conferenceRepository.exists(conferenceId)) { conferenceRepository.delete(conferenceId); + return true; } + return false; } - public void addDeadline(ConferenceDto conferenceDto) { + public ConferenceDto addDeadline(ConferenceDto conferenceDto) { conferenceDto.getDeadlines().add(new Deadline()); + return conferenceDto; } - public void removeDeadline(ConferenceDto conferenceDto, Integer deadlineIndex) throws IOException { + public ConferenceDto removeDeadline(ConferenceDto conferenceDto, Integer deadlineIndex) throws IOException { if (conferenceDto.getDeadlines().get(deadlineIndex).getId() != null) { conferenceDto.getRemovedDeadlineIds().add(conferenceDto.getDeadlines().get(deadlineIndex).getId()); } conferenceDto.getDeadlines().remove((int) deadlineIndex); + return conferenceDto; } - public void addPaper(ConferenceDto conferenceDto) { + public ConferenceDto addPaper(ConferenceDto conferenceDto) { Paper paper = new Paper(); paper.setTitle(userService.getCurrentUser().getLastName() + "_" + conferenceDto.getTitle() + "_" + (new Date()).getTime()); paper.setStatus(Paper.PaperStatus.DRAFT); conferenceDto.getPapers().add(paper); + return conferenceDto; } - public void removePaper(ConferenceDto conferenceDto, Integer paperIndex) throws IOException { + public ConferenceDto removePaper(ConferenceDto conferenceDto, Integer paperIndex) throws IOException { Paper removedPaper = conferenceDto.getPapers().remove((int) paperIndex); if (removedPaper.getId() != null) { conferenceDto.getNotSelectedPapers().add(removedPaper); } + return conferenceDto; } - public void takePart(ConferenceDto conferenceDto) throws IOException { + public ConferenceDto takePart(ConferenceDto conferenceDto) throws IOException { conferenceDto.getUsers().add(new ConferenceUser(userService.getCurrentUser())); conferenceDto.setDisabledTakePart(true); + return conferenceDto; } - public List getNotSelectPapers(List paperIds) { + private List getNotSelectPapers(List paperIds) { return paperService.findAllNotSelect(paperIds); } @@ -180,7 +183,7 @@ public class ConferenceService { return Arrays.asList(ConferenceUser.Deposit.values()); } - public Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException { + private Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException { conference.setTitle(conferenceDto.getTitle()); conference.setDescription(conferenceDto.getDescription()); conference.setUrl(conferenceDto.getUrl()); @@ -198,7 +201,7 @@ public class ConferenceService { } - public boolean isCurrentUserParticipant(List conferenceUsers) { + private boolean isCurrentUserParticipant(List conferenceUsers) { return conferenceUsers.stream().anyMatch(participant -> participant.getUser().equals(userService.getCurrentUser())); } @@ -213,7 +216,7 @@ public class ConferenceService { return convert(findAllActive(), ConferenceDto::new); } - public List findAllActive() { + private List findAllActive() { return conferenceRepository.findAllActive(new Date()); } @@ -227,7 +230,7 @@ public class ConferenceService { conferenceRepository.updatePingConference(conferenceDto.getId()); } - public Conference findOne(Integer conferenceId) { + private Conference findOne(Integer conferenceId) { return conferenceRepository.findOne(conferenceId); } @@ -253,7 +256,7 @@ public class ConferenceService { modelMap.addAttribute("offshoreSales", offshoreSales); } - public void sendNotificationAfterUpdateDeadlines(Conference conference, List oldDeadlines) { + private void sendNotificationAfterUpdateDeadlines(Conference conference, List oldDeadlines) { if (oldDeadlines.size() != conference.getDeadlines().size()) { conferenceNotificationService.updateDeadlineNotification(conference); return; @@ -267,7 +270,7 @@ public class ConferenceService { } } - public Deadline copyDeadline(Deadline oldDeadline) { + private Deadline copyDeadline(Deadline oldDeadline) { Deadline newDeadline = new Deadline(oldDeadline.getDate(), oldDeadline.getDescription()); newDeadline.setId(oldDeadline.getId()); return newDeadline; diff --git a/src/test/java/conference/module_test/ConferenceServiceTest.java b/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java similarity index 64% rename from src/test/java/conference/module_test/ConferenceServiceTest.java rename to src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java index 200b6f1..9fe8301 100644 --- a/src/test/java/conference/module_test/ConferenceServiceTest.java +++ b/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java @@ -1,4 +1,4 @@ -package conference.module_test; +package ru.ulstu.conference.service; import org.junit.Before; import org.junit.Test; @@ -11,9 +11,6 @@ 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.ConferenceNotificationService; -import ru.ulstu.conference.service.ConferenceService; -import ru.ulstu.conference.service.ConferenceUserService; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.paper.model.Paper; @@ -24,11 +21,12 @@ import ru.ulstu.user.service.UserService; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -126,28 +124,82 @@ public class ConferenceServiceTest { } @Test - public void testFindAllConferences() { + public void getExistConferenceById() { + when(conferenceRepository.findOne(1)).thenReturn(conferenceWithId); + when(paperService.findAllNotSelect(new ArrayList<>())).thenReturn(papers); + when(userService.getCurrentUser()).thenReturn(user); + conferenceDto.setDisabledTakePart(true); + assertEquals(conferenceDto, conferenceService.getExistConferenceById(1)); + } + + @Test + public void getNewConference() { + ConferenceDto newConferenceDto = new ConferenceDto(); + newConferenceDto.setNotSelectedPapers(papers); + when(paperService.findAllNotSelect(new ArrayList<>())).thenReturn(papers); + assertEquals(newConferenceDto, conferenceService.getNewConference()); + } + + @Test + public void findAll() { when(conferenceRepository.findAll(new Sort(Sort.Direction.DESC, "beginDate"))).thenReturn(conferences); assertEquals(conferences, conferenceService.findAll()); } @Test - public void testCopyFromDto() throws IOException { + 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); - assertEquals(conferenceWithoutId, conferenceService.copyFromDto(new Conference(), conferenceDto)); + when(conferenceRepository.save(new Conference())).thenReturn(conferenceWithId); + assertEquals(conferenceWithId.getId(), conferenceService.create(conferenceDto)); } @Test - public void testIsCurrentUserParticipant() throws IOException { + 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.create(conferenceDto)); + } + + @Test + public void delete() { + when(conferenceRepository.exists(1)).thenReturn(true); + assertTrue(conferenceService.delete(1)); + } + + @Test + public void addDeadline() { + ConferenceDto newConferenceDto = conferenceService.addDeadline(conferenceDto); + assertEquals(conferenceDto, newConferenceDto); + } + + @Test + public void removeDeadline() throws IOException { + ConferenceDto newConferenceDto = conferenceService.removeDeadline(conferenceDto, 0); + assertEquals(conferenceDto, newConferenceDto); + } + + @Test + public void addPaper() { when(userService.getCurrentUser()).thenReturn(user); - assertEquals(true, conferenceService.isCurrentUserParticipant(conferenceUsers)); + ConferenceDto newConferenceDto = conferenceService.addPaper(conferenceDto); + assertEquals(conferenceDto, newConferenceDto); } @Test - public void testTakePart() throws IOException { + public void removePaper() throws IOException { + ConferenceDto newConferenceDto = conferenceService.removePaper(conferenceDto, 0); + assertEquals(conferenceDto, newConferenceDto); + } + + @Test + public void takePart() throws IOException { when(userService.getCurrentUser()).thenReturn(user); ConferenceDto newConferenceDto = new ConferenceDto(); conferenceService.takePart(newConferenceDto); @@ -155,18 +207,31 @@ public class ConferenceServiceTest { } @Test - public void testFindOneDto() throws IOException { - when(conferenceRepository.findOne(1)).thenReturn(conferenceWithId); - assertEquals(conferenceDto, conferenceService.findOneDto(1)); + public void getAllUsers() { + List users = Collections.singletonList(user); + when(userService.findAll()).thenReturn(users); + assertEquals(users, conferenceService.getAllUsers()); + } + + + @Test + public void filter() { } @Test - public void testCreate() 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(conferenceRepository.save(conferenceWithoutId)).thenReturn(conferenceWithoutId); - assertNull(conferenceService.create(conferenceDto)); + public void isAttachedToConference() { } -} + + @Test + public void ping() { + } + + + @Test + public void checkEmptyFieldsOfDeadline() { + } + + @Test + public void filterEmptyDeadlines() { + } +} \ No newline at end of file From 7952787229fa31ec706ce6491ab5db8e5f72307f Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Wed, 15 May 2019 01:14:02 +0400 Subject: [PATCH 10/68] #104 add new tests, fixes --- .../ru/ulstu/conference/model/Conference.java | 4 +- .../conference/service/ConferenceService.java | 4 +- .../service/ConferenceServiceTest.java | 40 ++++++++++--------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index 7e18ac0..4df1a1f 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -42,12 +42,12 @@ public class Conference extends BaseEntity { @Column(name = "begin_date") @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(pattern = "yyyy-MM-dd") - private Date beginDate; + private Date beginDate = new Date(); @Column(name = "end_date") @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(pattern = "yyyy-MM-dd") - private Date endDate; + private Date endDate = new Date(); @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "conference_id", unique = true) diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index c69d273..550c884 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -225,9 +225,9 @@ public class ConferenceService { } @Transactional - public void ping(ConferenceDto conferenceDto) throws IOException { + public int ping(ConferenceDto conferenceDto) throws IOException { pingService.addPing(findOne(conferenceDto.getId())); - conferenceRepository.updatePingConference(conferenceDto.getId()); + return conferenceRepository.updatePingConference(conferenceDto.getId()); } private Conference findOne(Integer conferenceId) { diff --git a/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java b/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java index 9fe8301..af583f9 100644 --- a/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java +++ b/src/test/java/ru/ulstu/conference/service/ConferenceServiceTest.java @@ -9,12 +9,14 @@ 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.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.repository.ConferenceRepository; 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.ping.service.PingService; import ru.ulstu.timeline.service.EventService; import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; @@ -47,11 +49,14 @@ public class ConferenceServiceTest { @Mock UserService userService; + @Mock + EventService eventService; + @Mock ConferenceNotificationService conferenceNotificationService; @Mock - EventService eventService; + PingService pingService; @InjectMocks ConferenceService conferenceService; @@ -62,12 +67,9 @@ public class ConferenceServiceTest { private List conferenceUsers; private Conference conferenceWithId; - private Conference conferenceWithoutId; - private ConferenceUser conferenceUser; private Paper paperWithId; private Paper paperWithoutId; - private Deadline deadline; private ConferenceDto conferenceDto; private User user; @@ -76,7 +78,7 @@ public class ConferenceServiceTest { public void setUp() throws Exception { conferences = new ArrayList<>(); conferenceWithId = new Conference(); - conferenceWithoutId = new Conference(); + Conference conferenceWithoutId = new Conference(); conferenceWithId.setId(1); conferenceWithId.setTitle("Название"); @@ -97,11 +99,11 @@ public class ConferenceServiceTest { papers.add(paperWithoutId); deadlines = new ArrayList<>(); - deadline = new Deadline(new Date(), "desc"); + Deadline deadline = new Deadline(new Date(), "desc"); deadline.setId(1); deadlines.add(deadline); - conferenceUser = new ConferenceUser(); + ConferenceUser conferenceUser = new ConferenceUser(); conferenceUser.setDeposit(ConferenceUser.Deposit.ARTICLE); conferenceUser.setParticipation(ConferenceUser.Participation.INTRAMURAL); user = new User(); @@ -164,7 +166,7 @@ public class ConferenceServiceTest { 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.create(conferenceDto)); + assertEquals(conferenceWithId.getId(), conferenceService.update(conferenceDto)); } @Test @@ -216,22 +218,24 @@ public class ConferenceServiceTest { @Test public void filter() { + ConferenceFilterDto conferenceFilterDto = new ConferenceFilterDto(); + conferenceFilterDto.setFilterUserId(1); + conferenceFilterDto.setYear(2019); + when(userService.findById(1)).thenReturn(user); + when(conferenceRepository.findByUserAndYear(user, 2019)).thenReturn(conferences); + assertEquals(Collections.singletonList(conferenceDto), conferenceService.filter(conferenceFilterDto)); } @Test public void isAttachedToConference() { + when(conferenceRepository.isPaperAttached(1)).thenReturn(true); + assertTrue(conferenceService.isAttachedToConference(1)); } @Test - public void ping() { - } - - - @Test - public void checkEmptyFieldsOfDeadline() { - } - - @Test - public void filterEmptyDeadlines() { + public void ping() throws IOException { + when(conferenceRepository.findOne(1)).thenReturn(conferenceWithId); + when(conferenceRepository.updatePingConference(1)).thenReturn(1); + assertEquals(1, conferenceService.ping(conferenceDto)); } } \ No newline at end of file From 11ef644e5e8e5c2b157ee937341f4a4aa8c4ef04 Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 15 May 2019 02:48:44 +0400 Subject: [PATCH 11/68] 88 inviting user base --- .../ru/ulstu/configuration/Constants.java | 3 + .../user/controller/UserControllerV2.java | 127 +----------------- .../ru/ulstu/user/service/MailService.java | 14 ++ .../ru/ulstu/user/service/UserService.java | 32 ++++- .../user/service/UserSessionService.java | 2 +- .../java/ru/ulstu/user/util/UserUtils.java | 5 + .../mail_templates/userInviteEmail.html | 21 +++ src/main/resources/templates/default.html | 7 +- .../templates/users/inviteModal.html | 32 +++++ .../resources/templates/users/profile.html | 9 -- 10 files changed, 114 insertions(+), 138 deletions(-) create mode 100644 src/main/resources/mail_templates/userInviteEmail.html create mode 100644 src/main/resources/templates/users/inviteModal.html diff --git a/src/main/java/ru/ulstu/configuration/Constants.java b/src/main/java/ru/ulstu/configuration/Constants.java index 0a2268a..51806b6 100644 --- a/src/main/java/ru/ulstu/configuration/Constants.java +++ b/src/main/java/ru/ulstu/configuration/Constants.java @@ -5,7 +5,10 @@ public class Constants { public static final String MAIL_ACTIVATE = "Account activation"; public static final String MAIL_RESET = "Password reset"; + public static final String MAIL_INVITE = "Account registration"; + public static final int MIN_PASSWORD_LENGTH = 6; + public static final int MAX_PASSWORD_LENGTH = 32; public static final String LOGIN_REGEX = "^[_'.@A-Za-z0-9-]*$"; diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java index f15872e..3447a49 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java @@ -1,36 +1,21 @@ package ru.ulstu.user.controller; -import com.sun.org.apache.xpath.internal.operations.Mod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; -import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; import ru.ulstu.configuration.Constants; -import ru.ulstu.core.model.response.PageableItems; -import ru.ulstu.core.model.response.Response; import ru.ulstu.odin.controller.OdinController; -import ru.ulstu.odin.model.OdinMetadata; -import ru.ulstu.odin.model.OdinVoid; -import ru.ulstu.odin.service.OdinService; import ru.ulstu.user.model.*; import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserSessionService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import javax.validation.Valid; - -import static ru.ulstu.user.controller.UserController.URL; @Controller @RequestMapping(value = "/users") @@ -49,130 +34,32 @@ public class UserControllerV2 extends OdinController { private final UserService userService; private final UserSessionService userSessionService; - private final OdinService odinRolesService; - private final OdinService odinSessionsService; public UserControllerV2(UserService userService, - UserSessionService userSessionService, - OdinService odinRolesService, - OdinService odinSessionsService) { + UserSessionService userSessionService) { super(UserListDto.class, UserDto.class); this.userService = userService; this.userSessionService = userSessionService; - this.odinRolesService = odinRolesService; - this.odinSessionsService = odinSessionsService; } @GetMapping("/profile") public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { HttpSession session = request.getSession(false); final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); - modelMap.addAttribute("userDto", new UserDto(userSessionService.getUserDtoBySessionId(sessionId))); + modelMap.addAttribute("userDto", new UserDto(userSessionService.getUserBySessionId(sessionId))); } @PostMapping("/profile") public void updateUserProfile(ModelMap modelMap, HttpServletRequest request, UserDto userDto) { HttpSession session = request.getSession(false); final String sessionId = session.getAttribute(Constants.SESSION_ID_ATTR).toString(); - User user = userSessionService.getUserDtoBySessionId(sessionId); + User user = userSessionService.getUserBySessionId(sessionId); modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); } - @GetMapping(ROLES_URL) - @Secured(UserRoleConstants.ADMIN) - public Response> getUserRoles() { - log.debug("REST: UserController.getUserRoles()"); - return new Response<>(userService.getUserRoles()); - } - - @GetMapping(ROLES_META_URL) - @Secured(UserRoleConstants.ADMIN) - public Response getUserRolesMetaData() { - log.debug("REST: UserController.getUserRolesMetaData()"); - return new Response<>(odinRolesService.getListModel(UserRoleDto.class)); - } - - @GetMapping(SESSIONS_URL) - @Secured(UserRoleConstants.ADMIN) - public Response> getUserSessions(@RequestParam(value = "offset", defaultValue = "0") int offset, - @RequestParam(value = "count", defaultValue = "10") int count) { - log.debug("REST: UserController.getUserSessions()"); - return new Response<>(userSessionService.getSessions(offset, count)); - } - - @GetMapping(SESSIONS_META_URL) - @Secured(UserRoleConstants.ADMIN) - public Response getUserSessionsMetaData() { - log.debug("REST: UserController.getUserSessionsMetaData()"); - return new Response<>(odinSessionsService.getListModel(UserSessionListDto.class)); - } - - @GetMapping("") - @Secured(UserRoleConstants.ADMIN) - public Response> getAllUsers(@RequestParam(value = "offset", defaultValue = "0") int offset, - @RequestParam(value = "count", defaultValue = "10") int count) { - log.debug("REST: UserController.getAllUsers( {}, {} )", offset, count); - return new Response<>(userService.getAllUsers(offset, count)); - } - - @GetMapping("/{userId}") - @Secured(UserRoleConstants.ADMIN) - public Response getUser(@PathVariable Integer userId) { - log.debug("REST: UserController.getUser( {} )", userId); - return new Response<>(userService.getUserWithRolesById(userId)); - } - - - @PostMapping("") - @Secured(UserRoleConstants.ADMIN) - public Response createUser(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.createUser( {} )", userDto.getLogin()); - return new Response<>(userService.createUser(userDto)); - } - - @PutMapping("") - @Secured(UserRoleConstants.ADMIN) - public Response updateUser(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.updateUser( {} )", userDto.getLogin()); - return new Response<>(userService.updateUser(userDto)); - } - - @DeleteMapping("/{userId}") - @Secured(UserRoleConstants.ADMIN) - public Response deleteUser(@PathVariable Integer userId) { - log.debug("REST: UserController.deleteUser( {} )", userId); - return new Response<>(userService.deleteUser(userId)); - } - - @PostMapping(REGISTER_URL) - public Response registerUser(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.registerUser( {} )", userDto.getLogin()); - return new Response<>(userService.createUser(userDto)); - } - - @PostMapping(ACTIVATE_URL) - public Response activateUser(@RequestParam("key") String activationKey) { - log.debug("REST: UserController.activateUser( {} )", activationKey); - return new Response<>(userService.activateUser(activationKey)); - } - - // TODO: add page for user password change (user-profile) - @PostMapping("/change-password") - public Response changePassword(@Valid @RequestBody UserDto userDto) { - log.debug("REST: UserController.changePassword( {} )", userDto.getLogin()); - return new Response<>(userService.changeUserPassword(userDto)); - } - - @PostMapping(PASSWORD_RESET_REQUEST_URL) - public Response requestPasswordReset(@RequestParam("email") String email) { - log.debug("REST: UserController.requestPasswordReset( {} )", email); - return new Response<>(userService.requestUserPasswordReset(email)); - } - - @PostMapping(PASSWORD_RESET_URL) - public Response finishPasswordReset(@RequestParam("key") String key, - @RequestBody UserResetPasswordDto userResetPasswordDto) { - log.debug("REST: UserController.requestPasswordReset( {} )", key); - return new Response<>(userService.completeUserPasswordReset(key, userResetPasswordDto)); + @PostMapping("/invite" ) + public String inviteUser(@RequestParam(value = "email") String email){ + userService.inviteUser(email); + return "redirect:/"; } } diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index da1da6d..da8e098 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -78,6 +78,15 @@ public class MailService { sendEmail(user.getEmail(), subject, content); } + @Async + public void sendEmailFromTemplate(Map variables, String templateName, String subject, String email) { + Context context = new Context(); + variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue())); + context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); + String content = templateEngine.process(templateName, context); + sendEmail(email, subject, content); + } + @Async public void sendActivationEmail(User user) { sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE); @@ -87,4 +96,9 @@ public class MailService { public void sendPasswordResetMail(User user) { sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); } + + @Async + public void sendInviteMail(Map variables, String email) { + sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email); + } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 202cf10..ef11796 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -1,5 +1,6 @@ package ru.ulstu.user.service; +import com.google.common.collect.ImmutableMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import ru.ulstu.configuration.ApplicationProperties; +import ru.ulstu.configuration.Constants; import ru.ulstu.core.error.EntityIdIsNullException; import ru.ulstu.core.jpa.OffsetablePageRequest; import ru.ulstu.core.model.BaseEntity; @@ -36,13 +38,7 @@ import ru.ulstu.user.repository.UserRepository; import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.util.UserUtils; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; @Service @@ -55,6 +51,7 @@ public class UserService implements UserDetailsService { private final UserMapper userMapper; private final MailService mailService; private final ApplicationProperties applicationProperties; + private final Random random; public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, @@ -68,6 +65,7 @@ public class UserService implements UserDetailsService { this.userMapper = userMapper; this.mailService = mailService; this.applicationProperties = applicationProperties; + this.random = new Random(); } private User getUserByEmail(String email) { @@ -315,4 +313,24 @@ public class UserService implements UserDetailsService { public List filterByAgeAndDegree(boolean hasDegree, boolean hasAge) { return userRepository.filterByAgeAndDegree(hasDegree, hasAge); } + + public void inviteUser(String email) { + if (userRepository.findOneByEmailIgnoreCase(email) != null) { + throw new UserEmailExistsException(email); + } + + String password = UserUtils.generatePassword(); + + User user = new User(); + user.setPassword(passwordEncoder.encode(password)); + user.setLogin(email); + user.setEmail(email); + user.setFirstName(""); + user.setLastName(""); + user.setActivated(true); + userRepository.save(user); + + Map variables = ImmutableMap.of("password", password, "email", email); + mailService.sendInviteMail(variables, email); + } } diff --git a/src/main/java/ru/ulstu/user/service/UserSessionService.java b/src/main/java/ru/ulstu/user/service/UserSessionService.java index 03415de..ae289d0 100644 --- a/src/main/java/ru/ulstu/user/service/UserSessionService.java +++ b/src/main/java/ru/ulstu/user/service/UserSessionService.java @@ -55,7 +55,7 @@ public class UserSessionService { log.debug("User session {} closed", sessionId); } - public User getUserDtoBySessionId(String sessionId) { + public User getUserBySessionId(String sessionId) { return userSessionRepository.findOneBySessionId(sessionId).getUser(); } } diff --git a/src/main/java/ru/ulstu/user/util/UserUtils.java b/src/main/java/ru/ulstu/user/util/UserUtils.java index de585a5..ec58dd9 100644 --- a/src/main/java/ru/ulstu/user/util/UserUtils.java +++ b/src/main/java/ru/ulstu/user/util/UserUtils.java @@ -5,6 +5,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; +import ru.ulstu.configuration.Constants; public class UserUtils { private static final int DEF_COUNT = 20; @@ -32,4 +33,8 @@ public class UserUtils { } return null; } + + public static String generatePassword() { + return RandomStringUtils.randomAscii(Constants.MIN_PASSWORD_LENGTH, Constants.MAX_PASSWORD_LENGTH); + } } diff --git a/src/main/resources/mail_templates/userInviteEmail.html b/src/main/resources/mail_templates/userInviteEmail.html new file mode 100644 index 0000000..180f0c5 --- /dev/null +++ b/src/main/resources/mail_templates/userInviteEmail.html @@ -0,0 +1,21 @@ + + + + Account activation + + + + +

+ Аккаунт в системе NG-Tracker был создан. + Данные для входа: + Логин - + Пароль - +

+

+ Regards, +
+ Balance Team. +

+ + diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 4f9ec12..0c92865 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -62,18 +62,21 @@ КИАС РФФИ +
-
- - -

Incorrect login

-

-
@@ -77,6 +78,7 @@
+

- - + + + + + + + + + + + + + + + + + +
From dc34c7bfb7944a04403cf0f3f2c67136471df979 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Thu, 16 May 2019 18:07:18 +0400 Subject: [PATCH 23/68] #104 fixed conference-row style --- src/main/resources/public/css/conference.css | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/resources/public/css/conference.css b/src/main/resources/public/css/conference.css index dc83057..60a866d 100644 --- a/src/main/resources/public/css/conference.css +++ b/src/main/resources/public/css/conference.css @@ -23,11 +23,17 @@ body { text-decoration: none; margin: 0; } - .conference-row .d-flex .text-decoration:nth-child(1) { margin-left: 5px; } +.conference-row .d-flex .text-decoration span.h6.float-left.m-2 { + max-width: 470px; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + .conference-row .d-flex .icon-delete { width: 29px; height: 29px; From 728fa4297278be50a17d55e2036d828cc940b27a Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Fri, 17 May 2019 02:01:26 +0400 Subject: [PATCH 24/68] 87 error handling --- ...ntrollerV2.java => UserMvcController.java} | 19 +++++++++++------ .../ru/ulstu/user/service/UserService.java | 5 ++++- src/main/resources/templates/error/error.html | 21 +++++++++++++++++++ .../resources/templates/users/profile.html | 9 ++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) rename src/main/java/ru/ulstu/user/controller/{UserControllerV2.java => UserMvcController.java} (77%) create mode 100644 src/main/resources/templates/error/error.html diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java similarity index 77% rename from src/main/java/ru/ulstu/user/controller/UserControllerV2.java rename to src/main/java/ru/ulstu/user/controller/UserMvcController.java index eee98a1..c35dd79 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -3,14 +3,15 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.configuration.Constants; import ru.ulstu.odin.controller.OdinController; -import ru.ulstu.user.error.UserSendingMailException; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserListDto; @@ -22,20 +23,26 @@ import javax.servlet.http.HttpSession; @Controller @RequestMapping(value = "/users") -public class UserControllerV2 extends OdinController { +public class UserMvcController extends OdinController { - private final Logger log = LoggerFactory.getLogger(UserControllerV2.class); + private final Logger log = LoggerFactory.getLogger(UserMvcController.class); private final UserService userService; private final UserSessionService userSessionService; - public UserControllerV2(UserService userService, - UserSessionService userSessionService) { + public UserMvcController(UserService userService, + UserSessionService userSessionService) { super(UserListDto.class, UserDto.class); this.userService = userService; this.userSessionService = userSessionService; } + @ExceptionHandler(Exception.class) + public String errorHandler(Model model, Exception exception) { + model.addAttribute("error", exception.getMessage()); + return "/error/error"; + } + @GetMapping("/profile") public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { HttpSession session = request.getSession(false); @@ -52,7 +59,7 @@ public class UserControllerV2 extends OdinController { } @PostMapping("/invite") - public String inviteUser(@RequestParam(value = "email") String email) throws UserSendingMailException { + public String inviteUser(@RequestParam(value = "email") String email, ModelMap modelMap) { userService.inviteUser(email); return "redirect:/"; } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 083b0f5..78f9d66 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -52,6 +52,8 @@ import java.util.stream.Collectors; @Service @Transactional public class UserService implements UserDetailsService { + private static final String INVITE_USER_EXCEPTION = "Во время отправки приглашения произошла ошибка"; + private final Logger log = LoggerFactory.getLogger(UserService.class); private final UserRepository userRepository; private final PasswordEncoder passwordEncoder; @@ -213,6 +215,7 @@ public class UserService implements UserDetailsService { user.setFirstName(updateUser.getFirstName()); user.setLastName(updateUser.getLastName()); user.setEmail(updateUser.getEmail()); + user.setLogin(updateUser.getLogin()); user = userRepository.save(user); log.debug("Updated Information for User: {}", user.getLogin()); return userMapper.userEntityToUserDto(user); @@ -340,7 +343,7 @@ public class UserService implements UserDetailsService { try { mailService.sendInviteMail(variables, email); } catch (MessagingException e) { - throw new UserSendingMailException(email); + throw new UserSendingMailException(INVITE_USER_EXCEPTION); } } } diff --git a/src/main/resources/templates/error/error.html b/src/main/resources/templates/error/error.html new file mode 100644 index 0000000..53c9054 --- /dev/null +++ b/src/main/resources/templates/error/error.html @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/users/profile.html b/src/main/resources/templates/users/profile.html index c97e539..93adfc7 100644 --- a/src/main/resources/templates/users/profile.html +++ b/src/main/resources/templates/users/profile.html @@ -49,6 +49,15 @@ class="alert alert-danger">Incorrect email

+
+ + +

Incorrect email

+

+