1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.job

File SolrDocumentIteratorTest.java

 

Code metrics

4
51
5
1
166
111
7
0.14
10.2
5
1.4

Classes

Class Line # Actions
SolrDocumentIteratorTest 59 51 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.search.solr.internal.job;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25    import java.util.Locale;
26   
27    import javax.inject.Provider;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.apache.commons.lang3.tuple.ImmutablePair;
31    import org.apache.commons.lang3.tuple.Pair;
32    import org.apache.solr.client.solrj.SolrQuery;
33    import org.apache.solr.client.solrj.response.QueryResponse;
34    import org.apache.solr.common.SolrDocument;
35    import org.apache.solr.common.SolrDocumentList;
36    import org.junit.Before;
37    import org.junit.Rule;
38    import org.junit.Test;
39    import org.xwiki.component.util.DefaultParameterizedType;
40    import org.xwiki.localization.LocaleUtils;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.model.reference.DocumentReferenceResolver;
43    import org.xwiki.model.reference.WikiReference;
44    import org.xwiki.search.solr.internal.api.FieldUtils;
45    import org.xwiki.search.solr.internal.api.SolrInstance;
46    import org.xwiki.search.solr.internal.reference.SolrReferenceResolver;
47    import org.xwiki.test.mockito.MockitoComponentMockingRule;
48   
49    import static org.junit.Assert.*;
50    import static org.mockito.ArgumentMatchers.*;
51    import static org.mockito.Mockito.*;
52   
53    /**
54    * Unit tests for {@link SolrDocumentIterator}.
55    *
56    * @version $Id: ecc29937a11c2fcf5c761acd815ba668f0bf30d5 $
57    * @since 5.4.5
58    */
 
59    public class SolrDocumentIteratorTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<DocumentIterator<String>> mocker =
63    new MockitoComponentMockingRule<DocumentIterator<String>>(SolrDocumentIterator.class);
64   
65    private SolrInstance solr;
66   
67    private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver;
68   
 
69  3 toggle @Before
70    public void configure() throws Exception
71    {
72  3 solr = mock(SolrInstance.class);
73   
74  3 Provider<SolrInstance> solrInstanceProvider =
75    mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, SolrInstance.class));
76  3 when(solrInstanceProvider.get()).thenReturn(solr);
77   
78  3 this.solrDocumentReferenceResolver =
79    this.mocker.getInstance(new DefaultParameterizedType(null, DocumentReferenceResolver.class,
80    SolrDocument.class));
81    }
82   
 
83  1 toggle @Test
84    public void size() throws Exception
85    {
86  1 SolrDocumentList results = mock(SolrDocumentList.class);
87  1 when(results.getNumFound()).thenReturn(12L);
88   
89  1 QueryResponse response = mock(QueryResponse.class);
90  1 when(response.getResults()).thenReturn(results);
91   
92  1 when(solr.query(any(SolrQuery.class))).thenReturn(response);
93   
94  1 DocumentIterator<String> iterator = mocker.getComponentUnderTest();
95   
96  1 WikiReference rootReference = new WikiReference("wiki");
97  1 iterator.setRootReference(rootReference);
98   
99  1 assertEquals(12, iterator.size());
100   
101  1 SolrReferenceResolver resolver = mocker.getInstance(SolrReferenceResolver.class);
102  1 verify(resolver).getQuery(rootReference);
103    }
104   
 
105  1 toggle @Test
106    public void sizeWithException() throws Exception
107    {
108  1 assertEquals(0, mocker.getComponentUnderTest().size());
109    }
110   
 
111  1 toggle @Test
112    public void iterate() throws Exception
113    {
114  1 SolrDocumentList firstResults = new SolrDocumentList();
115  1 firstResults.add(createSolrDocument("chess", Arrays.asList("A", "B"), "C", "", "1.3"));
116  1 firstResults.add(createSolrDocument("chess", Arrays.asList("M"), "N", "en", "2.4"));
117   
118  1 QueryResponse firstResponse = mock(QueryResponse.class);
119  1 when(firstResponse.getNextCursorMark()).thenReturn("foo");
120  1 when(firstResponse.getResults()).thenReturn(firstResults);
121   
122  1 SolrDocumentList secondResults = new SolrDocumentList();
123  1 secondResults.add(createSolrDocument("tennis", Arrays.asList("X", "Y", "Z"), "V", "fr", "1.1"));
124   
125  1 QueryResponse secondResponse = mock(QueryResponse.class);
126  1 when(secondResponse.getNextCursorMark()).thenReturn("bar");
127  1 when(secondResponse.getResults()).thenReturn(secondResults);
128   
129  1 when(solr.query(any(SolrQuery.class))).thenReturn(firstResponse, secondResponse, secondResponse);
130   
131  1 DocumentIterator<String> iterator = mocker.getComponentUnderTest();
132   
133  1 WikiReference rootReference = new WikiReference("wiki");
134  1 iterator.setRootReference(rootReference);
135   
136  1 List<Pair<DocumentReference, String>> actualResult = new ArrayList<Pair<DocumentReference, String>>();
137  4 while (iterator.hasNext()) {
138  3 actualResult.add(iterator.next());
139    }
140   
141  1 SolrReferenceResolver resolver = mocker.getInstance(SolrReferenceResolver.class);
142  1 verify(resolver).getQuery(rootReference);
143   
144  1 List<Pair<DocumentReference, String>> expectedResult = new ArrayList<Pair<DocumentReference, String>>();
145  1 DocumentReference documentReference = new DocumentReference("chess", Arrays.asList("A", "B"), "C");
146  1 expectedResult.add(new ImmutablePair<DocumentReference, String>(documentReference, "1.3"));
147  1 documentReference = new DocumentReference("chess", Arrays.asList("M"), "N", Locale.ENGLISH);
148  1 expectedResult.add(new ImmutablePair<DocumentReference, String>(documentReference, "2.4"));
149  1 documentReference = new DocumentReference("tennis", Arrays.asList("X", "Y", "Z"), "V", Locale.FRENCH);
150  1 expectedResult.add(new ImmutablePair<DocumentReference, String>(documentReference, "1.1"));
151   
152  1 assertEquals(expectedResult, actualResult);
153    }
154   
 
155  3 toggle private SolrDocument createSolrDocument(String wiki, List<String> spaces, String name, String locale, String version)
156    {
157  3 SolrDocument doc = new SolrDocument();
158  3 DocumentReference docRef = new DocumentReference(wiki, spaces, name);
159  3 if (!StringUtils.isEmpty(locale)) {
160  2 docRef = new DocumentReference(docRef, LocaleUtils.toLocale(locale));
161    }
162  3 when(this.solrDocumentReferenceResolver.resolve(doc)).thenReturn(docRef);
163  3 doc.setField(FieldUtils.VERSION, version);
164  3 return doc;
165    }
166    }