PublisherServiceDefaultTest.java
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package org.legrog.web.publisher;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.entities.*;
import org.legrog.test.MockitoExtension;
import org.legrog.web.xyz.SharedService;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
/**
* Class testing PublisherServiceDefault
*/
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Service layer for all publisher-related data")
public class PublisherServiceDefaultTest {
Logger logger = LoggerFactory.getLogger(getClass());
PublisherServiceDefault publisherServiceDefault;
PublisherServiceDefault publisherServiceDefaultSpy;
PublisherVersion publisherVersion;
PublisherVersion publisherVersion1;
Publisher publisher;
PublisherRepository publisherRepository;
@Mock
PublisherVersion publisherVersionMock;
@Captor
ArgumentCaptor<IndexedPublisher> indexedPublisherArgumentCaptor;
@Captor
ArgumentCaptor<List <Publisher>> publishersArgumentCaptor;
@BeforeEach
public void setUp(@Mock PublisherRepository publisherRepository,
@Mock PublisherVersionRepository publisherVersionRepository,
@Mock PublisherActionRepository publisherActionRepository,
@Mock PublisherSearchRepository publisherSearchRepository,
@Mock SharedService sharedService) throws Exception {
publisherServiceDefault = Mockito.spy(new PublisherServiceDefault(publisherRepository,
publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService)
{
public List<IndexedPublisher> convertPublishersIntoIndexedPublishers(List<Publisher> publishers) {
return new ArrayList<>();
}
}
);
publisherServiceDefaultSpy = Mockito.spy(publisherServiceDefault);
publisherVersion = new PublisherVersion();
publisherVersion1 = new PublisherVersion();
this.publisherRepository = publisherRepository;
}
@Nested
@DisplayName("addNewPublisher method")
class AddNewPublisherTests {
@DisplayName("When a new publisher is added, both Publisher and PublisherVersion should be saved in the right state")
@Test
public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) {
publisherServiceDefault.addNewPublisher(publisherVersion);
publisher = publisherVersion.getPublisher();
assertThat(publisher.getVersions()).containsExactly(publisherVersion);
assertThat(publisherVersion.getPublisher()).isEqualTo(publisher);
verify(publisherRepository).save(publisher);
verify(publisherVersionRepository).save(publisherVersion);
}
}
@Nested
@DisplayName("addVersionToPublisher method")
class AddVersionToPublisherTests {
@DisplayName("When a new version of a publisher is added, setting it up, attaching it to publisher and saving both")
@Test
public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository,
@Mock PublisherVersionRepository publisherVersionRepository) {
publisherServiceDefault.addNewPublisher(publisherVersion);
publisher = publisherVersion.getPublisher();
publisherServiceDefault.addVersionToPublisher(publisher, publisherVersion1);
assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher);
assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1);
verify(publisherRepository, times(2)).save(publisher);
verify(publisherVersionRepository).save(publisherVersion1);
}
}
@Nested
@DisplayName("addVersionToPublisher method")
class ValidatePublisherVersionTests {
@DisplayName("When a PublisherVersion is validated, it should be the right one")
@Test
public void testValidateVersionRight() {
Set<PublisherVersion> publisherVersions;
publisher = new Publisher();
publisherVersion = new PublisherVersion();
publisherVersions = new HashSet<>();
publisherVersions.add(publisherVersion);
publisherVersions.add(publisherVersionMock);
publisher.setVersions(publisherVersions);
when(publisherVersionMock.getPublisher()).thenReturn(publisher);
publisherServiceDefault.validatePublisherVersion(publisherVersionMock);
assertThat(publisher.getValidatedVersion()).isEqualTo(publisherVersionMock);
}
@DisplayName("When a publisherVersion is validated, the indexed IndexedPublisher should have all attributes right")
@Test
public void testValidateIndexPublisher(@Mock PublisherSearchRepository publisherSearchRepository) {
publisher = new Publisher();
publisher.setPublisherId(111);
when(publisherVersionMock.getPublisher()).thenReturn(publisher);
when(publisherVersionMock.getPublisherName()).thenReturn("nom");
when(publisherVersionMock.getPublisherHistory()).thenReturn("histoire");
publisherServiceDefault.validatePublisherVersion(publisherVersionMock);
try {
Mockito.verify(publisherSearchRepository).save(indexedPublisherArgumentCaptor.capture());
} catch (IndexingException e) {
logger.error("IndexingException {}", e);
}
IndexedPublisher indexedPublisher = indexedPublisherArgumentCaptor.getValue();
assertThat(indexedPublisher.getPublisherId()).isEqualTo(111);
assertThat(indexedPublisher.getPublisherName()).isEqualTo("nom");
assertThat(indexedPublisher.getPublisherHistory()).isEqualTo("histoire");
}
@DisplayName("When a validate creates a PublisherAction it should be saved and have all attributes right")
@Test
public void testValidateCreateAction(@Mock PublisherActionRepository publisherActionRepository) {
PublisherAction publisherAction;
ActionType actionType = ActionType.VALIDATE;
publisher = new Publisher();
when(publisherVersionMock.getPublisher()).thenReturn(publisher);
publisherAction = publisherServiceDefault.validatePublisherVersion(publisherVersionMock);
verify(publisherActionRepository).save(publisherAction);
assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock);
assertThat(publisherAction.getPublisher()).isEqualTo(publisher);
assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock);
assertThat(publisherAction.getActionType()).isEqualTo(actionType);
}
}
@Nested
@DisplayName("search method")
class SearchTests {
@DisplayName("When called, should delegate search to PublisherSearchRepository")
@Test
public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
publisherServiceDefault.search("3");
Mockito.verify(publisherSearchRepository).search("3");
}
@DisplayName("When getting IndexedPublishers from search, should convertIndexedAccountsIntoAccounts them")
@Test
public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
when(publisherSearchRepository.search("4")).thenReturn(indexedPublishers);
publisherServiceDefault.search("4");
verify(publisherServiceDefault, times(1)).convertIndexedPublishersIntoPublisherVersions(indexedPublishers);
}
@DisplayName("When called, should return the PublisherVersions it gets from convertIndexedAccountsIntoAccounts")
@Test
public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
List<PublisherVersion> publisherVersions = new ArrayList<>();
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
Mockito.doReturn(publisherVersions).when(publisherServiceDefault).convertIndexedPublishersIntoPublisherVersions(indexedPublishers);
when(publisherSearchRepository.search("5")).thenReturn(indexedPublishers);
assertThat(publisherServiceDefault.search("5")).isEqualTo(publisherVersions);
}
}
@Nested
@DisplayName("convertIndexedAccountsIntoAccounts method")
class ConvertTests {
@DisplayName("When called, should return the PublisherVersions it gets from findByPublisherVersionIdIn")
@Test
public void testReturnFromFind(@Mock PublisherVersionRepository publisherVersionRepository) {
List<PublisherVersion> publisherVersions = new ArrayList<>();
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
when(publisherVersionRepository.findByPublisherVersionIdIn(Mockito.any())).thenReturn(publisherVersions);
assertThat(publisherServiceDefault.convertIndexedPublishersIntoPublisherVersions(indexedPublishers)).isEqualTo(publisherVersions);
}
}
@Nested
@DisplayName("reindexAllPublishers method")
class ReindexAllPublishersTests {
@DisplayName("When called, should follow the call sequence")
@Test
public void testCorrectCalls(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
publisherServiceDefault.reindexAllPublishers();
Mockito.verify(publisherRepository).findByValidatedVersionIsNotNull();
verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(Mockito.any());
Mockito.verify(publisherSearchRepository).reindex(Mockito.any());
}
@DisplayName("When called, should send publishers it gets from findByValidatedVersionIsNotNull to convertPublishersIntoIndexedPublishers")
@Test
public void testTransmitsPublishers(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
List<Publisher> publishers = new ArrayList<>();
Publisher publisher = new Publisher();
publishers.add(publisher);
when(publisherRepository.findByValidatedVersionIsNotNull()).thenReturn(publishers);
publisherServiceDefault.reindexAllPublishers();
verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(publishersArgumentCaptor.capture());
assertThat(publishersArgumentCaptor.getValue()).isEqualTo(publishers);
}
@DisplayName("When called, should send indexedPublishers it gets from convertPublishersIntoIndexedPublishers to reindex")
@Test
public void testTransmitIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
indexedPublishers.add(new IndexedPublisher());
when(publisherServiceDefaultSpy.convertPublishersIntoIndexedPublishers(Mockito.any())).thenReturn(indexedPublishers);
publisherServiceDefaultSpy.reindexAllPublishers();
verify(publisherSearchRepository).reindex(indexedPublishers);
}
@DisplayName("When called, should return the number of indexedPublishers it's sent to indexing")
@Test
public void testReturnCorrectNumber() throws IndexingException {
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
indexedPublishers.add(new IndexedPublisher());
when(publisherServiceDefaultSpy.convertPublishersIntoIndexedPublishers(Mockito.any())).thenReturn(indexedPublishers);
assertThat(publisherServiceDefaultSpy.reindexAllPublishers()).isEqualTo(indexedPublishers.size());
}
}
}