This project has retired. For details please refer to its Attic page.
QueryTranslatorTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.chemistry.opencmis.jcr.query;
21  
22  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
23  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
24  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
25  import org.apache.chemistry.opencmis.jcr.JcrTypeManager;
26  import org.apache.chemistry.opencmis.jcr.PathManager;
27  import org.apache.chemistry.opencmis.jcr.impl.DefaultDocumentTypeHandler;
28  import org.apache.chemistry.opencmis.jcr.impl.DefaultFolderTypeHandler;
29  import org.apache.chemistry.opencmis.jcr.impl.DefaultUnversionedDocumentTypeHandler;
30  import org.apache.chemistry.opencmis.jcr.type.JcrTypeHandlerManager;
31  import org.apache.chemistry.opencmis.jcr.util.ISO8601;
32  import org.apache.chemistry.opencmis.server.support.query.CalendarHelper;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import java.util.GregorianCalendar;
37  
38  import static org.junit.Assert.*;
39  
40  public class QueryTranslatorTest {
41      private String jcrTypeCondition;
42  
43      private QueryTranslator queryTranslator;
44  
45      @Before
46      public void setUp() throws Exception {
47          JcrTypeManager typeManager = new JcrTypeManager();
48          PathManager pathManager = new PathManager(PathManager.CMIS_ROOT_PATH);
49          JcrTypeHandlerManager typeHandlerManager = new JcrTypeHandlerManager(pathManager, typeManager);
50          typeHandlerManager.addHandler(new DefaultFolderTypeHandler());
51          typeHandlerManager.addHandler(new DefaultDocumentTypeHandler());
52          typeHandlerManager.addHandler(new DefaultUnversionedDocumentTypeHandler());
53  
54          queryTranslator = new QueryTranslator(typeManager) {
55  
56              @Override
57              protected String jcrPathFromId(String id) {
58                  assertNotNull(id);
59                  return "/jcr:" + id;
60              }
61  
62              @Override
63              protected String jcrPathFromCol(TypeDefinition fromType, String name) {
64                  assertNotNull(fromType);
65                  assertNotNull(name);
66                  return name.replace("cmis:", "@jcr:");
67              }
68  
69              @Override
70              protected String jcrTypeName(TypeDefinition fromType) {
71                  assertNotNull(fromType);
72                  return fromType.getQueryName().replace("cmis:", "jcr:");
73              }
74  
75              @Override
76              protected String jcrTypeCondition(TypeDefinition fromType) {
77                  assertNotNull(fromType);
78                  return jcrTypeCondition;
79              }
80          };
81      }
82  
83      @Test
84      public void testQueryTranslator() {
85          assertEquals(
86              "/jcr:root//element(*,jcr:document)",
87              queryTranslator.translateToXPath("select * from cmis:document"));
88  
89          assertEquals(
90              "/jcr:root//element(*,jcr:document)[@jcr:isLatestVersion = 'foo']",
91              queryTranslator.translateToXPath("select * from cmis:document where cmis:isLatestVersion='foo'"));
92  
93          assertEquals(
94              "/jcr:root//element(*,jcr:document)[jcr:like(@jcr:isLatestVersion, 'foo')]",
95              queryTranslator.translateToXPath("select * from cmis:document where cmis:isLatestVersion LIKE 'foo'"));
96  
97          assertEquals(
98              "/jcr:root//element(*,jcr:document)[not(jcr:like(@jcr:isLatestVersion, 'foo'))]",
99              queryTranslator.translateToXPath(
100                     "select * from cmis:document where cmis:isLatestVersion NOT LIKE 'foo'"));
101 
102         assertEquals(
103             "/jcr:root//element(*,jcr:document)[@jcr:isLatestVersion = 'foo' and @jcr:name != 'baz']",
104             queryTranslator.translateToXPath(
105                     "select * from cmis:document where cmis:isLatestVersion='foo' AND cmis:name<>'baz'"));
106 
107         assertEquals(
108             "/jcr:root//element(*,jcr:document)[not((@jcr:isLatestVersion > 'foo' or @jcr:name < 1.0))]",
109             queryTranslator.translateToXPath(
110                     "select * from cmis:document where NOT (cmis:isLatestVersion>'foo' OR cmis:name< 1.0)"));
111 
112         assertEquals(
113             "/jcr:root//element(*,jcr:document)[(@jcr:name = 'foo' or @jcr:objectId = 'baz' and @jcr:createdBy = 'bar')]",
114             queryTranslator.translateToXPath(
115                     "select * from cmis:document where cmis:name = 'foo' or cmis:objectId = 'baz' " +
116                     "and cmis:createdBy = 'bar'"));
117 
118         assertEquals(
119             "/jcr:root//element(*,jcr:document)[(@jcr:name = 'foo' and @jcr:objectId = 'baz' or @jcr:createdBy = 'bar')]",
120             queryTranslator.translateToXPath(
121                     "select * from cmis:document where cmis:name = 'foo' and cmis:objectId = 'baz' " +
122                     "or cmis:createdBy = 'bar'"));
123 
124         assertEquals(
125             "/jcr:root//element(*,jcr:document)[@jcr:name = 'foo' and (@jcr:objectId = 'baz' or @jcr:createdBy = 'bar')]",
126             queryTranslator.translateToXPath(
127                 "select * from cmis:document where cmis:name = 'foo' and (cmis:objectId = 'baz' " +
128                 "or cmis:createdBy = 'bar')"));
129 
130         assertEquals(
131                 "/jcr:root/jcr:folderId/element(*,jcr:document)",
132                 queryTranslator.translateToXPath(
133                         "select * from cmis:document where IN_FOLDER('folderId')"));
134 
135         assertEquals(
136             "/jcr:root/jcr:folderId/element(*,jcr:document)",
137             queryTranslator.translateToXPath("select * from cmis:document where not(not(IN_FOLDER('folderId')))"));
138 
139         assertEquals(
140             "/jcr:root/jcr:folderId//element(*,jcr:document)",
141             queryTranslator.translateToXPath("select * from cmis:document where IN_TREE('folderId')"));
142 
143         assertEquals(
144             "/jcr:root/jcr:folderId//element(*,jcr:document)",
145             queryTranslator.translateToXPath("select * from cmis:document where not(not(IN_TREE('folderId')))"));
146 
147         assertEquals(
148             "/jcr:root/jcr:folderId/element(*,jcr:document)[@jcr:name <= 1]",
149             queryTranslator.translateToXPath(
150                     "select * from cmis:document where IN_FOLDER('folderId') AND cmis:name <= 1"));
151 
152         assertEquals(
153             "/jcr:root/jcr:folderId//element(*,jcr:document)[@jcr:name >= 'name' and @jcr:name = true]",
154             queryTranslator.translateToXPath(
155                     "select * from cmis:document where IN_TREE('folderId') AND cmis:name >= 'name' " +
156                     "AND cmis:name = TRUE"));
157 
158         GregorianCalendar date = new GregorianCalendar();
159         assertEquals(
160             "/jcr:root/jcr:folderId/element(*,jcr:document)[not(@jcr:creationDate = xs:dateTime('" +
161                     ISO8601.format(date) + "'))]",
162             queryTranslator.translateToXPath(
163                     "select * from cmis:document where NOT(NOT IN_FOLDER('folderId') OR cmis:creationDate = TIMESTAMP '" +
164                     CalendarHelper.toString(date) + "')"));
165 
166 // TODO: adjust to full text parser
167 //        assertEquals(
168 //            "/jcr:root//element(*,jcr:document)[jcr:contains(., '\u4E2D\u6587')]",
169 //            queryTranslator.translateToXPath("select * from cmis:document where contains('\u4E2D\u6587')"));
170     }
171 
172     @Test
173     public void testQueryWithOrderBy() {
174         assertEquals(
175             "/jcr:root//element(*,jcr:document)order by @jcr:name ascending",
176             queryTranslator.translateToXPath("select * from cmis:document order by cmis:name"));
177 
178         assertEquals(
179             "/jcr:root//element(*,jcr:document)[@jcr:isLatestVersion = 'foo']order by @jcr:name descending",
180             queryTranslator.translateToXPath(
181                 "select * from cmis:document where cmis:isLatestVersion='foo' order by cmis:name desc"));
182 
183         assertEquals(
184             "/jcr:root//element(*,jcr:document)[jcr:like(@jcr:isLatestVersion, 'foo')]order by @jcr:name ascending," +
185                 "@jcr:objectId descending",
186             queryTranslator.translateToXPath(
187                 "select * from cmis:document where cmis:isLatestVersion LIKE 'foo' order by cmis:name asc, cmis:objectId desc"));
188     }
189 
190     @Test
191     public void testQueryTranslatorWithTypeCondition() {
192         jcrTypeCondition = "@jcr:primaryType = nt:base";
193 
194         assertEquals(
195             "/jcr:root//element(*,jcr:document)[@jcr:primaryType = nt:base]",
196             queryTranslator.translateToXPath("select * from cmis:document"));
197 
198         assertEquals(
199             "/jcr:root//element(*,jcr:document)[@jcr:primaryType = nt:base and @jcr:isLatestVersion = 'foo']",
200             queryTranslator.translateToXPath("select * from cmis:document where cmis:isLatestVersion='foo'"));
201 
202         assertEquals(
203             "/jcr:root/jcr:folderId/element(*,jcr:document)[@jcr:primaryType = nt:base]",
204             queryTranslator.translateToXPath("select * from cmis:document where IN_FOLDER('folderId')"));
205 
206         assertEquals(
207             "/jcr:root/jcr:folderId/element(*,jcr:document)[@jcr:primaryType = nt:base]",
208             queryTranslator.translateToXPath("select * from cmis:document where not(not(IN_FOLDER('folderId')))"));
209 
210         assertEquals(
211             "/jcr:root/jcr:folderId//element(*,jcr:document)[@jcr:primaryType = nt:base]",
212             queryTranslator.translateToXPath("select * from cmis:document where IN_TREE('folderId')"));
213 
214         assertEquals(
215             "/jcr:root/jcr:folderId//element(*,jcr:document)[@jcr:primaryType = nt:base]",
216             queryTranslator.translateToXPath("select * from cmis:document where not(not(IN_TREE('folderId')))"));
217 
218         assertEquals(
219             "/jcr:root/jcr:folderId/element(*,jcr:document)[@jcr:primaryType = nt:base and @jcr:name <= 1]",
220             queryTranslator.translateToXPath(
221                     "select * from cmis:document where IN_FOLDER('folderId') AND cmis:name <= 1"));
222     }
223 
224     @Test
225     public void testQueryTranslatorQueryTooSpecific() {
226         try {
227             queryTranslator.translateToXPath(
228                 "select * from cmis:document where NOT IN_FOLDER('folderId')");
229             fail();
230         }
231         catch (CmisInvalidArgumentException expected) { }
232 
233         try {
234             queryTranslator.translateToXPath(
235                 "select * from cmis:document where NOT(NOT IN_FOLDER('folderId') AND cmis:name = 'name')");
236             fail();
237         }
238         catch (CmisInvalidArgumentException expected) { }
239 
240         try {
241             queryTranslator.translateToXPath(
242                 "select * from cmis:document where IN_FOLDER('folderId') OR cmis:name = 'name'");
243             fail();
244         }
245         catch (CmisInvalidArgumentException expected) { }
246 
247         try {
248             queryTranslator.translateToXPath(
249                 "select * from cmis:document where NOT(IN_FOLDER('folderId') AND cmis:name = 'name')");
250             fail();
251         }
252         catch (CmisInvalidArgumentException expected) { }
253 
254         try {
255             queryTranslator.translateToXPath(
256                 "select * from cmis:document where IN_FOLDER('folder1Id') OR IN_TREE('folder2Id')");
257             fail();
258         }
259         catch (CmisInvalidArgumentException expected) { }
260 
261         try {
262             queryTranslator.translateToXPath(
263                 "select * from cmis:document where IN_FOLDER('folder1Id') AND NOT IN_TREE('folder2Id')");
264             fail();
265         }
266         catch (CmisInvalidArgumentException expected) { }
267     }
268 
269     @Test
270     public void testNotImplemented() {
271         try {
272             queryTranslator.translateToXPath("select * from cmis:document where cmis:name in (1,2,3)");
273             fail();
274         }
275         catch (CmisNotSupportedException expected) {}
276 
277         try {
278             queryTranslator.translateToXPath("select * from cmis:document where 'foo' = ANY cmis:name");
279             fail();
280         }
281         catch (CmisNotSupportedException expected) {}
282     }
283 
284     @Test
285     public void testInvalidQuery() {
286         try {
287             queryTranslator.translateToXPath("");
288             fail();
289         } catch (CmisInvalidArgumentException expected) {
290         }
291 
292         try {
293             queryTranslator.translateToXPath("select * from cmis:something");
294             fail();
295         } catch (CmisInvalidArgumentException expected) {
296         }
297 
298         try {
299             queryTranslator.translateToXPath("select * from cmis:document WHERE");
300             fail();
301         } catch (CmisInvalidArgumentException expected) {
302         }
303 
304         try {
305             queryTranslator.translateToXPath("select * from cmis:document WHERE cmis:something = 'foo'");
306             fail();
307         } catch (CmisInvalidArgumentException expected) {
308         }
309     }
310 
311 }