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

Closed
SemenovaMasha wants to merge 1 commits from 104-paper-unit-tests into dev

View File

@ -0,0 +1,173 @@
package ru.ulstu.paper.service;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.AdditionalAnswers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.service.FileService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.ReferenceDto;
import ru.ulstu.paper.repository.PaperRepository;
import ru.ulstu.timeline.service.EventService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class PaperServiceTest {
@Mock
PaperRepository paperRepository;
@Mock
PaperNotificationService paperNotificationService;
@Mock
EventService eventService;
@Mock
DeadlineService deadlineService;
@Mock
FileService fileService;
@InjectMocks
PaperService paperService;
private final static String TITLE = "TestTitle";
private final static String DESCRIPTION = "TestDescription";
private Date testDate;
private List<Paper> paperList;
private ReferenceDto referenceDto;
private Paper paper;
private List<Deadline> deadlines;
@Before
public void setUp() throws ParseException {
testDate = new SimpleDateFormat("dd-MM-yyyy").parse("01-01-2019");
Deadline deadline = new Deadline();
deadline.setDate(new SimpleDateFormat("dd-MM-yyyy").parse("01-01-2000"));
deadline.setDescription(DESCRIPTION);
paperList = new ArrayList<>();
Paper p = new Paper();
p.setId(1);
p.setTitle(TITLE);
p.setStatus(Paper.PaperStatus.COMPLETED);
paperList.add(p);
p = new Paper();
p.setId(2);
p.setTitle(TITLE);
p.setStatus(Paper.PaperStatus.DRAFT);
p.getDeadlines().add(deadline);
paperList.add(p);
referenceDto = new ReferenceDto();
referenceDto.setAuthors("authors");
referenceDto.setPublicationTitle("title");
referenceDto.setPublicationYear(2010);
referenceDto.setPublisher("publisher");
referenceDto.setPages("200");
referenceDto.setJournalOrCollectionTitle("journal");
referenceDto.setFormatStandard(ReferenceDto.FormatStandard.SPRINGER);
paper = new Paper();
paper.setId(1);
paper.setTitle(TITLE);
deadlines = new ArrayList<>();
deadline = new Deadline();
deadline.setDate(new SimpleDateFormat("dd-MM-yyyy").parse("01-01-2000"));
deadlines.add(deadline);
paper.setDeadlines(deadlines);
}
@Test
public void createNewPaperTest() {
when(paperRepository.save(any(Paper.class))).then(AdditionalAnswers.returnsFirstArg());
Paper actual = paperService.create(TITLE, null, testDate);
assertEquals(TITLE, actual.getTitle());
assertEquals(testDate, actual.getDeadlines().get(0).getDate());
assertEquals("первый дедлайн", actual.getDeadlines().get(0).getDescription());
assertEquals(Paper.PaperStatus.DRAFT, actual.getStatus());
assertEquals(Paper.PaperType.OTHER, actual.getType());
}
@Test
public void updatePaperDtoTest() throws IOException {
PaperDto paperDto = new PaperDto();
paperDto.setTitle(TITLE);
paperDto.setStatus(Paper.PaperStatus.COMPLETED);
paperDto.setType(Paper.PaperType.OTHER);
Paper paper = new Paper();
when(paperRepository.findOne(anyInt())).thenReturn(paper);
when(paperRepository.save(any(Paper.class))).then(AdditionalAnswers.returnsFirstArg());
when(deadlineService.saveOrCreate(any())).thenReturn(null);
when(fileService.saveOrCreate(any())).thenReturn(null);
paperService.update(paperDto);
assertEquals(TITLE, paper.getTitle());
assertEquals(Paper.PaperStatus.COMPLETED, paper.getStatus());
assertEquals(Paper.PaperType.OTHER, paper.getType());
}
@Test
public void findAllActiveTest() {
when(paperRepository.findAll()).thenReturn(paperList);
List<Paper> actual = paperService.findAllActive();
assertEquals(1, actual.size());
assertEquals(2, actual.get(0).getId().intValue());
}
@Test
public void closeFailedPapersTest() {
when(paperRepository.findAll()).thenReturn(paperList);
when(paperRepository.save(any(Paper.class))).then(AdditionalAnswers.returnsFirstArg());
paperService.closeFailedPapers();
assertEquals(Paper.PaperStatus.FAILED, paperList.get(1).getStatus());
assertEquals(Paper.PaperStatus.COMPLETED, paperList.get(0).getStatus());
}
@Test
public void lastDeadlineFailedTest() {
assertTrue(paperList.get(1).lastDeadlineFailed());
}
@Test
public void getSpringerFormattedReferenceTest() {
String result = paperService.getFormattedReference(referenceDto);
assertEquals("authors (2010) title. journal, publisher, pp 200", result);
}
@Test
public void getGostFormattedReferenceTest() {
referenceDto.setFormatStandard(ReferenceDto.FormatStandard.GOST);
String result = paperService.getFormattedReference(referenceDto);
assertEquals("authors title // journal. publisher, 2010. С. 200.", result);
}
}