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.crud;
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.List;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.api.Document;
28  import org.apache.chemistry.opencmis.client.api.Folder;
29  import org.apache.chemistry.opencmis.client.api.Relationship;
30  import org.apache.chemistry.opencmis.client.api.Session;
31  import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
32  import org.apache.chemistry.opencmis.tck.CmisTestResult;
33  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
34  
35  /**
36   * Simple relationship test.
37   */
38  public class CreateAndDeleteRelationshipTest extends AbstractSessionTest {
39  
40      @Override
41      public void init(Map<String, String> parameters) {
42          super.init(parameters);
43          setName("Create and Delete Relationship Test");
44          setDescription("Creates a relationship between two documents, checks the newly created relationship and finally deletes the created relationship.");
45      }
46  
47      @Override
48      public void run(Session session) {
49          CmisTestResult f;
50          boolean found;
51  
52          if (hasRelationships(session)) {
53              // create a test folder
54              Folder testFolder = createTestFolder(session);
55  
56              try {
57                  // create documents
58                  Document doc1 = createDocument(session, testFolder, "doc1.txt", "doc1");
59                  Document doc2 = createDocument(session, testFolder, "doc2.txt", "doc2");
60  
61                  // create relationship
62                  Relationship rel = createRelationship(session, "rel1", doc1, doc2);
63  
64                  f = createResult(FAILURE, "Source document id does not match relationship source id!");
65                  addResult(assertEquals(doc1.getId(), rel.getSourceId().getId(), null, f));
66  
67                  f = createResult(FAILURE, "Target document id does not match relationship target id!");
68                  addResult(assertEquals(doc2.getId(), rel.getTarget().getId(), null, f));
69  
70                  // check the source document
71                  doc1.refresh();
72                  List<Relationship> doc1rels = doc1.getRelationships();
73  
74                  f = createResult(FAILURE, "Source document has no relationships but must have at least one!");
75                  addResult(assertListNotEmpty(doc1rels, null, f));
76  
77                  if (doc1rels != null) {
78                      found = false;
79                      for (Relationship r : doc1rels) {
80                          if (rel.getId().equals(r.getId())) {
81                              found = true;
82                              break;
83                          }
84                      }
85  
86                      f = createResult(FAILURE,
87                              "Newly created relationship not found in the relationships of the source document!");
88                      addResult(assertIsTrue(found, null, f));
89                  }
90  
91                  found = false;
92                  for (Relationship r : session.getRelationships(doc1, true, RelationshipDirection.SOURCE, null,
93                          SELECT_ALL_NO_CACHE_OC)) {
94                      if (rel.getId().equals(r.getId())) {
95                          found = true;
96                          break;
97                      }
98                  }
99  
100                 f = createResult(
101                         FAILURE,
102                         "Newly created relationship not found in the relationships returned by getObjectRelationships() for the source document!");
103                 addResult(assertIsTrue(found, null, f));
104 
105                 // check the target document
106                 doc2.refresh();
107                 List<Relationship> doc2rels = doc2.getRelationships();
108 
109                 f = createResult(FAILURE, "Target document has no relationships but must have at least one!");
110                 addResult(assertListNotEmpty(doc2rels, null, f));
111 
112                 if (doc2rels != null) {
113                     found = false;
114                     for (Relationship r : doc2rels) {
115                         if (rel.getId().equals(r.getId())) {
116                             found = true;
117                             break;
118                         }
119                     }
120 
121                     f = createResult(FAILURE,
122                             "Newly created relationship not found in the relationships of the target document!");
123                     addResult(assertIsTrue(found, null, f));
124                 }
125 
126                 found = false;
127                 for (Relationship r : session.getRelationships(doc2, true, RelationshipDirection.TARGET, null,
128                         SELECT_ALL_NO_CACHE_OC)) {
129                     if (rel.getId().equals(r.getId())) {
130                         found = true;
131                         break;
132                     }
133                 }
134 
135                 f = createResult(
136                         FAILURE,
137                         "Newly created relationship not found in the relationships returned by getObjectRelationships() for the target document!");
138                 addResult(assertIsTrue(found, null, f));
139 
140                 // remove
141                 deleteObject(rel);
142                 deleteObject(doc2);
143                 deleteObject(doc1);
144             } finally {
145                 // delete the test folder
146                 deleteTestFolder();
147             }
148         } else {
149             addResult(createResult(SKIPPED, "Relationships not supported. Test skipped!"));
150         }
151     }
152 }