View Javadoc

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  package org.apache.chemistry.opencmis.tck.tests.query;
20  
21  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.SKIPPED;
23  
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.api.Folder;
28  import org.apache.chemistry.opencmis.client.api.ItemIterable;
29  import org.apache.chemistry.opencmis.client.api.QueryResult;
30  import org.apache.chemistry.opencmis.client.api.QueryStatement;
31  import org.apache.chemistry.opencmis.client.api.Session;
32  import org.apache.chemistry.opencmis.commons.PropertyIds;
33  import org.apache.chemistry.opencmis.tck.CmisTestResult;
34  
35  public class QueryPagingTest extends AbstractQueryTest {
36  
37      private static final String CONTENT = "TCK test content.";
38  
39      private static final int NUM_DOCS = 20;
40  
41      @Override
42      public void init(Map<String, String> parameters) {
43          super.init(parameters);
44          setName("Query Paging Test");
45          setDescription("Performs IN_FOLDER queries with pages and checks if the page sizes are correct.");
46      }
47  
48      @Override
49      public void run(Session session) {
50          CmisTestResult f;
51  
52          if (supportsQuery(session) && !isFulltextOnly(session)) {
53              // create a test folder
54              Folder testFolder = createTestFolder(session);
55  
56              try {
57                  // create documents
58                  for (int i = 0; i < NUM_DOCS; i++) {
59                      createDocument(session, testFolder, "doc" + i, CONTENT);
60                  }
61  
62                  // query the folder
63                  QueryStatement statement = session.createQueryStatement("SELECT ? FROM ? WHERE IN_FOLDER(?)");
64                  statement.setProperty(1, "cmis:document", PropertyIds.OBJECT_ID);
65                  statement.setType(2, "cmis:document");
66                  statement.setString(3, testFolder.getId());
67  
68                  // first page
69                  ItemIterable<QueryResult> iter1 = statement.query(false).skipTo(0).getPage(5);
70                  int count1 = countResults(iter1);
71                  long total1 = iter1.getTotalNumItems();
72  
73                  f = createResult(FAILURE,
74                          "Repository returned more hits than requested for the first test page! (maxItems=5, returned="
75                                  + count1 + ")");
76                  addResult(assertIsTrue(count1 <= 5, null, f));
77  
78                  f = createInfoResult("Repository did return fewer hits than requested for the first test page. (maxItems=5, returned="
79                          + count1 + ")");
80                  addResult(assertIsTrue(count1 >= 5, null, f));
81  
82                  if (total1 == -1) {
83                      addResult(createInfoResult("Repository did not return numItems for the first test page."));
84                  } else {
85                      f = createResult(FAILURE, "Returned numItems doesn't match the number of documents!");
86                      addResult(assertEquals((long) NUM_DOCS, total1, null, f));
87                  }
88  
89                  // second page
90                  ItemIterable<QueryResult> iter2 = statement.query(false).skipTo(5).getPage(10);
91                  int count2 = countResults(iter2);
92                  long total2 = iter2.getTotalNumItems();
93  
94                  f = createResult(FAILURE,
95                          "Repository returned more hits than requested for the second test page! (maxItems=10, returned="
96                                  + count2 + ")");
97                  addResult(assertIsTrue(count2 <= 10, null, f));
98  
99                  f = createInfoResult("Repository did return fewer hits than requested for the second test page. (maxItems=5, returned="
100                         + count2 + ")");
101                 addResult(assertIsTrue(count2 >= 10, null, f));
102 
103                 if (total2 == -1) {
104                     addResult(createInfoResult("Repository did not return numItems for the second test page."));
105                 } else {
106                     f = createResult(FAILURE, "Returned numItems doesn't match the number of documents!");
107                     addResult(assertEquals((long) NUM_DOCS, total2, null, f));
108                 }
109             } finally {
110                 // delete the test folder
111                 deleteTestFolder();
112             }
113         } else {
114             addResult(createResult(SKIPPED, "Metadata query not supported. Test Skipped!"));
115         }
116     }
117 
118     private int countResults(ItemIterable<QueryResult> iter) {
119         int count = 0;
120         for (Iterator<QueryResult> iterator = iter.iterator(); iterator.hasNext();) {
121             iterator.next();
122             count++;
123         }
124 
125         return count;
126     }
127 }