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