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.CmisObject;
27  import org.apache.chemistry.opencmis.client.api.Folder;
28  import org.apache.chemistry.opencmis.client.api.OperationContext;
29  import org.apache.chemistry.opencmis.client.api.Session;
30  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
31  import org.apache.chemistry.opencmis.tck.CmisTestResult;
32  
33  /**
34   * Query LIKE test.
35   */
36  public class QueryLikeTest extends AbstractQueryTest {
37  
38      private static final String CONTENT = "TCK test content.";
39      private static final int PAGE_SIZE = 10;
40  
41      @Override
42      public void init(Map<String, String> parameters) {
43          super.init(parameters);
44          setName("Query LIKE Test");
45          setDescription("Performs a LIKE query and checks if only matching objects are returned.");
46      }
47  
48      @Override
49      public void run(Session session) {
50          if (supportsQuery(session) && !isFulltextOnly(session)) {
51              // create a test folder
52              Folder testFolder = createTestFolder(session);
53  
54              try {
55                  for (char c = 'a'; c <= 'z'; c++) {
56                      createDocument(session, testFolder, c + "Document", CONTENT);
57                      createFolder(session, testFolder, c + "Folder");
58                  }
59  
60                  OperationContext context = session.createOperationContext();
61                  context.setFilterString("cmis:name,cmis:creationDate");
62                  context.setCacheEnabled(false);
63                  context.setIncludeAcls(false);
64                  context.setIncludeAllowableActions(false);
65                  context.setIncludePathSegments(false);
66                  context.setIncludePolicies(false);
67                  context.setIncludeRelationships(IncludeRelationships.NONE);
68                  context.setRenditionFilterString("cmis:none");
69                  context.setOrderBy("cmis:creationDate");
70  
71                  CmisTestResult f;
72  
73                  for (char c = 'a'; c <= 'z'; c++) {
74                      // query documents
75                      long timestamp = Long.MIN_VALUE;
76                      long count = 0;
77  
78                      for (CmisObject o : session.queryObjects("cmis:document", "cmis:name LIKE '" + c + "%'", false,
79                              context).getPage(PAGE_SIZE)) {
80  
81                          if (o.getName() == null || o.getName().length() == 0) {
82                              addResult(createResult(
83                                      FAILURE,
84                                      "Documents without name should not be returned by this query! Document ID: "
85                                              + o.getId()));
86                          } else {
87                              f = createResult(FAILURE, "Document name should start with '" + c + "' but the name is '"
88                                      + o.getName() + "'.");
89                              addResult(assertEquals(c, Character.toLowerCase(o.getName().charAt(0)), null, f));
90                          }
91  
92                          if (o.getCreationDate() == null) {
93                              addResult(createResult(FAILURE,
94                                      "Found document without creation date! Document ID: " + o.getId()));
95                          } else {
96                              f = createResult(FAILURE,
97                                      "Query results should be ordered by cmis:creationDate but they are not!");
98                              addResult(assertIsTrue(timestamp <= o.getCreationDate().getTimeInMillis(), null, f));
99  
100                             timestamp = o.getCreationDate().getTimeInMillis();
101                         }
102 
103                         count++;
104                     }
105 
106                     f = createResult(FAILURE, "No documents starting with '" + c
107                             + "' have been found, but there must be at least one!");
108                     addResult(assertIsTrue(count > 0, null, f));
109 
110                     f = createResult(FAILURE, "A page of " + PAGE_SIZE
111                             + " query hits has been requested, but the repository returned " + count + ".");
112                     addResult(assertIsTrue(count <= PAGE_SIZE, null, f));
113 
114                     // query folders
115                     timestamp = Long.MIN_VALUE;
116                     count = 0;
117 
118                     for (CmisObject o : session.queryObjects("cmis:folder", "cmis:name LIKE '" + c + "%'", false,
119                             context).getPage(PAGE_SIZE)) {
120 
121                         if (o.getName() == null || o.getName().length() == 0) {
122                             addResult(createResult(FAILURE,
123                                     "Folder without name should not be returned by this query! Folder ID: " + o.getId()));
124                         } else {
125                             f = createResult(FAILURE,
126                                     "Folder name should start with '" + c + "' but the name is '" + o.getName() + "'.");
127                             addResult(assertEquals(c, Character.toLowerCase(o.getName().charAt(0)), null, f));
128                         }
129 
130                         if (o.getCreationDate() == null) {
131                             addResult(createResult(FAILURE,
132                                     "Found folder without creation date! Folder ID: " + o.getId()));
133                         } else {
134                             f = createResult(FAILURE,
135                                     "Query results should be ordered by cmis:creationDate but they are not!");
136                             addResult(assertIsTrue(timestamp <= o.getCreationDate().getTimeInMillis(), null, f));
137 
138                             timestamp = o.getCreationDate().getTimeInMillis();
139                         }
140 
141                         count++;
142                     }
143 
144                     f = createResult(FAILURE, "No folders starting with '" + c
145                             + "' have been found, but there must be at least one!");
146                     addResult(assertIsTrue(count > 0, null, f));
147 
148                     f = createResult(FAILURE, "A page of " + PAGE_SIZE
149                             + " query hits has been requested, but the repository returned " + count + ".");
150                     addResult(assertIsTrue(count <= PAGE_SIZE, null, f));
151                 }
152             } finally {
153                 // delete the test folder
154                 deleteTestFolder();
155             }
156         } else {
157             addResult(createResult(SKIPPED, "Metadata query not supported. Test Skipped!"));
158         }
159     }
160 }