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.collectionspace.services.client.CollectionObjectClient;
42 import org.collectionspace.services.client.DimensionClient;
43 import org.collectionspace.services.client.DimensionFactory;
44 import org.collectionspace.services.client.PayloadOutputPart;
45 import org.collectionspace.services.client.PoxPayloadIn;
46 import org.collectionspace.services.client.PoxPayloadOut;
47 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
49 import org.collectionspace.services.client.IntakeClient;
50 import org.collectionspace.services.intake.IntakesCommon;
52 import org.collectionspace.services.client.RelationClient;
53 import org.collectionspace.services.client.workflow.WorkflowClient;
54 import org.collectionspace.services.dimension.DimensionsCommon;
55 import org.collectionspace.services.relation.RelationsCommon;
56 import org.collectionspace.services.relation.RelationsCommonList;
57 import org.collectionspace.services.relation.RelationshipType;
62 * @version $Revision:$
64 public class RelationIntegrationTest extends CollectionSpaceIntegrationTest {
66 final Logger logger = LoggerFactory
67 .getLogger(RelationIntegrationTest.class);
69 // Get clients for the CollectionSpace services
71 private CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
72 private RelationClient relationClient = new RelationClient();
73 private IntakeClient intakeClient = new IntakeClient();
74 private DimensionClient dimensionClient = new DimensionClient();
76 private static final int OBJECTS_TO_INTAKE = 1;
80 * Object as xml string.
83 * @param clazz the clazz
86 static protected String objectAsXmlString(Object o, Class<?> clazz) {
87 StringWriter sw = new StringWriter();
89 JAXBContext jc = JAXBContext.newInstance(clazz);
90 Marshaller m = jc.createMarshaller();
91 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
94 } catch (Exception e) {
100 private PoxPayloadOut createDimensionInstance(String commonPartName, String dimensionType, String dimensionValue, String entryDate) {
101 DimensionsCommon dimensionsCommon = new DimensionsCommon();
102 dimensionsCommon.setDimension(dimensionType);
103 dimensionsCommon.setValue(new BigDecimal(dimensionValue));
104 dimensionsCommon.setValueDate(entryDate);
105 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(
106 commonPartName, dimensionsCommon);
108 if (logger.isDebugEnabled()) {
109 logger.debug("to be created, dimension common");
110 logger.debug(objectAsXmlString(dimensionsCommon,
111 DimensionsCommon.class));
117 protected PoxPayloadOut createDimensionInstance(String identifier) {
118 DimensionClient client = new DimensionClient();
119 return createDimensionInstance(client.getCommonPartName(), identifier);
123 * Creates the dimension instance.
125 * @param identifier the identifier
126 * @return the multipart output
128 protected PoxPayloadOut createDimensionInstance(String commonPartName, String identifier) {
129 final String dimensionValue = Long.toString(System.currentTimeMillis());
131 return createDimensionInstance(commonPartName,
132 "dimensionType-" + identifier,
134 "entryDate-" + identifier);
137 @Test void deleteCollectionObjectRelationshipToLockedDimension() {
138 // First create a CollectionObject
139 CollectionobjectsCommon co = new CollectionobjectsCommon();
140 fillCollectionObject(co, createIdentifier());
141 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
142 PayloadOutputPart commonPart = multipart.addPart(collectionObjectClient.getCommonPartName(), co);
144 // Make the "create" call and check the response
145 Response response = collectionObjectClient.create(multipart);
146 String collectionObjectCsid = null;
148 Assert.assertEquals(response.getStatus(), Response.Status.CREATED
150 collectionObjectCsid = extractId(response);
155 // Create a new dimension record
156 multipart = this.createDimensionInstance(createIdentifier());
157 // Make the call to create and check the response
158 response = dimensionClient.create(multipart);
159 String dimensionCsid1 = null;
161 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
162 dimensionCsid1 = extractId(response);
167 // Relate the entities, by creating a new relation object
168 RelationsCommon relation = new RelationsCommon();
169 fillRelation(relation, collectionObjectCsid, CollectionobjectsCommon.class.getSimpleName(),
170 dimensionCsid1, DimensionsCommon.class.getSimpleName(),
171 "collectionobject-dimension");
172 // Create the part and fill it with the relation object
173 multipart = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
174 commonPart = multipart.addPart(relationClient.getCommonPartName(), relation);
175 // Create the relationship
176 response = relationClient.create(multipart);
177 String relationCsid1 = null;
179 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
180 relationCsid1 = extractId(response);
185 // Now lock the dimension record.
187 @SuppressWarnings("unused")
188 Response workflowResponse = dimensionClient.updateWorkflowWithTransition(
189 dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_LOCK);
190 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
191 workflowResponse.close();
193 // Finally, try to delete the relationship
195 // Try to delete the relationship -should fail because we don't allow delete if one of the sides is locked.
196 response = relationClient.delete(relationCsid1);
198 Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
203 // Also, try to soft-delete. This should also fail.
204 workflowResponse = dimensionClient.updateWorkflowWithTransition(dimensionCsid1, WorkflowClient.WORKFLOWTRANSITION_DELETE);
205 System.out.println("Locked dimension record with CSID=" + dimensionCsid1);
206 workflowResponse.close();
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 Response 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.readEntity(RelationsCommonList.class);
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(multiPartResponse.readEntity(String.class));
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 Response 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.readEntity(RelationsCommonList.class);
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(multiPartResponse.readEntity(String.class));
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();