| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 55 |
|
|
| 56 |
|
@version |
| 57 |
|
@since |
| 58 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (60) |
Complexity: 7 |
Complexity Density: 0.14 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 69 |
3 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 83 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 105 |
1 |
@Test... |
| 106 |
|
public void sizeWithException() throws Exception |
| 107 |
|
{ |
| 108 |
1 |
assertEquals(0, mocker.getComponentUnderTest().size()); |
| 109 |
|
} |
| 110 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 2 |
Complexity Density: 0.07 |
1PASS
|
|
| 111 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 155 |
3 |
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 |
|
} |