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.OK;
23  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.SKIPPED;
24  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.chemistry.opencmis.client.api.CmisObject;
31  import org.apache.chemistry.opencmis.client.api.ItemIterable;
32  import org.apache.chemistry.opencmis.client.api.ObjectType;
33  import org.apache.chemistry.opencmis.client.api.QueryResult;
34  import org.apache.chemistry.opencmis.client.api.Session;
35  import org.apache.chemistry.opencmis.commons.PropertyIds;
36  import org.apache.chemistry.opencmis.commons.data.PropertyData;
37  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
39  import org.apache.chemistry.opencmis.tck.CmisTestResult;
40  import org.apache.chemistry.opencmis.tck.impl.CmisTestResultImpl;
41  
42  /**
43   * Query smoke test.
44   */
45  public class QuerySmokeTest extends AbstractQueryTest {
46  
47      @Override
48      public void init(Map<String, String> parameters) {
49          super.init(parameters);
50          setName("Query Smoke Test");
51          setDescription("Performs a simple query and checks if the format of the results is correct. It does not check if the results are complete!");
52      }
53  
54      @Override
55      public void run(Session session) {
56          CmisTestResult f;
57  
58          if (supportsQuery(session)) {
59              String testType = "cmis:document";
60              String statement = "SELECT * FROM " + testType;
61  
62              addResult(createInfoResult("Query: " + statement));
63  
64              ObjectType type = session.getTypeDefinition(testType);
65  
66              f = createResult(FAILURE, "Test type definition '" + testType + "' not found!");
67              addResult(assertNotNull(type, null, f));
68              if (type == null) {
69                  return;
70              }
71  
72              PropertyDefinition<?> objectIdPropDef = type.getPropertyDefinitions().get(PropertyIds.OBJECT_ID);
73  
74              f = createResult(FAILURE, "Object Id property definition does not exist!");
75              addResult(assertNotNull(objectIdPropDef, null, f));
76  
77              String objectIdQueryName = null;
78              if (objectIdPropDef != null) {
79                  objectIdQueryName = objectIdPropDef.getQueryName();
80              }
81  
82              int pageSize = 100;
83  
84              ItemIterable<QueryResult> resultSet = session.query(statement, false);
85  
86              if (resultSet == null) {
87                  addResult(createResult(FAILURE, "Query result set is null! (OpenCMIS issue???)"));
88              } else {
89                  int i = 0;
90                  // testing 100 results should be sufficient for this test
91                  ItemIterable<QueryResult> queryIterable = resultSet.getPage(pageSize);
92                  for (QueryResult qr : queryIterable) {
93                      if (qr == null) {
94                          addResult(createResult(FAILURE, "Query result is null! (OpenCMIS issue???)"));
95                      } else {
96                          addResult(checkQueryResult(session, qr, type, "Query result: " + i));
97  
98                          if (objectIdQueryName != null) {
99                              String objectId = (String) qr.getPropertyByQueryName(objectIdQueryName).getFirstValue();
100 
101                             try {
102                                 CmisObject object = session.getObject(objectId, SELECT_ALL_NO_CACHE_OC);
103                                 addResult(checkObject(session, object, getAllProperties(object),
104                                         "Query hit check. Id: " + objectId));
105                             } catch (CmisObjectNotFoundException e) {
106                                 addResult(createResult(FAILURE,
107                                         "Query hit references an object that doesn't exist. Id: " + objectId, e, false));
108                             }
109                         }
110                         // TODO: check more
111                     }
112                     i++;
113                 }
114 
115                 f = createResult(FAILURE, "More query results (" + i + ") than expected (page size = " + pageSize
116                         + ")!");
117                 addResult(assertIsFalse((i > pageSize), null, f));
118 
119                 if (queryIterable.getTotalNumItems() == -1) {
120                     addResult(createResult(WARNING, "Repository did not return numItems."));
121                 }
122 
123                 addResult(createInfoResult(i + " query results for \"" + statement + "\" (page size = " + pageSize
124                         + ")"));
125             }
126         } else {
127             addResult(createResult(SKIPPED, "Query not supported. Test Skipped!"));
128         }
129     }
130 
131     protected CmisTestResult checkQueryResult(Session session, QueryResult qr, ObjectType type, String message) {
132         List<CmisTestResult> results = new ArrayList<CmisTestResult>();
133 
134         CmisTestResult f;
135 
136         if (qr.getProperties().isEmpty()) {
137             addResult(results, createResult(FAILURE, "Query result is empty!"));
138         } else {
139             for (PropertyDefinition<?> propDef : type.getPropertyDefinitions().values()) {
140                 if (propDef.getQueryName() == null) {
141                     continue;
142                 }
143 
144                 PropertyData<?> pd = qr.getPropertyByQueryName(propDef.getQueryName());
145 
146                 if (pd == null) {
147                     addResult(results,
148                             createResult(FAILURE, "Query property not in result set: " + propDef.getQueryName()));
149                 } else {
150                     if (PropertyIds.OBJECT_ID.equals(propDef.getId())
151                             || PropertyIds.OBJECT_TYPE_ID.equals(propDef.getId())
152                             || PropertyIds.BASE_TYPE_ID.equals(propDef.getId())) {
153                         f = createResult(FAILURE, "Query property must not be empty: " + propDef.getQueryName());
154                         addResult(results, assertStringNotEmpty((String) pd.getFirstValue(), null, f));
155                     }
156                 }
157             }
158         }
159 
160         CmisTestResultImpl result = createResult(getWorst(results), message);
161         result.getChildren().addAll(results);
162 
163         return result.getStatus().getLevel() <= OK.getLevel() ? null : result;
164     }
165 }