personalweb

archived
git clone git://git.wimdupont.com/personalweb.git
Log | Files | Refs | LICENSE

commit 3e62f3a001a3827ddd2fb7cbbce715933cb718ec
parent 8d46cf2c71b605d50d9ce04531bcdac4267d96bc
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat,  8 Jul 2023 16:18:28 +0200

added some unit tests


Former-commit-id: 1eff82b79776d1e09860fd5eee64a717cb44f0d1
Diffstat:
Msrc/test/java/com/wimdupont/personalweb/converter/BookToDtoConverterTest.java | 17++---------------
Msrc/test/java/com/wimdupont/personalweb/converter/GuideMetaToDtoConverterTest.java | 20+++++++++++++++-----
Asrc/test/java/com/wimdupont/personalweb/model/dao/BookMother.java | 24++++++++++++++++++++++++
Asrc/test/java/com/wimdupont/personalweb/model/dao/GuideMother.java | 24++++++++++++++++++++++++
Asrc/test/java/com/wimdupont/personalweb/service/BookServiceTest.java | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test/java/com/wimdupont/personalweb/service/GuideServiceTest.java | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test/java/com/wimdupont/personalweb/service/RssFeedGeneratorTest.java | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 279 insertions(+), 20 deletions(-)

diff --git a/src/test/java/com/wimdupont/personalweb/converter/BookToDtoConverterTest.java b/src/test/java/com/wimdupont/personalweb/converter/BookToDtoConverterTest.java @@ -1,6 +1,7 @@ package com.wimdupont.personalweb.converter; import com.wimdupont.personalweb.model.dao.Book; +import com.wimdupont.personalweb.model.dao.BookMother; import com.wimdupont.personalweb.model.dto.BookDto; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -8,13 +9,6 @@ import org.junit.jupiter.api.Test; class BookToDtoConverterTest { - private static final String ID = "id"; - private static final String TITLE = "title"; - private static final String AUTHOR = "author"; - private static final String ISBN = "isbn"; - private static final String CATEGORY = "category"; - private static final String SERIES = "series"; - private static final Double SERIES_NUMBER = 2.5; private BookToDtoConverter converter; private Book book; @@ -22,14 +16,7 @@ class BookToDtoConverterTest { @BeforeEach void setup() { converter = new BookToDtoConverter(); - book = new Book(); - book.setId(ID); - book.setTitle(TITLE); - book.setAuthor(AUTHOR); - book.setIsbn(ISBN); - book.setCategory(CATEGORY); - book.setSeries(SERIES); - book.setSeriesNumber(SERIES_NUMBER); + book = BookMother.withDefaults(); } @Test diff --git a/src/test/java/com/wimdupont/personalweb/converter/GuideMetaToDtoConverterTest.java b/src/test/java/com/wimdupont/personalweb/converter/GuideMetaToDtoConverterTest.java @@ -1,6 +1,5 @@ package com.wimdupont.personalweb.converter; -import com.wimdupont.personalweb.model.GuideMeta; import com.wimdupont.personalweb.model.dto.GuideMetaDto; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -9,7 +8,6 @@ import org.junit.jupiter.api.Test; class GuideMetaToDtoConverterTest { private GuideMetaToDtoConverter converter; - private final GuideMeta guideMeta = () -> "guide.adoc"; @BeforeEach void setup() { @@ -17,9 +15,21 @@ class GuideMetaToDtoConverterTest { } @Test - void convert() { - GuideMetaDto result = converter.convertForMetaData(guideMeta); - Assertions.assertEquals(result.title(), guideMeta.getPath().replace(".adoc", "")); + void convertWithAdocExtension() { + GuideMetaDto result = converter.convertForMetaData(() -> "guide.adoc"); + Assertions.assertEquals("guide", result.title()); + } + + @Test + void convertWithOtherExtension() { + GuideMetaDto result = converter.convertForMetaData(() -> "guide.md"); + Assertions.assertEquals("guide.md", result.title()); + } + + @Test + void convertWithoutExtension() { + GuideMetaDto result = converter.convertForMetaData(() -> "guide"); + Assertions.assertEquals("guide", result.title()); } } diff --git a/src/test/java/com/wimdupont/personalweb/model/dao/BookMother.java b/src/test/java/com/wimdupont/personalweb/model/dao/BookMother.java @@ -0,0 +1,24 @@ +package com.wimdupont.personalweb.model.dao; + +public class BookMother { + + private static final String ID = "id"; + private static final String TITLE = "title"; + private static final String AUTHOR = "author"; + private static final String ISBN = "isbn"; + private static final String CATEGORY = "category"; + private static final String SERIES = "series"; + private static final Double SERIES_NUMBER = 2.5; + + public static Book withDefaults() { + var book = new Book(); + book.setId(ID); + book.setTitle(TITLE); + book.setAuthor(AUTHOR); + book.setIsbn(ISBN); + book.setCategory(CATEGORY); + book.setSeries(SERIES); + book.setSeriesNumber(SERIES_NUMBER); + return book; + } +} diff --git a/src/test/java/com/wimdupont/personalweb/model/dao/GuideMother.java b/src/test/java/com/wimdupont/personalweb/model/dao/GuideMother.java @@ -0,0 +1,24 @@ +package com.wimdupont.personalweb.model.dao; + +import java.time.LocalDateTime; + +public class GuideMother { + + private static final String PATH = "path"; + private static final String CONTENT_BASE_64 = "contentBase64"; + private static final String HTML_TEXT = "htmlText"; + private static final String CONTENT_SHA_256 = "contentSha256"; + private static final LocalDateTime CREATED_DATE = LocalDateTime.of(2000, 1, 1, 0, 0); + private static final LocalDateTime MODIFIED_DATE = LocalDateTime.of(2002, 1, 1, 0, 0); + + public static Guide.Builder withDefaults() { + return Guide.Builder.newBuilder() + .path(PATH) + .contentBase64(CONTENT_BASE_64) + .htmlText(HTML_TEXT) + .contentSha256(CONTENT_SHA_256) + .createdDate(CREATED_DATE) + .modifiedDate(MODIFIED_DATE); + } + +} diff --git a/src/test/java/com/wimdupont/personalweb/service/BookServiceTest.java b/src/test/java/com/wimdupont/personalweb/service/BookServiceTest.java @@ -0,0 +1,68 @@ +package com.wimdupont.personalweb.service; + + +import com.wimdupont.personalweb.converter.BookToDtoConverter; +import com.wimdupont.personalweb.model.dao.Book; +import com.wimdupont.personalweb.model.dao.BookMother; +import com.wimdupont.personalweb.model.dto.BookDto; +import com.wimdupont.personalweb.repository.BookRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Sort; + +import java.util.List; + +@ExtendWith(MockitoExtension.class) +class BookServiceTest { + + + private List<Book> bookList; + @Mock + private BookRepository bookRepository; + @Mock + private BookToDtoConverter bookToDtoConverter; + @InjectMocks + private BookService bookService; + + + @BeforeEach + public void setup() { + bookList = List.of( + BookMother.withDefaults(), + BookMother.withDefaults(), + BookMother.withDefaults() + ); + } + + @Test + public void findAllSortedByCategory() { + Mockito.when(bookRepository.findAll(Mockito.any(Sort.class))) + .thenReturn(bookList); + Mockito.when(bookToDtoConverter.convert(Mockito.any(Book.class))) + .thenReturn(BookDto.Builder.newBuilder() + .id("1") + .category("Fun") + .build(), + BookDto.Builder.newBuilder() + .id("2") + .category("Fun") + .build(), + BookDto.Builder.newBuilder() + .id("3") + .category("Serious") + .build()); + + var result = bookService.findAllSortedByCategory(); + Mockito.verify(bookToDtoConverter, Mockito.times(3)).convert(Mockito.any(Book.class)); + Assertions.assertEquals(2, result.size()); + Assertions.assertEquals(result.get("Fun").size(), 2); + Assertions.assertEquals(result.get("Serious").size(), 1); + } + +} diff --git a/src/test/java/com/wimdupont/personalweb/service/GuideServiceTest.java b/src/test/java/com/wimdupont/personalweb/service/GuideServiceTest.java @@ -0,0 +1,90 @@ +package com.wimdupont.personalweb.service; + +import com.wimdupont.personalweb.api.GitlabApi; +import com.wimdupont.personalweb.api.dto.RepositoryFile; +import com.wimdupont.personalweb.api.dto.RepositoryTreeItem; +import com.wimdupont.personalweb.model.dao.Guide; +import com.wimdupont.personalweb.model.dao.GuideMother; +import com.wimdupont.personalweb.repository.GuideRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Optional; + +@ExtendWith(MockitoExtension.class) +class GuideServiceTest { + + private List<RepositoryTreeItem> repositoryTreeItems; + private RepositoryFile repositoryFile; + @Mock + private GuideRepository guideRepository; + @Mock + private GitlabApi gitlabApi; + @InjectMocks + private GuideService guideService; + + @BeforeEach + public void setup() { + repositoryTreeItems = List.of(new RepositoryTreeItem("path", "name")); + repositoryFile = new RepositoryFile("path", "contentSha256", "contentBase64"); + } + + @Test + public void findRepositoryFiles() { + Mockito.when(gitlabApi.getRepositoryTree()) + .thenReturn(Optional.of(repositoryTreeItems)); + Mockito.when(gitlabApi.getRepositoryFile("path")) + .thenReturn(Optional.of(repositoryFile)); + + var result = guideService.findRepositoryFiles(); + + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals(repositoryFile, result.get(0)); + } + + @Test + public void removeNotFound() { + Mockito.when(guideRepository.findAll()) + .thenReturn(List.of( + GuideMother.withDefaults().path("path").build(), + GuideMother.withDefaults().path("someOtherPath").build())); + + guideService.removeNotFound(List.of(repositoryFile)); + + Mockito.verify(guideRepository, Mockito.times(1)).delete(Mockito.any(Guide.class)); + } + + @Test + public void findAllToUpsertWhenHashFound() { + var guideToUpsert = GuideMother.withDefaults().contentSha256("contentSha256").build(); + Mockito.when(guideRepository.findByContentSha256(Mockito.any(String.class))) + .thenReturn(Optional.of(guideToUpsert)); + + var result = guideService.findAllToUpsert(List.of(repositoryFile)); + + Mockito.verify(guideRepository, Mockito.times(1)).findByContentSha256(Mockito.any(String.class)); + Assertions.assertEquals(0, result.size()); + } + + @Test + public void findAllToUpsertWhenHashNotFound() { + Mockito.when(guideRepository.findByContentSha256(Mockito.any(String.class))) + .thenReturn(Optional.empty()); + + var result = guideService.findAllToUpsert(List.of(repositoryFile)); + + Mockito.verify(guideRepository, Mockito.times(1)).findByContentSha256(Mockito.any(String.class)); + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals(repositoryFile.path(), result.get(0).getPath()); + Assertions.assertEquals(repositoryFile.contentSha256(), result.get(0).getContentSha256()); + Assertions.assertEquals(repositoryFile.contentBase64(), result.get(0).getContentBase64()); + } + +} diff --git a/src/test/java/com/wimdupont/personalweb/service/RssFeedGeneratorTest.java b/src/test/java/com/wimdupont/personalweb/service/RssFeedGeneratorTest.java @@ -0,0 +1,56 @@ +package com.wimdupont.personalweb.service; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.ZoneId; + +@ExtendWith(MockitoExtension.class) +class RssFeedGeneratorTest { + + private static final LocalDateTime LAST_MODIFIED_DATE = LocalDateTime.of(2000, 1, 1, 0, 0); + @Mock + private ArticleService articleService; + @Mock + private File file; + @InjectMocks + private RssFeedGenerator rssFeedGenerator; + + @Test + public void generate() throws IOException { + Mockito.when(articleService.getHtmlFiles()) + .thenReturn(new File[]{file}); + Mockito.when(articleService.getHtmlFileString("file", false)) + .thenReturn("content"); + Mockito.when(file.getName()) + .thenReturn("file"); + Mockito.when(file.lastModified()) + .thenReturn(LAST_MODIFIED_DATE.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); + + rssFeedGenerator.generate(); + + var channel = rssFeedGenerator.getRssFeed().channel(); + Assertions.assertEquals("2.0", rssFeedGenerator.getRssFeed().version()); + Assertions.assertEquals("Wim Dupont", channel.title()); + Assertions.assertEquals("https://wimdupont.com", channel.link()); + Assertions.assertEquals("Updates from Wim Dupont", channel.description()); + Assertions.assertEquals("en-US", channel.language()); + Assertions.assertNotNull(channel.pubDate()); + Assertions.assertEquals(1, channel.item().size()); + Assertions.assertEquals("file", channel.item().get(0).title()); + Assertions.assertEquals("Wim Dupont", channel.item().get(0).author()); + Assertions.assertEquals("https://wimdupont.com/blog/article/file", channel.item().get(0).link()); + Assertions.assertEquals("content", channel.item().get(0).description()); + Assertions.assertEquals(LAST_MODIFIED_DATE.toString(), channel.item().get(0).pubDate()); + Assertions.assertEquals("file-" + LAST_MODIFIED_DATE, channel.item().get(0).guid()); + } + +}