From f1ca0418fe9c188853af2b36a7a66e873fd2603b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 3 Jun 2019 00:11:25 +0400 Subject: [PATCH] #104 paper unit tests --- .../ulstu/paper/service/PaperServiceTest.java | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/test/java/ru/ulstu/paper/service/PaperServiceTest.java diff --git a/src/test/java/ru/ulstu/paper/service/PaperServiceTest.java b/src/test/java/ru/ulstu/paper/service/PaperServiceTest.java new file mode 100644 index 0000000..b6acf13 --- /dev/null +++ b/src/test/java/ru/ulstu/paper/service/PaperServiceTest.java @@ -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 paperList; + private ReferenceDto referenceDto; + private Paper paper; + private List 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 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); + } +} -- 2.34.1