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

File DatabaseDocumentIteratorTest.java

 

Code metrics

6
86
4
1
218
155
7
0.08
21.5
4
1.75

Classes

Class Line # Actions
DatabaseDocumentIteratorTest 57 86 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 2 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.Collection;
25    import java.util.Collections;
26    import java.util.HashMap;
27    import java.util.List;
28    import java.util.Locale;
29    import java.util.Map;
30   
31    import org.apache.commons.lang.StringUtils;
32    import org.apache.commons.lang3.tuple.ImmutablePair;
33    import org.apache.commons.lang3.tuple.Pair;
34    import org.junit.Before;
35    import org.junit.Rule;
36    import org.junit.Test;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.EntityReferenceResolver;
40    import org.xwiki.model.reference.EntityReferenceSerializer;
41    import org.xwiki.query.Query;
42    import org.xwiki.query.QueryFilter;
43    import org.xwiki.query.QueryManager;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
46   
47    import static org.junit.Assert.*;
48    import static org.mockito.ArgumentMatchers.*;
49    import static org.mockito.Mockito.*;
50   
51    /**
52    * Unit tests for {@link DatabaseDocumentIterator}.
53    *
54    * @version $Id: 721c20e8b1212afe90c9a0e52489cb98258cec36 $
55    * @since 5.4.5
56    */
 
57    public class DatabaseDocumentIteratorTest
58    {
59    @Rule
60    public MockitoComponentMockingRule<DocumentIterator<String>> mocker =
61    new MockitoComponentMockingRule<DocumentIterator<String>>(DatabaseDocumentIterator.class);
62   
63    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
64   
65    private EntityReferenceResolver<String> explicitEntityReferenceResolver;
66   
 
67  2 toggle @Before
68    public void configure() throws Exception
69    {
70    // We explicitly leave the list of wikis unsorted.
71  2 Collection<String> wikiIds = Arrays.asList("chess", "tennis");
72   
73  2 WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
74  2 when(wikiDescriptorManager.getAllIds()).thenReturn(wikiIds);
75   
76  2 this.localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
77  2 this.explicitEntityReferenceResolver = this.mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "explicit");
78    }
79   
 
80  1 toggle @Test
81    public void iterateAllWikis() throws Exception
82    {
83  1 Query emptyQuery = mock(Query.class);
84  1 when(emptyQuery.execute()).thenReturn(Collections.emptyList());
85   
86  1 Query chessQuery = mock(Query.class);
87  1 when(chessQuery.setOffset(0)).thenReturn(chessQuery);
88  1 when(chessQuery.setOffset(100)).thenReturn(emptyQuery);
89  1 when(chessQuery.execute()).thenReturn(Arrays.<Object> asList(
90    new Object[] {"Blog.Code", "WebHome", "", "3.2"},
91    new Object[] {"Main", "Welcome", "en", "1.1"},
92    new Object[] {"XWiki.Syntax", "Links", "fr", "2.5"}));
93   
94  1 DocumentReference chessBlogCodeWebHome =
95    createDocumentReference("chess", Arrays.asList("Blog", "Code"), "WebHome", null);
96  1 DocumentReference chessMainWelcome =
97    createDocumentReference("chess", Arrays.asList("Main"), "Welcome", Locale.ENGLISH);
98  1 DocumentReference chessXWikiSyntaxLinks =
99    createDocumentReference("chess", Arrays.asList("XWiki", "Syntax"), "Links", Locale.FRENCH);
100   
101  1 Query tennisQuery = mock(Query.class);
102  1 when(tennisQuery.setOffset(0)).thenReturn(tennisQuery);
103  1 when(tennisQuery.setOffset(100)).thenReturn(emptyQuery);
104  1 when(tennisQuery.execute()).thenReturn(Arrays.<Object> asList(
105    new Object[] {"Main", "Welcome", "en", "2.1"},
106    new Object[] {"XWiki.Syntax", "Links", "fr", "1.3"}));
107   
108  1 DocumentReference tennisMainWelcome =
109    createDocumentReference("tennis", Arrays.asList("Main"), "Welcome", Locale.ENGLISH);
110  1 DocumentReference tennisXWikiSyntaxLinks =
111    createDocumentReference("tennis", Arrays.asList("XWiki", "Syntax"), "Links", Locale.FRENCH);
112   
113  1 Query query = mock(Query.class);
114  1 when(query.setLimit(anyInt())).thenReturn(query);
115  1 when(query.getNamedParameters()).thenReturn(Collections.<String, Object> emptyMap());
116  1 when(query.setWiki("chess")).thenReturn(chessQuery);
117  1 when(query.setWiki("tennis")).thenReturn(tennisQuery);
118   
119  1 Query chessCountQuery = mock(Query.class);
120  1 when(chessCountQuery.execute()).thenReturn(Collections.<Object> singletonList(3L));
121   
122  1 Query tennisCountQuery = mock(Query.class);
123  1 when(tennisCountQuery.execute()).thenReturn(Collections.<Object> singletonList(2L));
124   
125  1 Query countQuery = mock(Query.class);
126  1 when(countQuery.addFilter(mocker.<QueryFilter> getInstance(QueryFilter.class, "count"))).thenReturn(countQuery);
127  1 when(countQuery.setWiki("chess")).thenReturn(chessCountQuery);
128  1 when(countQuery.setWiki("tennis")).thenReturn(tennisCountQuery);
129   
130  1 QueryManager queryManager = mocker.getInstance(QueryManager.class);
131  1 when(queryManager.createQuery("select doc.space, doc.name, doc.language, doc.version from XWikiDocument doc"
132    + " order by doc.space, doc.name, doc.language", Query.HQL)).thenReturn(query);
133  1 when(queryManager.createQuery("", Query.HQL)).thenReturn(countQuery);
134   
135  1 DocumentIterator<String> iterator = mocker.getComponentUnderTest();
136   
137  1 assertEquals(5L, iterator.size());
138   
139  1 List<Pair<DocumentReference, String>> actualResults = new ArrayList<Pair<DocumentReference, String>>();
140  6 while (iterator.hasNext()) {
141  5 actualResults.add(iterator.next());
142    }
143   
144  1 List<Pair<DocumentReference, String>> expectedResults = new ArrayList<Pair<DocumentReference, String>>();
145  1 expectedResults.add(new ImmutablePair<DocumentReference, String>(chessBlogCodeWebHome, "3.2"));
146  1 expectedResults.add(new ImmutablePair<DocumentReference, String>(chessMainWelcome, "1.1"));
147  1 expectedResults.add(new ImmutablePair<DocumentReference, String>(chessXWikiSyntaxLinks, "2.5"));
148  1 expectedResults.add(new ImmutablePair<DocumentReference, String>(tennisMainWelcome, "2.1"));
149  1 expectedResults.add(new ImmutablePair<DocumentReference, String>(tennisXWikiSyntaxLinks, "1.3"));
150   
151  1 assertEquals(expectedResults, actualResults);
152    }
153   
 
