View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.models.util;
35  
36  import static org.hamcrest.CoreMatchers.*;
37  import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
38  import static org.junit.Assert.*;
39  import static org.mockito.Mockito.*;
40  
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.context.WebContext;
43  import info.magnolia.jcr.util.NodeUtil;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  import javax.jcr.Node;
49  import javax.jcr.Property;
50  import javax.jcr.RepositoryException;
51  
52  import org.junit.After;
53  import org.junit.Before;
54  import org.junit.Test;
55  
56  /**
57   * Tests for {@link Pagination}.
58   *
59   * Offset count test cases.
60   */
61  public class PaginationTest {
62  
63      private WebContext context;
64  
65      @Before
66      public void setUp() {
67          context = mock(WebContext.class);
68          MgnlContext.setInstance(context);
69      }
70  
71      @After
72      public void tearDown() {
73          MgnlContext.setInstance(null);
74      }
75  
76      @Test
77      public void testPageOffset() {
78          // GIVEN
79          final Pagination pager = new Pagination();
80  
81          // WHEN
82          pager.setCount(3);
83          pager.setResults(10000);
84  
85          // THEN
86          assertEquals(0, pager.getOffset());
87  
88          // WHEN
89          pager.setCount(55);
90          pager.setResults(30);
91  
92          // THEN
93          assertEquals(0, pager.getOffset());
94      }
95  
96      @Test
97      public void testPageOffsetWithCurrentPage() {
98          // GIVEN
99          final TestPagination pager = new TestPagination();
100 
101         // WHEN
102         pager.setCount(20);
103         pager.setResults(30);
104         pager.setCurrentPage(2);
105 
106         // THEN
107         assertEquals(0, pager.getOffset());
108 
109         // WHEN
110         pager.setCount(55);
111         pager.setResults(10);
112         pager.setCurrentPage(8);
113 
114         // THEN
115         assertEquals(50, pager.getOffset());
116     }
117 
118     @Test
119     public void getPageLinkWithLinkAndWithoutQueryString() throws RepositoryException {
120         // GIVEN
121         final int page = 1;
122         final String currentLink = "http://www.demo-link/to-a-page/and-a-subpage.html";
123         final Pagination pagination = new Pagination(currentLink, getMockContentItems(0), mock(Node.class));
124 
125         // WHEN
126         final String link = pagination.getPageLink(page);
127 
128         // THEN
129         assertThat(link, is(String.format("%s?%s=%d", currentLink, Pagination.PARAMETER_CURRENT_PAGE, page)));
130     }
131 
132     @Test
133     public void getPageLinkWithLinkAndWithQueryStringToSelf() throws RepositoryException {
134         // GIVEN
135         final int page = 1;
136         final String currentLink = String.format("http://www.demo-link/to-a-page/and-a-subpage.html?%s=%d", Pagination.PARAMETER_CURRENT_PAGE, page);
137         final Pagination pagination = new Pagination(currentLink, getMockContentItems(0), mock(Node.class));
138 
139         // WHEN
140         final String link = pagination.getPageLink(page);
141 
142         // THEN
143         assertThat(link, is(currentLink));
144     }
145 
146     @Test
147     public void getPageLinkWithLinkAndWithQueryStringAndOtherParam() throws RepositoryException {
148         // GIVEN
149         final String currentLink = String.format("http://www.demo-link/to-a-page/and-a-subpage.html?%s=1&otherParam=otherValue", Pagination.PARAMETER_CURRENT_PAGE);
150         final Pagination pagination = new Pagination(currentLink, getMockContentItems(0), mock(Node.class));
151 
152         // WHEN
153         final String link = pagination.getPageLink(2);
154 
155         // THEN
156         assertThat(link, containsString("otherParam=otherValue"));
157     }
158 
159     @Test
160     public void getPageLinkWithoutCurrentPage() throws RepositoryException {
161         // GIVEN
162         final int page = 3;
163         final String currentLink = "http://www.demo-link/to-a-page/and-a-subpage.html?otherParam=otherValue";
164         final Pagination pagination = new Pagination(currentLink, getMockContentItems(0), mock(Node.class));
165 
166         // WHEN
167         final String link = pagination.getPageLink(page);
168 
169         // THEN
170         assertThat(link, containsString("otherParam=otherValue"));
171         assertThat(link, containsString(String.format("%s=%d", Pagination.PARAMETER_CURRENT_PAGE, page)));
172     }
173 
174     @Test
175     public void getNumPagesWithEmptyList() throws RepositoryException {
176         // GIVEN
177         final Pagination pagination = new Pagination("", getMockContentItems(0), mock(Node.class));
178 
179         // WHEN
180         final int numPages = pagination.getNumPages();
181 
182         // THEN
183         assertThat(numPages, is(0));
184     }
185 
186     @Test
187     public void getNumPagesSmallerThanMax() throws RepositoryException {
188         // GIVEN
189         final Pagination pagination = new Pagination("", getMockContentItems(Pagination.DEFAULT_MAX_RESULTS), mock(Node.class));
190 
191         // WHEN
192         final int numPages = pagination.getNumPages();
193 
194         // THEN
195         assertThat(numPages, is(1));
196     }
197 
198     @Test
199     public void getNumPagesBiggerThanMax() throws RepositoryException {
200         // GIVEN
201         final Pagination pagination = new Pagination("", getMockContentItems(Pagination.DEFAULT_MAX_RESULTS + 1), mock(Node.class));
202 
203         // WHEN
204         final int numPages = pagination.getNumPages();
205 
206         // THEN
207         assertThat(numPages, is(2));
208     }
209 
210     @Test
211     public void getNumPagesWithMaxResultsSet() throws RepositoryException {
212         // GIVEN
213         final Node content = mock(Node.class);
214         final Property property = mock(Property.class);
215         when(property.getLong()).thenReturn(10L);
216         when(content.hasProperty(Pagination.PROPERTY_MAX_RESULTS)).thenReturn(true);
217         when(content.getProperty(Pagination.PROPERTY_MAX_RESULTS)).thenReturn(property);
218 
219         final Pagination pagination = new Pagination("", getMockContentItems(100), content);
220 
221         // WHEN
222         final int numPages = pagination.getNumPages();
223 
224         // THEN
225         assertThat(numPages, is(10));
226     }
227 
228     @Test
229     public void getBeginAndEndIndex() throws RepositoryException {
230         // GIVEN
231         final Node content = mock(Node.class);
232         final Property property = mock(Property.class);
233         when(property.getLong()).thenReturn(10L);
234         when(content.hasProperty(Pagination.PROPERTY_MAX_RESULTS)).thenReturn(true);
235         when(content.getProperty(Pagination.PROPERTY_MAX_RESULTS)).thenReturn(property);
236         final Pagination pagination = new Pagination("", getMockContentItems(100), content);
237 
238         assertThat(pagination.getCount(), is(100));
239 
240         // WHEN
241         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("2");
242 
243         // THEN
244         assertThat(pagination.getNumPages(), is(10));
245         assertThat(pagination.getCurrentPage(), is(2));
246         assertThat("With 100 items we expect to start with item 10 on page 2", pagination.getOffset(), is(10));
247         assertThat(pagination.getBeginIndex(), is(1));
248         assertThat(pagination.getEndIndex(), is(4));
249 
250         // WHEN
251         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("4");
252 
253         // THEN
254         assertThat(pagination.getNumPages(), is(10));
255         assertThat(pagination.getCurrentPage(), is(4));
256         assertThat("With 100 items we expect to start with item 30 on page 4", pagination.getOffset(), is(30));
257         assertThat(pagination.getBeginIndex(), is(2));
258         assertThat(pagination.getEndIndex(), is(6));
259 
260         // WHEN
261         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("9");
262 
263         // THEN
264         assertThat(pagination.getNumPages(), is(10));
265         assertThat(pagination.getCurrentPage(), is(9));
266         assertThat("With 100 items we expect to start with item 80 on page 9", pagination.getOffset(), is(80));
267         assertThat(pagination.getBeginIndex(), is(7));
268         assertThat(pagination.getEndIndex(), is(10));
269 
270         // WHEN
271         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("12");
272 
273         // THEN
274         assertThat(pagination.getNumPages(), is(10));
275         assertThat(pagination.getCurrentPage(), is(12));
276         assertThat("With 100 items we expect to start with item 100 on page 12", pagination.getOffset(), is(100));
277         assertThat(pagination.getBeginIndex(), is(10));
278         assertThat(pagination.getEndIndex(), is(10));
279     }
280 
281     @Test
282     public void getPageItems() throws RepositoryException {
283         // GIVEN
284         final Node content = mock(Node.class);
285         final Property property = mock(Property.class);
286         when(property.getLong()).thenReturn(10L);
287         when(content.hasProperty(Pagination.PROPERTY_MAX_RESULTS)).thenReturn(true);
288         when(content.getProperty(Pagination.PROPERTY_MAX_RESULTS)).thenReturn(property);
289         final Pagination pagination = new Pagination("", getMockContentItems(100), content);
290 
291         assertThat(pagination.getCount(), is(100));
292 
293         // WHEN
294         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("2");
295         List<Node> nodes = NodeUtil.asList(pagination.getPageItems());
296 
297         // THEN
298         assertThat(nodes, hasSize(10));
299         assertThat(nodes.get(0).getIdentifier(), is("10"));
300         assertThat(nodes.get(9).getIdentifier(), is("19"));
301 
302         // WHEN
303         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("7");
304         nodes = NodeUtil.asList(pagination.getPageItems());
305 
306         // THEN
307         assertThat(nodes, hasSize(10));
308         assertThat(nodes.get(0).getIdentifier(), is("60"));
309         assertThat(nodes.get(9).getIdentifier(), is("69"));
310 
311         // WHEN
312         when(context.getParameter(Pagination.PARAMETER_CURRENT_PAGE)).thenReturn("10");
313         nodes = NodeUtil.asList(pagination.getPageItems());
314 
315         // THEN
316         assertThat(nodes, hasSize(10));
317         assertThat(nodes.get(0).getIdentifier(), is("90"));
318         assertThat(nodes.get(9).getIdentifier(), is("99"));
319     }
320 
321     /**
322      * Returns a {@link java.util.List} of {@link javax.jcr.Node}s.
323      */
324     private List<Node> getMockContentItems(int num) throws RepositoryException {
325         final List<Node> items = new ArrayList<Node>();
326         for (int i = 0; i < num; i++) {
327             final Node node = mock(Node.class);
328             when(node.getName()).thenReturn(String.valueOf(i));
329             when(node.getPath()).thenReturn(String.format("/%d", i));
330             when(node.getIdentifier()).thenReturn(String.valueOf(i));
331             items.add(i, node);
332         }
333         return items;
334     }
335 
336     /**
337      * Test class of {@link Pagination}.
338      */
339     public class TestPagination extends Pagination {
340 
341         private int currentPage;
342         private int maxResultsPerPage;
343         private int count;
344 
345         @Override
346         protected int getOffset() {
347             int offset = ((currentPage - 1) * maxResultsPerPage);
348             if (offset > count) {
349                 int pages = count / maxResultsPerPage;
350                 offset = pages * maxResultsPerPage;
351             }
352             return offset;
353         }
354 
355         public void setCurrentPage(int currentPage) {
356             this.currentPage = currentPage;
357         }
358 
359         @Override
360         public void setResults(int maxResultsPerPage) {
361             this.maxResultsPerPage = maxResultsPerPage;
362         }
363 
364         @Override
365         public void setCount(int count) {
366             this.count = count;
367         }
368     }
369 
370 }