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> {
* @return Accounts looked for
*/
List<Account> findByUserIdIn(List<Integer> integers);
/**
*
* @return accounts that have a presentation
*/
List<Account> findByPresentationIsNotNull();
}
......
......@@ -12,4 +12,10 @@ public interface AccountSearchRepository {
* @return Accounts that match the string
*/
List<IndexedAccount> search(String string) throws SearchingException;
/**
*
* @param indexedAccounts IndexedAccounts to reindex
*/
public void reindex(List<IndexedAccount> indexedAccounts) throws IndexingException;
}
......
......@@ -4,6 +4,7 @@ import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -20,6 +21,8 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository {
SolrClient solrClient;
protected static String collectionName = "accounts";
@Inject
AccountSearchRepositorySolrj(SolrClient solrClient) {
this.solrClient = solrClient;
......@@ -34,7 +37,7 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository {
SolrQuery solrQuery = new SolrQuery(string);
QueryResponse queryResponse;
try {
queryResponse = solrClient.query("accounts", solrQuery);
queryResponse = solrClient.query(collectionName, solrQuery);
} catch (IOException ioe) {
throw new SearchingException(ioe);
} catch (SolrServerException sse) {
......@@ -48,4 +51,18 @@ public class AccountSearchRepositorySolrj implements AccountSearchRepository {
return new ArrayList<>();
}
}
@Override
public void reindex(List<IndexedAccount> indexedAccounts) throws IndexingException {
try {
UpdateResponse updateResponse = solrClient.addBeans(collectionName, indexedAccounts);
solrClient.commit(collectionName);
logger.trace("reindex indexedAccounts SolrJ UpdateResponse {}", updateResponse);
} catch (IOException ioe) {
throw new IndexingException(ioe);
} catch (SolrServerException sse) {
logger.error("SolrServerException {}", sse);
throw new IndexingException(sse.getRootCause());
}
}
}
......
......@@ -22,5 +22,9 @@ public interface PublisherSearchRepository {
*/
List<IndexedPublisher> search(String string) throws SearchingException;
public void reindex(List<IndexedPublisher> inxdexedPublishers);
/**
*
* @param inxdexedPublishers IndexedPublishers to reindex
*/
public void reindex(List<IndexedPublisher> inxdexedPublishers) throws IndexingException;
}
......
......@@ -21,6 +21,8 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
SolrClient solrClient;
protected static String collectionName = "publishers";
@Inject
PublisherSearchRepositorySolrj(SolrClient solrClient) {
this.solrClient = solrClient;
......@@ -33,7 +35,7 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
@Override
public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException {
try {
UpdateResponse updateResponse = solrClient.addBean(indexedPublisher, 1);
UpdateResponse updateResponse = solrClient.addBean(collectionName, indexedPublisher, 1);
logger.trace("validatePublisherVersion SolrJ UpdateResponse {}", updateResponse);
} catch (IOException ioe) {
throw new IndexingException(ioe);
......@@ -50,7 +52,7 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
SolrQuery solrQuery = new SolrQuery(string);
QueryResponse queryResponse;
try {
queryResponse = solrClient.query("publishers", solrQuery);
queryResponse = solrClient.query(collectionName, solrQuery);
} catch (IOException ioe) {
throw new SearchingException(ioe);
} catch (SolrServerException sse) {
......@@ -66,7 +68,16 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
}
@Override
public void reindex(List<IndexedPublisher> inxdexedPublishers) {
public void reindex(List<IndexedPublisher> indexedPublishers) throws IndexingException {
try {
UpdateResponse updateResponse = solrClient.addBeans(collectionName, indexedPublishers);
solrClient.commit(collectionName);
logger.trace("reindex inxdexedPublishers SolrJ UpdateResponse {}", updateResponse);
} catch (IOException ioe) {
throw new IndexingException(ioe);
} catch (SolrServerException sse) {
logger.error("SolrServerException {}", sse);
throw new IndexingException(sse.getRootCause());
}
}
}
......
......@@ -2,6 +2,7 @@ package org.legrog.web.account;
import org.legrog.entities.Account;
import org.legrog.entities.IndexedAccount;
import org.legrog.entities.IndexingException;
import org.legrog.entities.SearchingException;
import javax.validation.constraints.NotNull;
......@@ -30,5 +31,9 @@ public interface AccountService {
*/
List<Account> convertIndexedAccountsIntoAccounts(List<IndexedAccount> indexedAccounts);
public int reindexAllAccounts();
/**
*
* @return number of indexed accounts
*/
public int reindexAllAccounts() throws IndexingException;
}
......
......@@ -63,8 +63,21 @@ public class AccountServiceDefault implements AccountService {
return new ArrayList<>();
}
protected List<IndexedAccount> convertAccountsIntoIndexedAccounts(List<Account> accounts) {
List<IndexedAccount> indexedAccounts = new ArrayList<>(accounts.size());
accounts.forEach(account -> indexedAccounts.add(new IndexedAccount(account)));
return indexedAccounts;
}
@Override
public int reindexAllAccounts() {
return 0;
public int reindexAllAccounts() throws IndexingException {
List<Account> accounts = accountRepository.findByPresentationIsNotNull();
List<IndexedAccount> indexedAccounts = convertAccountsIntoIndexedAccounts(accounts);
accountSearchRepository.reindex(indexedAccounts);
return indexedAccounts.size();
}
}
\ No newline at end of file
......
......@@ -89,5 +89,5 @@ public interface PublisherService {
*
* @return number of indexed publishers
*/
public int reindexAllPublishers();
public int reindexAllPublishers() throws IndexingException;
}
\ No newline at end of file
......
......@@ -158,15 +158,19 @@ public class PublisherServiceDefault implements PublisherService {
}
protected List<IndexedPublisher> convertPublishersIntoIndexedPublishers(List<Publisher> publishers) {
return new ArrayList<>();
List<IndexedPublisher> indexedPublishers = new ArrayList<>(publishers.size());
publishers.forEach(publisher -> indexedPublishers.add(new IndexedPublisher(publisher)));
return indexedPublishers;
}
@Override
public int reindexAllPublishers() {
public int reindexAllPublishers() throws IndexingException {
List<Publisher> publishers = publisherRepository.findByValidatedVersionIsNotNull();
List<IndexedPublisher> indexedPublishers = convertPublishersIntoIndexedPublishers(new ArrayList<>());
List<IndexedPublisher> indexedPublishers = convertPublishersIntoIndexedPublishers(publishers);
publisherSearchRepository.reindex(indexedPublishers);
return 0;
return indexedPublishers.size();
}
}
......
package org.legrog.web.xyz;
import org.legrog.entities.IndexingException;
import org.legrog.web.account.AccountService;
import org.legrog.web.publisher.PublisherService;
......@@ -29,7 +30,7 @@ public class ReindexView implements Serializable {
//no args constructor to make it proxyable
}
public void reindexAll() {
public void reindexAll() throws IndexingException {
indexedPublishersNumber = publisherService.reindexAllPublishers();
indexedAccountsNumber = accountService.reindexAllAccounts();
}
......
......@@ -4,8 +4,48 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:outputLabel value="Hello, world"/>
</f:view>
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<body>
<ul>
<li>
<a jsf:outcome="/index">Menu principal</a>
</li>
<li>Éditeurs
<ul>
<li>
<a jsf:outcome="/publisher/publisherSearch">Recherche dans les éditeurs</a>
</li>convertPublishersIntoIndexedPublishers
<li>
<a jsf:outcome="/publisher/listPublisherVersions">Liste des versions des éditeurs</a>
</li>
<li>
<a jsf:outcome="/publisher/listPublisherActions">Liste des actions des éditeurs</a>
</li>convertPublishersIntoIndexedPublishers
<li>
<a jsf:outcome="/publisher/publisherVersion">Ajouter un éditeur</a>
</li>
</ul>
</li>
<li>Pays
<ul>
<li>
<a jsf:outcome="listCountries">Liste des pays</a>
</li>
<li>
<a jsf:outcome="addCountry">Ajouter un pays</a>
</li>
</ul>
</li>
</ul>
<form action="" jsf:id="reindex">
<button jsf:action="#{reindexView.reindexAll}">Réindexer</button>
</form>
<p jsf:rendered="#{reindexView.indexedPublishersNumber > 0}">Nombre d'éditeurs réindexés : ${reindexView.indexedPublishersNumber}.</p>
<p jsf:rendered="#{reindexView.indexedAccountsNumber > 0}">Nombre de comptes réindexés : ${reindexView.indexedAccountsNumber}.</p>
</body>
</html>
......
......@@ -54,13 +54,13 @@ public class PublisherSearchRepositorySolrjTest {
@DisplayName("when called with right parameters, should addBean IndexedPublisher with commitWithinMs of 1 to repository")
public void addBeanTest(@Mock IndexedPublisher indexedPublisher) throws IndexingException, SolrServerException, IOException {
publisherSearchRepository.save(indexedPublisher);
verify(solrClient).addBean(indexedPublisher, 1);
verify(solrClient).addBean(PublisherSearchRepositorySolrj.collectionName, indexedPublisher, 1);
}
@Test
@DisplayName("When repository in IO error, should throw an IndexingException")
public void addBeanIOETest(@Mock IndexedPublisher indexedPublisher) throws SolrServerException, IOException {
when(solrClient.addBean(indexedPublisher, 1)).thenThrow(new IOException());
when(solrClient.addBean(PublisherSearchRepositorySolrj.collectionName, indexedPublisher, 1)).thenThrow(new IOException());
Assertions.assertThrows(IndexingException.class, () -> publisherSearchRepository.save(indexedPublisher));
}
......
......@@ -5,6 +5,8 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.legrog.entities.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
......@@ -21,11 +23,23 @@ import static org.mockito.Mockito.when;
*/
public class AccountServiceDefaultTest {
AccountServiceDefault accountServiceDefault;
AccountServiceDefault accountServiceDefaultSpy;
@Captor
ArgumentCaptor<List <Account>> accountsArgumentCaptor;
@BeforeEach
public void setUp(@Mock AccountRepository accountRepository,
@Mock AccountSearchRepository accountSearchRepository) throws Exception {
accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository));
accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository)
{
public List<IndexedAccount> convertAccountsIntoIndexedAccounts(List<Account> accounts) {
return new ArrayList<>();
}
}
);
}
@Nested
......@@ -75,4 +89,47 @@ public class AccountServiceDefaultTest {
}
}
@Nested
@DisplayName("reindexAllAccounts method")
class ReindexAllAccountsTests {
@DisplayName("When called, should follow the call sequence")
@Test
public void testCorrectCalls(@Mock AccountRepository accountRepository, @Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
accountServiceDefault.reindexAllAccounts();
Mockito.verify(accountRepository).findByPresentationIsNotNull();
verify(accountServiceDefault, times(1)).convertAccountsIntoIndexedAccounts(Mockito.any());
Mockito.verify(accountSearchRepository).reindex(Mockito.any());
}
@DisplayName("When called, should send accounts it gets from findByPresentationIsNotNull to convertAccountsIntoIndexedAccounts")
@Test
public void testTransmitsAccounts(@Mock AccountRepository accountRepository, @Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
List<Account> accounts = new ArrayList<>();
Account account = new Account();
accounts.add(account);
when(accountRepository.findByPresentationIsNotNull()).thenReturn(accounts);
accountServiceDefault.reindexAllAccounts();
verify(accountServiceDefault, times(1)).convertAccountsIntoIndexedAccounts(accountsArgumentCaptor.capture());
assertThat(accountsArgumentCaptor.getValue()).isEqualTo(accounts);
}
@DisplayName("When called, should send indexedAccounts it gets from convertAccountsIntoIndexedAccounts to reindex")
@Test
public void testTransmitIndexedAccounts(@Mock AccountSearchRepository accountSearchRepository) throws IndexingException {
List<IndexedAccount> indexedAccounts = new ArrayList<>();
indexedAccounts.add(new IndexedAccount());
when(accountServiceDefaultSpy.convertAccountsIntoIndexedAccounts(Mockito.any())).thenReturn(indexedAccounts);
accountServiceDefaultSpy.reindexAllAccounts();
verify(accountSearchRepository).reindex(indexedAccounts);
}
@DisplayName("When called, should return the number of indexedAccounts it's sent to indexing")
@Test
public void testReturnCorrectNumber() throws IndexingException {
List<IndexedAccount> indexedAccounts = new ArrayList<>();
indexedAccounts.add(new IndexedAccount());
when(accountServiceDefaultSpy.convertAccountsIntoIndexedAccounts(Mockito.any())).thenReturn(indexedAccounts);
assertThat(accountServiceDefaultSpy.reindexAllAccounts()).isEqualTo(indexedAccounts.size());
}
}
}
......
......@@ -35,11 +35,12 @@ public class PublisherServiceDefaultTest {
Logger logger = LoggerFactory.getLogger(getClass());
PublisherServiceDefault publisherServiceDefault;
PublisherServiceDefault publisherServiceDefaultSpy;
PublisherVersion publisherVersion;
PublisherVersion publisherVersion1;
Publisher publisher;
PublisherRepository publisherRepository;
PublisherVersionRepository publisherVersionRepository;
@Mock
PublisherVersion publisherVersionMock;
......@@ -57,7 +58,15 @@ public class PublisherServiceDefaultTest {
@Mock PublisherSearchRepository publisherSearchRepository,
@Mock SharedService sharedService) throws Exception {
publisherServiceDefault = Mockito.spy(new PublisherServiceDefault(publisherRepository,
publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService));
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;
......@@ -214,7 +223,7 @@ public class PublisherServiceDefaultTest {
class ReindexAllPublishersTests {
@DisplayName("When called, should follow the call sequence")
@Test
public void testCorrectCalls(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) {
public void testCorrectCalls(@Mock PublisherRepository publisherRepository, @Mock PublisherSearchRepository publisherSearchRepository) throws IndexingException {
publisherServiceDefault.reindexAllPublishers();
Mockito.verify(publisherRepository).findByValidatedVersionIsNotNull();
verify(publisherServiceDefault, times(1)).convertPublishersIntoIndexedPublishers(Mockito.any());
......@@ -223,13 +232,33 @@ public class PublisherServiceDefaultTest {
@DisplayName("When called, should send publishers it gets from findByValidatedVersionIsNotNull to convertPublishersIntoIndexedPublishers")
@Test
public void testTransmitsPublishers(@Mock PublisherRepository publisherRepository) {
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());
// Si reindexAllPublishers fait convertPublishersIntoIndexedPublishers(new ArrayList<>()), je crois que ce test devrait échouer, mais ce n'est pas le cas
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());
}
}
}
......
......@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.entities.IndexingException;
import org.legrog.test.MockitoExtension;
import org.legrog.web.account.AccountService;
import org.legrog.web.publisher.PublisherService;
......@@ -40,7 +41,7 @@ public class ReindexViewTest {
@DisplayName("when called, should call for reindexing of publishers and accounts")
@Test
public void reindexAllTest(@Mock PublisherService publisherService, @Mock AccountService accountService) {
public void reindexAllTest(@Mock PublisherService publisherService, @Mock AccountService accountService) throws IndexingException {
reindexView.reindexAll();
Mockito.verify(publisherService).reindexAllPublishers();
Mockito.verify(accountService).reindexAllAccounts();
......@@ -48,7 +49,7 @@ public class ReindexViewTest {
@DisplayName("when called, should set its indexed numbers to values returned by reindexers")
@Test
public void setReturnIntegers(@Mock PublisherService publisherService, @Mock AccountService accountService) {
public void setReturnIntegers(@Mock PublisherService publisherService, @Mock AccountService accountService) throws IndexingException {
when(publisherService.reindexAllPublishers()).thenReturn(1);
when(accountService.reindexAllAccounts()).thenReturn(2);
reindexView.reindexAll();
......