personalweb

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

commit 507057f3d7836f7fc6809fed5819abe57050c319
parent d48e599b6d75f2b6dc18cb4f94cfc4474a1c62a8
Author: Wim Dupont <wim@wimdupont.com>
Date:   Thu, 16 Feb 2023 23:31:41 +0100

retrieve guides from gitlab


Former-commit-id: 16d3e8f82d96701c373db6e546d5079f0e968504
Diffstat:
Msrc/main/java/com/wimdupont/personalweb/api/AffirmationApi.java | 2+-
Asrc/main/java/com/wimdupont/personalweb/api/dto/Affirmation.java | 5+++++
Dsrc/main/java/com/wimdupont/personalweb/api/dto/affirmation/Affirmation.java | 5-----
Msrc/main/java/com/wimdupont/personalweb/controller/GuideController.java | 24++++++++++++++----------
Dsrc/main/java/com/wimdupont/personalweb/converter/BookToBookDtoConverter.java | 22----------------------
Asrc/main/java/com/wimdupont/personalweb/converter/BookToDtoConverter.java | 21+++++++++++++++++++++
Dsrc/main/java/com/wimdupont/personalweb/converter/Converter.java | 8--------
Asrc/main/java/com/wimdupont/personalweb/converter/GuideMetaToDtoConverter.java | 25+++++++++++++++++++++++++
Asrc/main/java/com/wimdupont/personalweb/model/GuideMeta.java | 13+++++++++++++
Asrc/main/java/com/wimdupont/personalweb/model/dao/Guide.java | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/main/java/com/wimdupont/personalweb/model/dto/GuideMetaDto.java | 48++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/main/java/com/wimdupont/personalweb/repository/GuideRepository.java | 20++++++++++++++++++++
Msrc/main/java/com/wimdupont/personalweb/service/AdocConverter.java | 4++++
Msrc/main/java/com/wimdupont/personalweb/service/BookService.java | 10+++++-----
Msrc/main/java/com/wimdupont/personalweb/service/GuideService.java | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Msrc/main/java/com/wimdupont/personalweb/service/ScheduledFileGenerator.java | 21++++++++++++++++++++-
Asrc/main/java/com/wimdupont/personalweb/service/SyndFeedService.java | 25+++++++++++++++++++++++++
Msrc/main/java/com/wimdupont/personalweb/util/Constants.java | 9---------
Asrc/main/java/com/wimdupont/personalweb/util/DateUtil.java | 14++++++++++++++
Asrc/main/resources/db/migration/V1_1__add-guide-table.sql | 9+++++++++
Asrc/main/resources/db/migration/V1_2_0__insert-arch-install-guide.sql | 7+++++++
Msrc/main/resources/templates/guides.html | 3++-
Dsrc/test/java/com/wimdupont/personalweb/converter/BookToBookDtoConverterTest.java | 41-----------------------------------------
Asrc/test/java/com/wimdupont/personalweb/converter/BookToDtoConverterTest.java | 41+++++++++++++++++++++++++++++++++++++++++
24 files changed, 412 insertions(+), 111 deletions(-)

