From f0f4a2f19ca09e81650a13d96019ba6f36f64915 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 20 Jul 2019 14:08:46 +0400 Subject: [PATCH] fix delete --- .../activity/api/ActivityRepository.java | 2 + .../common/service/ActivityService.java | 10 +- .../conference/service/ConferenceService.java | 9 - .../activity/grant/service/GrantService.java | 47 ---- .../activity/paper/service/PaperService.java | 8 - .../project/service/ProjectService.java | 24 -- .../students/service/TaskService.java | 14 -- src/test/java/grant/GrantServiceTest.java | 225 ------------------ 8 files changed, 9 insertions(+), 330 deletions(-) delete mode 100644 src/test/java/grant/GrantServiceTest.java diff --git a/src/main/java/ru/ulstu/activity/api/ActivityRepository.java b/src/main/java/ru/ulstu/activity/api/ActivityRepository.java index 58cda60..e0be94d 100644 --- a/src/main/java/ru/ulstu/activity/api/ActivityRepository.java +++ b/src/main/java/ru/ulstu/activity/api/ActivityRepository.java @@ -12,4 +12,6 @@ public interface ActivityRepository { T getById(Integer id); Page findAll(Pageable pageable); + + void deleteById(Integer id); } diff --git a/src/main/java/ru/ulstu/activity/common/service/ActivityService.java b/src/main/java/ru/ulstu/activity/common/service/ActivityService.java index c51b750..eeae0bd 100644 --- a/src/main/java/ru/ulstu/activity/common/service/ActivityService.java +++ b/src/main/java/ru/ulstu/activity/common/service/ActivityService.java @@ -74,10 +74,14 @@ public abstract class ActivityService findAll(int offset, int count) { final Page page = activityRepository.findAll(new OffsetablePageRequest(offset, count)); diff --git a/src/main/java/ru/ulstu/activity/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/activity/conference/service/ConferenceService.java index 3614766..ca3f824 100644 --- a/src/main/java/ru/ulstu/activity/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/activity/conference/service/ConferenceService.java @@ -69,15 +69,6 @@ public class ConferenceService extends ActivityService this.pingService = pingService; } - public GrantDto getExistGrantById(Integer id) { - return new GrantDto(findById(id)); - } - @Override public PageableItems findAll(int offset, int count) { final Page page = grantRepository.findAll(new OffsetablePageRequest(offset, count)); @@ -128,16 +120,6 @@ public class GrantService extends ActivityService 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 getGrantStatuses() { return Arrays.asList(Grant.GrantStatus.values()); } @@ -158,40 +140,12 @@ public class GrantService extends ActivityService } } - 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 getGrantPapers(List paperIds) { - return paperService.findAllSelect(paperIds); - } - - public List getAllUncompletedPapers() { - return paperService.findAllNotCompleted(); - } - - private List getCompletedPapersAuthors(Paper.PaperType type) { - List 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 public PageableItems findAllActive(int offset, int count) { Page activeGrantsPage = grantRepository.findAllActive(new OffsetablePageRequest(offset, count)); return new PageableItems<>(activeGrantsPage.getTotalElements(), activeGrantsPage.getContent()); - } public GrantDto findGrantById(Integer id) { diff --git a/src/main/java/ru/ulstu/activity/paper/service/PaperService.java b/src/main/java/ru/ulstu/activity/paper/service/PaperService.java index 45325a8..30adfec 100644 --- a/src/main/java/ru/ulstu/activity/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/activity/paper/service/PaperService.java @@ -231,14 +231,6 @@ public class PaperService extends ActivityService .orElseThrow(() -> new EntityNotFoundException("Paper with id=" + paperId + " not found")); } - public List findAllNotSelect(List paperIds) { - if (!paperIds.isEmpty()) { - return sortPapers(paperRepository.findByIdNotInAndConferencesIsNullAndStatusNot(paperIds, COMPLETED)); - } else { - return sortPapers(paperRepository.findByConferencesIsNullAndStatusNot(COMPLETED)); - } - } - public List findAllNotCompleted() { return convert(paperRepository.findByStatusNot(COMPLETED), PaperDto::new); } diff --git a/src/main/java/ru/ulstu/activity/project/service/ProjectService.java b/src/main/java/ru/ulstu/activity/project/service/ProjectService.java index 52cedc0..a601f47 100644 --- a/src/main/java/ru/ulstu/activity/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/activity/project/service/ProjectService.java @@ -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 getProjectExecutors(ProjectDto projectDto) { - return userService.findAll(); - } - public ProjectDto findProjectById(Integer projectId) { return getNewActivityDto(findById(projectId)); } diff --git a/src/main/java/ru/ulstu/activity/students/service/TaskService.java b/src/main/java/ru/ulstu/activity/students/service/TaskService.java index f7b62ee..64f3567 100644 --- a/src/main/java/ru/ulstu/activity/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/activity/students/service/TaskService.java @@ -118,20 +118,6 @@ public class TaskService extends ActivityService { 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())); diff --git a/src/test/java/grant/GrantServiceTest.java b/src/test/java/grant/GrantServiceTest.java deleted file mode 100644 index 5cb632c..0000000 --- a/src/test/java/grant/GrantServiceTest.java +++ /dev/null @@ -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 grants; - private List deadlines; - private List 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 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 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)); - } -} \ No newline at end of file