Jean-Francois Leveque

ImplementationTDD de recherche (hors page recherche)

......@@ -20,5 +20,5 @@ public interface PublisherSearchRepository {
* @param string String looked for in IndexedPublishers
* @return list of matching IndexedPublishers
*/
public List<IndexedPublisher> search(String string);
public List<IndexedPublisher> search(String string) throws SearchingException;
}
......
package org.legrog.entities;
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;
......@@ -43,7 +45,22 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
}
@Override
public List<IndexedPublisher> search(String string) {
return null;
public List<IndexedPublisher> search(String string) throws SearchingException {
SolrQuery solrQuery = new SolrQuery(string);
QueryResponse queryResponse;
try {
queryResponse = solrClient.query(solrQuery);
} catch (IOException ioe) {
throw new SearchingException(ioe);
} catch (SolrServerException sse) {
logger.error("SolrServerException {}", sse);
throw new SearchingException(sse.getRootCause());
}
if (queryResponse != null) {
return queryResponse.getBeans(IndexedPublisher.class);
} else {
return null;
}
}
}
......
......@@ -2,5 +2,8 @@ package org.legrog.entities;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface PublisherVersionRepository extends JpaRepository<PublisherVersion, Integer> {
List<PublisherVersion> findByPublisherVersionIdIn(List<Integer> integers);
}
......
package org.legrog.entities;
/**
* Exception when searching fails, whatever the reason. Has to be dealt with at service level.
*/
public class SearchingException extends Exception {
final Throwable rootCause;
SearchingException(Throwable rootCause) {
this.rootCause = rootCause;
}
public Throwable getRootCause() {
return rootCause;
}
}
\ No newline at end of file
package org.legrog.web.publisher;
import org.legrog.entities.PublisherVersion;
import org.legrog.entities.SearchingException;
import javax.inject.Inject;
import java.util.List;
......@@ -26,7 +27,7 @@ public class PublisherSearchView {
//no args constructor to make it proxyable
}
public List<PublisherVersion> search(String string) {
public List<PublisherVersion> search(String string) throws SearchingException{
return publisherService.search(string);
}
}
......
package org.legrog.web.publisher;
import org.legrog.entities.IndexedPublisher;
import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherAction;
import org.legrog.entities.PublisherVersion;
import org.legrog.entities.*;
import javax.validation.constraints.NotNull;
import java.util.List;
......@@ -79,7 +76,7 @@ public interface PublisherService {
* @param string String searched indexed publishers
* @return indexed publisher that matches the String
*/
List<PublisherVersion> search(@NotNull String string);
List<PublisherVersion> search(@NotNull String string) throws SearchingException;
/**
*
......
......@@ -10,10 +10,7 @@ import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
* Implémentation de l'interface PublisherService
......@@ -139,13 +136,16 @@ public class PublisherServiceDefault implements PublisherService {
}
@Override
public List<PublisherVersion> search(@NotNull String string) {
public List<PublisherVersion> search(@NotNull String string) throws SearchingException {
return convert(publisherSearchRepository.search(string));
}
@Override
public List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers) {
return null;
List<Integer> integers = new ArrayList<>(indexedPublishers.size());
indexedPublishers.forEach(indexedPublisher -> integers.add(indexedPublisher.getPublisherId()));
return publisherVersionRepository.findByPublisherVersionIdIn(integers);
}
}
......
package org.legrog.entities;
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.beans.BindingException;
import org.apache.solr.common.params.SolrParams;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.test.MockitoExtension;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -19,8 +24,8 @@ import static org.mockito.Mockito.when;
import static org.assertj.core.api.Assertions.assertThat;
/*
Classe testant PublisherSearchRepositorySolrj
/**
* Classe testant PublisherSearchRepositorySolrj
*/
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
......@@ -32,6 +37,9 @@ public class PublisherSearchRepositorySolrjTest {
private PublisherSearchRepository publisherSearchRepository;
private SolrClient solrClient;
@Captor
ArgumentCaptor<SolrParams> solrParamsArgumentCaptor;
@BeforeEach
public void setup(@Mock SolrClient solrClient) {
publisherSearchRepository = new PublisherSearchRepositorySolrj(solrClient);
......@@ -69,4 +77,43 @@ public class PublisherSearchRepositorySolrjTest {
}
}
@Nested
@DisplayName("search method")
class SearchTests {
@DisplayName("when repository in IO error, should throw a SearchingException")
@Test
public void searchIOETest() throws IOException, SolrServerException{
when(solrClient.query(Mockito.any())).thenThrow(new IOException());
Assertions.assertThrows(SearchingException.class, () -> publisherSearchRepository.search("a"));
}
@DisplayName("when repository in SolrServerException, should throw an SearchingException with its root cause")
@Test
public void searchSSETest() throws SolrServerException, IOException {
when(solrClient.query(Mockito.any())).thenThrow(new SolrServerException(new BindingException("BE test 2")));
try {
publisherSearchRepository.search("b");
} catch (SearchingException se) {
logger.error("SearchingException {}", se);
assertThat(se.getRootCause().getClass()).isEqualTo(BindingException.class);
}
}
@DisplayName("when called, it parameter should be embedded in a query sent through SolrClient")
@Test
public void searchParameterTest() throws IOException, SolrServerException {
try {
publisherSearchRepository.search("c");
Mockito.verify(solrClient).query(solrParamsArgumentCaptor.capture());
} catch (SearchingException se) {
logger.error("SearchingException {}", se);
}
SolrQuery solrQuery = (SolrQuery) solrParamsArgumentCaptor.getValue();
assertThat(solrQuery.getQuery()).isEqualTo("c");
}
}
}
......
......@@ -8,6 +8,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.entities.PublisherVersion;
import org.legrog.entities.SearchingException;
import org.legrog.test.MockitoExtension;
import org.mockito.Mock;
import org.mockito.Mockito;
......@@ -41,14 +42,14 @@ public class PublisherSearchViewTest {
@Test
@DisplayName("when called, should delegate search to PublisherService with same string")
public void searchUsesPublisherService(@Mock PublisherService publisherService) {
public void searchUsesPublisherService(@Mock PublisherService publisherService) throws SearchingException{
publisherSearchView.search("1");
Mockito.verify(publisherService).search("1");
}
@Test
@DisplayName("when called, should return the answer it gets from PublisherService")
public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) {
public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) throws SearchingException {
List<PublisherVersion> publisherVersionList = new ArrayList<>();
when(publisherService.search("2")).thenReturn(publisherVersionList);
assertThat(publisherSearchView.search("2")).isEqualTo(publisherVersionList);
......
......@@ -164,7 +164,7 @@ public class PublisherServiceDefaultTest {
@DisplayName("When called, should delegate search to PublisherSearchRepository")
@Test
public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) {
public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
publisherServiceDefault.search("3");
Mockito.verify(publisherSearchRepository).search("3");
......@@ -172,7 +172,7 @@ public class PublisherServiceDefaultTest {
@DisplayName("When getting IndexedPublishers from search, should convert them")
@Test
public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) {
public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
when(publisherSearchRepository.search("4")).thenReturn(indexedPublishers);
......@@ -182,7 +182,7 @@ public class PublisherServiceDefaultTest {
@DisplayName("When called, should return the PublisherVersions it gets from convert")
@Test
public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) {
public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
List<PublisherVersion> publisherVersions = new ArrayList<>();
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
Mockito.doReturn(publisherVersions).when(publisherServiceDefault).convert(indexedPublishers);
......@@ -195,6 +195,15 @@ public class PublisherServiceDefaultTest {
@Nested
@DisplayName("convert method")
class ConvertTests {
@DisplayName("When called, should return the PublisherVersions it gets from findByPublisherVersionIdIn")
@Test
public void testReturnFromFind(@Mock PublisherVersionRepository publisherVersionRepository) {
List<Integer> integers = new ArrayList<>();
List<PublisherVersion> publisherVersions = new ArrayList<>();
List<IndexedPublisher> indexedPublishers = new ArrayList<>();
when(publisherVersionRepository.findByPublisherVersionIdIn(Mockito.any())).thenReturn(publisherVersions);
assertThat(publisherServiceDefault.convert(indexedPublishers)).isEqualTo(publisherVersions);
}
}
}
......