1
package by.andd3dfx.mappers;
3
import static org.hamcrest.CoreMatchers.is;
4
import static org.hamcrest.CoreMatchers.nullValue;
5
import static org.hamcrest.MatcherAssert.assertThat;
7
import by.andd3dfx.dto.ArticleDto;
8
import by.andd3dfx.dto.ArticleUpdateDto;
9
import by.andd3dfx.dto.AuthorDto;
10
import by.andd3dfx.persistence.entities.Article;
11
import by.andd3dfx.persistence.entities.Author;
12
import java.time.LocalDateTime;
13
import java.util.Arrays;
15
import org.junit.jupiter.api.Test;
16
import org.mapstruct.factory.Mappers;
18
class ArticleMapperTest {
20
private ArticleMapper mapper = Mappers.getMapper(ArticleMapper.class);
24
Article article = buildArticle();
26
ArticleDto articleDto = mapper.toArticleDto(article);
28
checkCompareAssertions(articleDto, article);
32
void toArticleDtoForNull() {
33
assertThat(mapper.toArticleDto(null), nullValue());
37
void toArticleDtoForAbsentAuthor() {
38
Article article = buildArticle();
39
article.setAuthor(null);
41
ArticleDto articleDto = mapper.toArticleDto(article);
43
checkCompareAssertionsWithoutAuthor(articleDto, article);
47
void toArticleDtoList() {
48
Article article = buildArticle();
49
List<Article> articles = Arrays.asList(article);
51
List<ArticleDto> articleDtoItems = mapper.toArticleDtoList(articles);
53
assertThat("Wrong result list size", articleDtoItems.size(), is(1));
54
checkCompareAssertions(articleDtoItems.get(0), article);
58
void toArticleDtoListForNull() {
59
assertThat(mapper.toArticleDtoList(null), nullValue());
64
ArticleDto articleDto = buildArticleDto();
66
Article article = mapper.toArticle(articleDto);
68
checkCompareAssertions(articleDto, article);
72
void toArticleForNull() {
73
assertThat(mapper.toArticle(null), nullValue());
77
void toArticleForAbsentAuthor() {
78
ArticleDto articleDto = buildArticleDto();
79
articleDto.setAuthor(null);
81
Article article = mapper.toArticle(articleDto);
83
checkCompareAssertionsWithoutAuthor(articleDto, article);
87
void toArticleWithTarget() {
88
ArticleUpdateDto source = new ArticleUpdateDto();
89
final String NEW_TITLE = "New title";
90
source.setTitle(NEW_TITLE);
91
Article target = buildArticle();
92
final String OLD_TEXT = target.getText();
94
mapper.toArticle(source, target);
96
assertThat(target.getTitle(), is(NEW_TITLE));
97
assertThat(target.getText(), is(OLD_TEXT));
101
void toArticleWithTargetForNull() {
102
Article target = buildArticle();
103
final String OLD_TEXT = target.getText();
105
mapper.toArticle(null, target);
107
assertThat(target.getText(), is(OLD_TEXT));
110
private Article buildArticle() {
111
Article article = new Article();
113
article.setTitle("Some tittle value");
114
article.setSummary("Some summary value");
115
article.setText("Some text");
117
Author author = new Author();
119
author.setFirstName("John");
120
author.setLastName("Deer");
122
article.setAuthor(author);
123
article.setDateCreated(LocalDateTime.of(1980, 9, 21, 0, 0));
124
article.setDateUpdated(LocalDateTime.of(2011, 3, 5, 0, 0));
128
private ArticleDto buildArticleDto() {
129
ArticleDto articleDto = new ArticleDto();
130
articleDto.setId(123L);
131
articleDto.setTitle("Some tittle value");
132
articleDto.setSummary("Some summary value");
133
articleDto.setText("Some text");
135
AuthorDto authorDto = new AuthorDto();
136
authorDto.setId(321L);
137
authorDto.setFirstName("John");
138
authorDto.setLastName("Deer");
140
articleDto.setAuthor(authorDto);
141
articleDto.setDateCreated(LocalDateTime.of(1980, 9, 21, 0, 0));
142
articleDto.setDateUpdated(LocalDateTime.of(2011, 3, 5, 0, 0));
146
private void checkCompareAssertions(ArticleDto articleDto, Article article) {
147
assertThat("Wrong id", article.getId(), is(articleDto.getId()));
148
assertThat("Wrong title", article.getTitle(), is(articleDto.getTitle()));
149
assertThat("Wrong summary", article.getSummary(), is(articleDto.getSummary()));
150
assertThat("Wrong text", article.getText(), is(articleDto.getText()));
152
assertThat("Wrong author id", article.getAuthor().getId(), is(articleDto.getAuthor().getId()));
153
assertThat("Wrong author first name", article.getAuthor().getFirstName(),
154
is(articleDto.getAuthor().getFirstName()));
155
assertThat("Wrong author last name", article.getAuthor().getLastName(),
156
is(articleDto.getAuthor().getLastName()));
158
assertThat("Wrong date created", article.getDateCreated(), is(articleDto.getDateCreated()));
159
assertThat("Wrong date updated", article.getDateUpdated(), is(articleDto.getDateUpdated()));
162
private void checkCompareAssertionsWithoutAuthor(ArticleDto articleDto, Article article) {
163
assertThat("Wrong id", article.getId(), is(articleDto.getId()));
164
assertThat("Wrong title", article.getTitle(), is(articleDto.getTitle()));
165
assertThat("Wrong summary", article.getSummary(), is(articleDto.getSummary()));
166
assertThat("Wrong text", article.getText(), is(articleDto.getText()));
168
assertThat("Wrong date created", article.getDateCreated(), is(articleDto.getDateCreated()));
169
assertThat("Wrong date updated", article.getDateUpdated(), is(articleDto.getDateUpdated()));