WIP: Try vue #244
@ -12,4 +12,6 @@ public interface ActivityRepository<T> {
|
||||
T getById(Integer id);
|
||||
|
||||
Page<T> findAll(Pageable pageable);
|
||||
|
||||
void deleteById(Integer id);
|
||||
}
|
||||
|
@ -74,10 +74,14 @@ public abstract class ActivityService<L extends ActivityListDto, T extends Abstr
|
||||
return newEntity;
|
||||
}
|
||||
|
||||
public abstract boolean delete(Integer id);
|
||||
|
||||
public abstract T findById(Integer id);
|
||||
public boolean delete(Integer id) {
|
||||
activityRepository.deleteById(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public T findById(Integer id) {
|
||||
return activityRepository.getById(id);
|
||||
}
|
||||
|
||||
public PageableItems<T> findAll(int offset, int count) {
|
||||
final Page<T> page = activityRepository.findAll(new OffsetablePageRequest(offset, count));
|
||||
|
@ -69,15 +69,6 @@ public class ConferenceService extends ActivityService<ConferenceListDto, Confer
|
||||
return new ConferenceDto(update(conference));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean delete(Integer conferenceId) {
|
||||
if (conferenceRepository.existsById(conferenceId)) {
|
||||
conferenceRepository.deleteById(conferenceId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConferenceListDto getActivityListDto(Conference entity) {
|
||||
return new ConferenceListDto(entity);
|
||||
|
@ -5,7 +5,6 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.Errors;
|
||||
import ru.ulstu.activity.common.service.ActivityService;
|
||||
import ru.ulstu.activity.deadline.service.DeadlineService;
|
||||
import ru.ulstu.activity.file.service.FileService;
|
||||
@ -13,8 +12,6 @@ import ru.ulstu.activity.grant.model.Grant;
|
||||
import ru.ulstu.activity.grant.model.GrantDashboardDto;
|
||||
import ru.ulstu.activity.grant.model.GrantDto;
|
||||
import ru.ulstu.activity.grant.model.GrantListDto;
|
||||
import ru.ulstu.activity.paper.model.Paper;
|
||||
import ru.ulstu.activity.paper.model.PaperDto;
|
||||
import ru.ulstu.activity.paper.service.PaperService;
|
||||
import ru.ulstu.activity.ping.service.PingService;
|
||||
import ru.ulstu.activity.project.service.ProjectService;
|
||||
@ -23,7 +20,6 @@ import ru.ulstu.activity.timeline.service.EventService;
|
||||
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
||||
import ru.ulstu.core.model.response.PageableItems;
|
||||
import ru.ulstu.core.util.DateUtils;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -74,10 +70,6 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
|
||||
this.pingService = pingService;
|
||||
}
|
||||
|
||||
public GrantDto getExistGrantById(Integer id) {
|
||||
return new GrantDto(findById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageableItems<Grant> findAll(int offset, int count) {
|
||||
final Page<Grant> page = grantRepository.findAll(new OffsetablePageRequest(offset, count));
|
||||
@ -128,16 +120,6 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
|
||||
return eventService.findByGrant(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean delete(Integer grantId) {
|
||||
Grant grant = findById(grantId);
|
||||
if (grant != null) {
|
||||
grantRepository.delete(grant);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Grant.GrantStatus> getGrantStatuses() {
|
||||
return Arrays.asList(Grant.GrantStatus.values());
|
||||
}
|
||||
@ -158,40 +140,12 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
|
||||
}
|
||||
}
|
||||
|
||||
private void checkEmptyLeader(GrantDto grantDto, Errors errors) {
|
||||
if (grantDto.getLeaderId().equals(-1)) {
|
||||
errors.rejectValue("leaderId", "errorCode", "Укажите руководителя");
|
||||
}
|
||||
}
|
||||
|
||||
private void checkEmptyDeadlines(GrantDto grantDto, Errors errors) {
|
||||
if (grantDto.getDeadlines().isEmpty()) {
|
||||
errors.rejectValue("deadlines", "errorCode", "Не может быть пусто");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkSameDeadline(GrantDto grantDto, Integer id) {
|
||||
Date date = DateUtils.clearTime(grantDto.getDeadlines().get(0).getDate()); //дата с сайта киас
|
||||
Date foundGrantDate = DateUtils.clearTime(deadlineService.findByGrantIdAndDate(id, date));
|
||||
return foundGrantDate != null && foundGrantDate.compareTo(date) == 0;
|
||||
}
|
||||
|
||||
public List<PaperDto> getGrantPapers(List<Integer> paperIds) {
|
||||
return paperService.findAllSelect(paperIds);
|
||||
}
|
||||
|
||||
public List<PaperDto> getAllUncompletedPapers() {
|
||||
return paperService.findAllNotCompleted();
|
||||
}
|
||||
|
||||
private List<User> getCompletedPapersAuthors(Paper.PaperType type) {
|
||||
List<Paper> papers = paperService.findAllCompletedByType(type);
|
||||
return papers.stream()
|
||||
.filter(paper -> paper.getAuthors() != null)
|
||||
.flatMap(paper -> paper.getAuthors().stream())
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void createFromKias() throws IOException, ParseException {
|
||||
for (GrantDto grantDto : kiasService.getNewGrantsDto()) {
|
||||
@ -210,7 +164,6 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
|
||||
public PageableItems<Grant> findAllActive(int offset, int count) {
|
||||
Page<Grant> activeGrantsPage = grantRepository.findAllActive(new OffsetablePageRequest(offset, count));
|
||||
return new PageableItems<>(activeGrantsPage.getTotalElements(), activeGrantsPage.getContent());
|
||||
|
||||
}
|
||||
|
||||
public GrantDto findGrantById(Integer id) {
|
||||
|
@ -231,14 +231,6 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
|
||||
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + paperId + " not found"));
|
||||
}
|
||||
|
||||
public List<Paper> findAllNotSelect(List<Integer> paperIds) {
|
||||
if (!paperIds.isEmpty()) {
|
||||
return sortPapers(paperRepository.findByIdNotInAndConferencesIsNullAndStatusNot(paperIds, COMPLETED));
|
||||
} else {
|
||||
return sortPapers(paperRepository.findByConferencesIsNullAndStatusNot(COMPLETED));
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaperDto> findAllNotCompleted() {
|
||||
return convert(paperRepository.findByStatusNot(COMPLETED), PaperDto::new);
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package ru.ulstu.activity.project.service;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.activity.common.service.ActivityService;
|
||||
import ru.ulstu.activity.deadline.service.DeadlineService;
|
||||
import ru.ulstu.activity.file.service.FileService;
|
||||
@ -15,8 +14,6 @@ import ru.ulstu.activity.timeline.model.Event;
|
||||
import ru.ulstu.activity.timeline.service.EventService;
|
||||
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
||||
import ru.ulstu.core.model.response.PageableItems;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@ -32,7 +29,6 @@ public class ProjectService extends ActivityService<ProjectListDto, Project, Pro
|
||||
private final ProjectRepository projectRepository;
|
||||
private final DeadlineService deadlineService;
|
||||
private final FileService fileService;
|
||||
private final UserService userService;
|
||||
private final PingService pingService;
|
||||
private final EventService eventService;
|
||||
|
||||
@ -40,13 +36,11 @@ public class ProjectService extends ActivityService<ProjectListDto, Project, Pro
|
||||
ProjectNotificationService projectNotificationService,
|
||||
DeadlineService deadlineService,
|
||||
FileService fileService,
|
||||
UserService userService,
|
||||
PingService pingService, EventService eventService) {
|
||||
super(projectRepository, projectNotificationService, pingService, eventService);
|
||||
this.projectRepository = projectRepository;
|
||||
this.deadlineService = deadlineService;
|
||||
this.fileService = fileService;
|
||||
this.userService = userService;
|
||||
this.pingService = pingService;
|
||||
this.eventService = eventService;
|
||||
}
|
||||
@ -65,16 +59,6 @@ public class ProjectService extends ActivityService<ProjectListDto, Project, Pro
|
||||
return eventService.findByProject(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean delete(Integer projectId) {
|
||||
if (projectRepository.existsById(projectId)) {
|
||||
Project project = projectRepository.getOne(projectId);
|
||||
projectRepository.delete(project);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected Project copyFromDto(Project project, ProjectDto projectDto) throws IOException {
|
||||
project.setDescription(projectDto.getDescription());
|
||||
project.setStatus(projectDto.getStatus() == null ? TECHNICAL_TASK : projectDto.getStatus());
|
||||
@ -103,14 +87,6 @@ public class ProjectService extends ActivityService<ProjectListDto, Project, Pro
|
||||
return new ProjectDto(entity);
|
||||
}
|
||||
|
||||
public Project findById(Integer id) {
|
||||
return projectRepository.getOne(id);
|
||||
}
|
||||
|
||||
public List<User> getProjectExecutors(ProjectDto projectDto) {
|
||||
return userService.findAll();
|
||||
}
|
||||
|
||||
public ProjectDto findProjectById(Integer projectId) {
|
||||
return getNewActivityDto(findById(projectId));
|
||||
}
|
||||
|
@ -118,20 +118,6 @@ public class TaskService extends ActivityService<TaskListDto, Task, TaskDto> {
|
||||
return eventService.findByTask(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean delete(Integer taskId) {
|
||||
if (taskRepository.existsById(taskId)) {
|
||||
Task scheduleTask = taskRepository.getOne(taskId);
|
||||
Scheduler sch = schedulerRepository.findOneByTask(scheduleTask);
|
||||
if (sch != null) {
|
||||
schedulerRepository.deleteById(sch.getId());
|
||||
}
|
||||
taskRepository.deleteById(taskId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void copyMainPart(Task newTask, Task task) {
|
||||
newTask.setTitle(task.getTitle());
|
||||
newTask.setTags(tagService.saveOrCreate(task.getTags()));
|
||||
|
@ -1,225 +0,0 @@
|
||||
package grant;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import ru.ulstu.activity.deadline.model.Deadline;
|
||||
import ru.ulstu.activity.deadline.service.DeadlineService;
|
||||
import ru.ulstu.activity.grant.model.Grant;
|
||||
import ru.ulstu.activity.grant.model.GrantDto;
|
||||
import ru.ulstu.activity.grant.repository.GrantRepository;
|
||||
import ru.ulstu.activity.grant.service.GrantNotificationService;
|
||||
import ru.ulstu.activity.grant.service.GrantService;
|
||||
import ru.ulstu.activity.paper.model.Paper;
|
||||
import ru.ulstu.activity.paper.model.PaperDto;
|
||||
import ru.ulstu.activity.paper.service.PaperService;
|
||||
import ru.ulstu.activity.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.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class GrantServiceTest {
|
||||
|
||||
@Mock
|
||||
GrantRepository grantRepository;
|
||||
|
||||
@Mock
|
||||
DeadlineService deadlineService;
|
||||
|
||||
@Mock
|
||||
PaperService paperService;
|
||||
|
||||
@Mock
|
||||
UserService userService;
|
||||
|
||||
@Mock
|
||||
EventService eventService;
|
||||
|
||||
@Mock
|
||||
GrantNotificationService grantNotificationService;
|
||||
|
||||
@InjectMocks
|
||||
GrantService grantService;
|
||||
|
||||
private final static Integer ID = 1;
|
||||
private final static Integer INDEX = 0;
|
||||
private final static String TITLE = "Title";
|
||||
private final static String COMMENT = "Comment";
|
||||
private final static boolean TRUE = true;
|
||||
private final static Integer YEAR = 2019;
|
||||
private final static Integer MAX_DISPLAY_SIZE = 50;
|
||||
|
||||
private List<Grant> grants;
|
||||
private List<Deadline> deadlines;
|
||||
private List<PaperDto> paperDtos;
|
||||
private GrantDto grantDto;
|
||||
private Deadline deadline;
|
||||
private User leader;
|
||||
|
||||
private Grant grantWithId;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
grants = new ArrayList<>();
|
||||
paperDtos = new ArrayList<>();
|
||||
grantWithId = new Grant();
|
||||
|
||||
deadlines = new ArrayList<>();
|
||||
deadline = new Deadline(new Date(), COMMENT);
|
||||
deadline.setId(ID);
|
||||
deadlines.add(deadline);
|
||||
|
||||
leader = Mockito.mock(User.class);
|
||||
|
||||
List<Paper> papers = new ArrayList<>();
|
||||
Paper paperWithId = new Paper();
|
||||
paperWithId.setId(ID);
|
||||
paperWithId.setTitle(TITLE);
|
||||
papers.add(paperWithId);
|
||||
PaperDto paperDto = new PaperDto(paperWithId);
|
||||
paperDtos.add(paperDto);
|
||||
|
||||
Set<User> authors = new HashSet<>();
|
||||
User author = leader;
|
||||
authors.add(author);
|
||||
|
||||
grantWithId.setId(ID);
|
||||
grantWithId.setTitle(TITLE);
|
||||
grantWithId.setComment(COMMENT);
|
||||
grantWithId.setDeadlines(deadlines);
|
||||
grantWithId.setLeader(leader);
|
||||
grantWithId.setPapers(papers);
|
||||
grantWithId.setAuthors(authors);
|
||||
|
||||
grants.add(grantWithId);
|
||||
grantDto = new GrantDto(grantWithId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExistGrantById() {
|
||||
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
||||
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
GrantDto result = grantService.getExistGrantById(ID);
|
||||
|
||||
assertEquals(newGrantDto.getId(), result.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
when(grantRepository.findAll()).thenReturn(grants);
|
||||
|
||||
assertEquals(Collections.singletonList(grantWithId), grantService.findAll(0, 100).getItems());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void create() {
|
||||
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
|
||||
when(userService.getUserByLogin("admin")).thenReturn(leader);
|
||||
when(grantRepository.save(new Grant())).thenReturn(grantWithId);
|
||||
|
||||
Grant newGrant = new Grant();
|
||||
newGrant.setId(ID);
|
||||
newGrant.setTitle(TITLE);
|
||||
newGrant.setComment(COMMENT);
|
||||
newGrant.setDeadlines(deadlines);
|
||||
newGrant.setLeader(leader);
|
||||
|
||||
assertEquals(newGrant, grantService.create(grantDto));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGrantStatuses() {
|
||||
assertEquals(Arrays.asList(Grant.GrantStatus.values()), grantService.getGrantStatuses());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGrantPapers() {
|
||||
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
||||
|
||||
assertEquals(paperDtos, grantService.getGrantPapers(Collections.singletonList(ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUncompletedPapers() {
|
||||
when(paperService.findAllNotCompleted()).thenReturn(paperDtos);
|
||||
paperDtos.stream()
|
||||
.forEach(paperDto -> {
|
||||
paperDto.setTitle(StringUtils.abbreviate(paperDto.getTitle(), MAX_DISPLAY_SIZE));
|
||||
});
|
||||
|
||||
assertEquals(paperDtos, grantService.getAllUncompletedPapers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() throws IOException {
|
||||
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
||||
assertTrue(grantService.delete(grantWithId.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDeadline() {
|
||||
GrantDto newGrantDto = new GrantDto();
|
||||
newGrantDto.getRemovedDeadlineIds().add(ID);
|
||||
grantDto.getDeadlines().add(deadline);
|
||||
GrantDto result = grantService.removeDeadline(grantDto, INDEX);
|
||||
|
||||
assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
||||
Grant findGrant = grantService.findById(ID);
|
||||
|
||||
assertEquals(grantWithId.getId(), findGrant.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachPaper() {
|
||||
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
||||
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
|
||||
if (!newGrantDto.getPaperIds().isEmpty()) {
|
||||
newGrantDto.getPapers().clear();
|
||||
newGrantDto.setPapers(paperDtos);
|
||||
} else {
|
||||
newGrantDto.getPapers().clear();
|
||||
}
|
||||
|
||||
assertEquals(newGrantDto.getPapers(), grantService.attachPaper(grantDto));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterEmptyDeadlines() {
|
||||
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
|
||||
newGrantDto.setDeadlines(newGrantDto.getDeadlines().stream()
|
||||
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
assertEquals(newGrantDto.getDeadlines(), grantService.filterEmptyDeadlines(grantDto));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user