#104 add 3 more tests

This commit is contained in:
T-Midnight 2019-06-07 22:39:20 +04:00
parent 888fecf687
commit e4f5bede2a
2 changed files with 39 additions and 49 deletions

View File

@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.service.FileService; import ru.ulstu.file.service.FileService;
@ -76,13 +77,11 @@ public class GrantService extends BaseService {
this.pingService = pingService; this.pingService = pingService;
} }
//++
public GrantDto getExistGrantById(Integer id) { public GrantDto getExistGrantById(Integer id) {
GrantDto grantDto = new GrantDto(findById(id)); GrantDto grantDto = new GrantDto(findById(id));
return grantDto; return grantDto;
} }
//++
public List<Grant> findAll() { public List<Grant> findAll() {
return grantRepository.findAll(); return grantRepository.findAll();
} }
@ -91,7 +90,6 @@ public class GrantService extends BaseService {
return convert(findAll(), GrantDto::new); return convert(findAll(), GrantDto::new);
} }
//++
@Transactional @Transactional
public Grant create(GrantDto grantDto) throws IOException { public Grant create(GrantDto grantDto) throws IOException {
Grant newGrant = copyFromDto(new Grant(), grantDto); Grant newGrant = copyFromDto(new Grant(), grantDto);
@ -164,7 +162,6 @@ public class GrantService extends BaseService {
return grant.getId(); return grant.getId();
} }
//++
@Transactional @Transactional
public boolean delete(Integer grantId) throws IOException { public boolean delete(Integer grantId) throws IOException {
Grant grant = findById(grantId); Grant grant = findById(grantId);
@ -175,30 +172,10 @@ public class GrantService extends BaseService {
return false; return false;
} }
//++
public List<Grant.GrantStatus> getGrantStatuses() { public List<Grant.GrantStatus> getGrantStatuses() {
return Arrays.asList(Grant.GrantStatus.values()); return Arrays.asList(Grant.GrantStatus.values());
} }
// @Transactional
// public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) {
// Grant grant = new Grant();
// grant.setTitle(title);
// grant.setComment("Комментарий к гранту 1");
// grant.setProject(projectId);
// grant.setStatus(APPLICATION);
// grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
// grant.getAuthors().add(user);
// grant.setLeader(user);
// grant.getPapers().add(paper);
// grant = grantRepository.save(grant);
//
// eventService.createFromGrant(grant);
// grantNotificationService.sendCreateNotification(grant);
//
// return grant;
// }
public boolean save(GrantDto grantDto, Errors errors) throws IOException { public boolean save(GrantDto grantDto, Errors errors) throws IOException {
grantDto.setName(grantDto.getTitle()); grantDto.setName(grantDto.getTitle());
filterEmptyDeadlines(grantDto); filterEmptyDeadlines(grantDto);
@ -278,26 +255,24 @@ public class GrantService extends BaseService {
.collect(toList()); .collect(toList());
} }
//++
public List<PaperDto> getGrantPapers(List<Integer> paperIds) { public List<PaperDto> getGrantPapers(List<Integer> paperIds) {
return paperService.findAllSelect(paperIds); return paperService.findAllSelect(paperIds);
} }
//++
public List<PaperDto> getAllUncompletedPapers() { public List<PaperDto> getAllUncompletedPapers() {
return paperService.findAllNotCompleted(); return paperService.findAllNotCompleted();
} }
public void attachPaper(GrantDto grantDto) { public List<PaperDto> attachPaper(GrantDto grantDto) {
if (!grantDto.getPaperIds().isEmpty()) { if (!grantDto.getPaperIds().isEmpty()) {
grantDto.getPapers().clear(); grantDto.getPapers().clear();
grantDto.setPapers(getGrantPapers(grantDto.getPaperIds())); grantDto.setPapers(getGrantPapers(grantDto.getPaperIds()));
} else { } else {
grantDto.getPapers().clear(); grantDto.getPapers().clear();
} }
return grantDto.getPapers();
} }
//++
public GrantDto removeDeadline(GrantDto grantDto, Integer deadlineId) { public GrantDto removeDeadline(GrantDto grantDto, Integer deadlineId) {
if (grantDto.getDeadlines().get(deadlineId).getId() != null) { if (grantDto.getDeadlines().get(deadlineId).getId() != null) {
grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId()); grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId());
@ -329,10 +304,11 @@ public class GrantService extends BaseService {
.collect(toList()); .collect(toList());
} }
public void filterEmptyDeadlines(GrantDto grantDto) { public List<Deadline> filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream() grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription())) .filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
.collect(Collectors.toList())); .collect(Collectors.toList()));
return grantDto.getDeadlines();
} }
@Transactional @Transactional
@ -354,12 +330,10 @@ public class GrantService extends BaseService {
return grantRepository.findAllActive(); return grantRepository.findAllActive();
} }
//+
public Grant findById(Integer id) { public Grant findById(Integer id) {
return grantRepository.findOne(id); return grantRepository.findOne(id);
} }
//+
@Transactional @Transactional
public void ping(int grantId) throws IOException { public void ping(int grantId) throws IOException {
pingService.addPing(findById(grantId)); pingService.addPing(findById(grantId));

View File

@ -28,6 +28,7 @@ import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -103,7 +104,6 @@ public class GrantServiceTest {
paperDto = new PaperDto(paperWithId); paperDto = new PaperDto(paperWithId);
paperDtos.add(paperDto); paperDtos.add(paperDto);
authors = new HashSet<>(); authors = new HashSet<>();
author = leader; author = leader;
authors.add(author); authors.add(author);
@ -184,7 +184,7 @@ public class GrantServiceTest {
} }
@Test @Test
public void removeDeadline() throws IOException { public void removeDeadline() {
GrantDto newGrantDto = new GrantDto(); GrantDto newGrantDto = new GrantDto();
newGrantDto.getRemovedDeadlineIds().add(ID); newGrantDto.getRemovedDeadlineIds().add(ID);
grantDto.getDeadlines().add(deadline); grantDto.getDeadlines().add(deadline);
@ -193,23 +193,39 @@ public class GrantServiceTest {
assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds()); assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds());
} }
@Test
public void findById() {
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
Grant findGrant = grantService.findById(ID);
// @Test assertEquals(grantWithId.getId(), findGrant.getId());
// public void findAllDto() { }
// when(grantRepository.findAll()).thenReturn(grants);
// List<Grant> list = convert(grants, GrantDto::new);
// grantDtos.clear();
// //List<GrantDto> listDto = new ArrayList<>();
// list.forEach(grant -> {
// GrantDto grantDto = new GrantDto(grant);
// grantDtos.add(grantDto);
// });
//
//
// //grantDtos.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
// //List<GrantDto> result = ;
// assertEquals(grantDtos, grantService.findAllDto());
// }
@Test
public void attachPaper() {
when(grantRepository.findOne(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.findOne(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));
}
} }