1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.query.solr

File SolrQueryExecutorTest.java

 

Code metrics

0
66
5
1
235
153
5
0.08
13.2
5
1

Classes

Class Line # Actions
SolrQueryExecutorTest 73 66 0% 5 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.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.component.internal.ContextComponentManagerProvider;
40    import org.xwiki.component.util.DefaultParameterizedType;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.model.reference.DocumentReferenceResolver;
43    import org.xwiki.query.QueryExecutor;
44    import org.xwiki.query.QueryManager;
45    import org.xwiki.query.internal.DefaultQuery;
46    import org.xwiki.query.internal.DefaultQueryExecutorManager;
47    import org.xwiki.query.internal.DefaultQueryManager;
48    import org.xwiki.query.solr.internal.SolrQueryExecutor;
49    import org.xwiki.search.solr.internal.api.SolrInstance;
50    import org.xwiki.security.authorization.AuthorizationManager;
51    import org.xwiki.security.authorization.Right;
52    import org.xwiki.test.annotation.ComponentList;
53    import org.xwiki.test.mockito.MockitoComponentMockingRule;
54   
55    import com.xpn.xwiki.XWikiContext;
56    import com.xpn.xwiki.doc.XWikiDocument;
57    import com.xpn.xwiki.test.MockitoOldcoreRule;
58    import com.xpn.xwiki.test.reference.ReferenceComponentList;
59   
60    import static org.junit.Assert.assertEquals;
61    import static org.mockito.ArgumentMatchers.any;
62    import static org.mockito.Mockito.doReturn;
63    import static org.mockito.Mockito.mock;
64    import static org.mockito.Mockito.when;
65   
66    /**
67    * Basic test for the {@link SolrQueryExecutor}.
68    *
69    * @version $Id: 9ac40bcf7a99b9c907bf3a0166fe7c59340c062e $
70    */
71    @ComponentList({DefaultQueryManager.class, DefaultQueryExecutorManager.class, ContextComponentManagerProvider.class})
72    @ReferenceComponentList
 
