This project has retired. For details please refer to its Attic page.
QueryRootFolderTest 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.SKIPPED;
23  
24  import java.util.GregorianCalendar;
25  import java.util.Map;
26  
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  /**
35   * Root folder query test.
36   */
37  public class QueryRootFolderTest extends AbstractQueryTest {
38  
39      @Override
40      public void init(Map<String, String> parameters) {
41          super.init(parameters);
42          setName("Query Root Folder Test");
43          setDescription("Performs a query that should return the root folder name and id.");
44      }
45  
46      @Override
47      public void run(Session session) {
48          if (supportsQuery(session)) {
49              queryById(session);
50              queryByDate(session);
51          } else {
52              addResult(createResult(SKIPPED, "Query not supported. Test Skipped!"));
53          }
54      }
55  
56      protected void queryById(Session session) {
57          CmisTestResult f;
58  
59          String testType = "cmis:folder";
60  
61          Folder rootFolder = session.getRootFolder();
62  
63          QueryStatement statement = session
64                  .createQueryStatement("SELECT ? AS folderName, ? AS folderId FROM ? WHERE ? = ?");
65  
66          statement.setProperty(1, testType, PropertyIds.NAME);
67          statement.setProperty(2, testType, PropertyIds.OBJECT_ID);
68          statement.setType(3, testType);
69          statement.setProperty(4, testType, PropertyIds.OBJECT_ID);
70          statement.setString(5, rootFolder.getId());
71  
72          addResult(createInfoResult("Query: " + statement.toQueryString()));
73  
74          int count = 0;
75          for (QueryResult qr : statement.query(false)) {
76              count++;
77  
78              String folderName = qr.getPropertyValueByQueryName("folderName");
79              String folderId = qr.getPropertyValueByQueryName("folderId");
80  
81              f = createResult(FAILURE, "Query result does not match root folder name!");
82              addResult(assertEquals(rootFolder.getName(), folderName, null, f));
83  
84              f = createResult(FAILURE, "Query result does not match root folder id!");
85              addResult(assertEquals(rootFolder.getId(), folderId, null, f));
86          }
87  
88          f = createResult(FAILURE, "The query should return exactly one result but returned " + count + "!");
89          addResult(assertEquals(1, count, null, f));
90      }
91  
92      protected void queryByDate(Session session) {
93          CmisTestResult f;
94  
95          String testType = "cmis:folder";
96  
97          Folder rootFolder = session.getRootFolder();
98  
99          GregorianCalendar before = new GregorianCalendar();
100         before.setTimeInMillis(rootFolder.getCreationDate().getTimeInMillis() - (60 * 60 * 1000));
101 
102         GregorianCalendar after = new GregorianCalendar();
103         after.setTimeInMillis(rootFolder.getCreationDate().getTimeInMillis() + (60 * 60 * 1000));
104 
105         QueryStatement statement = session
106                 .createQueryStatement("SELECT ? AS folderName, ? AS folderId FROM ? WHERE ? > TIMESTAMP ? AND ? < TIMESTAMP ?");
107 
108         statement.setProperty(1, testType, PropertyIds.NAME);
109         statement.setProperty(2, testType, PropertyIds.OBJECT_ID);
110         statement.setType(3, testType);
111         statement.setProperty(4, testType, PropertyIds.CREATION_DATE);
112         statement.setDateTime(5, before);
113         statement.setProperty(6, testType, PropertyIds.CREATION_DATE);
114         statement.setDateTime(7, after);
115 
116         addResult(createInfoResult("Query: " + statement.toQueryString()));
117 
118         boolean found = false;
119         for (QueryResult qr : statement.query(false)) {
120             String folderId = qr.getPropertyValueByQueryName("folderId");
121 
122             if (rootFolder.getId().equals(folderId)) {
123                 found = true;
124 
125                 String folderName = qr.getPropertyValueByQueryName("folderName");
126 
127                 f = createResult(FAILURE, "Query result does not match root folder name!");
128                 addResult(assertEquals(rootFolder.getName(), folderName, null, f));
129                 break;
130             }
131         }
132 
133         f = createResult(FAILURE, "The query should return the root folder but does not!");
134         addResult(assertIsTrue(found, null, f));
135     }
136 }