View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.functions;
35  
36  import static info.magnolia.test.hamcrest.ExceptionMatcher.strictlyInstanceOf;
37  import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException;
38  import static org.hamcrest.Matchers.*;
39  import static org.junit.Assert.*;
40  
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.jcr.util.ContentMap;
43  import info.magnolia.jcr.util.NodeTypes;
44  import info.magnolia.jcr.util.NodeUtil;
45  import info.magnolia.repository.RepositoryConstants;
46  import info.magnolia.test.RepositoryTestCase;
47  import info.magnolia.test.hamcrest.Execution;
48  
49  import java.util.Arrays;
50  import java.util.Collection;
51  import java.util.List;
52  
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  import javax.jcr.Session;
56  
57  import org.hamcrest.collection.IsIterableContainingInOrder;
58  import org.junit.Test;
59  
60  /**
61   * Tests for {@link SearchTemplatingFunctions}.
62   */
63  public class SearchTemplatingFunctionsTest extends RepositoryTestCase {
64  
65      private SearchTemplatingFunctions searchFunctions = new SearchTemplatingFunctions();
66  
67      @Test
68      public void checkWorkspaceIsNotBlank() throws Exception {
69          // WHEN
70          // THEN
71          assertThat(new Execution() {
72              @Override
73              public void evaluate() throws Exception {
74                  searchFunctions.searchContent(null, "foo", "/", "foo:bar");
75              }
76          }, throwsAnException(strictlyInstanceOf(IllegalArgumentException.class).withMessage(containsString("can't be null or empty"))));
77      }
78  
79      @Test
80      public void checkQueryStringIsNotBlank() throws Exception {
81          // WHEN
82          // THEN
83          assertThat(new Execution() {
84              @Override
85              public void evaluate() throws Exception {
86                  searchFunctions.searchContent("baz", " ", "/", "foo:bar");
87              }
88          }, throwsAnException(strictlyInstanceOf(IllegalArgumentException.class).withMessage(containsString("can't be null or empty"))));
89      }
90  
91      @Test
92      public void checkStartPathIsNotBlank() throws Exception {
93          // WHEN
94          // THEN
95          assertThat(new Execution() {
96              @Override
97              public void evaluate() throws Exception {
98                  searchFunctions.searchContent("baz", "foo", null, "foo:bar");
99              }
100         }, throwsAnException(strictlyInstanceOf(IllegalArgumentException.class).withMessage(containsString("can't be null or empty"))));
101     }
102 
103     @Test
104     public void checkLimitIsGreaterThanZero() throws Exception {
105         // WHEN
106         // THEN
107         assertThat(new Execution() {
108             @Override
109             public void evaluate() throws Exception {
110                 searchFunctions.searchContent("baz", "foo", "/", "foo:bar", 0, 100);
111             }
112         }, throwsAnException(strictlyInstanceOf(IllegalArgumentException.class).withMessage(containsString("must be greater than 0"))));
113     }
114 
115     @Test
116     public void checkOffsetIsGreaterThanOrEqualsToZero() throws Exception {
117         // WHEN
118         // THEN
119         assertThat(new Execution() {
120             @Override
121             public void evaluate() throws Exception {
122                 searchFunctions.searchContent("baz", "foo", "/", "foo:bar", 10, -1);
123             }
124         }, throwsAnException(strictlyInstanceOf(IllegalArgumentException.class).withMessage(containsString("must be greater than or equal to 0"))));
125     }
126 
127     @Test
128     public void searchContentOfRootNode() throws Exception {
129         // GIVEN
130         Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
131         Node node = NodeUtil.createPath(session.getRootNode(), "someNodePath", NodeTypes.ContentNode.NAME);
132         node.setProperty("someProperty", "someValue");
133         session.save();
134 
135         // WHEN
136         Collection<ContentMap> contentMaps = searchFunctions.searchContent(RepositoryConstants.CONFIG, "someValue", "/", null);
137 
138         // THEN
139         assertThat(contentMaps, hasSize(1));
140     }
141 
142     @Test
143     public void orderByJcrScoreDescSortsCorrectly() throws Exception {
144         // GIVEN
145         Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
146         Node node1 = NodeUtil.createPath(session.getRootNode(), "path1", NodeTypes.Page.NAME);
147         node1.setProperty("title", "aaa aaa");
148         Node node2 = NodeUtil.createPath(session.getRootNode(), "path2", NodeTypes.Page.NAME);
149         node2.setProperty("title", "aaa aaa aaa");
150         Node node3 = NodeUtil.createPath(session.getRootNode(), "path3", NodeTypes.Page.NAME);
151         node3.setProperty("title", "aaa");
152         session.save();
153 
154         // WHEN
155         Collection<ContentMap> contentMaps = searchFunctions.searchContent(RepositoryConstants.CONFIG, "aaa", "/", null);
156 
157         // THEN
158         List<String> expected = Arrays.asList(node2.getName(), node1.getName(), node3.getName());
159         String[] actual = contentMaps.stream().map(contentMap -> {
160             try {
161                 return contentMap.getJCRNode().getName();
162             } catch (RepositoryException e) {
163                 throw new RuntimeException(e);
164             }
165         }).toArray(String[]::new);
166 
167         assertThat(expected, IsIterableContainingInOrder.contains(actual));
168     }
169 }