Jean-Francois Leveque

https://tree.taiga.io/project/jr-utily-grog-v3/us/169 Mettre en place un solutio…

…n pour re-indexer toute la base dans SolR
...@@ -11,4 +11,10 @@ public interface AccountRepository extends JpaRepository<Account, Integer> { ...@@ -11,4 +11,10 @@ public interface AccountRepository extends JpaRepository<Account, Integer> {
11 * @return Accounts looked for 11 * @return Accounts looked for
12 */ 12 */
13 List<Account> findByUserIdIn(List<Integer> integers); 13 List<Account> findByUserIdIn(List<Integer> integers);
14 +
15 + /**
16 + *
17 + * @return accounts that have a presentation
18 + */
19 + List<Account> findByPresentationIsNotNull();
14 } 20 }
......
...@@ -12,4 +12,10 @@ public interface AccountSearchRepository { ...@@ -12,4 +12,10 @@ public interface AccountSearchRepository {
12 * @return Accounts that match the string 12 * @return Accounts that match the string
13 */ 13 */
14 List<IndexedAccount> search(String string) throws SearchingException; 14 List<IndexedAccount> search(String string) throws SearchingException;
15 +
16 + /**
17 + *
18 + * @param indexedAccounts IndexedAccounts to reindex
19 + */
20 + public void reindex(List<IndexedAccount> indexedAccounts) throws IndexingException;
15 } 21 }
......
...@@ -4,6 +4,7 @@ import org.apache.solr.client.solrj.SolrClient; ...@@ -4,6 +4,7 @@ import org.apache.solr.client.solrj.SolrClient;
4 import org.apache.solr.client.solrj.SolrQuery; 4 import org.apache.solr.client.solrj.SolrQuery;
5 import org.apache.solr.client.solrj.SolrServerException; 5 import org.apache.solr.client.solrj.SolrServerException;
6 import org.apache.solr.client.solrj.response.QueryResponse; 6 import org.apache.solr.client.solrj.response.QueryResponse;
7 +import org.apache.solr.client.solrj.response.UpdateResponse;
7 import org.slf4j.Logger; 8 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
9 10
...@@ -20,6 +21,8 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository { ...@@ -20,6 +21,8 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository {
20 21
21 SolrClient solrClient; 22 SolrClient solrClient;
22 23
24 + protected static String collectionName = "accounts";
25 +
23 @Inject 26 @Inject
24 AccountSearchRepositorySolrj(SolrClient solrClient) { 27 AccountSearchRepositorySolrj(SolrClient solrClient) {
25 this.solrClient = solrClient; 28 this.solrClient = solrClient;
...@@ -34,7 +37,7 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository { ...@@ -34,7 +37,7 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository {
34 SolrQuery solrQuery = new SolrQuery(string); 37 SolrQuery solrQuery = new SolrQuery(string);
35 QueryResponse queryResponse; 38 QueryResponse queryResponse;
36 try { 39 try {
37 - queryResponse = solrClient.query("accounts", solrQuery); 40 + queryResponse = solrClient.query(collectionName, solrQuery);
38 } catch (IOException ioe) { 41 } catch (IOException ioe) {
39 throw new SearchingException(ioe); 42 throw new SearchingException(ioe);
40 } catch (SolrServerException sse) { 43 } catch (SolrServerException sse) {
...@@ -48,4 +51,18 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository { ...@@ -48,4 +51,18 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository {
48 return new ArrayList<>(); 51 return new ArrayList<>();
49 } 52 }
50 } 53 }
54 +
55 + @Override
56 + public void reindex(List<IndexedAccount> indexedAccounts) throws IndexingException {
57 + try {
58 + UpdateResponse updateResponse = solrClient.addBeans(collectionName, indexedAccounts);
59 + solrClient.commit(collectionName);
60 + logger.trace("reindex indexedAccounts SolrJ UpdateResponse {}", updateResponse);
61 + } catch (IOException ioe) {
62 + throw new IndexingException(ioe);
63 + } catch (SolrServerException sse) {
64 + logger.error("SolrServerException {}", sse);
65 + throw new IndexingException(sse.getRootCause());
66 + }
67 + }
51 } 68 }
......
...@@ -22,5 +22,9 @@ public interface PublisherSearchRepository { ...@@ -22,5 +22,9 @@ public interface PublisherSearchRepository {
22 */ 22 */
23 List<IndexedPublisher> search(String string) throws SearchingException; 23 List<IndexedPublisher> search(String string) throws SearchingException;
24 24
25 - public void reindex(List<IndexedPublisher> inxdexedPublishers); 25 + /**
26 + *
27 + * @param inxdexedPublishers IndexedPublishers to reindex
28 + */
29 + public void reindex(List<IndexedPublisher> inxdexedPublishers) throws IndexingException;
26 } 30 }
......
...@@ -21,6 +21,8 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository ...@@ -21,6 +21,8 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
21 21
22 SolrClient solrClient; 22 SolrClient solrClient;
23 23
24 + protected static String collectionName = "publishers";
25 +
24 @Inject 26 @Inject
25 PublisherSearchRepositorySolrj(SolrClient solrClient) { 27 PublisherSearchRepositorySolrj(SolrClient solrClient) {
26 this.solrClient = solrClient; 28 this.solrClient = solrClient;
...@@ -33,7 +35,7 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository ...@@ -33,7 +35,7 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
33 @Override 35 @Override
34 public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException { 36 public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException {
35 try { 37 try {
36 - UpdateResponse updateResponse = solrClient.addBean(indexedPublisher, 1); 38 + UpdateResponse updateResponse = solrClient.addBean(collectionName, indexedPublisher, 1);
37 logger.trace("validatePublisherVersion SolrJ UpdateResponse {}", updateResponse); 39 logger.trace("validatePublisherVersion SolrJ UpdateResponse {}", updateResponse);
38 } catch (IOException ioe) { 40 } catch (IOException ioe) {
39 throw new IndexingException(ioe); 41 throw new IndexingException(ioe);
...@@ -50,7 +52,7 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository ...@@ -50,7 +52,7 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
50 SolrQuery solrQuery = new SolrQuery(string); 52 SolrQuery solrQuery = new SolrQuery(string);
51 QueryResponse queryResponse; 53 QueryResponse queryResponse;
52 try { 54 try {
53 - queryResponse = solrClient.query("publishers", solrQuery); 55 + queryResponse = solrClient.query(collectionName, solrQuery);
54 } catch (IOException ioe) { 56 } catch (IOException ioe) {
55 throw new SearchingException(ioe); 57 throw new SearchingException(ioe);
56 } catch (SolrServerException sse) { 58 } catch (SolrServerException sse) {
...@@ -66,7 +68,16 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository ...@@ -66,7 +68,16 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
66 } 68 }
67 69
68 @Override 70 @Override
69 - public void reindex(List<IndexedPublisher> inxdexedPublishers) { 71 + public void reindex(List<IndexedPublisher> indexedPublishers) throws IndexingException {
70 - 72 + try {
73 + UpdateResponse updateResponse = solrClient.addBeans(collectionName, indexedPublishers);
74 + solrClient.commit(collectionName);
75 + logger.trace("reindex inxdexedPublishers SolrJ UpdateResponse {}", updateResponse);
76 + } catch (IOException ioe) {
77 + throw new IndexingException(ioe);
78 + } catch (SolrServerException sse) {
79 + logger.error("SolrServerException {}", sse);
80 + throw new IndexingException(sse.getRootCause());
81 + }
71 } 82 }
72 } 83 }
......
...@@ -2,6 +2,7 @@ package org.legrog.web.account; ...@@ -2,6 +2,7 @@ package org.legrog.web.account;
2 2
3 import org.legrog.entities.Account; 3 import org.legrog.entities.Account;
4 import org.legrog.entities.IndexedAccount; 4 import org.legrog.entities.IndexedAccount;
5 +import org.legrog.entities.IndexingException;
5 import org.legrog.entities.SearchingException; 6 import org.legrog.entities.SearchingException;
6 7
7 import javax.validation.constraints.NotNull; 8 import javax.validation.constraints.NotNull;
...@@ -30,5 +31,9 @@ public interface AccountService { ...@@ -30,5 +31,9 @@ public interface AccountService {
30 */ 31 */
31 List<Account> convertIndexedAccountsIntoAccounts(List<IndexedAccount> indexedAccounts); 32 List<Account> convertIndexedAccountsIntoAccounts(List<IndexedAccount> indexedAccounts);
32 33
33 - public int reindexAllAccounts(); 34 + /**
35 + *
36 + * @return number of indexed accounts
37 + */
38 + public int reindexAllAccounts() throws IndexingException;
34 } 39 }
......
...@@ -63,8 +63,21 @@ public class AccountServiceDefault implements AccountService { ...@@ -63,8 +63,21 @@ public class AccountServiceDefault implements AccountService {
63 return new ArrayList<>(); 63 return new ArrayList<>();
64 } 64 }
65 65
66 + protected List<IndexedAccount> convertAccountsIntoIndexedAccounts(List<Account> accounts) {
67 + List<IndexedAccount> indexedAccounts = new ArrayList<>(accounts.size());
68 +
69 + accounts.forEach(account -> indexedAccounts.add(new IndexedAccount(account)));
70 +
71 + return indexedAccounts;
72 + }
73 +
66 @Override 74 @Override
67 - public int reindexAllAccounts() { 75 + public int reindexAllAccounts() throws IndexingException {
68 - return 0; 76 + List<Account> accounts = accountRepository.findByPresentationIsNotNull();
77 + List<IndexedAccount> indexedAccounts = convertAccountsIntoIndexedAccounts(accounts);
78 + accountSearchRepository.reindex(indexedAccounts);
79 + return indexedAccounts.size();
69 } 80 }
81 +
82 +
70 } 83 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -89,5 +89,5 @@ public interface PublisherService { ...@@ -89,5 +89,5 @@ public interface PublisherService {
89 * 89 *
90 * @return number of indexed publishers 90 * @return number of indexed publishers
91 */ 91 */
92 - public int reindexAllPublishers(); 92 + public int reindexAllPublishers() throws IndexingException;
93 } 93 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -158,15 +158,19 @@ public class PublisherServiceDefault implements PublisherService { ...@@ -158,15 +158,19 @@ public class PublisherServiceDefault implements PublisherService {
158 } 158 }
159 159
160 protected List<IndexedPublisher> convertPublishersIntoIndexedPublishers(List<Publisher> publishers) { 160 protected List<IndexedPublisher> convertPublishersIntoIndexedPublishers(List<Publisher> publishers) {
161 - return new ArrayList<>(); 161 + List<IndexedPublisher> indexedPublishers = new ArrayList<>(publishers.size());
162 +
163 + publishers.forEach(publisher -> indexedPublishers.add(new IndexedPublisher(publisher)));
164 +
165 + return indexedPublishers;
162 } 166 }
163 167
164 @Override 168 @Override
165 - public int reindexAllPublishers() { 169 + public int reindexAllPublishers() throws IndexingException {
166 List<Publisher> publishers = publisherRepository.findByValidatedVersionIsNotNull(); 170 List<Publisher> publishers = publisherRepository.findByValidatedVersionIsNotNull();
167 - List<IndexedPublisher> indexedPublishers = convertPublishersIntoIndexedPublishers(new ArrayList<>()); 171 + List<IndexedPublisher> indexedPublishers = convertPublishersIntoIndexedPublishers(publishers);
168 publisherSearchRepository.reindex(indexedPublishers); 172 publisherSearchRepository.reindex(indexedPublishers);
169 - return 0; 173 + return indexedPublishers.size();
170 } 174 }
171 175
172 } 176 }
......
1 package org.legrog.web.xyz; 1 package org.legrog.web.xyz;
2 2
3 +import org.legrog.entities.IndexingException;
3 import org.legrog.web.account.AccountService; 4 import org.legrog.web.account.AccountService;
4 import org.legrog.web.publisher.PublisherService; 5 import org.legrog.web.publisher.PublisherService;
5 6
...@@ -29,7 +30,7 @@ public class ReindexView implements Serializable { ...@@ -29,7 +30,7 @@ public class ReindexView implements Serializable {
29 //no args constructor to make it proxyable 30 //no args constructor to make it proxyable
30 } 31 }
31 32
32 - public void reindexAll() { 33 + public void reindexAll() throws IndexingException {
33 indexedPublishersNumber = publisherService.reindexAllPublishers(); 34 indexedPublishersNumber = publisherService.reindexAllPublishers();
34 indexedAccountsNumber = accountService.reindexAllAccounts(); 35 indexedAccountsNumber = accountService.reindexAllAccounts();
35 } 36 }
......
...@@ -4,8 +4,48 @@ ...@@ -4,8 +4,48 @@
4 <html xmlns="http://www.w3.org/1999/xhtml" 4 <html xmlns="http://www.w3.org/1999/xhtml"
5 xmlns:h="http://xmlns.jcp.org/jsf/html" 5 xmlns:h="http://xmlns.jcp.org/jsf/html"
6 xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 6 xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
7 - xmlns:f="http://xmlns.jcp.org/jsf/core"> 7 + xmlns:f="http://xmlns.jcp.org/jsf/core"
8 -<f:view> 8 + xmlns:jsf="http://xmlns.jcp.org/jsf">
9 - <h:outputLabel value="Hello, world"/> 9 +<body>
10 -</f:view> 10 +<ul>
11 + <li>
12 + <a jsf:outcome="/index">Menu principal</a>
13 + </li>
14 + <li>Éditeurs
15 + <ul>
16 + <li>
17 + <a jsf:outcome="/publisher/publisherSearch">Recherche dans les éditeurs</a>
18 + </li>convertPublishersIntoIndexedPublishers
19 + <li>
20 + <a jsf:outcome="/publisher/listPublisherVersions">Liste des versions des éditeurs</a>
21 + </li>
22 + <li>
23 + <a jsf:outcome="/publisher/listPublisherActions">Liste des actions des éditeurs</a>
24 + </li>convertPublishersIntoIndexedPublishers
25 + <li>
26 + <a jsf:outcome="/publisher/publisherVersion">Ajouter un éditeur</a>
27 + </li>
28 + </ul>
29 + </li>
30 + <li>Pays
31 + <ul>
32 + <li>
33 + <a jsf:outcome="listCountries">Liste des pays</a>
34 + </li>
35 + <li>
36 + <a jsf:outcome="addCountry">Ajouter un pays</a>
37 + </li>
38 + </ul>
39 + </li>
40 +</ul>
41 +
42 +<form action="" jsf:id="reindex">
43 + <button jsf:action="#{reindexView.reindexAll}">Réindexer</button>
44 +</form>
45 +
46 +<p jsf:rendered="#{reindexView.indexedPublishersNumber > 0}">Nombre d'éditeurs réindexés : ${reindexView.indexedPublishersNumber}.</p>
47 +
48 +<p jsf:rendered="#{reindexView.indexedAccountsNumber > 0}">Nombre de comptes réindexés : ${reindexView.indexedAccountsNumber}.</p>
49 +
50 +</body>
11 </html> 51 </html>
......
...@@ -54,13 +54,13 @@ public class PublisherSearchRepositorySolrjTest { ...@@ -54,13 +54,13 @@ public class PublisherSearchRepositorySolrjTest {
54 @DisplayName("when called with right parameters, should addBean IndexedPublisher with commitWithinMs of 1 to repository") 54 @DisplayName("when called with right parameters, should addBean IndexedPublisher with commitWithinMs of 1 to repository")
55 public void addBeanTest(@Mock IndexedPublisher indexedPublisher) throws IndexingException, SolrServerException, IOException { 55 public void addBeanTest(@Mock IndexedPublisher indexedPublisher) throws IndexingException, SolrServerException, IOException {
56 publisherSearchRepository.save(indexedPublisher); 56 publisherSearchRepository.save(indexedPublisher);
57 - verify(solrClient).addBean(indexedPublisher, 1); 57 + verify(solrClient).addBean(PublisherSearchRepositorySolrj.collectionName, indexedPublisher, 1);
58 } 58 }
59 59
60 @Test 60 @Test
61 @DisplayName("When repository in IO error, should throw an IndexingException") 61 @DisplayName("When repository in IO error, should throw an IndexingException")
62 public void addBeanIOETest(@Mock IndexedPublisher indexedPublisher) throws SolrServerException, IOException { 62 public void addBeanIOETest(@Mock IndexedPublisher indexedPublisher) throws SolrServerException, IOException {
63 - when(solrClient.addBean(indexedPublisher, 1)).thenThrow(new IOException()); 63 + when(solrClient.addBean(PublisherSearchRepositorySolrj.collectionName, indexedPublisher, 1)).thenThrow(new IOException());
64 Assertions.assertThrows(IndexingException.class, () -> publisherSearchRepository.save(indexedPublisher)); 64 Assertions.assertThrows(IndexingException.class, () -> publisherSearchRepository.save(indexedPublisher));
65 } 65 }
66 66
......
...@@ -5,6 +5,8 @@ import org.junit.jupiter.api.DisplayName; ...@@ -5,6 +5,8 @@ import org.junit.jupiter.api.DisplayName;
5 import org.junit.jupiter.api.Nested; 5 import org.junit.jupiter.api.Nested;
6 import org.junit.jupiter.api.Test; 6 import org.junit.jupiter.api.Test;
7 import org.legrog.entities.*; 7 import org.legrog.entities.*;
8 +import org.mockito.ArgumentCaptor;
9 +import org.mockito.Captor;
8 import org.mockito.Mock; 10 import org.mockito.Mock;
9 import org.mockito.Mockito; 11 import org.mockito.Mockito;
10 12
...@@ -21,11 +23,23 @@ import static org.mockito.Mockito.when; ...@@ -21,11 +23,23 @@ import static org.mockito.Mockito.when;
21 */ 23 */
22 public class AccountServiceDefaultTest { 24 public class AccountServiceDefaultTest {
23 AccountServiceDefault accountServiceDefault; 25 AccountServiceDefault accountServiceDefault;
26 + AccountServiceDefault accountServiceDefaultSpy;
27 +
28 + @Captor
29 + ArgumentCaptor<List <Account>> accountsArgumentCaptor;
30 +
24 31
25 @BeforeEach 32 @BeforeEach
26 public void setUp(@Mock AccountRepository accountRepository, 33 public void setUp(@Mock AccountRepository accountRepository,
27 @Mock AccountSearchRepository accountSearchRepository) throws Exception { 34 @Mock AccountSearchRepository accountSearchRepository) throws Exception {
28 - accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository)); 35 + accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository)
36 + {
37 +
38 + public List<IndexedAccount> convertAccountsIntoIndexedAccounts(List<Account> accounts) {
39 + return new ArrayList<>();
40 + }
41 + }
42 + );
29 } 43 }
30 44
31 @Nested 45 @Nested
...@@ -75,4 +89,47 @@ public class AccountServiceDefaultTest { ...@@ -75,4 +89,47 @@ public class AccountServiceDefaultTest {
75 } 89 }
76 90
77 } 91 }
92 + @Nested
93 + @DisplayName("reindexAllAccounts method")
94 + class ReindexAllAccountsTests {
95 + @DisplayName("When called, should follow the call sequence")
96 + @Test
97 + public void testCorrectCalls(@Mock AccountRepository accountRepository, @Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
98 + accountServiceDefault.reindexAllAccounts();
99 + Mockito.verify(accountRepository).findByPresentationIsNotNull();
100 + verify(accountServiceDefault, times(1)).convertAccountsIntoIndexedAccounts(Mockito.any());
101 + Mockito.verify(accountSearchRepository).reindex(Mockito.any());
102 + }
103 +
104 + @DisplayName("When called, should send accounts it gets from findByPresentationIsNotNull to convertAccountsIntoIndexedAccounts")
105 + @Test
106 + public void testTransmitsAccounts(@Mock AccountRepository accountRepository, @Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
107 + List<Account> accounts = new ArrayList<>();
108 + Account account = new Account();
109 + accounts.add(account);
110 + when(accountRepository.findByPresentationIsNotNull()).thenReturn(accounts);
111 + accountServiceDefault.reindexAllAccounts();
112 + verify(accountServiceDefault, times(1)).convertAccountsIntoIndexedAccounts(accountsArgumentCaptor.capture());
113 + assertThat(accountsArgumentCaptor.getValue()).isEqualTo(accounts);
114 + }
115 +
116 + @DisplayName("When called, should send indexedAccounts it gets from convertAccountsIntoIndexedAccounts to reindex")
117 + @Test
118 + public void testTransmitIndexedAccounts(@Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
119 + List<IndexedAccount> indexedAccounts = new ArrayList<>();
120 + indexedAccounts.add(new IndexedAccount());
121 + when(accountServiceDefaultSpy.convertAccountsIntoIndexedAccounts(Mockito.any())).thenReturn(indexedAccounts);
122 + accountServiceDefaultSpy.reindexAllAccounts();
123 + verify(accountSearchRepository).reindex(indexedAccounts);
124 + }
125 +
126 + @DisplayName("When called, should return the number of indexedAccounts it's sent to indexing")
127 + @Test
128 + public void testReturnCorrectNumber() throws IndexingException {
129 + List<IndexedAccount> indexedAccounts = new ArrayList<>();
130 + indexedAccounts.add(new IndexedAccount());
131 + when(accountServiceDefaultSpy.convertAccountsIntoIndexedAccounts(Mockito.any())).thenReturn(indexedAccounts);
132 + assertThat(accountServiceDefaultSpy.reindexAllAccounts()).isEqualTo(indexedAccounts.size());
133 + }
134 + }
78 } 135 }
......
...@@ -35,11 +35,12 @@ public class PublisherServiceDefaultTest { ...@@ -35,11 +35,12 @@ public class PublisherServiceDefaultTest {
35 Logger logger = LoggerFactory.getLogger(getClass()); 35 Logger logger = LoggerFactory.getLogger(getClass());
36 36
37 PublisherServiceDefault publisherServiceDefault; 37 PublisherServiceDefault publisherServiceDefault;
38 + PublisherServiceDefault publisherServiceDefaultSpy;
39 +
38 PublisherVersion publisherVersion; 40 PublisherVersion publisherVersion;
39 PublisherVersion publisherVersion1; 41 PublisherVersion publisherVersion1;
40 Publisher publisher; 42 Publisher publisher;
41 PublisherRepository publisherRepository; 43 PublisherRepository publisherRepository;
42 - PublisherVersionRepository publisherVersionRepository;
43 44
44 @Mock 45 @Mock
45 PublisherVersion publisherVersionMock; 46 PublisherVersion publisherVersionMock;
...@@ -57,7 +58,15 @@ public class PublisherServiceDefaultTest { ...@@ -57,7 +58,15 @@ public class PublisherServiceDefaultTest {
57 @Mock PublisherSearchRepository publisherSearchRepository, 58 @Mock PublisherSearchRepository publisherSearchRepository,
58 @Mock SharedService sharedService) throws Exception { 59 @Mock SharedService sharedService) throws Exception {
59 publisherServiceDefault = Mockito.spy(new PublisherServiceDefault(publisherRepository, 60 publisherServiceDefault = Mockito.spy(new PublisherServiceDefault(publisherRepository,
60 - publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService)); 61 + publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService)
62 + {
63 +
64 + public List<IndexedPublisher> convertPublishersIntoIndexedPublishers(List<Publisher> publishers) {
65 + return new ArrayList<>();
66 + }
67 + }
68 + );
69 + publisherServiceDefaultSpy = Mockito.spy(publisherServiceDefault);
61 publisherVersion = new PublisherVersion(); 70 publisherVersion = new PublisherVersion();
62 publisherVersion1 = new PublisherVersion(); 71 publisherVersion1 = new PublisherVersion();
63 this.publisherRepository = publisherRepository; 72 this.publisherRepository = publisherRepository;
...@@ -214,7 +223,7 @@ public class PublisherServiceDefaultTest { ...@@ -214,7 +223,7 @@ public class PublisherServiceDefaultTest {
214 class ReindexAllPublishersTests { 223 class ReindexAllPublishersTests {
215 @DisplayName("When called, should follow the call sequence") 224 @DisplayName("When called, should follow the call sequence")
216 @Test 225 @Test
217 - public void testCorrectCalls(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) { 226 + public void testCorrectCalls(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
218 publisherServiceDefault.reindexAllPublishers(); 227 publisherServiceDefault.reindexAllPublishers();
219 Mockito.verify(publisherRepository).findByValidatedVersionIsNotNull(); 228 Mockito.verify(publisherRepository).findByValidatedVersionIsNotNull();
220 verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(Mockito.any()); 229 verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(Mockito.any());
...@@ -223,13 +232,33 @@ public class PublisherServiceDefaultTest { ...@@ -223,13 +232,33 @@ public class PublisherServiceDefaultTest {
223 232
224 @DisplayName("When called, should send publishers it gets from findByValidatedVersionIsNotNull to convertPublishersIntoIndexedPublishers") 233 @DisplayName("When called, should send publishers it gets from findByValidatedVersionIsNotNull to convertPublishersIntoIndexedPublishers")
225 @Test 234 @Test
226 - public void testTransmitsPublishers(@Mock PublisherRepository publisherRepository) { 235 + public void testTransmitsPublishers(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
227 List<Publisher> publishers = new ArrayList<>(); 236 List<Publisher> publishers = new ArrayList<>();
237 + Publisher publisher = new Publisher();
238 + publishers.add(publisher);
228 when(publisherRepository.findByValidatedVersionIsNotNull()).thenReturn(publishers); 239 when(publisherRepository.findByValidatedVersionIsNotNull()).thenReturn(publishers);
229 publisherServiceDefault.reindexAllPublishers(); 240 publisherServiceDefault.reindexAllPublishers();
230 verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(publishersArgumentCaptor.capture()); 241 verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(publishersArgumentCaptor.capture());
231 - // Si reindexAllPublishers fait convertPublishersIntoIndexedPublishers(new ArrayList<>()), je crois que ce test devrait échouer, mais ce n'est pas le cas
232 assertThat(publishersArgumentCaptor.getValue()).isEqualTo(publishers); 242 assertThat(publishersArgumentCaptor.getValue()).isEqualTo(publishers);
233 } 243 }
244 +
245 + @DisplayName("When called, should send indexedPublishers it gets from convertPublishersIntoIndexedPublishers to reindex")
246 + @Test
247 + public void testTransmitIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
248 + List<IndexedPublisher> indexedPublishers = new ArrayList<>();
249 + indexedPublishers.add(new IndexedPublisher());
250 + when(publisherServiceDefaultSpy.convertPublishersIntoIndexedPublishers(Mockito.any())).thenReturn(indexedPublishers);
251 + publisherServiceDefaultSpy.reindexAllPublishers();
252 + verify(publisherSearchRepository).reindex(indexedPublishers);
253 + }
254 +
255 + @DisplayName("When called, should return the number of indexedPublishers it's sent to indexing")
256 + @Test
257 + public void testReturnCorrectNumber() throws IndexingException {
258 + List<IndexedPublisher> indexedPublishers = new ArrayList<>();
259 + indexedPublishers.add(new IndexedPublisher());
260 + when(publisherServiceDefaultSpy.convertPublishersIntoIndexedPublishers(Mockito.any())).thenReturn(indexedPublishers);
261 + assertThat(publisherServiceDefaultSpy.reindexAllPublishers()).isEqualTo(indexedPublishers.size());
262 + }
234 } 263 }
235 } 264 }
......
...@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; ...@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
7 import org.junit.jupiter.api.extension.ExtendWith; 7 import org.junit.jupiter.api.extension.ExtendWith;
8 import org.junit.platform.runner.JUnitPlatform; 8 import org.junit.platform.runner.JUnitPlatform;
9 import org.junit.runner.RunWith; 9 import org.junit.runner.RunWith;
10 +import org.legrog.entities.IndexingException;
10 import org.legrog.test.MockitoExtension; 11 import org.legrog.test.MockitoExtension;
11 import org.legrog.web.account.AccountService; 12 import org.legrog.web.account.AccountService;
12 import org.legrog.web.publisher.PublisherService; 13 import org.legrog.web.publisher.PublisherService;
...@@ -40,7 +41,7 @@ public class ReindexViewTest { ...@@ -40,7 +41,7 @@ public class ReindexViewTest {
40 41
41 @DisplayName("when called, should call for reindexing of publishers and accounts") 42 @DisplayName("when called, should call for reindexing of publishers and accounts")
42 @Test 43 @Test
43 - public void reindexAllTest(@Mock PublisherService publisherService, @Mock AccountService accountService) { 44 + public void reindexAllTest(@Mock PublisherService publisherService, @Mock AccountService accountService) throws IndexingException {
44 reindexView.reindexAll(); 45 reindexView.reindexAll();
45 Mockito.verify(publisherService).reindexAllPublishers(); 46 Mockito.verify(publisherService).reindexAllPublishers();
46 Mockito.verify(accountService).reindexAllAccounts(); 47 Mockito.verify(accountService).reindexAllAccounts();
...@@ -48,7 +49,7 @@ public class ReindexViewTest { ...@@ -48,7 +49,7 @@ public class ReindexViewTest {
48 49
49 @DisplayName("when called, should set its indexed numbers to values returned by reindexers") 50 @DisplayName("when called, should set its indexed numbers to values returned by reindexers")
50 @Test 51 @Test
51 - public void setReturnIntegers(@Mock PublisherService publisherService, @Mock AccountService accountService) { 52 + public void setReturnIntegers(@Mock PublisherService publisherService, @Mock AccountService accountService) throws IndexingException {
52 when(publisherService.reindexAllPublishers()).thenReturn(1); 53 when(publisherService.reindexAllPublishers()).thenReturn(1);
53 when(accountService.reindexAllAccounts()).thenReturn(2); 54 when(accountService.reindexAllAccounts()).thenReturn(2);
54 reindexView.reindexAll(); 55 reindexView.reindexAll();
......