|
1
|
|
|
package com.dawn.jat.illuminati.post.service; |
|
2
|
|
|
|
|
3
|
|
|
import static org.hamcrest.CoreMatchers.is; |
|
4
|
|
|
import static org.hamcrest.MatcherAssert.assertThat; |
|
5
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
6
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
7
|
|
|
|
|
8
|
|
|
import com.dawn.jat.illuminati.core.convert.Converter; |
|
9
|
|
|
import com.dawn.jat.illuminati.post.dto.PostDto; |
|
10
|
|
|
import com.dawn.jat.illuminati.post.entity.PostEntity; |
|
11
|
|
|
import com.dawn.jat.illuminati.post.entity.PostSummaryEntity; |
|
12
|
|
|
import com.dawn.jat.illuminati.post.exception.PostNotFoundException; |
|
13
|
|
|
import com.dawn.jat.illuminati.post.repository.PostRepository; |
|
14
|
|
|
import com.dawn.jat.illuminati.post.repository.PostSummaryRepository; |
|
15
|
|
|
|
|
16
|
|
|
import java.util.ArrayList; |
|
17
|
|
|
import java.util.Arrays; |
|
18
|
|
|
import java.util.HashMap; |
|
19
|
|
|
import java.util.List; |
|
20
|
|
|
import java.util.Optional; |
|
21
|
|
|
|
|
22
|
|
|
import org.junit.jupiter.api.Assertions; |
|
23
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
24
|
|
|
import org.junit.jupiter.api.Test; |
|
25
|
|
|
import org.junit.jupiter.api.extension.ExtendWith; |
|
26
|
|
|
import org.mockito.InjectMocks; |
|
27
|
|
|
import org.mockito.Mock; |
|
28
|
|
|
import org.mockito.Mockito; |
|
29
|
|
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
30
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
@ExtendWith(MockitoExtension.class) |
|
34
|
|
|
@SpringBootTest |
|
35
|
|
|
public class PostServiceTest { |
|
36
|
|
|
private static PostEntity postEntity; |
|
37
|
|
|
private static PostDto postDto; |
|
38
|
|
|
private static PostSummaryEntity postSummaryEntity; |
|
39
|
|
|
|
|
40
|
|
|
@Mock |
|
41
|
|
|
PostRepository postRepository; |
|
42
|
|
|
|
|
43
|
|
|
@Mock |
|
44
|
|
|
PostSummaryRepository postSummaryRepository; |
|
45
|
|
|
|
|
46
|
|
|
@Mock |
|
47
|
|
|
Converter converter; |
|
48
|
|
|
|
|
49
|
|
|
@InjectMocks |
|
50
|
|
|
PostService postService; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initializing postEntity object. |
|
54
|
|
|
*/ |
|
55
|
|
|
@BeforeAll |
|
56
|
|
|
public static void init() { |
|
57
|
|
|
postEntity = new PostEntity("how-to-apply-agile-methodology", |
|
58
|
|
|
"How to apply Agile methodology", |
|
59
|
|
|
"Guide", |
|
60
|
|
|
"01/01/2020", |
|
61
|
|
|
new ArrayList<>(Arrays.asList("Agile")), |
|
62
|
|
|
"Phat Ho"); |
|
63
|
|
|
HashMap tags = new HashMap(); |
|
64
|
|
|
tags.put("System Design", Boolean.TRUE); |
|
65
|
|
|
tags.put("OOP", Boolean.TRUE); |
|
66
|
|
|
postDto = new PostDto("5e80afe11de7a40da7f97052", |
|
67
|
|
|
"how-to-apply-agile-methodology", |
|
68
|
|
|
"How to apply Agile methodology new", |
|
69
|
|
|
"How to apply Agile methodology new", |
|
70
|
|
|
"01/01/2020 new", |
|
71
|
|
|
"Li Li new", |
|
72
|
|
|
"new content", |
|
73
|
|
|
tags); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
@Test |
|
77
|
|
|
public void findPostSummary_whenNoRecord() { |
|
78
|
|
|
Mockito.when(postSummaryRepository.findAll()).thenReturn(Arrays.asList()); |
|
79
|
|
|
assertThat(postService.findPostSummary().size(), is(0)); |
|
80
|
|
|
Mockito.verify(postSummaryRepository, Mockito.times(1)).findAll(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
@Test |
|
84
|
|
|
public void findPostSummary_whenRecord() { |
|
85
|
|
|
List mockPostEntities = Arrays.asList(postSummaryEntity); |
|
86
|
|
|
|
|
87
|
|
|
Mockito.when(postSummaryRepository.findAll()).thenReturn(mockPostEntities); |
|
88
|
|
|
assertThat(postService.findPostSummary().size(), is(1)); |
|
89
|
|
|
assertThat(postService.findPostSummary().get(0), is(postSummaryEntity)); |
|
90
|
|
|
Mockito.verify(postSummaryRepository, Mockito.times(2)).findAll(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
@Test |
|
94
|
|
|
public void findPostBySlug() { |
|
95
|
|
|
String mockSlug = postEntity.getSlug(); |
|
96
|
|
|
Optional<PostEntity> mockPostEntities = Optional.of(postEntity); |
|
97
|
|
|
|
|
98
|
|
|
Mockito.when(postRepository.findBySlug(mockSlug)).thenReturn(mockPostEntities); |
|
99
|
|
|
assertThat(postService.findBySlug(mockSlug), is(mockPostEntities)); |
|
100
|
|
|
Mockito.verify(postRepository, Mockito.times(1)).findBySlug(mockSlug); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
@Test |
|
104
|
|
|
void deletePostBySlug() { |
|
105
|
|
|
String mockSlug = postEntity.getSlug(); |
|
106
|
|
|
|
|
107
|
|
|
postService.deleteBySlug(mockSlug); |
|
108
|
|
|
Mockito.verify(postRepository, Mockito.times(1)).deleteBySlug(mockSlug); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
@Test |
|
112
|
|
|
void create() { |
|
113
|
|
|
Mockito.when(converter.convertPostDtoToEntity(eq(postDto), any(PostEntity.class))) |
|
114
|
|
|
.thenReturn(postEntity); |
|
115
|
|
|
Mockito.when(postRepository.savePost(any(PostEntity.class))) |
|
116
|
|
|
.thenReturn(postEntity); |
|
117
|
|
|
assertThat(postService.create(postDto), is(postEntity)); |
|
118
|
|
|
Mockito.verify(postRepository, Mockito.times(1)).savePost(postEntity); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
@Test |
|
122
|
|
|
void savePost_WithDto_SuccessfullySaveDto() { |
|
123
|
|
|
|
|
124
|
|
|
PostEntity expectedPost = new PostEntity("how-to-apply-agile-methodology", |
|
125
|
|
|
"How to apply Agile methodology new", |
|
126
|
|
|
"How to apply Agile methodology new", |
|
127
|
|
|
"01/01/2020 new", |
|
128
|
|
|
new ArrayList<>(Arrays.asList("System Design", "OOP")), |
|
129
|
|
|
"Li Li new"); |
|
130
|
|
|
expectedPost.setContent("new content"); |
|
131
|
|
|
Mockito.when(postRepository.findById(postDto.getId())) |
|
132
|
|
|
.thenReturn(Optional.of(postEntity)); |
|
133
|
|
|
Mockito.when(postRepository.findById(postDto.getId())) |
|
134
|
|
|
.thenReturn(Optional.of(postEntity)); |
|
135
|
|
|
|
|
136
|
|
|
postService.save(postDto); |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
Mockito.verify(postRepository, Mockito.times(1)).findById(postDto.getId()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
@Test |
|
143
|
|
|
void savePost_WithDto_ThrowPostNotFoundException() { |
|
144
|
|
|
PostEntity expectedPost = new PostEntity("how-to-apply-agile-methodology", |
|
145
|
|
|
"How to apply Agile methodology new", |
|
146
|
|
|
"How to apply Agile methodology new", |
|
147
|
|
|
"01/01/2020 new", |
|
148
|
|
|
new ArrayList<>(Arrays.asList("System Design", "OOP")), |
|
149
|
|
|
"Li Li new"); |
|
150
|
|
|
expectedPost.setContent("new content"); |
|
151
|
|
|
Mockito.when(postRepository.findById(postDto.getId())) |
|
152
|
|
|
.thenReturn(Optional.empty()); |
|
153
|
|
|
|
|
154
|
|
|
Assertions.assertThrows(PostNotFoundException.class, () -> { |
|
155
|
|
|
postService.save(postDto); |
|
156
|
|
|
}); |
|
157
|
|
|
} |
|
158
|
|
|
} |