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.Map;
25  
26  import org.apache.chemistry.opencmis.client.api.Document;
27  import org.apache.chemistry.opencmis.client.api.Folder;
28  import org.apache.chemistry.opencmis.client.api.QueryResult;
29  import org.apache.chemistry.opencmis.client.api.QueryStatement;
30  import org.apache.chemistry.opencmis.client.api.Session;
31  import org.apache.chemistry.opencmis.commons.PropertyIds;
32  import org.apache.chemistry.opencmis.tck.CmisTestResult;
33  
34  public class QueryForObject extends AbstractQueryTest {
35  
36      private static final String CONTENT = "TCK test content.";
37  
38      @Override
39      public void init(Map<String, String> parameters) {
40          super.init(parameters);
41          setName("Query For Object Test");
42          setDescription("Creates a folder and a document, queries them by object id, and deletes both.");
43      }
44  
45      @Override
46      public void run(Session session) {
47          if (supportsQuery(session) && !isFulltextOnly(session)) {
48              // create a test folder
49              Folder testFolder = createTestFolder(session);
50  
51              try {
52                  // create a test document
53                  Document document = createDocument(session, testFolder, "testdoc.txt", CONTENT);
54  
55                  // find the folder
56                  runFolderQuery(session, testFolder);
57  
58                  // find the document
59                  runDocumentQuery(session, document);
60  
61                  // clean up
62                  document.delete(true);
63              } finally {
64                  // delete the test folder
65                  deleteTestFolder();
66              }
67  
68          } else {
69              addResult(createResult(SKIPPED, "Metadata query not supported. Test Skipped!"));
70          }
71      }
72  
73      public void runFolderQuery(Session session, Folder testFolder) {
74          if (!Boolean.TRUE.equals(testFolder.getType().isQueryable())) {
75              addResult(createResult(SKIPPED, "Folder type '" + testFolder.getType().getId()
76                      + "' is not queryable. Folder query test skipped!"));
77              return;
78          }
79  
80          CmisTestResult f;
81  
82          QueryStatement[] statements = new QueryStatement[] {
83                  session.createQueryStatement("SELECT * FROM ? WHERE ? = ?"),
84                  session.createQueryStatement("SELECT * FROM ? WHERE ? IN (?)") };
85  
86          for (QueryStatement statement : statements) {
87              statement.setType(1, testFolder.getType());
88              statement.setProperty(2, testFolder.getType().getPropertyDefinitions().get(PropertyIds.OBJECT_ID));
89              statement.setString(3, testFolder.getId());
90  
91              addResult(createInfoResult("Folder query: " + statement.toQueryString()));
92  
93              int count = 0;
94              for (QueryResult qr : statement.query(false)) {
95                  count++;
96  
97                  String objectId = qr.getPropertyValueByQueryName("cmis:objectId");
98  
99                  f = createResult(FAILURE, "Folder query returned unexpected object. Id: " + objectId);
100                 addResult(assertEquals(testFolder.getId(), objectId, null, f));
101             }
102 
103             f = createResult(FAILURE, "Folder query should return exactly one hit, but returned " + count + ".");
104             addResult(assertEquals(1, count, null, f));
105         }
106     }
107 
108     public void runDocumentQuery(Session session, Document testDocument) {
109         if (!Boolean.TRUE.equals(testDocument.getType().isQueryable())) {
110             addResult(createResult(SKIPPED, "Document type '" + testDocument.getType().getId()
111                     + "' is not queryable. Document query test skipped!"));
112             return;
113         }
114 
115         CmisTestResult f;
116 
117         QueryStatement[] statements = new QueryStatement[] {
118                 session.createQueryStatement("SELECT * FROM ? WHERE ? = ?"),
119                 session.createQueryStatement("SELECT * FROM ? WHERE ? IN (?)") };
120 
121         for (QueryStatement statement : statements) {
122             statement.setType(1, testDocument.getType());
123             statement.setProperty(2, testDocument.getType().getPropertyDefinitions().get(PropertyIds.OBJECT_ID));
124             statement.setString(3, testDocument.getId());
125 
126             addResult(createInfoResult("Document query: " + statement.toQueryString()));
127 
128             int count = 0;
129             for (QueryResult qr : statement.query(false)) {
130                 count++;
131 
132                 String objectId = qr.getPropertyValueByQueryName("cmis:objectId");
133 
134                 f = createResult(FAILURE, "Document query returned unexpected object. Id: " + objectId);
135                 addResult(assertEquals(testDocument.getId(), objectId, null, f));
136             }
137 
138             f = createResult(FAILURE, "Document query should return exactly one hit, but returned " + count + ".");
139             addResult(assertEquals(1, count, null, f));
140         }
141     }
142 }