2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.IntegrationTests.test;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.jboss.resteasy.client.ClientResponse;
39 import org.collectionspace.services.client.CollectionObjectClient;
40 import org.collectionspace.services.client.PayloadOutputPart;
41 import org.collectionspace.services.client.PoxPayloadIn;
42 import org.collectionspace.services.client.PoxPayloadOut;
43 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
45 import org.collectionspace.services.client.IntakeClient;
46 import org.collectionspace.services.intake.IntakesCommon;
48 import org.collectionspace.services.client.RelationClient;
49 import org.collectionspace.services.relation.RelationsCommon;
50 import org.collectionspace.services.relation.RelationsCommonList;
51 import org.collectionspace.services.relation.RelationshipType;
56 * @version $Revision:$
58 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
60 final Logger logger = LoggerFactory
61 .getLogger(RelationIntegrationTest.class);
63 // Get clients for the CollectionSpace services
65 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
66 private RelationClient relationClient = new RelationClient();
67 private IntakeClient intakeClient = new IntakeClient();
69 private static final int OBJECTS_TO_INTAKE = 1;
72 public void relateCollectionObjectToIntake() {
74 // First create a CollectionObject
76 CollectionobjectsCommon co = new CollectionobjectsCommon();
77 fillCollectionObject(co, createIdentifier());
79 // Next, create a part object
80 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
81 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
82 commonPart.setLabel(collectionObjectClient.getCommonPartName());
84 // Make the create call and check the response
85 ClientResponse<Response> response = collectionObjectClient.create(multipart);
86 String collectionObjectCsid = null;
88 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
90 collectionObjectCsid = extractId(response);
92 response.releaseConnection();
96 // Next, create an Intake object
97 IntakesCommon intake = new IntakesCommon();
98 fillIntake(intake, createIdentifier());
99 // Create the a part object
100 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
101 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
102 commonPart.setLabel(intakeClient.getCommonPartName());
104 // Make the call to create and check the response
105 response = intakeClient.create(multipart);
106 String intakeCsid = null;
108 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
109 intakeCsid = extractId(response);
111 response.releaseConnection();
114 // Lastly, relate the two entities, by creating a new relation object
115 RelationsCommon relation = new RelationsCommon();
116 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
117 intakeCsid, IntakesCommon.class.getSimpleName(),
118 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
119 // Create the part and fill it with the relation object
120 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
121 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
122 commonPart.setLabel(relationClient.getCommonPartName());
124 // Make the call to crate
125 response = relationClient.create(multipart);
126 String relationCsid = null;
128 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
129 relationCsid = extractId(response);
131 response.releaseConnection();
135 // Now try to retrieve the Intake record of the CollectionObject.
137 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
138 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
139 collectionObjectCsid,
140 null, //CollectionobjectsCommon.class.getSimpleName(),
143 null ); //IntakesCommon.class.getSimpleName());
144 RelationsCommonList relationList = null;
146 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
147 relationList = resultResponse.getEntity();
149 resultResponse.releaseConnection();
153 // Each relation returned in the list needs to match what we
156 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
157 Assert.assertFalse(relationListItems.isEmpty());
160 RelationsCommon resultRelation = null;
161 for(RelationsCommonList.RelationListItem listItem : relationListItems){
163 String foundCsid = listItem.getCsid();
164 ClientResponse<String> multiPartResponse = null;
166 multiPartResponse = relationClient.read(foundCsid);
167 int responseStatus = multiPartResponse.getStatus();
168 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
169 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
170 resultRelation = (RelationsCommon) extractPart(input,
171 relationClient.getCommonPartName(),
172 RelationsCommon.class);
173 } catch (Exception e) {
176 multiPartResponse.releaseConnection();
179 Assert.assertEquals(resultRelation.getSubjectCsid(), collectionObjectCsid);
180 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
181 Assert.assertEquals(resultRelation.getObjectCsid(), intakeCsid);
182 System.out.println();
188 public void relateCollectionObjectsToIntake() {
189 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
192 private void relateCollectionObjectsToIntake(int numberOfObjects) {
195 // First create a list of CollectionObjects
197 CollectionobjectsCommon co = new CollectionobjectsCommon();
198 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
199 for (int i = 0; i < numberOfObjects; i++) {
200 fillCollectionObject(co, createIdentifier());
202 // Next, create a part object
203 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
204 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
205 commonPart.setLabel(collectionObjectClient.getCommonPartName());
207 // Make the create call and check the response
208 ClientResponse<Response> response = collectionObjectClient.create(multipart);
209 String collectionObjectCsid = null;
211 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
213 collectionObjectCsid = extractId(response);
214 collectionObjectIDList.add(collectionObjectCsid);
216 response.releaseConnection();
221 // Next, create an Intake object
222 IntakesCommon intake = new IntakesCommon();
223 fillIntake(intake, createIdentifier());
224 // Create the a part object
225 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
226 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
227 commonPart.setLabel(intakeClient.getCommonPartName());
229 // Make the call to create and check the response
230 ClientResponse<Response> response = intakeClient.create(multipart);
231 String intakeCsid = null;
233 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
234 intakeCsid = extractId(response);
236 response.releaseConnection();
239 // Lastly, relate the two entities, by creating a new relation object
240 RelationsCommon relation = new RelationsCommon();
241 for (String collectionObjectCsid : collectionObjectIDList) {
242 fillRelation(relation,
243 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
244 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
245 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
246 // Create the part and fill it with the relation object
247 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
248 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
249 commonPart.setLabel(relationClient.getCommonPartName());
251 // Make the call to crate
252 response = relationClient.create(multipart);
253 String relationCsid = null;
255 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
256 relationCsid = extractId(response);
258 response.releaseConnection();
263 // Now try to retrieve the Intake record of the CollectionObject.
265 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
266 RelationsCommonList relationList = null;
267 for (String collectionObjectCsid : collectionObjectIDList) {
268 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
270 null, //IntakesCommon.class.getSimpleName(), //subject
272 collectionObjectCsid,
273 null); //CollectionobjectsCommon.class.getSimpleName()); //object
276 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
277 relationList = resultResponse.getEntity();
279 resultResponse.releaseConnection();
283 // Each relation returned in the list needs to match what we
286 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
287 Assert.assertFalse(relationListItems.isEmpty());
290 RelationsCommon resultRelation = null;
291 for(RelationsCommonList.RelationListItem listItem : relationListItems){
292 String foundCsid = listItem.getCsid();
293 ClientResponse<String> multiPartResponse = null;
295 multiPartResponse = relationClient.read(foundCsid);
296 int responseStatus = multiPartResponse.getStatus();
297 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
298 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
299 resultRelation = (RelationsCommon) extractPart(input,
300 relationClient.getCommonPartName(),
301 RelationsCommon.class);
302 } catch (Exception e) {
305 multiPartResponse.releaseConnection();
308 Assert.assertEquals(resultRelation.getSubjectCsid(), intakeCsid);
309 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
310 Assert.assertEquals(resultRelation.getObjectCsid(), collectionObjectCsid);
311 System.out.println();