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;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
40 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.collectionspace.services.client.CollectionObjectClient;
43 import org.collectionspace.services.client.PayloadOutputPart;
44 import org.collectionspace.services.client.PoxPayloadIn;
45 import org.collectionspace.services.client.PoxPayloadOut;
46 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
48 import org.collectionspace.services.client.IntakeClient;
49 import org.collectionspace.services.intake.IntakesCommon;
51 import org.collectionspace.services.client.RelationClient;
52 import org.collectionspace.services.relation.RelationsCommon;
53 import org.collectionspace.services.relation.RelationsCommonList;
54 import org.collectionspace.services.relation.RelationshipType;
59 * @version $Revision:$
61 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
63 final Logger logger = LoggerFactory
64 .getLogger(RelationIntegrationTest.class);
66 // Get clients for the CollectionSpace services
68 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
69 private RelationClient relationClient = new RelationClient();
70 private IntakeClient intakeClient = new IntakeClient();
72 private static final int OBJECTS_TO_INTAKE = 1;
75 public void relateCollectionObjectToIntake() {
78 // First create a CollectionObject
80 CollectionobjectsCommon co = new CollectionobjectsCommon();
81 fillCollectionObject(co, createIdentifier());
83 // Next, create a part object
84 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
85 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
86 commonPart.setLabel(collectionObjectClient.getCommonPartName());
88 // Make the create call and check the response
89 ClientResponse<Response> response = collectionObjectClient.create(multipart);
90 String collectionObjectCsid = null;
92 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
94 collectionObjectCsid = extractId(response);
96 response.releaseConnection();
100 // Next, create an Intake object
101 IntakesCommon intake = new IntakesCommon();
102 fillIntake(intake, createIdentifier());
103 // Create the a part object
104 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
105 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
106 commonPart.setLabel(intakeClient.getCommonPartName());
108 // Make the call to create and check the response
109 response = intakeClient.create(multipart);
110 String intakeCsid = null;
112 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
113 intakeCsid = extractId(response);
115 response.releaseConnection();
118 // Lastly, relate the two entities, by creating a new relation object
119 RelationsCommon relation = new RelationsCommon();
120 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
121 intakeCsid, IntakesCommon.class.getSimpleName(),
122 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
123 // Create the part and fill it with the relation object
124 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
125 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
126 commonPart.setLabel(relationClient.getCommonPartName());
128 // Make the call to crate
129 response = relationClient.create(multipart);
130 String relationCsid = null;
132 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
133 relationCsid = extractId(response);
135 response.releaseConnection();
139 // Now try to retrieve the Intake record of the CollectionObject.
141 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
142 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
143 collectionObjectCsid,
144 CollectionobjectsCommon.class.getSimpleName(),
147 IntakesCommon.class.getSimpleName());
148 RelationsCommonList relationList = null;
150 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
151 relationList = resultResponse.getEntity();
153 resultResponse.releaseConnection();
157 // Each relation returned in the list needs to match what we
160 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
161 Assert.assertFalse(relationListItems.isEmpty());
164 RelationsCommon resultRelation = null;
165 for(RelationsCommonList.RelationListItem listItem : relationListItems){
167 String foundCsid = listItem.getCsid();
168 ClientResponse<String> multiPartResponse = null;
170 multiPartResponse = relationClient.read(foundCsid);
171 int responseStatus = multiPartResponse.getStatus();
172 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
173 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
174 resultRelation = (RelationsCommon) extractPart(input,
175 relationClient.getCommonPartName(),
176 RelationsCommon.class);
177 } catch (Exception e) {
180 multiPartResponse.releaseConnection();
183 Assert.assertEquals(resultRelation.getDocumentId1(), collectionObjectCsid);
184 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
185 Assert.assertEquals(resultRelation.getDocumentId2(), intakeCsid);
186 System.out.println();
192 public void relateCollectionObjectsToIntake() {
193 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
196 private void relateCollectionObjectsToIntake(int numberOfObjects) {
199 // First create a list of CollectionObjects
201 CollectionobjectsCommon co = new CollectionobjectsCommon();
202 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
203 for (int i = 0; i < numberOfObjects; i++) {
204 fillCollectionObject(co, createIdentifier());
206 // Next, create a part object
207 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
208 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
209 commonPart.setLabel(collectionObjectClient.getCommonPartName());
211 // Make the create call and check the response
212 ClientResponse<Response> response = collectionObjectClient.create(multipart);
213 String collectionObjectCsid = null;
215 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
217 collectionObjectCsid = extractId(response);
218 collectionObjectIDList.add(collectionObjectCsid);
220 response.releaseConnection();
225 // Next, create an Intake object
226 IntakesCommon intake = new IntakesCommon();
227 fillIntake(intake, createIdentifier());
228 // Create the a part object
229 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
230 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
231 commonPart.setLabel(intakeClient.getCommonPartName());
233 // Make the call to create and check the response
234 ClientResponse<Response> response = intakeClient.create(multipart);
235 String intakeCsid = null;
237 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
238 intakeCsid = extractId(response);
240 response.releaseConnection();
243 // Lastly, relate the two entities, by creating a new relation object
244 RelationsCommon relation = new RelationsCommon();
245 for (String collectionObjectCsid : collectionObjectIDList) {
246 fillRelation(relation,
247 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
248 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
249 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
250 // Create the part and fill it with the relation object
251 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
252 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
253 commonPart.setLabel(relationClient.getCommonPartName());
255 // Make the call to crate
256 response = relationClient.create(multipart);
257 String relationCsid = null;
259 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
260 relationCsid = extractId(response);
262 response.releaseConnection();
267 // Now try to retrieve the Intake record of the CollectionObject.
269 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
270 RelationsCommonList relationList = null;
271 for (String collectionObjectCsid : collectionObjectIDList) {
272 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
274 IntakesCommon.class.getSimpleName(), //subject
276 collectionObjectCsid,
277 CollectionobjectsCommon.class.getSimpleName()); //object
280 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
281 relationList = resultResponse.getEntity();
283 resultResponse.releaseConnection();
287 // Each relation returned in the list needs to match what we
290 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
291 Assert.assertFalse(relationListItems.isEmpty());
294 RelationsCommon resultRelation = null;
295 for(RelationsCommonList.RelationListItem listItem : relationListItems){
296 String foundCsid = listItem.getCsid();
297 ClientResponse<String> multiPartResponse = null;
299 multiPartResponse = relationClient.read(foundCsid);
300 int responseStatus = multiPartResponse.getStatus();
301 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
302 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
303 resultRelation = (RelationsCommon) extractPart(input,
304 relationClient.getCommonPartName(),
305 RelationsCommon.class);
306 } catch (Exception e) {
309 multiPartResponse.releaseConnection();
312 Assert.assertEquals(resultRelation.getDocumentId1(), intakeCsid);
313 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
314 Assert.assertEquals(resultRelation.getDocumentId2(), collectionObjectCsid);
315 System.out.println();