This project has retired. For details please refer to its Attic page.
CreateAndDeleteRelationshipTest 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.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 folder 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",
59  						"doc1");
60  				Document doc2 = createDocument(session, testFolder, "doc2.txt",
61  						"doc2");
62  
63  				// create relationship
64  				Relationship rel = createRelationship(session, "rel1", doc1,
65  						doc2);
66  
67  				f = createResult(FAILURE,
68  						"Source document id does not match relationship source id!");
69  				addResult(assertEquals(doc1.getId(), rel.getSourceId().getId(),
70  						null, f));
71  
72  				f = createResult(FAILURE,
73  						"Target document id does not match relationship target id!");
74  				addResult(assertEquals(doc2.getId(), rel.getTarget().getId(),
75  						null, f));
76  
77  				// check the source document
78  				doc1.refresh();
79  				List<Relationship> doc1rels = doc1.getRelationships();
80  
81  				f = createResult(FAILURE,
82  						"Source document has no relationships but must have at least one!");
83  				addResult(assertListNotEmpty(doc1rels, null, f));
84  
85  				if (doc1rels != null) {
86  					found = false;
87  					for (Relationship r : doc1rels) {
88  						if (rel.getId().equals(r.getId())) {
89  							found = true;
90  							break;
91  						}
92  					}
93  
94  					f = createResult(
95  							FAILURE,
96  							"Newly created relationship not found in the relationships of the source document!");
97  					addResult(assertIsTrue(found, null, f));
98  				}
99  
100 				found = false;
101 				for (Relationship r : session.getRelationships(doc1, true,
102 						RelationshipDirection.SOURCE, null,
103 						SELECT_ALL_NO_CACHE_OC)) {
104 					if (rel.getId().equals(r.getId())) {
105 						found = true;
106 						break;
107 					}
108 				}
109 
110 				f = createResult(
111 						FAILURE,
112 						"Newly created relationship not found in the relationships returned by getObjectRelationships() for the source document!");
113 				addResult(assertIsTrue(found, null, f));
114 
115 				// check the target document
116 				doc2.refresh();
117 				List<Relationship> doc2rels = doc2.getRelationships();
118 
119 				f = createResult(FAILURE,
120 						"Target document has no relationships but must have at least one!");
121 				addResult(assertListNotEmpty(doc2rels, null, f));
122 
123 				if (doc2rels != null) {
124 					found = false;
125 					for (Relationship r : doc2rels) {
126 						if (rel.getId().equals(r.getId())) {
127 							found = true;
128 							break;
129 						}
130 					}
131 
132 					f = createResult(
133 							FAILURE,
134 							"Newly created relationship not found in the relationships of the target document!");
135 					addResult(assertIsTrue(found, null, f));
136 				}
137 
138 				found = false;
139 				for (Relationship r : session.getRelationships(doc2, true,
140 						RelationshipDirection.TARGET, null,
141 						SELECT_ALL_NO_CACHE_OC)) {
142 					if (rel.getId().equals(r.getId())) {
143 						found = true;
144 						break;
145 					}
146 				}
147 
148 				f = createResult(
149 						FAILURE,
150 						"Newly created relationship not found in the relationships returned by getObjectRelationships() for the target document!");
151 				addResult(assertIsTrue(found, null, f));
152 
153 				// remove
154 				deleteObject(rel);
155 				deleteObject(doc2);
156 				deleteObject(doc1);
157 			} finally {
158 				// delete the test folder
159 				deleteTestFolder();
160 			}
161 		} else {
162 			addResult(createResult(SKIPPED,
163 					"Relationships not supported. Test Skipped!"));
164 		}
165 	}
166 }