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() {
77 // First create a CollectionObject
79 CollectionobjectsCommon co = new CollectionobjectsCommon();
80 fillCollectionObject(co, createIdentifier());
82 // Next, create a part object
83 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
84 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
85 commonPart.setLabel(collectionObjectClient.getCommonPartName());
87 // Make the create call and check the response
88 ClientResponse<Response> response = collectionObjectClient.create(multipart);
89 String collectionObjectCsid = null;
91 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
93 collectionObjectCsid = extractId(response);
95 response.releaseConnection();
99 // Next, create an Intake object
100 IntakesCommon intake = new IntakesCommon();
101 fillIntake(intake, createIdentifier());
102 // Create the a part object
103 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
104 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
105 commonPart.setLabel(intakeClient.getCommonPartName());
107 // Make the call to create and check the response
108 response = intakeClient.create(multipart);
109 String intakeCsid = null;
111 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
112 intakeCsid = extractId(response);
114 response.releaseConnection();
117 // Lastly, relate the two entities, by creating a new relation object
118 RelationsCommon relation = new RelationsCommon();
119 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
120 intakeCsid, IntakesCommon.class.getSimpleName(),
121 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
122 // Create the part and fill it with the relation object
123 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
124 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
125 commonPart.setLabel(relationClient.getCommonPartName());
127 // Make the call to crate
128 response = relationClient.create(multipart);
129 String relationCsid = null;
131 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
132 relationCsid = extractId(response);
134 response.releaseConnection();
138 // Now try to retrieve the Intake record of the CollectionObject.
140 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
141 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
142 collectionObjectCsid,
143 null, //CollectionobjectsCommon.class.getSimpleName(),
146 null ); //IntakesCommon.class.getSimpleName());
147 RelationsCommonList relationList = null;
149 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
150 relationList = resultResponse.getEntity();
152 resultResponse.releaseConnection();
156 // Each relation returned in the list needs to match what we
159 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
160 Assert.assertFalse(relationListItems.isEmpty());
163 RelationsCommon resultRelation = null;
164 for(RelationsCommonList.RelationListItem listItem : relationListItems){
166 String foundCsid = listItem.getCsid();
167 ClientResponse<String> multiPartResponse = null;
169 multiPartResponse = relationClient.read(foundCsid);
170 int responseStatus = multiPartResponse.getStatus();
171 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
172 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
173 resultRelation = (RelationsCommon) extractPart(input,
174 relationClient.getCommonPartName(),
175 RelationsCommon.class);
176 } catch (Exception e) {
179 multiPartResponse.releaseConnection();
182 Assert.assertEquals(resultRelation.getSubjectCsid(), collectionObjectCsid);
183 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
184 Assert.assertEquals(resultRelation.getObjectCsid(), intakeCsid);
185 System.out.println();
191 public void relateCollectionObjectsToIntake() {
192 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
195 private void relateCollectionObjectsToIntake(int numberOfObjects) {
198 // First create a list of CollectionObjects
200 CollectionobjectsCommon co = new CollectionobjectsCommon();
201 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
202 for (int i = 0; i < numberOfObjects; i++) {
203 fillCollectionObject(co, createIdentifier());
205 // Next, create a part object
206 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
207 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
208 commonPart.setLabel(collectionObjectClient.getCommonPartName());
210 // Make the create call and check the response
211 ClientResponse<Response> response = collectionObjectClient.create(multipart);
212 String collectionObjectCsid = null;
214 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
216 collectionObjectCsid = extractId(response);
217 collectionObjectIDList.add(collectionObjectCsid);
219 response.releaseConnection();
224 // Next, create an Intake object
225 IntakesCommon intake = new IntakesCommon();
226 fillIntake(intake, createIdentifier());
227 // Create the a part object
228 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
229 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
230 commonPart.setLabel(intakeClient.getCommonPartName());
232 // Make the call to create and check the response
233 ClientResponse<Response> response = intakeClient.create(multipart);
234 String intakeCsid = null;
236 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
237 intakeCsid = extractId(response);
239 response.releaseConnection();
242 // Lastly, relate the two entities, by creating a new relation object
243 RelationsCommon relation = new RelationsCommon();
244 for (String collectionObjectCsid : collectionObjectIDList) {
245 fillRelation(relation,
246 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
247 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
248 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
249 // Create the part and fill it with the relation object
250 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
251 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
252 commonPart.setLabel(relationClient.getCommonPartName());
254 // Make the call to crate
255 response = relationClient.create(multipart);
256 String relationCsid = null;
258 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
259 relationCsid = extractId(response);
261 response.releaseConnection();
266 // Now try to retrieve the Intake record of the CollectionObject.
268 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
269 RelationsCommonList relationList = null;
270 for (String collectionObjectCsid : collectionObjectIDList) {
271 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
273 null, //IntakesCommon.class.getSimpleName(), //subject
275 collectionObjectCsid,
276 null); //CollectionobjectsCommon.class.getSimpleName()); //object
279 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
280 relationList = resultResponse.getEntity();
282 resultResponse.releaseConnection();
286 // Each relation returned in the list needs to match what we
289 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
290 Assert.assertFalse(relationListItems.isEmpty());
293 RelationsCommon resultRelation = null;
294 for(RelationsCommonList.RelationListItem listItem : relationListItems){
295 String foundCsid = listItem.getCsid();
296 ClientResponse<String> multiPartResponse = null;
298 multiPartResponse = relationClient.read(foundCsid);
299 int responseStatus = multiPartResponse.getStatus();
300 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
301 PoxPayloadIn input = new PoxPayloadIn(multiPartResponse.getEntity());
302 resultRelation = (RelationsCommon) extractPart(input,
303 relationClient.getCommonPartName(),
304 RelationsCommon.class);
305 } catch (Exception e) {
308 multiPartResponse.releaseConnection();
311 Assert.assertEquals(resultRelation.getSubjectCsid(), intakeCsid);
312 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
313 Assert.assertEquals(resultRelation.getObjectCsid(), collectionObjectCsid);
314 System.out.println();