PublisherSearchRepositorySolrjTest.java
4.75 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
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;
import java.io.IOException;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Classe testant PublisherSearchRepositorySolrj
*/
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Indexes and searches with SearchRepository")
public class PublisherSearchRepositorySolrjTest {
Logger logger = LoggerFactory.getLogger(getClass());
private PublisherSearchRepository publisherSearchRepository;
private SolrClient solrClient;
@Captor
ArgumentCaptor<SolrParams> solrParamsArgumentCaptor;
@BeforeEach
public void setup(@Mock SolrClient solrClient) {
publisherSearchRepository = new PublisherSearchRepositorySolrj(solrClient);
this.solrClient = solrClient;
}
@Nested
@DisplayName("save method")
class SaveTests {
@Test
@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);
}
@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());
Assertions.assertThrows(IndexingException.class, () -> publisherSearchRepository.save(indexedPublisher));
}
@Test
@DisplayName("When repository in SolrServerException, should throw an IndexingException with its root cause")
public void addBeanSSETest(@Mock IndexedPublisher indexedPublisher) throws SolrServerException, IOException{
when(solrClient.addBean(indexedPublisher, 1)).thenThrow(new SolrServerException(new BindingException("BE test")));
try {
publisherSearchRepository.save(indexedPublisher);
} catch (IndexingException ie) {
logger.error("IndexingException {}", ie);
assertThat(ie.getRootCause().getClass()).isEqualTo(BindingException.class);
}
}
}
@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.anyString(), 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(Mockito.anyString(), solrParamsArgumentCaptor.capture());
} catch (SearchingException se) {
logger.error("SearchingException {}", se);
}
SolrQuery solrQuery = (SolrQuery) solrParamsArgumentCaptor.getValue();
assertThat(solrQuery.getQuery()).isEqualTo("c");
}
}
}