diff --git a/src/main/java/com/wimdupont/personalweb/api/AffirmationApi.java b/src/main/java/com/wimdupont/personalweb/api/AffirmationApi.java @@ -1,6 +1,6 @@ package com.wimdupont.personalweb.api; -import com.wimdupont.personalweb.api.dto.affirmation.Affirmation; +import com.wimdupont.personalweb.api.dto.Affirmation; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; diff --git a/src/main/java/com/wimdupont/personalweb/api/dto/Affirmation.java b/src/main/java/com/wimdupont/personalweb/api/dto/Affirmation.java @@ -0,0 +1,5 @@ +package com.wimdupont.personalweb.api.dto; + +public record Affirmation(String affirmation) { + +} diff --git a/src/main/java/com/wimdupont/personalweb/api/dto/affirmation/Affirmation.java b/src/main/java/com/wimdupont/personalweb/api/dto/affirmation/Affirmation.java @@ -1,5 +0,0 @@ -package com.wimdupont.personalweb.api.dto.affirmation; - -public record Affirmation(String affirmation) { - -} diff --git a/src/main/java/com/wimdupont/personalweb/controller/GuideController.java b/src/main/java/com/wimdupont/personalweb/controller/GuideController.java @@ -1,5 +1,7 @@ package com.wimdupont.personalweb.controller; +import com.wimdupont.personalweb.converter.GuideMetaToDtoConverter; +import com.wimdupont.personalweb.model.dao.Guide; import com.wimdupont.personalweb.service.GuideService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -7,33 +9,35 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import java.io.IOException; +import java.util.Optional; @Controller @RequestMapping("/guides") public class GuideController { private final GuideService guideService; + private final GuideMetaToDtoConverter guideMetaToDtoConverter; - public GuideController(GuideService guideService) { + public GuideController(GuideService guideService, + GuideMetaToDtoConverter guideMetaToDtoConverter) { this.guideService = guideService; + this.guideMetaToDtoConverter = guideMetaToDtoConverter; } @GetMapping public String getGuides(Model model) { - model.addAttribute("guides", guideService.getFileDtoList()); + model.addAttribute("guides", guideService.findAllMetaData().stream() + .map(guideMetaToDtoConverter::convertForMetaData) + .toList()); return "guides"; } @GetMapping(value = "/{name}") public String getGuide(@PathVariable(value = "name") String name, Model model) { - String guide; - try { - guide = guideService.getHtmlFileString(name, true); - } catch (IOException e) { - guide = String.format("Guide with name \"%s\" not found.", name); - } - model.addAttribute("guide", guide); + var text = Optional.ofNullable(guideService.findGuideByTitle(name) + .orElseThrow().getText()) + .orElse("Oops, nothing here."); + model.addAttribute("guide", text); model.addAttribute("title", name); return "guide"; } diff --git a/src/main/java/com/wimdupont/personalweb/converter/BookToBookDtoConverter.java b/src/main/java/com/wimdupont/personalweb/converter/BookToBookDtoConverter.java @@ -1,22 +0,0 @@ -package com.wimdupont.personalweb.converter; - -import com.wimdupont.personalweb.model.dao.Book; -import com.wimdupont.personalweb.model.dto.BookDto; -import org.springframework.stereotype.Component; - -@Component -public class BookToBookDtoConverter implements Converter<Book, BookDto> { - - @Override - public BookDto convert(Book entity) { - return BookDto.Builder.newBuilder() - .id(entity.getId()) - .title(entity.getTitle()) - .author(entity.getAuthor()) - .isbn(entity.getIsbn()) - .category(entity.getCategory()) - .series(entity.getSeries()) - .seriesNumber(entity.getSeriesNumber()) - .build(); - } -} diff --git a/src/main/java/com/wimdupont/personalweb/converter/BookToDtoConverter.java b/src/main/java/com/wimdupont/personalweb/converter/BookToDtoConverter.java @@ -0,0 +1,21 @@ +package com.wimdupont.personalweb.converter; + +import com.wimdupont.personalweb.model.dao.Book; +import com.wimdupont.personalweb.model.dto.BookDto; +import org.springframework.stereotype.Component; + +@Component +public class BookToDtoConverter { + + public BookDto convert(Book entity) { + return BookDto.Builder.newBuilder() + .id(entity.getId()) + .title(entity.getTitle()) + .author(entity.getAuthor()) + .isbn(entity.getIsbn()) + .category(entity.getCategory()) + .series(entity.getSeries()) + .seriesNumber(entity.getSeriesNumber()) + .build(); + } +} diff --git a/src/main/java/com/wimdupont/personalweb/converter/Converter.java b/src/main/java/com/wimdupont/personalweb/converter/Converter.java @@ -1,8 +0,0 @@ -package com.wimdupont.personalweb.converter; - - -public interface Converter<E, D> { - - D convert(E entity); - -} diff --git a/src/main/java/com/wimdupont/personalweb/converter/GuideMetaToDtoConverter.java b/src/main/java/com/wimdupont/personalweb/converter/GuideMetaToDtoConverter.java @@ -0,0 +1,25 @@ +package com.wimdupont.personalweb.converter; + +import com.wimdupont.personalweb.model.GuideMeta; +import com.wimdupont.personalweb.model.dto.GuideMetaDto; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Optional; + +@Component +public class GuideMetaToDtoConverter { + + public GuideMetaDto convertForMetaData(GuideMeta guideMeta) { + return GuideMetaDto.Builder.newBuilder() + .title(guideMeta.getTitle()) + .createdDate(Optional.ofNullable(guideMeta.getCreatedDate()) + .map(LocalDateTime::toLocalDate) + .orElse(LocalDate.now())) + .lastModifiedDate(Optional.ofNullable(guideMeta.getLastModifiedDate()) + .map(LocalDateTime::toLocalDate) + .orElse(LocalDate.now())) + .build(); + } +} diff --git a/src/main/java/com/wimdupont/personalweb/model/GuideMeta.java b/src/main/java/com/wimdupont/personalweb/model/GuideMeta.java @@ -0,0 +1,13 @@ +package com.wimdupont.personalweb.model; + +import java.time.LocalDateTime; + + +public interface GuideMeta { + + String getTitle(); + + LocalDateTime getCreatedDate(); + + LocalDateTime getLastModifiedDate(); +} diff --git a/src/main/java/com/wimdupont/personalweb/model/dao/Guide.java b/src/main/java/com/wimdupont/personalweb/model/dao/Guide.java @@ -0,0 +1,79 @@ +package com.wimdupont.personalweb.model.dao; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import org.hibernate.annotations.GenericGenerator; + +import java.time.LocalDateTime; + +@Entity +public class Guide { + + @Id + @GeneratedValue(generator = "uuid") + @GenericGenerator(name = "uuid", strategy = "uuid") + private String id; + private String title; + private String adocUrl; + private String feedUrl; + private String text; + private LocalDateTime createdDate; + private LocalDateTime lastModifiedDate; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAdocUrl() { + return adocUrl; + } + + public void setAdocUrl(String adocUrl) { + this.adocUrl = adocUrl; + } + + public String getFeedUrl() { + return feedUrl; + } + + public void setFeedUrl(String feedUrl) { + this.feedUrl = feedUrl; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public LocalDateTime getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(LocalDateTime createdDate) { + this.createdDate = createdDate; + } + + public LocalDateTime getLastModifiedDate() { + return lastModifiedDate; + } + + public void setLastModifiedDate(LocalDateTime lastModifiedDate) { + this.lastModifiedDate = lastModifiedDate; + } +} diff --git a/src/main/java/com/wimdupont/personalweb/model/dto/GuideMetaDto.java b/src/main/java/com/wimdupont/personalweb/model/dto/GuideMetaDto.java @@ -0,0 +1,48 @@ +package com.wimdupont.personalweb.model.dto; + +import java.time.LocalDate; + +public record GuideMetaDto( + String title, + LocalDate createdDate, + LocalDate lastModifiedDate +) { + + private GuideMetaDto(Builder builder) { + this(builder.title, + builder.createdDate, + builder.lastModifiedDate); + } + + public static final class Builder { + private String title; + private LocalDate createdDate; + private LocalDate lastModifiedDate; + + private Builder() { + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder title(String val) { + title = val; + return this; + } + + public Builder createdDate(LocalDate val) { + createdDate = val; + return this; + } + + public Builder lastModifiedDate(LocalDate val) { + lastModifiedDate = val; + return this; + } + + public GuideMetaDto build() { + return new GuideMetaDto(this); + } + } +} diff --git a/src/main/java/com/wimdupont/personalweb/repository/GuideRepository.java b/src/main/java/com/wimdupont/personalweb/repository/GuideRepository.java @@ -0,0 +1,20 @@ +package com.wimdupont.personalweb.repository; + +import com.wimdupont.personalweb.model.GuideMeta; +import com.wimdupont.personalweb.model.dao.Guide; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface GuideRepository extends JpaRepository<Guide, String> { + + Optional<Guide> findGuideByTitle(String title); + + //Native to exclude columns + @Query(value = "select title, created_date, last_modified_date from guide order by created_date desc", nativeQuery = true) + List<GuideMeta> findAllMetaData(); +} diff --git a/src/main/java/com/wimdupont/personalweb/service/AdocConverter.java b/src/main/java/com/wimdupont/personalweb/service/AdocConverter.java @@ -24,4 +24,8 @@ public class AdocConverter { String article = Files.readString(Path.of(file.getAbsolutePath())); return ASCIIDOCTOR.convert(article, OPTIONS); } + + public String convert(String adocString){ + return ASCIIDOCTOR.convert(adocString, OPTIONS); + } } diff --git a/src/main/java/com/wimdupont/personalweb/service/BookService.java b/src/main/java/com/wimdupont/personalweb/service/BookService.java @@ -1,6 +1,6 @@ package com.wimdupont.personalweb.service; -import com.wimdupont.personalweb.converter.BookToBookDtoConverter; +import com.wimdupont.personalweb.converter.BookToDtoConverter; import com.wimdupont.personalweb.model.dto.BookDto; import com.wimdupont.personalweb.repository.BookRepository; import jakarta.transaction.Transactional; @@ -17,18 +17,18 @@ import java.util.stream.Collectors; public class BookService { private final BookRepository bookRepository; - private final BookToBookDtoConverter bookToBookDtoConverter; + private final BookToDtoConverter bookToDtoConverter; public BookService(BookRepository bookRepository, - BookToBookDtoConverter bookToBookDtoConverter) { + BookToDtoConverter bookToDtoConverter) { this.bookRepository = bookRepository; - this.bookToBookDtoConverter = bookToBookDtoConverter; + this.bookToDtoConverter = bookToDtoConverter; } public Map<String, List<BookDto>> findAllSortedByCategory() { return bookRepository.findAll(Sort.by("category", "author", "series", "seriesNumber", "title").ascending()) .stream() - .map(bookToBookDtoConverter::convert) + .map(bookToDtoConverter::convert) .collect(Collectors.groupingBy(BookDto::category, LinkedHashMap::new, Collectors.mapping(book -> book, Collectors.toList()))); diff --git a/src/main/java/com/wimdupont/personalweb/service/GuideService.java b/src/main/java/com/wimdupont/personalweb/service/GuideService.java @@ -1,18 +1,69 @@ package com.wimdupont.personalweb.service; -import com.wimdupont.personalweb.util.Constants.Guide; +import com.rometools.rome.feed.synd.SyndEntry; +import com.rometools.rome.io.FeedException; +import com.wimdupont.personalweb.model.GuideMeta; +import com.wimdupont.personalweb.model.dao.Guide; +import com.wimdupont.personalweb.repository.GuideRepository; +import com.wimdupont.personalweb.util.DateUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Optional; + @Service -public class GuideService extends AdocFileService { +public class GuideService { + + private static final Logger LOGGER = LoggerFactory.getLogger(GuideService.class); + + private final GuideRepository guideRepository; + private final SyndFeedService syndService; + + public GuideService(GuideRepository guideRepository, + SyndFeedService syndService) { + this.guideRepository = guideRepository; + this.syndService = syndService; + } - @Override - String getAdocDirectory() { - return Guide.GUIDES_DIRECTORY; + public Guide update(Guide guide) { + return guideRepository.save(guide); } - @Override - String getHtmlDirectory() { - return Guide.HTML_GUIDES_DIRECTORY; + public List<GuideMeta> findAllMetaData() { + return guideRepository.findAllMetaData(); + } + + public List<Guide> findAllToGenerate() { + var guidesToGenerate = new ArrayList<Guide>(); + for (Guide guide : guideRepository.findAll()) { + try { + var syndFeed = syndService.getSyndFeedForUrl(guide.getFeedUrl()); + var lastPubDate = DateUtil.toLocalDateTime(syndFeed.getPublishedDate()); + if (guide.getText() == null || guide.getLastModifiedDate() == null || lastPubDate.isAfter(guide.getLastModifiedDate())) { + var dateCreated = DateUtil.toLocalDateTime(Collections.min(syndFeed.getEntries(), + Comparator.comparing(SyndEntry::getUpdatedDate)).getUpdatedDate()); + guide.setCreatedDate(dateCreated); + guide.setLastModifiedDate(lastPubDate); + guidesToGenerate.add(guide); + } + } catch (IOException | FeedException e) { + throw new RuntimeException(e); + } + } + if (guidesToGenerate.isEmpty()) { + LOGGER.info("All guides are up to date."); + } + return guidesToGenerate; + } + + + public Optional<Guide> findGuideByTitle(String title) { + return guideRepository.findGuideByTitle(title); } } diff --git a/src/main/java/com/wimdupont/personalweb/service/ScheduledFileGenerator.java b/src/main/java/com/wimdupont/personalweb/service/ScheduledFileGenerator.java @@ -1,5 +1,6 @@ package com.wimdupont.personalweb.service; +import com.wimdupont.personalweb.model.dao.Guide; import jakarta.transaction.Transactional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,6 +9,9 @@ import org.springframework.stereotype.Service; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; @@ -37,8 +41,23 @@ public class ScheduledFileGenerator { public void generate() { LOG.info("File generator started."); generateHtmlFiles(articleService); - generateHtmlFiles(guideService); rssFeedGenerator.generate(); + generateGuides(guideService); + } + + private void generateGuides(GuideService guideService) { + for (Guide guide : guideService.findAllToGenerate()) { + try { + var url = new URL(guide.getAdocUrl()); + try (InputStream in = url.openStream()) { + var adocFile = new String(in.readAllBytes(), StandardCharsets.UTF_8); + guide.setText(adocConverter.convert(adocFile)); + LOG.info("Guide updated: {}", guideService.update(guide)); + } + } catch (IOException e) { + LOG.error(e.getMessage(), e); + } + } } private void generateHtmlFiles(AdocFileService adocFileService) { diff --git a/src/main/java/com/wimdupont/personalweb/service/SyndFeedService.java b/src/main/java/com/wimdupont/personalweb/service/SyndFeedService.java @@ -0,0 +1,25 @@ +package com.wimdupont.personalweb.service; + +import com.rometools.rome.feed.synd.SyndFeed; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.SyndFeedInput; +import org.springframework.stereotype.Service; +import org.xml.sax.InputSource; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +@Service +public class SyndFeedService { + + public SyndFeed getSyndFeedForUrl(String url) throws IOException, IllegalArgumentException, FeedException { + SyndFeed feed; + try (InputStream inputStream = new URL(url).openConnection().getInputStream()) { + InputSource source = new InputSource(inputStream); + SyndFeedInput input = new SyndFeedInput(); + feed = input.build(source); + } + return feed; + } +} diff --git a/src/main/java/com/wimdupont/personalweb/util/Constants.java b/src/main/java/com/wimdupont/personalweb/util/Constants.java @@ -17,13 +17,4 @@ public class Constants { public static final String HTML_ARTICLES_DIRECTORY = ARTICLES_DIRECTORY + "/html"; } - public static class Guide { - private Guide() { - } - - public static final String GUIDES_DIRECTORY = System.getProperty("user.home") + "/personalweb/guides"; - public static final String HTML_GUIDES_DIRECTORY = GUIDES_DIRECTORY + "/html"; - - } - } diff --git a/src/main/java/com/wimdupont/personalweb/util/DateUtil.java b/src/main/java/com/wimdupont/personalweb/util/DateUtil.java @@ -0,0 +1,14 @@ +package com.wimdupont.personalweb.util; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Date; + +public final class DateUtil { + private DateUtil() { + } + + public static LocalDateTime toLocalDateTime(Date date) { + return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + } +} diff --git a/src/main/resources/db/migration/V1_1__add-guide-table.sql b/src/main/resources/db/migration/V1_1__add-guide-table.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS guide ( + id VARCHAR(36) primary key NOT NULL, + adoc_url VARCHAR(400) UNIQUE NOT NULL, + feed_url VARCHAR(400) UNIQUE NOT NULL, + title VARCHAR(100) UNIQUE NOT NULL, + text TEXT, + created_date DATETIME, + last_modified_date DATETIME +) diff --git a/src/main/resources/db/migration/V1_2_0__insert-arch-install-guide.sql b/src/main/resources/db/migration/V1_2_0__insert-arch-install-guide.sql @@ -0,0 +1,7 @@ +INSERT INTO guide (id, adoc_url, feed_url, title, text, created_date, last_modified_date) VALUES + (UUID(), + 'https://gitlab.com/WimDupont/guides/-/raw/main/Arch%20Linux%20encrypted%20installation.adoc', + 'https://gitlab.com/WimDupont/guides/-/commits/main/Arch%20Linux%20encrypted%20installation.adoc?feed_token=PnjNF_m7PMKTraxkHzcq&format=atom', + "Arch Linux encrypted installation guide", + null, null, null) +; diff --git a/src/main/resources/templates/guides.html b/src/main/resources/templates/guides.html @@ -11,7 +11,8 @@ <div th:insert="~{navigation}"/> </header> <tr th:each="guide : ${guides}"> - <p><a th:href="@{/guides/{guide}(guide=${guide.name})}">[[${guide.name}]]</a> - [[${guide.date}]]</p> + <p><a th:href="@{/guides/{guide}(guide=${guide.title})}">[[${guide.title}]]</a> - + [[${guide.createdDate}]] <i><small>(last updated: [[${guide.lastModifiedDate}]])</small></i></p> </tr> </div> </body> diff --git a/src/test/java/com/wimdupont/personalweb/converter/BookToBookDtoConverterTest.java b/src/test/java/com/wimdupont/personalweb/converter/BookToBookDtoConverterTest.java @@ -1,41 +0,0 @@ -package com.wimdupont.personalweb.converter; - -import com.wimdupont.personalweb.model.dao.Book; -import com.wimdupont.personalweb.model.dto.BookDto; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class BookToBookDtoConverterTest { - - 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 BookToBookDtoConverter converter; - private Book book; - - @BeforeEach - void setup() { - converter = new BookToBookDtoConverter(); - 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); - } - - @Test - void convert() { - BookDto result = converter.convert(book); - Assertions.assertThat(result).usingRecursiveComparison().isEqualTo(book); - } - -} diff --git a/src/test/java/com/wimdupont/personalweb/converter/BookToDtoConverterTest.java b/src/test/java/com/wimdupont/personalweb/converter/BookToDtoConverterTest.java @@ -0,0 +1,41 @@ +package com.wimdupont.personalweb.converter; + +import com.wimdupont.personalweb.model.dao.Book; +import com.wimdupont.personalweb.model.dto.BookDto; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +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; + + @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); + } + + @Test + void convert() { + BookDto result = converter.convert(book); + Assertions.assertThat(result).usingRecursiveComparison().isEqualTo(book); + } + +}