154  1 toggle @Test
155    public void iterateOneWiki() throws Exception
156    {
157  1 DocumentReference rootReference = createDocumentReference("gang", Arrays.asList("A", "B"), "C", null);
158   
159  1 Query emptyQuery = mock(Query.class);
160  1 when(emptyQuery.execute()).thenReturn(Collections.emptyList());
161   
162  1 Query query = mock(Query.class);
163  1 when(query.setLimit(anyInt())).thenReturn(query);
164  1 when(query.setWiki(rootReference.getWikiReference().getName())).thenReturn(query);
165  1 when(query.setOffset(0)).thenReturn(query);
166  1 when(query.setOffset(100)).thenReturn(emptyQuery);
167  1 when(query.execute()).thenReturn(Collections.<Object>singletonList(new Object[] {"A.B", "C", "de", "3.1"}));
168   
169  1 Map<String, Object> namedParameters = new HashMap<String, Object>();
170  1 namedParameters.put("space", "A.B");
171  1 namedParameters.put("name", "C");
172  1 when(query.getNamedParameters()).thenReturn(namedParameters);
173   
174  1 Query countQuery = mock(Query.class);
175  1 when(countQuery.addFilter(mocker.<QueryFilter> getInstance(QueryFilter.class, "count"))).thenReturn(countQuery);
176   
177  1 QueryManager queryManager = mocker.getInstance(QueryManager.class);
178  1 String whereClause = " where doc.space = :space and doc.name = :name";
179  1 when(queryManager.createQuery("select doc.space, doc.name, doc.language, doc.version from XWikiDocument doc"
180    + whereClause + " order by doc.space, doc.name, doc.language", Query.HQL)).thenReturn(query);
181  1 when(queryManager.createQuery(whereClause, Query.HQL)).thenReturn(countQuery);
182   
183  1 DocumentIterator<String> iterator = mocker.getComponentUnderTest();
184  1 iterator.setRootReference(rootReference);
185   
186  1 List<Pair<DocumentReference, String>> actualResults = new ArrayList<Pair<DocumentReference, String>>();
187  2 while (iterator.hasNext()) {
188  1 actualResults.add(iterator.next());
189    }
190   
191  1 List<Pair<DocumentReference, String>> expectedResults = new ArrayList<Pair<DocumentReference, String>>();
192  1 expectedResults.add(new ImmutablePair<DocumentReference, String>(new DocumentReference(rootReference,
193    Locale.GERMAN), "3.1"));
194   
195  1 assertEquals(expectedResults, actualResults);
196   
197  1 verify(query).bindValue("space", "A.B");
198  1 verify(query).bindValue("name", "C");
199   
200  1 verify(countQuery).bindValue("space", "A.B");
201  1 verify(countQuery).bindValue("name", "C");
202    }
203   
 
204  6 toggle private DocumentReference createDocumentReference(String wiki, List<String> spaces, String name, Locale locale)
205    {
206  6 DocumentReference documentReference = new DocumentReference(wiki, spaces, name);
207  6 if (locale != null) {
208  4 documentReference = new DocumentReference(documentReference, locale);
209    }
210  6 String localSpaceReference = StringUtils.join(spaces, '.');
211  6 when(this.localEntityReferenceSerializer.serialize(documentReference.getParent())).thenReturn(
212    localSpaceReference);
213  6 when(
214    this.explicitEntityReferenceResolver.resolve(localSpaceReference, EntityType.SPACE,
215    documentReference.getWikiReference())).thenReturn(documentReference.getParent());
216  6 return documentReference;
217    }
218    }