articles-backend-app

Форк
0
171 строка · 5.8 Кб
1
package by.andd3dfx.mappers;
2

3
import static org.hamcrest.CoreMatchers.is;
4
import static org.hamcrest.CoreMatchers.nullValue;
5
import static org.hamcrest.MatcherAssert.assertThat;
6

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;
14
import java.util.List;
15
import org.junit.jupiter.api.Test;
16
import org.mapstruct.factory.Mappers;
17

18
class ArticleMapperTest {
19

20
    private ArticleMapper mapper = Mappers.getMapper(ArticleMapper.class);
21

22
    @Test
23
    void toArticleDto() {
24
        Article article = buildArticle();
25

26
        ArticleDto articleDto = mapper.toArticleDto(article);
27

28
        checkCompareAssertions(articleDto, article);
29
    }
30

31
    @Test
32
    void toArticleDtoForNull() {
33
        assertThat(mapper.toArticleDto(null), nullValue());
34
    }
35

36
    @Test
37
    void toArticleDtoForAbsentAuthor() {
38
        Article article = buildArticle();
39
        article.setAuthor(null);
40

41
        ArticleDto articleDto = mapper.toArticleDto(article);
42

43
        checkCompareAssertionsWithoutAuthor(articleDto, article);
44
    }
45

46
    @Test
47
    void toArticleDtoList() {
48
        Article article = buildArticle();
49
        List<Article> articles = Arrays.asList(article);
50

51
        List<ArticleDto> articleDtoItems = mapper.toArticleDtoList(articles);
52

53
        assertThat("Wrong result list size", articleDtoItems.size(), is(1));
54
        checkCompareAssertions(articleDtoItems.get(0), article);
55
    }
56

57
    @Test
58
    void toArticleDtoListForNull() {
59
        assertThat(mapper.toArticleDtoList(null), nullValue());
60
    }
61

62
    @Test
63
    void toArticle() {
64
        ArticleDto articleDto = buildArticleDto();
65

66
        Article article = mapper.toArticle(articleDto);
67

68
        checkCompareAssertions(articleDto, article);
69
    }
70

71
    @Test
72
    void toArticleForNull() {
73
        assertThat(mapper.toArticle(null), nullValue());
74
    }
75

76
    @Test
77
    void toArticleForAbsentAuthor() {
78
        ArticleDto articleDto = buildArticleDto();
79
        articleDto.setAuthor(null);
80

81
        Article article = mapper.toArticle(articleDto);
82

83
        checkCompareAssertionsWithoutAuthor(articleDto, article);
84
    }
85

86
    @Test
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();
93

94
        mapper.toArticle(source, target);
95

96
        assertThat(target.getTitle(), is(NEW_TITLE));
97
        assertThat(target.getText(), is(OLD_TEXT));
98
    }
99

100
    @Test
101
    void toArticleWithTargetForNull() {
102
        Article target = buildArticle();
103
        final String OLD_TEXT = target.getText();
104

105
        mapper.toArticle(null, target);
106

107
        assertThat(target.getText(), is(OLD_TEXT));
108
    }
109

110
    private Article buildArticle() {
111
        Article article = new Article();
112
        article.setId(123L);
113
        article.setTitle("Some tittle value");
114
        article.setSummary("Some summary value");
115
        article.setText("Some text");
116

117
        Author author = new Author();
118
        author.setId(321L);
119
        author.setFirstName("John");
120
        author.setLastName("Deer");
121

122
        article.setAuthor(author);
123
        article.setDateCreated(LocalDateTime.of(1980, 9, 21, 0, 0));
124
        article.setDateUpdated(LocalDateTime.of(2011, 3, 5, 0, 0));
125
        return article;
126
    }
127

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");
134

135
        AuthorDto authorDto = new AuthorDto();
136
        authorDto.setId(321L);
137
        authorDto.setFirstName("John");
138
        authorDto.setLastName("Deer");
139

140
        articleDto.setAuthor(authorDto);
141
        articleDto.setDateCreated(LocalDateTime.of(1980, 9, 21, 0, 0));
142
        articleDto.setDateUpdated(LocalDateTime.of(2011, 3, 5, 0, 0));
143
        return articleDto;
144
    }
145

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()));
151

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()));
157

158
        assertThat("Wrong date created", article.getDateCreated(), is(articleDto.getDateCreated()));
159
        assertThat("Wrong date updated", article.getDateUpdated(), is(articleDto.getDateUpdated()));
160
    }
161

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()));
167

168
        assertThat("Wrong date created", article.getDateCreated(), is(articleDto.getDateCreated()));
169
        assertThat("Wrong date updated", article.getDateUpdated(), is(articleDto.getDateUpdated()));
170
    }
171
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.