Jean-Francois Leveque

More tests

1 +package org.legrog.entities;
2 +
3 +import org.apache.solr.client.solrj.SolrClient;
4 +import org.apache.solr.client.solrj.SolrQuery;
5 +import org.apache.solr.client.solrj.SolrServerException;
6 +import org.apache.solr.client.solrj.beans.BindingException;
7 +import org.apache.solr.common.params.SolrParams;
8 +import org.junit.jupiter.api.*;
9 +import org.junit.jupiter.api.extension.ExtendWith;
10 +import org.junit.platform.runner.JUnitPlatform;
11 +import org.junit.runner.RunWith;
12 +import org.legrog.test.MockitoExtension;
13 +import org.mockito.ArgumentCaptor;
14 +import org.mockito.Captor;
15 +import org.mockito.Mock;
16 +import org.mockito.Mockito;
17 +import org.slf4j.Logger;
18 +import org.slf4j.LoggerFactory;
19 +
20 +import java.io.IOException;
21 +
22 +import static org.assertj.core.api.Assertions.assertThat;
23 +import static org.mockito.Mockito.verify;
24 +import static org.mockito.Mockito.when;
25 +
26 +/**
27 + * Classe testant AccountSearchRepositorySolrj
28 + */
29 +@RunWith(JUnitPlatform.class)
30 +@ExtendWith(MockitoExtension.class)
31 +@DisplayName("Indexes and searches with SearchRepository")
32 +
33 +public class AccountSearchRepositorySolrjTest {
34 + Logger logger = LoggerFactory.getLogger(getClass());
35 +
36 + private AccountSearchRepository accountSearchRepository;
37 + private SolrClient solrClient;
38 +
39 + @Captor
40 + ArgumentCaptor<SolrParams> solrParamsArgumentCaptor;
41 +
42 + @BeforeEach
43 + public void setup(@Mock SolrClient solrClient) {
44 + accountSearchRepository = new AccountSearchRepositorySolrj(solrClient);
45 + this.solrClient = solrClient;
46 + }
47 +
48 + @Nested
49 + @DisplayName("search method")
50 + class SearchTests {
51 +
52 + @DisplayName("when repository in IO error, should throw a SearchingException")
53 + @Test
54 + public void searchIOETest() throws IOException, SolrServerException{
55 + when(solrClient.query(Mockito.anyString(), Mockito.any())).thenThrow(new IOException());
56 + Assertions.assertThrows(SearchingException.class, () -> accountSearchRepository.search("a"));
57 + }
58 +
59 + @DisplayName("when repository in SolrServerException, should throw an SearchingException with its root cause")
60 + @Test
61 + public void searchSSETest() throws SolrServerException, IOException {
62 + when(solrClient.query(Mockito.any())).thenThrow(new SolrServerException(new BindingException("BE test 2")));
63 + try {
64 + accountSearchRepository.search("b");
65 + } catch (SearchingException se) {
66 + logger.error("SearchingException {}", se);
67 + assertThat(se.getRootCause().getClass()).isEqualTo(BindingException.class);
68 + }
69 + }
70 +
71 + @DisplayName("when called, it parameter should be embedded in a query sent through SolrClient")
72 + @Test
73 + public void searchParameterTest() throws IOException, SolrServerException {
74 + try {
75 + accountSearchRepository.search("c");
76 + Mockito.verify(solrClient).query(Mockito.anyString(), solrParamsArgumentCaptor.capture());
77 + } catch (SearchingException se) {
78 + logger.error("SearchingException {}", se);
79 + }
80 + SolrQuery solrQuery = (SolrQuery) solrParamsArgumentCaptor.getValue();
81 + assertThat(solrQuery.getQuery()).isEqualTo("c");
82 + }
83 +
84 + }
85 +
86 +
87 +}