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

File SolrQueryExecutorTest.java

 

Code metrics

0
50
5
1
195
130
5
0.1
10
5
1

Classes

Class Line # Actions
SolrQueryExecutorTest 67 50 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.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    * Basic test for the {@link SolrQueryExecutor}.
63    *
64    * @version $Id: d5bef83125ea835f8c8375c53acae42641ea10a7 $
65    */
66    @ComponentList({DefaultQueryManager.class, DefaultQueryExecutorManager.class, ContextComponentManagerProvider.class})
 
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   
 
101  3 toggle @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   
 
109  1 toggle @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   
 
117  1 toggle @Test
118    public void testMultiValuedQueryArgs() throws Exception
119    {
120  1 when(solr.query(any(SolrQuery.class))).then(new Answer<Object>()
121    {
 
122  1 toggle @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    // Check that the default list of supported locales is taken from the wiki configuration.
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    // The default list of supported locales should be taken from the wiki configuration.
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   
 
155  1 toggle @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    }