| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.query.solr; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.ParameterizedType; |
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Locale; |
| 25 |
|
|
| 26 |
|
import javax.inject.Provider; |
| 27 |
|
|
| 28 |
|
import org.apache.solr.client.solrj.SolrQuery; |
| 29 |
|
import org.apache.solr.client.solrj.response.QueryResponse; |
| 30 |
|
import org.apache.solr.common.SolrDocument; |
| 31 |
|
import org.apache.solr.common.SolrDocumentList; |
| 32 |
|
import org.apache.solr.common.params.SolrParams; |
| 33 |
|
import org.junit.Assert; |
| 34 |
|
import org.junit.Before; |
| 35 |
|
import org.junit.Rule; |
| 36 |
|
import org.junit.Test; |
| 37 |
|
import org.mockito.invocation.InvocationOnMock; |
| 38 |
|
import org.mockito.stubbing.Answer; |
| 39 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 40 |
|
import org.xwiki.component.internal.ContextComponentManagerProvider; |
| 41 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 42 |
|
import org.xwiki.model.reference.DocumentReference; |
| 43 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 44 |
|
import org.xwiki.query.QueryExecutor; |
| 45 |
|
import org.xwiki.query.QueryManager; |
| 46 |
|
import org.xwiki.query.internal.DefaultQuery; |
| 47 |
|
import org.xwiki.query.internal.DefaultQueryExecutorManager; |
| 48 |
|
import org.xwiki.query.internal.DefaultQueryManager; |
| 49 |
|
import org.xwiki.query.solr.internal.SolrQueryExecutor; |
| 50 |
|
import org.xwiki.search.solr.internal.api.SolrInstance; |
| 51 |
|
import org.xwiki.test.annotation.ComponentList; |
| 52 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 53 |
|
|
| 54 |
|
import com.xpn.xwiki.XWikiContext; |
| 55 |
|
import com.xpn.xwiki.test.MockitoOldcoreRule; |
| 56 |
|
|
| 57 |
|
import static org.junit.Assert.*; |
| 58 |
|
import static org.mockito.ArgumentMatchers.*; |
| 59 |
|
import static org.mockito.Mockito.*; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
@link |
| 63 |
|
|
| 64 |
|
@version |
| 65 |
|
|
| 66 |
|
@ComponentList({DefaultQueryManager.class, DefaultQueryExecutorManager.class, ContextComponentManagerProvider.class}) |
| |
|
| 100% |
Uncovered Elements: 0 (55) |
Complexity: 5 |
Complexity Density: 0.1 |
|
| 67 |
|
public class SolrQueryExecutorTest |
| 68 |
|
{ |
| 69 |
|
private static final String ITERABLE_PARAM_NAME = "multiParam"; |
| 70 |
|
|
| 71 |
|
private static final String[] ITERABLE_PARAM_EXPECTED = {"value1", "value2"}; |
| 72 |
|
|
| 73 |
|
private static final Iterable<String> ITERABLE_PARAM_VALUE = Arrays.asList(ITERABLE_PARAM_EXPECTED); |
| 74 |
|
|
| 75 |
|
private static final String INT_ARR_PARAM_NAME = "intArrayParam"; |
| 76 |
|
|
| 77 |
|
private static final String[] INT_ARR_PARAM_EXPECTED = {"-42", "4711"}; |
| 78 |
|
|
| 79 |
|
private static final int[] INT_ARR_PARAM_VALUE = {-42, 4711}; |
| 80 |
|
|
| 81 |
|
private static final String STR_ARR_PARAM_NAME = "stringArrayParam"; |
| 82 |
|
|
| 83 |
|
private static final String[] STR_ARR_PARAM_EXPECTED = {"valueA", "valueB"}; |
| 84 |
|
|
| 85 |
|
private static final String[] STR_ARR_PARAM_VALUE = STR_ARR_PARAM_EXPECTED; |
| 86 |
|
|
| 87 |
|
private static final String SINGLE_PARAM_NAME = "singleParam"; |
| 88 |
|
|
| 89 |
|
private static final Object SINGLE_PARAM_VALUE = new Object(); |
| 90 |
|
|
| 91 |
|
private static final Object SINGLE_PARAM_EXPECTED = SINGLE_PARAM_VALUE.toString(); |
| 92 |
|
|
| 93 |
|
public final MockitoComponentMockingRule<QueryExecutor> componentManager = |
| 94 |
|
new MockitoComponentMockingRule<QueryExecutor>(SolrQueryExecutor.class); |
| 95 |
|
|
| 96 |
|
@Rule |
| 97 |
|
public final MockitoOldcoreRule oldCore = new MockitoOldcoreRule(this.componentManager); |
| 98 |
|
|
| 99 |
|
private SolrInstance solr = mock(SolrInstance.class); |
| 100 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 101 |
3 |
@Before... |
| 102 |
|
public void configure() throws Exception |
| 103 |
|
{ |
| 104 |
3 |
ParameterizedType solrProviderType = new DefaultParameterizedType(null, Provider.class, SolrInstance.class); |
| 105 |
3 |
Provider<SolrInstance> provider = this.componentManager.registerMockComponent(solrProviderType); |
| 106 |
3 |
when(provider.get()).thenReturn(this.solr); |
| 107 |
|
} |
| 108 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 109 |
1 |
@Test... |
| 110 |
|
public void testExecutorRegistration() throws Exception |
| 111 |
|
{ |
| 112 |
1 |
QueryManager queryManager = this.componentManager.getInstance(QueryManager.class); |
| 113 |
|
|
| 114 |
1 |
Assert.assertTrue(queryManager.getLanguages().contains(SolrQueryExecutor.SOLR)); |
| 115 |
|
} |
| 116 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 117 |
1 |
@Test... |
| 118 |
|
public void testMultiValuedQueryArgs() throws Exception |
| 119 |
|
{ |
| 120 |
1 |
when(solr.query(any(SolrQuery.class))).then(new Answer<Object>() |
| 121 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 122 |
1 |
@Override... |
| 123 |
|
public Object answer(InvocationOnMock invocation) throws Throwable |
| 124 |
|
{ |
| 125 |
1 |
SolrQuery solrQuery = (SolrQuery) invocation.getArguments()[0]; |
| 126 |
|
|
| 127 |
1 |
Assert.assertArrayEquals(ITERABLE_PARAM_EXPECTED, solrQuery.getParams(ITERABLE_PARAM_NAME)); |
| 128 |
1 |
Assert.assertArrayEquals(INT_ARR_PARAM_EXPECTED, solrQuery.getParams(INT_ARR_PARAM_NAME)); |
| 129 |
1 |
Assert.assertArrayEquals(STR_ARR_PARAM_EXPECTED, solrQuery.getParams(STR_ARR_PARAM_NAME)); |
| 130 |
1 |
Assert.assertEquals(SINGLE_PARAM_EXPECTED, solrQuery.get(SINGLE_PARAM_NAME)); |
| 131 |
|
|
| 132 |
|
|
| 133 |
1 |
Assert.assertEquals("en,fr,de", solrQuery.get("xwiki.supportedLocales")); |
| 134 |
|
|
| 135 |
1 |
QueryResponse r = mock(QueryResponse.class); |
| 136 |
1 |
when(r.getResults()).thenReturn(new SolrDocumentList()); |
| 137 |
1 |
return r; |
| 138 |
|
} |
| 139 |
|
}); |
| 140 |
|
|
| 141 |
1 |
DefaultQuery query = new DefaultQuery("TestQuery", null); |
| 142 |
1 |
query.bindValue(ITERABLE_PARAM_NAME, ITERABLE_PARAM_VALUE); |
| 143 |
1 |
query.bindValue(INT_ARR_PARAM_NAME, INT_ARR_PARAM_VALUE); |
| 144 |
1 |
query.bindValue(STR_ARR_PARAM_NAME, STR_ARR_PARAM_VALUE); |
| 145 |
1 |
query.bindValue(SINGLE_PARAM_NAME, SINGLE_PARAM_VALUE); |
| 146 |
|
|
| 147 |
|
|
| 148 |
1 |
XWikiContext xcontext = this.oldCore.getXWikiContext(); |
| 149 |
1 |
doReturn(Arrays.asList(Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN)).when(this.oldCore.getSpyXWiki()) |
| 150 |
|
.getAvailableLocales(xcontext); |
| 151 |
|
|
| 152 |
1 |
this.componentManager.getComponentUnderTest().execute(query); |
| 153 |
|
} |
| 154 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
| 155 |
1 |
@Test... |
| 156 |
|
public void filterResponse() throws Exception |
| 157 |
|
{ |
| 158 |
1 |
ParameterizedType resolverType = |
| 159 |
|
new DefaultParameterizedType(null, DocumentReferenceResolver.class, SolrDocument.class); |
| 160 |
1 |
DocumentReferenceResolver<SolrDocument> resolver = this.componentManager.getInstance(resolverType); |
| 161 |
|
|
| 162 |
1 |
DocumentAccessBridge documentAccessBridge = this.componentManager.getInstance(DocumentAccessBridge.class); |
| 163 |
|
|
| 164 |
1 |
DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice"); |
| 165 |
1 |
when(documentAccessBridge.exists(aliceReference)).thenReturn(true); |
| 166 |
1 |
SolrDocument alice = new SolrDocument(); |
| 167 |
1 |
when(resolver.resolve(alice)).thenReturn(aliceReference); |
| 168 |
|
|
| 169 |
1 |
DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob"); |
| 170 |
1 |
when(documentAccessBridge.isDocumentViewable(bobReference)).thenReturn(true); |
| 171 |
1 |
SolrDocument bob = new SolrDocument(); |
| 172 |
1 |
when(resolver.resolve(bob)).thenReturn(bobReference); |
| 173 |
|
|
| 174 |
1 |
DocumentReference carolReference = new DocumentReference("wiki", "Users", "Carol"); |
| 175 |
1 |
when(documentAccessBridge.exists(carolReference)).thenReturn(true); |
| 176 |
1 |
when(documentAccessBridge.isDocumentViewable(carolReference)).thenReturn(true); |
| 177 |
1 |
SolrDocument carol = new SolrDocument(); |
| 178 |
1 |
when(resolver.resolve(carol)).thenReturn(carolReference); |
| 179 |
|
|
| 180 |
1 |
SolrDocumentList results = new SolrDocumentList(); |
| 181 |
1 |
results.addAll(Arrays.asList(alice, bob, carol)); |
| 182 |
1 |
results.setNumFound(3); |
| 183 |
|
|
| 184 |
1 |
QueryResponse response = mock(QueryResponse.class); |
| 185 |
1 |
when(response.getResults()).thenReturn(results); |
| 186 |
1 |
when(this.solr.query(any(SolrParams.class))).thenReturn(response); |
| 187 |
|
|
| 188 |
1 |
DefaultQuery query = new DefaultQuery("", null); |
| 189 |
1 |
query.checkCurrentUser(true); |
| 190 |
1 |
assertEquals(Arrays.asList(response), this.componentManager.getComponentUnderTest().execute(query)); |
| 191 |
|
|
| 192 |
1 |
assertEquals(1, results.getNumFound()); |
| 193 |
1 |
assertEquals(Arrays.asList(carol), results); |
| 194 |
|
} |
| 195 |
|
} |