Jean-Francois Leveque

ImplementationTDD de recherche (hors page recherche)

...@@ -20,5 +20,5 @@ public interface PublisherSearchRepository { ...@@ -20,5 +20,5 @@ public interface PublisherSearchRepository {
20 * @param string String looked for in IndexedPublishers 20 * @param string String looked for in IndexedPublishers
21 * @return list of matching IndexedPublishers 21 * @return list of matching IndexedPublishers
22 */ 22 */
23 - public List<IndexedPublisher> search(String string); 23 + public List<IndexedPublisher> search(String string) throws SearchingException;
24 } 24 }
......
1 package org.legrog.entities; 1 package org.legrog.entities;
2 2
3 import org.apache.solr.client.solrj.SolrClient; 3 import org.apache.solr.client.solrj.SolrClient;
4 +import org.apache.solr.client.solrj.SolrQuery;
4 import org.apache.solr.client.solrj.SolrServerException; 5 import org.apache.solr.client.solrj.SolrServerException;
6 +import org.apache.solr.client.solrj.response.QueryResponse;
5 import org.apache.solr.client.solrj.response.UpdateResponse; 7 import org.apache.solr.client.solrj.response.UpdateResponse;
6 import org.slf4j.Logger; 8 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
...@@ -43,7 +45,22 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository ...@@ -43,7 +45,22 @@ public class PublisherSearchRepositorySolrj implements PublisherSearchRepository
43 } 45 }
44 46
45 @Override 47 @Override
46 - public List<IndexedPublisher> search(String string) { 48 + public List<IndexedPublisher> search(String string) throws SearchingException {
49 + SolrQuery solrQuery = new SolrQuery(string);
50 + QueryResponse queryResponse;
51 + try {
52 + queryResponse = solrClient.query(solrQuery);
53 + } catch (IOException ioe) {
54 + throw new SearchingException(ioe);
55 + } catch (SolrServerException sse) {
56 + logger.error("SolrServerException {}", sse);
57 + throw new SearchingException(sse.getRootCause());
58 + }
59 +
60 + if (queryResponse != null) {
61 + return queryResponse.getBeans(IndexedPublisher.class);
62 + } else {
47 return null; 63 return null;
48 } 64 }
65 + }
49 } 66 }
......
...@@ -2,5 +2,8 @@ package org.legrog.entities; ...@@ -2,5 +2,8 @@ package org.legrog.entities;
2 2
3 import org.springframework.data.jpa.repository.JpaRepository; 3 import org.springframework.data.jpa.repository.JpaRepository;
4 4
5 +import java.util.List;
6 +
5 public interface PublisherVersionRepository extends JpaRepository<PublisherVersion, Integer> { 7 public interface PublisherVersionRepository extends JpaRepository<PublisherVersion, Integer> {
8 + List<PublisherVersion> findByPublisherVersionIdIn(List<Integer> integers);
6 } 9 }
......
1 +package org.legrog.entities;
2 +
3 +/**
4 + * Exception when searching fails, whatever the reason. Has to be dealt with at service level.
5 + */
6 +public class SearchingException extends Exception {
7 + final Throwable rootCause;
8 +
9 + SearchingException(Throwable rootCause) {
10 + this.rootCause = rootCause;
11 + }
12 +
13 + public Throwable getRootCause() {
14 + return rootCause;
15 + }
16 +}
...\ No newline at end of file ...\ No newline at end of file
1 package org.legrog.web.publisher; 1 package org.legrog.web.publisher;
2 2
3 import org.legrog.entities.PublisherVersion; 3 import org.legrog.entities.PublisherVersion;
4 +import org.legrog.entities.SearchingException;
4 5
5 import javax.inject.Inject; 6 import javax.inject.Inject;
6 import java.util.List; 7 import java.util.List;
...@@ -26,7 +27,7 @@ public class PublisherSearchView { ...@@ -26,7 +27,7 @@ public class PublisherSearchView {
26 //no args constructor to make it proxyable 27 //no args constructor to make it proxyable
27 } 28 }
28 29
29 - public List<PublisherVersion> search(String string) { 30 + public List<PublisherVersion> search(String string) throws SearchingException{
30 return publisherService.search(string); 31 return publisherService.search(string);
31 } 32 }
32 } 33 }
......
1 package org.legrog.web.publisher; 1 package org.legrog.web.publisher;
2 2
3 -import org.legrog.entities.IndexedPublisher; 3 +import org.legrog.entities.*;
4 -import org.legrog.entities.Publisher;
5 -import org.legrog.entities.PublisherAction;
6 -import org.legrog.entities.PublisherVersion;
7 4
8 import javax.validation.constraints.NotNull; 5 import javax.validation.constraints.NotNull;
9 import java.util.List; 6 import java.util.List;
...@@ -79,7 +76,7 @@ public interface PublisherService { ...@@ -79,7 +76,7 @@ public interface PublisherService {
79 * @param string String searched indexed publishers 76 * @param string String searched indexed publishers
80 * @return indexed publisher that matches the String 77 * @return indexed publisher that matches the String
81 */ 78 */
82 - List<PublisherVersion> search(@NotNull String string); 79 + List<PublisherVersion> search(@NotNull String string) throws SearchingException;
83 80
84 /** 81 /**
85 * 82 *
......
...@@ -10,10 +10,7 @@ import javax.ejb.Stateless; ...@@ -10,10 +10,7 @@ import javax.ejb.Stateless;
10 import javax.inject.Inject; 10 import javax.inject.Inject;
11 import javax.validation.constraints.NotNull; 11 import javax.validation.constraints.NotNull;
12 import java.sql.Timestamp; 12 import java.sql.Timestamp;
13 -import java.util.Date; 13 +import java.util.*;
14 -import java.util.HashSet;
15 -import java.util.List;
16 -import java.util.Set;
17 14
18 /** 15 /**
19 * Implémentation de l'interface PublisherService 16 * Implémentation de l'interface PublisherService
...@@ -139,13 +136,16 @@ public class PublisherServiceDefault implements PublisherService { ...@@ -139,13 +136,16 @@ public class PublisherServiceDefault implements PublisherService {
139 } 136 }
140 137
141 @Override 138 @Override
142 - public List<PublisherVersion> search(@NotNull String string) { 139 + public List<PublisherVersion> search(@NotNull String string) throws SearchingException {
143 return convert(publisherSearchRepository.search(string)); 140 return convert(publisherSearchRepository.search(string));
144 } 141 }
145 142
146 @Override 143 @Override
147 public List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers) { 144 public List<PublisherVersion> convert(List<IndexedPublisher> indexedPublishers) {
148 - return null; 145 + List<Integer> integers = new ArrayList<>(indexedPublishers.size());
146 + indexedPublishers.forEach(indexedPublisher -> integers.add(indexedPublisher.getPublisherId()));
147 +
148 + return publisherVersionRepository.findByPublisherVersionIdIn(integers);
149 } 149 }
150 150
151 } 151 }
......
1 package org.legrog.entities; 1 package org.legrog.entities;
2 2
3 import org.apache.solr.client.solrj.SolrClient; 3 import org.apache.solr.client.solrj.SolrClient;
4 +import org.apache.solr.client.solrj.SolrQuery;
4 import org.apache.solr.client.solrj.SolrServerException; 5 import org.apache.solr.client.solrj.SolrServerException;
5 import org.apache.solr.client.solrj.beans.BindingException; 6 import org.apache.solr.client.solrj.beans.BindingException;
7 +import org.apache.solr.common.params.SolrParams;
6 import org.junit.jupiter.api.*; 8 import org.junit.jupiter.api.*;
7 import org.junit.jupiter.api.extension.ExtendWith; 9 import org.junit.jupiter.api.extension.ExtendWith;
8 import org.junit.platform.runner.JUnitPlatform; 10 import org.junit.platform.runner.JUnitPlatform;
9 import org.junit.runner.RunWith; 11 import org.junit.runner.RunWith;
10 import org.legrog.test.MockitoExtension; 12 import org.legrog.test.MockitoExtension;
13 +import org.mockito.ArgumentCaptor;
14 +import org.mockito.Captor;
11 import org.mockito.Mock; 15 import org.mockito.Mock;
16 +import org.mockito.Mockito;
12 import org.slf4j.Logger; 17 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 18 import org.slf4j.LoggerFactory;
14 19
...@@ -19,8 +24,8 @@ import static org.mockito.Mockito.when; ...@@ -19,8 +24,8 @@ import static org.mockito.Mockito.when;
19 24
20 import static org.assertj.core.api.Assertions.assertThat; 25 import static org.assertj.core.api.Assertions.assertThat;
21 26
22 -/* 27 +/**
23 - Classe testant PublisherSearchRepositorySolrj 28 + * Classe testant PublisherSearchRepositorySolrj
24 */ 29 */
25 @RunWith(JUnitPlatform.class) 30 @RunWith(JUnitPlatform.class)
26 @ExtendWith(MockitoExtension.class) 31 @ExtendWith(MockitoExtension.class)
...@@ -32,6 +37,9 @@ public class PublisherSearchRepositorySolrjTest { ...@@ -32,6 +37,9 @@ public class PublisherSearchRepositorySolrjTest {
32 private PublisherSearchRepository publisherSearchRepository; 37 private PublisherSearchRepository publisherSearchRepository;
33 private SolrClient solrClient; 38 private SolrClient solrClient;
34 39
40 + @Captor
41 + ArgumentCaptor<SolrParams> solrParamsArgumentCaptor;
42 +
35 @BeforeEach 43 @BeforeEach
36 public void setup(@Mock SolrClient solrClient) { 44 public void setup(@Mock SolrClient solrClient) {
37 publisherSearchRepository = new PublisherSearchRepositorySolrj(solrClient); 45 publisherSearchRepository = new PublisherSearchRepositorySolrj(solrClient);
...@@ -69,4 +77,43 @@ public class PublisherSearchRepositorySolrjTest { ...@@ -69,4 +77,43 @@ public class PublisherSearchRepositorySolrjTest {
69 } 77 }
70 } 78 }
71 79
80 + @Nested
81 + @DisplayName("search method")
82 + class SearchTests {
83 +
84 + @DisplayName("when repository in IO error, should throw a SearchingException")
85 + @Test
86 + public void searchIOETest() throws IOException, SolrServerException{
87 + when(solrClient.query(Mockito.any())).thenThrow(new IOException());
88 + Assertions.assertThrows(SearchingException.class, () -> publisherSearchRepository.search("a"));
89 + }
90 +
91 + @DisplayName("when repository in SolrServerException, should throw an SearchingException with its root cause")
92 + @Test
93 + public void searchSSETest() throws SolrServerException, IOException {
94 + when(solrClient.query(Mockito.any())).thenThrow(new SolrServerException(new BindingException("BE test 2")));
95 + try {
96 + publisherSearchRepository.search("b");
97 + } catch (SearchingException se) {
98 + logger.error("SearchingException {}", se);
99 + assertThat(se.getRootCause().getClass()).isEqualTo(BindingException.class);
100 + }
101 + }
102 +
103 + @DisplayName("when called, it parameter should be embedded in a query sent through SolrClient")
104 + @Test
105 + public void searchParameterTest() throws IOException, SolrServerException {
106 + try {
107 + publisherSearchRepository.search("c");
108 + Mockito.verify(solrClient).query(solrParamsArgumentCaptor.capture());
109 + } catch (SearchingException se) {
110 + logger.error("SearchingException {}", se);
111 + }
112 + SolrQuery solrQuery = (SolrQuery) solrParamsArgumentCaptor.getValue();
113 + assertThat(solrQuery.getQuery()).isEqualTo("c");
114 + }
115 +
116 + }
117 +
118 +
72 } 119 }
......
...@@ -8,6 +8,7 @@ import org.junit.jupiter.api.extension.ExtendWith; ...@@ -8,6 +8,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.PublisherVersion; 10 import org.legrog.entities.PublisherVersion;
11 +import org.legrog.entities.SearchingException;
11 import org.legrog.test.MockitoExtension; 12 import org.legrog.test.MockitoExtension;
12 import org.mockito.Mock; 13 import org.mockito.Mock;
13 import org.mockito.Mockito; 14 import org.mockito.Mockito;
...@@ -41,14 +42,14 @@ public class PublisherSearchViewTest { ...@@ -41,14 +42,14 @@ public class PublisherSearchViewTest {
41 42
42 @Test 43 @Test
43 @DisplayName("when called, should delegate search to PublisherService with same string") 44 @DisplayName("when called, should delegate search to PublisherService with same string")
44 - public void searchUsesPublisherService(@Mock PublisherService publisherService) { 45 + public void searchUsesPublisherService(@Mock PublisherService publisherService) throws SearchingException{
45 publisherSearchView.search("1"); 46 publisherSearchView.search("1");
46 Mockito.verify(publisherService).search("1"); 47 Mockito.verify(publisherService).search("1");
47 } 48 }
48 49
49 @Test 50 @Test
50 @DisplayName("when called, should return the answer it gets from PublisherService") 51 @DisplayName("when called, should return the answer it gets from PublisherService")
51 - public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) { 52 + public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) throws SearchingException {
52 List<PublisherVersion> publisherVersionList = new ArrayList<>(); 53 List<PublisherVersion> publisherVersionList = new ArrayList<>();
53 when(publisherService.search("2")).thenReturn(publisherVersionList); 54 when(publisherService.search("2")).thenReturn(publisherVersionList);
54 assertThat(publisherSearchView.search("2")).isEqualTo(publisherVersionList); 55 assertThat(publisherSearchView.search("2")).isEqualTo(publisherVersionList);
......
...@@ -164,7 +164,7 @@ public class PublisherServiceDefaultTest { ...@@ -164,7 +164,7 @@ public class PublisherServiceDefaultTest {
164 164
165 @DisplayName("When called, should delegate search to PublisherSearchRepository") 165 @DisplayName("When called, should delegate search to PublisherSearchRepository")
166 @Test 166 @Test
167 - public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) { 167 + public void testDelegateSearchToPublisherSearchRepository(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
168 publisherServiceDefault.search("3"); 168 publisherServiceDefault.search("3");
169 Mockito.verify(publisherSearchRepository).search("3"); 169 Mockito.verify(publisherSearchRepository).search("3");
170 170
...@@ -172,7 +172,7 @@ public class PublisherServiceDefaultTest { ...@@ -172,7 +172,7 @@ public class PublisherServiceDefaultTest {
172 172
173 @DisplayName("When getting IndexedPublishers from search, should convert them") 173 @DisplayName("When getting IndexedPublishers from search, should convert them")
174 @Test 174 @Test
175 - public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) { 175 + public void testConvertReturnedIndexedPublishers(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
176 List<IndexedPublisher> indexedPublishers = new ArrayList<>(); 176 List<IndexedPublisher> indexedPublishers = new ArrayList<>();
177 177
178 when(publisherSearchRepository.search("4")).thenReturn(indexedPublishers); 178 when(publisherSearchRepository.search("4")).thenReturn(indexedPublishers);
...@@ -182,7 +182,7 @@ public class PublisherServiceDefaultTest { ...@@ -182,7 +182,7 @@ public class PublisherServiceDefaultTest {
182 182
183 @DisplayName("When called, should return the PublisherVersions it gets from convert") 183 @DisplayName("When called, should return the PublisherVersions it gets from convert")
184 @Test 184 @Test
185 - public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) { 185 + public void testReturnFromConvert(@Mock PublisherSearchRepository publisherSearchRepository) throws SearchingException {
186 List<PublisherVersion> publisherVersions = new ArrayList<>(); 186 List<PublisherVersion> publisherVersions = new ArrayList<>();
187 List<IndexedPublisher> indexedPublishers = new ArrayList<>(); 187 List<IndexedPublisher> indexedPublishers = new ArrayList<>();
188 Mockito.doReturn(publisherVersions).when(publisherServiceDefault).convert(indexedPublishers); 188 Mockito.doReturn(publisherVersions).when(publisherServiceDefault).convert(indexedPublishers);
...@@ -195,6 +195,15 @@ public class PublisherServiceDefaultTest { ...@@ -195,6 +195,15 @@ public class PublisherServiceDefaultTest {
195 @Nested 195 @Nested
196 @DisplayName("convert method") 196 @DisplayName("convert method")
197 class ConvertTests { 197 class ConvertTests {
198 + @DisplayName("When called, should return the PublisherVersions it gets from findByPublisherVersionIdIn")
199 + @Test
200 + public void testReturnFromFind(@Mock PublisherVersionRepository publisherVersionRepository) {
201 + List<Integer> integers = new ArrayList<>();
202 + List<PublisherVersion> publisherVersions = new ArrayList<>();
203 + List<IndexedPublisher> indexedPublishers = new ArrayList<>();
204 + when(publisherVersionRepository.findByPublisherVersionIdIn(Mockito.any())).thenReturn(publisherVersions);
205 + assertThat(publisherServiceDefault.convert(indexedPublishers)).isEqualTo(publisherVersions);
206 + }
198 207
199 } 208 }
200 } 209 }
......