73    public class SolrQueryExecutorTest
74    {
75    private static final String ITERABLE_PARAM_NAME = "multiParam";
76   
77    private static final String[] ITERABLE_PARAM_EXPECTED = {"value1", "value2"};
78   
79    private static final Iterable<String> ITERABLE_PARAM_VALUE = Arrays.asList(ITERABLE_PARAM_EXPECTED);
80   
81    private static final String INT_ARR_PARAM_NAME = "intArrayParam";
82   
83    private static final String[] INT_ARR_PARAM_EXPECTED = {"-42", "4711"};
84   
85    private static final int[] INT_ARR_PARAM_VALUE = {-42, 4711};
86   
87    private static final String STR_ARR_PARAM_NAME = "stringArrayParam";
88   
89    private static final String[] STR_ARR_PARAM_EXPECTED = {"valueA", "valueB"};
90   
91    private static final String[] STR_ARR_PARAM_VALUE = STR_ARR_PARAM_EXPECTED;
92   
93    private static final String SINGLE_PARAM_NAME = "singleParam";
94   
95    private static final Object SINGLE_PARAM_VALUE = new Object();
96   
97    private static final Object SINGLE_PARAM_EXPECTED = SINGLE_PARAM_VALUE.toString();
98   
99    public final MockitoComponentMockingRule<QueryExecutor> componentManager =
100    new MockitoComponentMockingRule<QueryExecutor>(SolrQueryExecutor.class);
101   
102    @Rule
103    public final MockitoOldcoreRule oldCore = new MockitoOldcoreRule(this.componentManager);
104   
105    private SolrInstance solr = mock(SolrInstance.class);
106   
 
107  3 toggle @Before
108    public void configure() throws Exception
109    {
110  3 ParameterizedType solrProviderType = new DefaultParameterizedType(null, Provider.class, SolrInstance.class);
111  3 Provider<SolrInstance> provider = this.componentManager.registerMockComponent(solrProviderType);
112  3 when(provider.get()).thenReturn(this.solr);
113    }
114   
 
115  1 toggle @Test
116    public void testExecutorRegistration() throws Exception
117    {
118  1 QueryManager queryManager = this.componentManager.getInstance(QueryManager.class);
119   
120  1 Assert.assertTrue(queryManager.getLanguages().contains(SolrQueryExecutor.SOLR));
121    }
122   
 
123  1 toggle @Test
124    public void testMultiValuedQueryArgs() throws Exception
125    {
126  1 when(solr.query(any(SolrQuery.class))).then(new Answer<Object>()
127    {
 
128  1 toggle @Override
129    public Object answer(InvocationOnMock invocation) throws Throwable
130    {
131  1 SolrQuery solrQuery = (SolrQuery) invocation.getArguments()[0];
132   
133  1 Assert.assertArrayEquals(ITERABLE_PARAM_EXPECTED, solrQuery.getParams(ITERABLE_PARAM_NAME));
134  1 Assert.assertArrayEquals(INT_ARR_PARAM_EXPECTED, solrQuery.getParams(INT_ARR_PARAM_NAME));
135  1 Assert.assertArrayEquals(STR_ARR_PARAM_EXPECTED, solrQuery.getParams(STR_ARR_PARAM_NAME));
136  1 Assert.assertEquals(SINGLE_PARAM_EXPECTED, solrQuery.get(SINGLE_PARAM_NAME));
137   
138    // Check that the default list of supported locales is taken from the wiki configuration.
139  1 Assert.assertEquals("en,fr,de", solrQuery.get("xwiki.supportedLocales"));
140   
141  1 QueryResponse r = mock(QueryResponse.class);
142  1 when(r.getResults()).thenReturn(new SolrDocumentList());
143  1 return r;
144    }
145    });
146   
147  1 DefaultQuery query = new DefaultQuery("TestQuery", null);
148  1 query.bindValue(ITERABLE_PARAM_NAME, ITERABLE_PARAM_VALUE);
149  1 query.bindValue(INT_ARR_PARAM_NAME, INT_ARR_PARAM_VALUE);
150  1 query.bindValue(STR_ARR_PARAM_NAME, STR_ARR_PARAM_VALUE);
151  1 query.bindValue(SINGLE_PARAM_NAME, SINGLE_PARAM_VALUE);
152   
153    // The default list of supported locales should be taken from the wiki configuration.
154  1 XWikiContext xcontext = this.oldCore.getXWikiContext();
155  1 doReturn(Arrays.asList(Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN)).when(this.oldCore.getSpyXWiki())
156    .getAvailableLocales(xcontext);
157   
158  1 this.componentManager.getComponentUnderTest().execute(query);
159    }
160   
 
161  1 toggle @Test
162    public void filterResponse() throws Exception
163    {
164  1 ParameterizedType resolverType =
165    new DefaultParameterizedType(null, DocumentReferenceResolver.class, SolrDocument.class);
166  1 DocumentReferenceResolver<SolrDocument> resolver = this.componentManager.getInstance(resolverType);
167   
168  1 AuthorizationManager authorizationManager = this.componentManager.getInstance(AuthorizationManager.class);
169   
170  1 DocumentReference currentUserReference = new DocumentReference("xwiki", "XWiki", "currentuser");
171  1 this.oldCore.getXWikiContext().setUserReference(currentUserReference);
172   
173  1 DocumentReference currentAuthorReference = new DocumentReference("xwiki", "XWiki", "currentauthor");
174  1 XWikiDocument currentDocument = new XWikiDocument(currentAuthorReference);
175  1 currentDocument.setContentAuthorReference(currentAuthorReference);
176  1 this.oldCore.getXWikiContext().setDoc(currentDocument);
177   
178  1 DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
179  1 when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, aliceReference)).thenReturn(true);
180  1 SolrDocument alice = new SolrDocument();
181  1 when(resolver.resolve(alice)).thenReturn(aliceReference);
182   
183  1 DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
184  1 when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, bobReference)).thenReturn(true);
185  1 when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, bobReference)).thenReturn(true);
186  1 SolrDocument bob = new SolrDocument();
187  1 when(resolver.resolve(bob)).thenReturn(bobReference);
188   
189  1 DocumentReference carolReference = new DocumentReference("wiki", "Users", "Carol");
190  1 when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, carolReference)).thenReturn(true);
191  1 SolrDocument carol = new SolrDocument();
192  1 when(resolver.resolve(carol)).thenReturn(carolReference);
193   
194  1 SolrDocumentList sourceResults = new SolrDocumentList();
195  1 sourceResults.addAll(Arrays.asList(alice, bob, carol));
196  1 sourceResults.setNumFound(3);
197   
198  1 QueryResponse response = mock(QueryResponse.class);
199  1 when(this.solr.query(any(SolrParams.class))).thenReturn(response);
200   
201  1 DefaultQuery query = new DefaultQuery("", null);
202   
203    // No right check
204   
205  1 when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
206   
207  1 SolrDocumentList results =
208    ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
209  1 assertEquals(Arrays.asList(alice, bob, carol), results);
210   
211    // Check current user right
212   
213  1 query.checkCurrentUser(true);
214  1 when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
215   
216  1 results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
217  1 assertEquals(Arrays.asList(bob, carol), results);
218   
219    // Check both current user and author rights
220   
221  1 query.checkCurrentAuthor(true);
222  1 when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
223   
224  1 results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
225  1 assertEquals(Arrays.asList(bob), results);
226   
227    // Check current author right
228   
229  1 query.checkCurrentUser(false);
230  1 when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
231   
232  1 results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
233  1 assertEquals(Arrays.asList(alice, bob), results);
234    }
235    }