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.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.chemistry.opencmis.client.api.Document;
29  import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
30  import org.apache.chemistry.opencmis.client.api.Folder;
31  import org.apache.chemistry.opencmis.client.api.ObjectId;
32  import org.apache.chemistry.opencmis.client.api.QueryResult;
33  import org.apache.chemistry.opencmis.client.api.QueryStatement;
34  import org.apache.chemistry.opencmis.client.api.Session;
35  import org.apache.chemistry.opencmis.commons.PropertyIds;
36  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
37  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
38  import org.apache.chemistry.opencmis.tck.CmisTestResult;
39  
40  public class QueryInFolderTest extends AbstractQueryTest {
41  
42      private static final String CONTENT = "TCK test content.";
43  
44      private static final int LEVEL1_DOCS = 5;
45      private static final int LEVEL1_FOLDERS = 5;
46      private static final int LEVEL2_DOCS = 5;
47      private static final int LEVEL2_FOLDERS = 5;
48  
49      @Override
50      public void init(Map<String, String> parameters) {
51          super.init(parameters);
52          setName("Query IN_FOLDER and IN_TREE Test");
53          setDescription("Performs IN_FOLDER and IN_TREE queries.");
54      }
55  
56      @Override
57      public void run(Session session) {
58          if (supportsQuery(session) && !isFulltextOnly(session)) {
59              // create a test folder
60              Folder testFolder = createTestFolder(session);
61  
62              try {
63                  Set<String> topLevelDocs = new HashSet<String>();
64                  Set<String> topLevelFolders = new HashSet<String>();
65  
66                  // create documents
67                  for (int i = 0; i < LEVEL1_DOCS; i++) {
68                      Document newDocument = createDocument(session, testFolder, "doc" + i, CONTENT);
69                      topLevelDocs.add(newDocument.getId());
70                  }
71  
72                  // create folders
73                  for (int i = 0; i < LEVEL1_FOLDERS; i++) {
74                      Folder newFolder = createFolder(session, testFolder, "folder" + i);
75                      topLevelFolders.add(newFolder.getId());
76  
77                      for (int j = 0; j < LEVEL2_DOCS; j++) {
78                          createDocument(session, newFolder, "doc" + j, CONTENT);
79                      }
80  
81                      for (int j = 0; j < LEVEL2_FOLDERS; j++) {
82                          createFolder(session, newFolder, "folder" + j);
83                      }
84                  }
85  
86                  doQuery(session, testFolder, "cmis:document", false, topLevelDocs, topLevelFolders);
87                  doQuery(session, testFolder, "cmis:document", true, topLevelDocs, topLevelFolders);
88                  doQuery(session, testFolder, "cmis:folder", false, topLevelDocs, topLevelFolders);
89                  doQuery(session, testFolder, "cmis:folder", true, topLevelDocs, topLevelFolders);
90              } finally {
91                  // delete the test folder
92                  deleteTestFolder();
93              }
94          } else {
95              addResult(createResult(SKIPPED, "Metadata query not supported. Test Skipped!"));
96          }
97      }
98  
99      private void doQuery(Session session, ObjectId testFolder, String type, boolean deep, Set<String> topLevelDocs,
100             Set<String> topLevelFolders) {
101         CmisTestResult f;
102 
103         String inWhat = (deep ? "IN_TREE" : "IN_FOLDER");
104 
105         QueryStatement statement = session.createQueryStatement("SELECT ? FROM ? WHERE " + inWhat + "(?)");
106         statement.setProperty(1, type, PropertyIds.OBJECT_ID);
107         statement.setType(2, type);
108         statement.setString(3, testFolder.getId());
109 
110         addResult(createInfoResult("Query: " + statement.toQueryString()));
111 
112         try {
113             int count = 0;
114 
115             for (QueryResult qr : statement.query(false).getPage(100)) {
116                 count++;
117 
118                 String objectId = qr.getPropertyValueByQueryName("cmis:objectId");
119 
120                 f = createResult(FAILURE, inWhat + " query returned an invalid object ID!");
121                 addResult(assertStringNotEmpty(objectId, null, f));
122 
123                 FileableCmisObject object = null;
124                 try {
125                     object = (FileableCmisObject) session.getObject(objectId);
126                 } catch (CmisObjectNotFoundException onf) {
127                     addResult(createResult(FAILURE, inWhat
128                             + " query returned an object ID of an object that doesn't exist!"));
129                 }
130 
131                 if (!deep && object != null) {
132                     f = createResult(FAILURE, inWhat + " query returned an object that should not be there!");
133                     addResult(assertIsTrue(
134                             topLevelDocs.contains(object.getId()) || topLevelFolders.contains(object.getId()), null, f));
135 
136                     boolean found = false;
137                     for (Folder parent : object.getParents()) {
138                         if (testFolder.getId().equals(parent.getId())) {
139                             found = true;
140                             break;
141                         }
142                     }
143 
144                     if (!found) {
145                         addResult(createResult(FAILURE, inWhat
146                                 + " query returned an object, which hasn't the test folder as a parent folder!"));
147                     }
148                 }
149             }
150 
151             addResult(createInfoResult("Hits: " + count));
152         } catch (CmisBaseException e) {
153             addResult(createResult(FAILURE,
154                     inWhat + " query failed: " + e.getClass().getSimpleName() + ": " + e.getMessage(), e, false));
155         }
156     }
157 }