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.io.StringWriter;
26 import java.math.BigDecimal;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.Marshaller;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import org.jboss.resteasy.client.ClientResponse;
43 import org.collectionspace.services.client.CollectionObjectClient;
44 import org.collectionspace.services.client.DimensionClient;
45 import org.collectionspace.services.client.DimensionFactory;
46 import org.collectionspace.services.client.PayloadOutputPart;
47 import org.collectionspace.services.client.PoxPayloadIn;
48 import org.collectionspace.services.client.PoxPayloadOut;
49 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
51 import org.collectionspace.services.client.IntakeClient;
52 import org.collectionspace.services.intake.IntakesCommon;
54 import org.collectionspace.services.client.RelationClient;
55 import org.collectionspace.services.client.workflow.WorkflowClient;
56 import org.collectionspace.services.dimension.DimensionsCommon;
57 import org.collectionspace.services.relation.RelationsCommon;
58 import org.collectionspace.services.relation.RelationsCommonList;
59 import org.collectionspace.services.relation.RelationshipType;
64 * @version $Revision:$
66 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
68 final Logger logger = LoggerFactory
69 .getLogger(RelationIntegrationTest.class);
71 // Get clients for the CollectionSpace services
73 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
74 private RelationClient relationClient = new RelationClient();
75 private IntakeClient intakeClient = new IntakeClient();
76 private DimensionClient dimensionClient = new DimensionClient();
78 private static final int OBJECTS_TO_INTAKE = 1;
82 * Object as xml string.
85 * @param clazz the clazz
88 static protected String objectAsXmlString(Object o, Class<?> clazz) {
89 StringWriter sw = new StringWriter();
91 JAXBContext jc = JAXBContext.newInstance(clazz);
92 Marshaller m = jc.createMarshaller();
93 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
96 } catch (Exception e) {
102 private PoxPayloadOut createDimensionInstance(String commonPartName, String dimensionType, String dimensionValue, String entryDate) {
103 DimensionsCommon dimensionsCommon = new DimensionsCommon();
104 dimensionsCommon.setDimension(dimensionType);
105 dimensionsCommon.setValue(new BigDecimal(dimensionValue));
106 dimensionsCommon.setValueDate(entryDate);
107 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(
108 commonPartName, dimensionsCommon);
110 if (logger.isDebugEnabled()) {
111 logger.debug("to be created, dimension common");
112 logger.debug(objectAsXmlString(dimensionsCommon,
113 DimensionsCommon.class));
119 protected PoxPayloadOut createDimensionInstance(String identifier) {
120 DimensionClient client = new DimensionClient();
121 return createDimensionInstance(client.getCommonPartName(), identifier);
125 * Creates the dimension instance.
127 * @param identifier the identifier
128 * @return the multipart output
130 protected PoxPayloadOut createDimensionInstance(String commonPartName, String identifier) {
131 final String dimensionValue = Long.toString(System.currentTimeMillis());
133 return createDimensionInstance(commonPartName,
134 "dimensionType-" + identifier,
136 "entryDate-" + identifier);
139 @Test void deleteCollectionObjectRelationshipToLockedDimension() {
140 // First create a CollectionObject
141 CollectionobjectsCommon co = new CollectionobjectsCommon();
142 fillCollectionObject(co, createIdentifier());
143 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
144 PayloadOutputPart commonPart = multipart.addPart(collectionObjectClient.getCommonPartName(), co);
146 // Make the "create" call and check the response
147 Response response = collectionObjectClient.create(multipart);
148 String collectionObjectCsid = null;
150 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
152 collectionObjectCsid = extractId(response);
157 // Create a new dimension record
158 multipart = this.createDimensionInstance(createIdentifier());
159 // Make the call to create and check the response
160 response = dimensionClient.create(multipart);
161 String dimensionCsid1 = null;
163 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
164 dimensionCsid1 = extractId(response);
169 // Relate the entities, by creating a new relation object
170 RelationsCommon relation = new RelationsCommon();
171 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
172 dimensionCsid1, DimensionsCommon.class.getSimpleName(),
173 "collectionobject-dimension");
174 // Create the part and fill it with the relation object
175 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
176 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
177 // Create the relationship
178 response = relationClient.create(multipart);
179 String relationCsid1 = null;
181 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
182 relationCsid1 = extractId(response);
187 // Now lock the dimension record.
189 @SuppressWarnings("unused")
190 Response workflowResponse = dimensionClient.updateWorkflowWithTransition(
191 dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_LOCK);
192 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
194 // Finally, try to delete the relationship
196 // Try to delete the relationship -should fail because we don't allow delete if one of the sides is locked.
197 response = relationClient.delete(relationCsid1);
199 Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
204 // Also, try to soft-delete. This should also fail.
205 workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_DELETE);
206 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
209 @Test void createCollectionObjectRelationshipToManyDimensions() {
211 // First create a CollectionObject
213 CollectionobjectsCommon co = new CollectionobjectsCommon();
214 fillCollectionObject(co, createIdentifier());
215 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
216 PayloadOutputPart commonPart = multipart.addPart(collectionObjectClient.getCommonPartName(), co);
218 // Make the create call and check the response
219 Response response = collectionObjectClient.create(multipart);
220 String collectionObjectCsid = null;
222 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
224 collectionObjectCsid = extractId(response);
229 //Next, create the first of three Dimension records to relate the collection object record
230 multipart = this.createDimensionInstance(createIdentifier());
231 // Make the call to create and check the response
232 response = dimensionClient.create(multipart);
233 String dimensionCsid1 = null;
235 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
236 dimensionCsid1 = extractId(response);
241 //Next, create a the second Dimension record
242 multipart = this.createDimensionInstance(createIdentifier());
243 // Make the call to create and check the response
244 response = dimensionClient.create(multipart);
245 String dimensionCsid2 = null;
247 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
248 dimensionCsid2 = extractId(response);
253 //Next, create a the 3rd Dimension record
254 multipart = this.createDimensionInstance(createIdentifier());
255 // Make the call to create and check the response
256 response = dimensionClient.create(multipart);
257 String dimensionCsid3 = null;
259 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
260 dimensionCsid3 = extractId(response);
265 // Relate the entities, by creating a new relation object
266 RelationsCommon relation = new RelationsCommon();
267 fillRelation(relation,
268 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), // the subject of the relationship
269 dimensionCsid1, DimensionsCommon.class.getSimpleName(), // the object of the relationship
270 "collectionobject-dimension");
271 // Create the part and fill it with the relation object
272 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
273 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
274 // Create the relationship
275 response = relationClient.create(multipart);
276 String relationCsid1 = null;
278 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
279 relationCsid1 = extractId(response);
283 // Wait 1 second and create the second relationship
286 } catch (InterruptedException e) {
287 // TODO Auto-generated catch block
290 relation = new RelationsCommon();
291 fillRelation(relation,
292 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), // the subject of the relationship
293 dimensionCsid2, DimensionsCommon.class.getSimpleName(), // the object of the relationship
294 "collectionobject-dimension");
295 // Create the part and fill it with the relation object
296 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
297 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
298 // Create the relationship
299 response = relationClient.create(multipart);
300 @SuppressWarnings("unused")
301 String relationCsid2 = null;
303 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
304 relationCsid2 = extractId(response);
308 // Wait 1 second and create the 3rd relationship
311 } catch (InterruptedException e) {
312 // TODO Auto-generated catch block
315 relation = new RelationsCommon();
316 fillRelation(relation,
317 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), // the subject of the relationship
318 dimensionCsid3, DimensionsCommon.class.getSimpleName(), // the object of the relationship
319 "collectionobject-dimension");
320 // Create the part and fill it with the relation object
321 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
322 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
323 // Create the relationship
324 response = relationClient.create(multipart);
325 @SuppressWarnings("unused")
326 String relationCsid3 = null;
328 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
329 relationCsid3 = extractId(response);
335 @Test void releteCollectionObjectToLockedDimension() {
337 // First create a CollectionObject
339 CollectionobjectsCommon co = new CollectionobjectsCommon();
340 fillCollectionObject(co, createIdentifier());
342 // Next, create a part object
343 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
344 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
345 commonPart.setLabel(collectionObjectClient.getCommonPartName());
347 // Make the create call and check the response
348 Response response = collectionObjectClient.create(multipart);
349 String collectionObjectCsid = null;
351 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
353 collectionObjectCsid = extractId(response);
358 //Next, create a Dimension record to relate the collection object to
359 multipart = this.createDimensionInstance(createIdentifier());
360 // Make the call to create and check the response
361 response = dimensionClient.create(multipart);
362 String dimensionCsid = null;
364 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
365 dimensionCsid = extractId(response);
370 @SuppressWarnings("unused")
371 Response workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid,
372 WorkflowClient.WORKFLOWTRANSITION_LOCK);
373 workflowResponse.close();
374 System.out.println("Locked dimension record with CSID=" + dimensionCsid);
376 // Lastly, relate the two entities, by creating a new relation object
377 RelationsCommon relation = new RelationsCommon();
378 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
379 dimensionCsid, DimensionsCommon.class.getSimpleName(),
380 "collectionobject-dimension");
381 // Create the part and fill it with the relation object
382 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
383 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
384 commonPart.setLabel(relationClient.getCommonPartName());
386 // Make the call to crate
387 Response relationresponse = relationClient.create(multipart);
388 String relationCsid = null;
390 Assert.assertEquals(relationresponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
391 relationCsid = extractId(response);
393 relationresponse.close();
398 public void relateCollectionObjectToIntake() {
400 // First create a CollectionObject
402 CollectionobjectsCommon co = new CollectionobjectsCommon();
403 fillCollectionObject(co, createIdentifier());
405 // Next, create a part object
406 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
407 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
408 commonPart.setLabel(collectionObjectClient.getCommonPartName());
410 // Make the create call and check the response
411 Response response = collectionObjectClient.create(multipart);
412 String collectionObjectCsid = null;
414 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
416 collectionObjectCsid = extractId(response);
422 // Next, create an Intake object
423 IntakesCommon intake = new IntakesCommon();
424 fillIntake(intake, createIdentifier());
425 // Create the a part object
426 multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
427 commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
428 commonPart.setLabel(intakeClient.getCommonPartName());
430 // Make the call to create and check the response
431 response = intakeClient.create(multipart);
432 String intakeCsid = null;
434 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
435 intakeCsid = extractId(response);
440 // Lastly, relate the two entities, by creating a new relation object
441 RelationsCommon relation = new RelationsCommon();
442 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
443 intakeCsid, IntakesCommon.class.getSimpleName(),
444 RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
445 // Create the part and fill it with the relation object
446 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
447 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
448 commonPart.setLabel(relationClient.getCommonPartName());
450 // Make the call to crate
451 response = relationClient.create(multipart);
452 String relationCsid = null;
454 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
455 relationCsid = extractId(response);
461 // Now try to retrieve the Intake record of the CollectionObject.
463 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
464 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
465 collectionObjectCsid,
466 null, //CollectionobjectsCommon.class.getSimpleName(),
469 null ); //IntakesCommon.class.getSimpleName());
470 RelationsCommonList relationList = null;
472 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
473 relationList = resultResponse.getEntity();
475 resultResponse.close();
479 // Each relation returned in the list needs to match what we
482 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
483 Assert.assertFalse(relationListItems.isEmpty());
486 RelationsCommon resultRelation = null;
487 for(RelationsCommonList.RelationListItem listItem : relationListItems){
489 String foundCsid = listItem.getCsid();
490 Response multiPartResponse = null;
492 multiPartResponse = relationClient.read(foundCsid);
493 int responseStatus = multiPartResponse.getStatus();
494 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
495 PoxPayloadIn input = new PoxPayloadIn((String)multiPartResponse.getEntity());
496 resultRelation = (RelationsCommon) extractPart(input,
497 relationClient.getCommonPartName(),
498 RelationsCommon.class);
499 } catch (Exception e) {
502 multiPartResponse.close();
505 Assert.assertEquals(resultRelation.getSubjectCsid(), collectionObjectCsid);
506 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
507 Assert.assertEquals(resultRelation.getObjectCsid(), intakeCsid);
508 System.out.println();
514 public void relateCollectionObjectsToIntake() {
515 relateCollectionObjectsToIntake(OBJECTS_TO_INTAKE);
518 private void relateCollectionObjectsToIntake(int numberOfObjects) {
521 // First create a list of CollectionObjects
523 CollectionobjectsCommon co = new CollectionobjectsCommon();
524 ArrayList<String>collectionObjectIDList = new ArrayList<String>(numberOfObjects);
525 for (int i = 0; i < numberOfObjects; i++) {
526 fillCollectionObject(co, createIdentifier());
528 // Next, create a part object
529 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
530 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
531 commonPart.setLabel(collectionObjectClient.getCommonPartName());
533 // Make the create call and check the response
534 Response response = collectionObjectClient.create(multipart);
535 String collectionObjectCsid = null;
537 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
539 collectionObjectCsid = extractId(response);
540 collectionObjectIDList.add(collectionObjectCsid);
547 // Next, create an Intake object
548 IntakesCommon intake = new IntakesCommon();
549 fillIntake(intake, createIdentifier());
550 // Create the a part object
551 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
552 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
553 commonPart.setLabel(intakeClient.getCommonPartName());
555 // Make the call to create and check the response
556 Response response = intakeClient.create(multipart);
557 String intakeCsid = null;
559 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
560 intakeCsid = extractId(response);
565 // Lastly, relate the two entities, by creating a new relation object
566 RelationsCommon relation = new RelationsCommon();
567 for (String collectionObjectCsid : collectionObjectIDList) {
568 fillRelation(relation,
569 intakeCsid, IntakesCommon.class.getSimpleName(), //subject
570 collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(), //object
571 RelationshipType.COLLECTIONOBJECT_INTAKE.toString()); //predicate
572 // Create the part and fill it with the relation object
573 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
574 commonPart = multipart.addPart(relation, MediaType.APPLICATION_XML_TYPE);
575 commonPart.setLabel(relationClient.getCommonPartName());
577 // Make the call to crate
578 response = relationClient.create(multipart);
579 String relationCsid = null;
581 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
582 relationCsid = extractId(response);
589 // Now try to retrieve the Intake record of the CollectionObject.
591 String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.toString();
592 RelationsCommonList relationList = null;
593 for (String collectionObjectCsid : collectionObjectIDList) {
594 ClientResponse<RelationsCommonList> resultResponse = relationClient.readList(
596 null, //IntakesCommon.class.getSimpleName(), //subject
598 collectionObjectCsid,
599 null); //CollectionobjectsCommon.class.getSimpleName()); //object
602 Assert.assertEquals(resultResponse.getStatus(), Response.Status.OK.getStatusCode());
603 relationList = resultResponse.getEntity();
605 resultResponse.close();
609 // Each relation returned in the list needs to match what we
612 List<RelationsCommonList.RelationListItem> relationListItems = relationList.getRelationListItem();
613 Assert.assertFalse(relationListItems.isEmpty());
616 RelationsCommon resultRelation = null;
617 for(RelationsCommonList.RelationListItem listItem : relationListItems){
618 String foundCsid = listItem.getCsid();
619 Response multiPartResponse = null;
621 multiPartResponse = relationClient.read(foundCsid);
622 int responseStatus = multiPartResponse.getStatus();
623 Assert.assertEquals(responseStatus, Response.Status.OK.getStatusCode());
624 PoxPayloadIn input = new PoxPayloadIn((String)multiPartResponse.getEntity());
625 resultRelation = (RelationsCommon) extractPart(input,
626 relationClient.getCommonPartName(),
627 RelationsCommon.class);
628 } catch (Exception e) {
631 multiPartResponse.close();
634 Assert.assertEquals(resultRelation.getSubjectCsid(), intakeCsid);
635 Assert.assertEquals(resultRelation.getRelationshipType(), RelationshipType.COLLECTIONOBJECT_INTAKE.toString());
636 Assert.assertEquals(resultRelation.getObjectCsid(), collectionObjectCsid);
637 System.out.println();