This project has retired. For details please refer to its Attic page.
TypesTest 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.basics;
20  
21  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
23  
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.chemistry.opencmis.client.api.ItemIterable;
29  import org.apache.chemistry.opencmis.client.api.ObjectType;
30  import org.apache.chemistry.opencmis.client.api.Session;
31  import org.apache.chemistry.opencmis.client.api.Tree;
32  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
33  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
34  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
35  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
36  import org.apache.chemistry.opencmis.tck.CmisTestResult;
37  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
38  
39  /**
40   * Types test.
41   */
42  public class TypesTest extends AbstractSessionTest {
43      @Override
44      public void init(Map<String, String> parameters) {
45          super.init(parameters);
46          setName("Types Test");
47          setDescription("Checks all types exposed by the repository for specification compliance.");
48      }
49  
50      @Override
51      public void run(Session session) {
52          CmisTestResult failure;
53  
54          // document
55          try {
56              TypeDefinition documentType = session.getTypeDefinition(BaseTypeId.CMIS_DOCUMENT.value());
57              addResult(checkTypeDefinition(session, documentType, "Document type spec compliance."));
58  
59              failure = createResult(FAILURE, "Document type has the wrong base type: " + documentType.getBaseTypeId());
60              addResult(assertEquals(BaseTypeId.CMIS_DOCUMENT, documentType.getBaseTypeId(), null, failure));
61          } catch (CmisObjectNotFoundException e) {
62              addResult(createResult(FAILURE, "Document type not available!", e, false));
63          }
64  
65          // folder
66          try {
67              TypeDefinition folderType = session.getTypeDefinition(BaseTypeId.CMIS_FOLDER.value());
68  
69              addResult(checkTypeDefinition(session, folderType, "Folder type spec compliance."));
70  
71              failure = createResult(FAILURE, "Folder type has the wrong base type: " + folderType.getBaseTypeId());
72              addResult(assertEquals(BaseTypeId.CMIS_FOLDER, folderType.getBaseTypeId(), null, failure));
73          } catch (CmisObjectNotFoundException e) {
74              addResult(createResult(FAILURE, "Folder type not available!", e, false));
75          }
76  
77          // relationship
78          try {
79              TypeDefinition relationshipType = session.getTypeDefinition(BaseTypeId.CMIS_RELATIONSHIP.value());
80              addResult(checkTypeDefinition(session, relationshipType, "Relationship type spec compliance."));
81  
82              failure = createResult(FAILURE,
83                      "Relationship type has the wrong base type: " + relationshipType.getBaseTypeId());
84              addResult(assertEquals(BaseTypeId.CMIS_RELATIONSHIP, relationshipType.getBaseTypeId(), null, failure));
85          } catch (CmisObjectNotFoundException e) {
86              addResult(createResult(WARNING, "Relationship type not available!", e, false));
87          }
88  
89          // policy
90          try {
91              TypeDefinition policyType = session.getTypeDefinition(BaseTypeId.CMIS_POLICY.value());
92              addResult(checkTypeDefinition(session, policyType, "Policy type spec compliance."));
93  
94              failure = createResult(FAILURE, "Policy type has the wrong base type: " + policyType.getBaseTypeId());
95              addResult(assertEquals(BaseTypeId.CMIS_POLICY, policyType.getBaseTypeId(), null, failure));
96          } catch (CmisInvalidArgumentException e) {
97              addResult(createResult(WARNING, "Policy type not available!", e, false));
98          } catch (CmisObjectNotFoundException e) {
99              addResult(createResult(WARNING, "Policy type not available!", e, false));
100         }
101 
102         int numOfTypes = runTypeChecks(session, session.getTypeDescendants(null, -1, true));
103 
104         addResult(createInfoResult("Checked " + numOfTypes + " type definitions."));
105     }
106 
107     private int runTypeChecks(Session session, List<Tree<ObjectType>> types) {
108         if (types == null) {
109             return 0;
110         }
111 
112         int numOfTypes = 0;
113         CmisTestResult failure;
114 
115         for (Tree<ObjectType> tree : types) {
116             failure = createResult(FAILURE, "Types tree contains null leaf!");
117             addResult(assertNotNull(tree, null, failure));
118 
119             if (tree != null) {
120                 numOfTypes++;
121 
122                 addResult(checkTypeDefinition(session, tree.getItem(), "Type spec compliance: "
123                         + tree.getItem().getId()));
124 
125                 // clear the cache to ensure that the type definition is
126                 // reloaded from the repository
127                 session.clear();
128 
129                 try {
130                     TypeDefinition reloadedType = session.getTypeDefinition(tree.getItem().getId());
131 
132                     addResult(checkTypeDefinition(session, reloadedType, "Type spec compliance: "
133                             + (reloadedType == null ? "?" : reloadedType.getId())));
134 
135                     failure = createResult(FAILURE,
136                             "Type fetched via getTypeDescendants() is does not macth type fetched via getTypeDefinition(): "
137                                     + tree.getItem().getId());
138                     addResult(assertEquals(tree.getItem(), reloadedType, null, failure));
139                 } catch (CmisObjectNotFoundException e) {
140                     addResult(createResult(FAILURE,
141                             "Type fetched via getTypeDescendants() is not available via getTypeDefinition(): "
142                                     + tree.getItem().getId(), e, false));
143                 }
144 
145                 // clear the cache again to ensure that the type definition
146                 // children are reloaded from the repository
147                 session.clear();
148 
149                 try {
150                     ItemIterable<ObjectType> reloadedTypeChildren = session.getTypeChildren(tree.getItem().getId(),
151                             true);
152 
153                     // check type children
154                     Map<String, ObjectType> typeChilden = new HashMap<String, ObjectType>();
155                     for (ObjectType childType : reloadedTypeChildren) {
156                         addResult(checkTypeDefinition(session, childType, "Type spec compliance: "
157                                 + (childType == null ? "?" : childType.getId())));
158 
159                         if (childType != null) {
160                             typeChilden.put(childType.getId(), childType);
161                         }
162                     }
163 
164                     // compare type children and type descendants
165                     if (tree.getChildren() == null) {
166                         failure = createResult(FAILURE,
167                                 "Type children fetched via getTypeDescendants() don't match type children fetched via getTypeChildren(): "
168                                         + tree.getItem().getId());
169                         addResult(assertEquals(0, typeChilden.size(), null, failure));
170                     } else {
171                         // collect the children
172                         Map<String, ObjectType> typeDescendants = new HashMap<String, ObjectType>();
173                         for (Tree<ObjectType> childType : tree.getChildren()) {
174                             if ((childType != null) && (childType.getItem() != null)) {
175                                 typeDescendants.put(childType.getItem().getId(), childType.getItem());
176                             }
177                         }
178 
179                         failure = createResult(FAILURE,
180                                 "Type children fetched via getTypeDescendants() don't match type children fetched via getTypeChildren(): "
181                                         + tree.getItem().getId());
182                         addResult(assertEquals(typeDescendants.size(), typeChilden.size(), null, failure));
183 
184                         for (ObjectType compareType : typeDescendants.values()) {
185                             failure = createResult(FAILURE,
186                                     "Type fetched via getTypeDescendants() doesn't match type fetched via getTypeChildren(): "
187                                             + tree.getItem().getId());
188                             addResult(assertEquals(compareType, typeChilden.get(compareType.getId()), null, failure));
189                         }
190                     }
191                 } catch (CmisObjectNotFoundException e) {
192                     addResult(createResult(FAILURE,
193                             "Type children fetched via getTypeDescendants() is not available via getTypeChildren(): "
194                                     + tree.getItem().getId(), e, false));
195                 }
196 
197                 numOfTypes += runTypeChecks(session, tree.getChildren());
198             }
199         }
200 
201         return numOfTypes;
202     }
203 }