Merge branch '104-module-test-conf' into 'dev'

Resolve "Реализовать модульные тесты"

See merge request romanov73/ng-tracker!92
This commit is contained in:
Anton Romanov 2019-05-17 07:27:21 +00:00
commit 83aba158b6
9 changed files with 415 additions and 48 deletions

View File

@ -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)

View File

@ -14,6 +14,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;
@ -219,4 +220,33 @@ public class ConferenceDto extends NameContainer {
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(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);
}
}

View File

@ -16,6 +16,7 @@ import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.name.BaseService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.service.PaperService;
import ru.ulstu.ping.model.Ping;
import ru.ulstu.ping.service.PingService;
import ru.ulstu.timeline.service.EventService;
import ru.ulstu.user.model.User;
@ -64,7 +65,7 @@ public class ConferenceService extends BaseService {
}
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 +73,7 @@ public class ConferenceService extends BaseService {
public ConferenceDto getNewConference() {
ConferenceDto conferenceDto = new ConferenceDto();
conferenceDto.setNotSelectedPapers(getNotSelectPapers(new ArrayList<Integer>()));
conferenceDto.setNotSelectedPapers(getNotSelectPapers(new ArrayList<>()));
return conferenceDto;
}
@ -87,10 +88,6 @@ public class ConferenceService extends BaseService {
return conferences;
}
public ConferenceDto findOneDto(Integer id) {
return new ConferenceDto(conferenceRepository.findOne(id));
}
public boolean save(ConferenceDto conferenceDto, Errors errors) throws IOException {
conferenceDto.setName(conferenceDto.getTitle());
filterEmptyDeadlines(conferenceDto);
@ -114,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)
@ -137,50 +134,56 @@ public class ConferenceService extends BaseService {
conferenceNotificationService.updateConferencesDatesNotification(conference, oldBeginDate, oldEndDate);
}
conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove);
return conference.getId();
return conference;
}
@Transactional
public void delete(Integer conferenceId) {
public boolean delete(Integer conferenceId) {
if (conferenceRepository.exists(conferenceId)) {
eventService.removeConferencesEvent(conferenceRepository.findOne(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<Paper> getNotSelectPapers(List<Integer> paperIds) {
private List<Paper> getNotSelectPapers(List<Integer> paperIds) {
return paperService.findAllNotSelect(paperIds);
}
@ -214,7 +217,7 @@ public class ConferenceService extends BaseService {
}
public boolean isCurrentUserParticipant(List<ConferenceUser> conferenceUsers) {
private boolean isCurrentUserParticipant(List<ConferenceUser> conferenceUsers) {
return conferenceUsers.stream().anyMatch(participant -> participant.getUser().equals(userService.getCurrentUser()));
}
@ -229,7 +232,7 @@ public class ConferenceService extends BaseService {
return convert(findAllActive(), ConferenceDto::new);
}
public List<Conference> findAllActive() {
private List<Conference> findAllActive() {
return conferenceRepository.findAllActive(new Date());
}
@ -238,12 +241,13 @@ public class ConferenceService extends BaseService {
}
@Transactional
public void ping(ConferenceDto conferenceDto) throws IOException {
pingService.addPing(findOne(conferenceDto.getId()));
public Ping ping(ConferenceDto conferenceDto) throws IOException {
Ping ping = pingService.addPing(findOne(conferenceDto.getId()));
conferenceRepository.updatePingConference(conferenceDto.getId());
return ping;
}
public Conference findOne(Integer conferenceId) {
private Conference findOne(Integer conferenceId) {
return conferenceRepository.findOne(conferenceId);
}
@ -269,7 +273,7 @@ public class ConferenceService extends BaseService {
modelMap.addAttribute("offshoreSales", offshoreSales);
}
public void sendNotificationAfterUpdateDeadlines(Conference conference, List<Deadline> oldDeadlines) {
private void sendNotificationAfterUpdateDeadlines(Conference conference, List<Deadline> oldDeadlines) {
if (oldDeadlines.size() != conference.getDeadlines().size()) {
conferenceNotificationService.updateDeadlineNotification(conference);
return;
@ -283,7 +287,7 @@ public class ConferenceService extends BaseService {
}
}
public Deadline copyDeadline(Deadline oldDeadline) {
private Deadline copyDeadline(Deadline oldDeadline) {
Deadline newDeadline = new Deadline(oldDeadline.getDate(), oldDeadline.getDescription());
newDeadline.setId(oldDeadline.getId());
return newDeadline;

View File

@ -65,6 +65,11 @@ public class Deadline extends BaseEntity {
return false;
}
Deadline deadline = (Deadline) o;
if (getId() == null && deadline.getId() == null &&
description == null && deadline.description == null &&
date == null && deadline.date == null) {
return true;
}
return getId().equals(deadline.getId()) &&
description.equals(deadline.description) &&
date.equals(deadline.date);

View File

@ -29,6 +29,7 @@ import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -264,4 +265,36 @@ public class Paper extends BaseEntity implements UserContainer {
.findAny()
.isPresent();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
Paper paper = (Paper) o;
return Objects.equals(title, paper.title) &&
status == paper.status &&
type == paper.type &&
Objects.equals(deadlines, paper.deadlines) &&
Objects.equals(comment, paper.comment) &&
Objects.equals(url, paper.url) &&
Objects.equals(locked, paper.locked) &&
Objects.equals(events, paper.events) &&
Objects.equals(files, paper.files) &&
Objects.equals(authors, paper.authors) &&
Objects.equals(latexText, paper.latexText) &&
Objects.equals(conferences, paper.conferences) &&
Objects.equals(grants, paper.grants);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), title, status, type, createDate, updateDate, deadlines, comment, url, locked, events, files, authors, latexText, conferences, grants);
}
}

View File

@ -23,10 +23,10 @@ public class PingService {
}
@Transactional
public void addPing(Conference conference) throws IOException {
public Ping addPing(Conference conference) throws IOException {
Ping newPing = new Ping(new Date(), userService.getCurrentUser());
newPing.setConference(conference);
pingRepository.save(newPing);
return pingRepository.save(newPing);
}
public Integer countPingYesterday(Conference conference, Calendar calendar) {

View File

@ -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;

View File

@ -24,24 +24,24 @@
</div>
<hr/>
<!--chart example-->
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-item nav-link active" id="nav-main-tab" data-toggle="tab"
href="#nav-stat1" role="tab" aria-controls="nav-stat1" aria-selected="true">Стат1</a>
<a class="nav-item nav-link" id="nav-latex-tab" data-toggle="tab"
href="#nav-stat2" role="tab" aria-controls="nav-stat2" aria-selected="false">Стат2</a>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-stat1" role="tabpanel"
aria-labelledby="nav-main-tab">
<div id="salesByType" style="width:100%; height:400px;"></div>
</div>
<div class="tab-pane fade" id="nav-stat2" role="tabpanel"
aria-labelledby="nav-profile-tab">
<div id="salesByRegion" style="width:100%; height:400px;"></div>
</div>
</div>
<!--<nav>-->
<!--<div class="nav nav-tabs" id="nav-tab" role="tablist">-->
<!--<a class="nav-item nav-link active" id="nav-main-tab" data-toggle="tab"-->
<!--href="#nav-stat1" role="tab" aria-controls="nav-stat1" aria-selected="true">Стат1</a>-->
<!--<a class="nav-item nav-link" id="nav-latex-tab" data-toggle="tab"-->
<!--href="#nav-stat2" role="tab" aria-controls="nav-stat2" aria-selected="false">Стат2</a>-->
<!--</div>-->
<!--</nav>-->
<!--<div class="tab-content" id="nav-tabContent">-->
<!--<div class="tab-pane fade show active" id="nav-stat1" role="tabpanel"-->
<!--aria-labelledby="nav-main-tab">-->
<!--<div id="salesByType" style="width:100%; height:400px;"></div>-->
<!--</div>-->
<!--<div class="tab-pane fade" id="nav-stat2" role="tabpanel"-->
<!--aria-labelledby="nav-profile-tab">-->
<!--<div id="salesByRegion" style="width:100%; height:400px;"></div>-->
<!--</div>-->
<!--</div>-->
<!--chart example-->
</div>
</section>

View File

@ -0,0 +1,289 @@
package ru.ulstu.conference.service;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
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.model.Ping;
import ru.ulstu.ping.service.PingService;
import ru.ulstu.timeline.service.EventService;
import ru.ulstu.user.model.User;
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.assertTrue;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ConferenceServiceTest {
@Mock
ConferenceRepository conferenceRepository;
@Mock
DeadlineService deadlineService;
@Mock
ConferenceUserService conferenceUserService;
@Mock
PaperService paperService;
@Mock
UserService userService;
@Mock
EventService eventService;
@Mock
ConferenceNotificationService conferenceNotificationService;
@Mock
PingService pingService;
@InjectMocks
ConferenceService conferenceService;
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;
private List<Paper> papers;
private List<ConferenceUser> conferenceUsers;
private Conference conferenceWithId;
private Paper paperWithId;
private Paper paperWithoutId;
private ConferenceDto conferenceDto;
private User user;
private Deadline deadline;
@Before
public void setUp() throws Exception {
conferences = new ArrayList<>();
conferenceWithId = new Conference();
conferenceWithId.setId(ID);
conferenceWithId.setTitle(NAME);
conferenceWithId.setDescription(DESCRIPTION);
paperWithId = new Paper();
paperWithId.setId(1);
paperWithId.setTitle(NAME);
paperWithoutId = new Paper();
paperWithoutId.setTitle(NAME);
papers = new ArrayList<>();
papers.add(paperWithId);
papers.add(paperWithoutId);
deadlines = new ArrayList<>();
deadline = new Deadline(new Date(), DESCRIPTION);
deadline.setId(ID);
deadlines.add(deadline);
ConferenceUser conferenceUser = new ConferenceUser();
conferenceUser.setDeposit(ConferenceUser.Deposit.ARTICLE);
conferenceUser.setParticipation(ConferenceUser.Participation.INTRAMURAL);
user = new User();
user.setFirstName(NAME);
conferenceUser.setUser(user);
conferenceUsers = new ArrayList<>();
conferenceUsers.add(conferenceUser);
conferences.add(conferenceWithId);
conferenceDto = new ConferenceDto(conferenceWithId);
}
@Test
public void getExistConferenceById() {
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(!TRUE);
ConferenceDto result = conferenceService.getExistConferenceById(ID);
assertEquals(newConferenceDto.getId(), result.getId());
assertEquals(newConferenceDto.getNotSelectedPapers(), result.getNotSelectedPapers());
assertEquals(newConferenceDto.isDisabledTakePart(), result.isDisabledTakePart());
}
@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());
}
@Test
public void findAll() {
when(conferenceRepository.findAll(SORT)).thenReturn(conferences);
assertEquals(Collections.singletonList(conferenceWithId), conferenceService.findAll());
}
@Test
public void create() throws IOException {
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);
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
public void delete() {
when(conferenceRepository.exists(ID)).thenReturn(true);
when(conferenceRepository.findOne(ID)).thenReturn(conferenceWithId);
assertTrue(conferenceService.delete(ID));
}
@Test
public void addDeadline() {
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.getDeadlines().add(new Deadline());
conferenceDto.getDeadlines().clear();
assertEquals(newConferenceDto.getDeadlines().get(0), conferenceService.addDeadline(conferenceDto).getDeadlines().get(0));
}
@Test
public void removeDeadline() throws IOException {
ConferenceDto newConferenceDto = new ConferenceDto();
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();
ConferenceDto result = conferenceService.addPaper(conferenceDto);
result.getPapers().get(INDEX).setTitle(NAME); // приходится вручную назначать название, т.е. название зависит от даты
assertEquals(newConferenceDto.getPapers(), result.getPapers());
}
@Test
public void removePaper() throws IOException {
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.getNotSelectedPapers().add(paperWithId);
newConferenceDto.getPapers().add(paperWithoutId);
conferenceDto.getPapers().add(paperWithId);
conferenceDto.getPapers().add(paperWithoutId);
ConferenceDto result = conferenceService.removePaper(conferenceDto, INDEX);
assertEquals(newConferenceDto.getPapers(), result.getPapers());
assertEquals(newConferenceDto.getNotSelectedPapers(), result.getNotSelectedPapers());
}
@Test
public void takePart() throws IOException {
when(userService.getCurrentUser()).thenReturn(user);
ConferenceDto newConferenceDto = new ConferenceDto();
newConferenceDto.setUsers(conferenceUsers);
newConferenceDto.setDisabledTakePart(TRUE);
conferenceDto.getPapers().clear();
ConferenceDto result = conferenceService.takePart(conferenceDto);
assertEquals(newConferenceDto.getUsers(), result.getUsers());
assertEquals(newConferenceDto.isDisabledTakePart(), result.isDisabledTakePart());
}
@Test
public void getAllUsers() {
List<User> users = Collections.singletonList(user);
when(userService.findAll()).thenReturn(users);
assertEquals(users, conferenceService.getAllUsers());
}
@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);
assertEquals(Collections.singletonList(conferenceDto), conferenceService.filter(conferenceFilterDto));
}
@Test
public void isAttachedToConference() {
when(conferenceRepository.isPaperAttached(ID)).thenReturn(TRUE);
assertTrue(conferenceService.isAttachedToConference(ID));
}
@Test
public void ping() throws IOException {
Ping ping = new Ping();
when(conferenceRepository.findOne(ID)).thenReturn(conferenceWithId);
when(pingService.addPing(conferenceWithId)).thenReturn(ping);
when(conferenceRepository.updatePingConference(ID)).thenReturn(INDEX);
assertEquals(ping, conferenceService.ping(conferenceDto));
}
}